Skip to content

Commit

Permalink
add scope to dyn_container
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Aug 16, 2023
1 parent 597f8e1 commit c0c1e63
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
18 changes: 10 additions & 8 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ impl ViewContext {
})
}

pub fn with_context<T>(cx: ViewContext, f: impl FnOnce() -> T) -> T {
ViewContext::save();
ViewContext::set_current(cx);
let value = f();
ViewContext::restore();
value
}

/// Use this method if you are creating a `View` that has a child.
///
/// Ensures that the child is initialized with the "correct" `ViewContext`
Expand All @@ -82,10 +90,7 @@ impl ViewContext {
let id = cx.new_id();
let mut child_cx = cx;
child_cx.id = id;
ViewContext::save();
ViewContext::set_current(child_cx);
let child = child();
ViewContext::restore();
let child = ViewContext::with_context(child_cx, child);
(id, child)
}

Expand Down Expand Up @@ -228,11 +233,8 @@ impl<V: View> AppHandle<V> {
}

let scope = Scope::new();
ViewContext::save();
let cx = ViewContext { id };
ViewContext::set_current(cx);
let view = with_scope(scope, app_logic);
ViewContext::restore();
let view = ViewContext::with_context(cx, || with_scope(scope, app_logic));
Self {
scope,
view,
Expand Down
31 changes: 17 additions & 14 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use floem_reactive::create_effect;
use floem_reactive::{as_child_of_current_scope, create_effect, Scope};
use glazier::kurbo::Rect;

use crate::{
Expand All @@ -9,18 +9,21 @@ use crate::{
view::{ChangeFlags, View},
};

pub struct DynamicContainer<CF: Fn(T) -> Box<dyn View> + 'static, T: 'static> {
type ChildFn<T> = dyn Fn(T) -> (Box<dyn View>, Scope);

pub struct DynamicContainer<T: 'static> {
id: Id,
child: Box<dyn View>,
child_fn: CF,
child_scope: Scope,
child_fn: Box<ChildFn<T>>,
phantom: PhantomData<T>,
cx: ViewContext,
}

pub fn dyn_container<CF: Fn(T) -> Box<dyn View> + 'static, T: 'static>(
update_view: impl Fn() -> T + 'static,
child: CF,
) -> DynamicContainer<CF, T> {
child_fn: CF,
) -> DynamicContainer<T> {
let cx = ViewContext::get_current();
let id = cx.new_id();

Expand All @@ -31,16 +34,18 @@ pub fn dyn_container<CF: Fn(T) -> Box<dyn View> + 'static, T: 'static>(
id.update_state(update_view(), false);
});

let child_fn = Box::new(as_child_of_current_scope(child_fn));
DynamicContainer {
id,
child: Box::new(crate::views::empty()),
child_fn: child,
child_scope: Scope::new(),
child_fn,
phantom: PhantomData,
cx: child_cx,
}
}

impl<CF: Fn(T) -> Box<dyn View> + 'static + Copy, T: 'static> View for DynamicContainer<CF, T> {
impl<T: 'static> View for DynamicContainer<T> {
fn id(&self) -> Id {
self.id
}
Expand Down Expand Up @@ -79,13 +84,11 @@ impl<CF: Fn(T) -> Box<dyn View> + 'static + Copy, T: 'static> View for DynamicCo
state: Box<dyn std::any::Any>,
) -> crate::view::ChangeFlags {
if let Ok(val) = state.downcast::<T>() {
ViewContext::save();
ViewContext::set_current(self.cx);

let child_fn = self.child_fn;
self.child = child_fn(*val);

ViewContext::restore();
ViewContext::with_context(self.cx, || {
let old_child_scope = self.child_scope;
(self.child, self.child_scope) = (self.child_fn)(*val);
old_child_scope.dispose();
});
cx.request_layout(self.id());
ChangeFlags::LAYOUT
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ impl<V: View + 'static, T> View for List<V, T> {
state: Box<dyn std::any::Any>,
) -> crate::view::ChangeFlags {
if let Ok(diff) = state.downcast() {
ViewContext::save();
ViewContext::set_current(self.cx);
apply_diff(cx.app_state, *diff, &mut self.children, &self.view_fn);
ViewContext::restore();
ViewContext::with_context(self.cx, || {
apply_diff(cx.app_state, *diff, &mut self.children, &self.view_fn);
});
cx.request_layout(self.id());
ChangeFlags::LAYOUT
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<V: View> View for Scroll<V> {

match &event {
Event::PointerDown(event) => {
if !self.hide_bar && event.button.is_secondary() {
if !self.hide_bar && event.button.is_primary() {
self.held = BarHeldState::None;

let pos = event.pos + scroll_offset;
Expand Down
7 changes: 3 additions & 4 deletions src/views/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ impl<V: View + 'static, T> View for Tab<V, T> {
if let Ok(state) = state.downcast::<TabState<T>>() {
match *state {
TabState::Diff(diff) => {
ViewContext::save();
ViewContext::set_current(self.cx);
apply_diff(cx.app_state, *diff, &mut self.children, &self.view_fn);
ViewContext::restore();
ViewContext::with_context(self.cx, || {
apply_diff(cx.app_state, *diff, &mut self.children, &self.view_fn);
});
}
TabState::Active(active) => {
self.active = active;
Expand Down
7 changes: 3 additions & 4 deletions src/views/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ impl<V: View + 'static, T> View for VirtualList<V, T> {
}
self.before_size = state.before_size;
self.after_size = state.after_size;
ViewContext::save();
ViewContext::set_current(self.cx);
apply_diff(cx.app_state, state.diff, &mut self.children, &self.view_fn);
ViewContext::restore();
ViewContext::with_context(self.cx, || {
apply_diff(cx.app_state, state.diff, &mut self.children, &self.view_fn);
});
cx.request_layout(self.id());
ChangeFlags::LAYOUT
} else {
Expand Down

0 comments on commit c0c1e63

Please sign in to comment.