From ec4d8dc538624bd9fd8783534435799e988012b6 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Wed, 13 Dec 2023 08:06:14 -0800 Subject: [PATCH] #53 Add pointer to context --- examples/async.rs | 6 ++++-- src/binding.rs | 2 +- src/views/button.rs | 2 +- src/views/drag.rs | 2 +- src/views/knob.rs | 2 +- src/views/map.rs | 16 ++++++++-------- src/views/state.rs | 20 +++++++++++--------- src/views/toggle.rs | 2 +- 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/examples/async.rs b/examples/async.rs index 0d4b0e93..8dd20c4e 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -4,6 +4,8 @@ use std::{ time::Duration, }; +// TODO: fixme + fn main() { rui(state( || "task not started".to_string(), @@ -11,9 +13,9 @@ fn main() { hstack(( button("press to begin", move |_| { spawn(move || { - on_main(move |cx| cx[s] = "task started".into()); + //on_main(move |cx| cx[s] = "task started".into()); sleep(Duration::from_secs(2)); - on_main(move |cx| cx[s] = "task complete".into()); + //on_main(move |cx| cx[s] = "task complete".into()); }); }), text(&cx[s]), diff --git a/src/binding.rs b/src/binding.rs index f232889d..501204fd 100644 --- a/src/binding.rs +++ b/src/binding.rs @@ -110,7 +110,7 @@ mod tests { let mut cx = Context::new(); let id = ViewId::default(); cx.init_state(id, &MyState::default); - let s = StateHandle::new(id); + let s = StateHandle::new(id, &mut cx); let b = bind(s, MyLens {}); diff --git a/src/views/button.rs b/src/views/button.rs index 917f2864..90b69132 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -66,7 +66,7 @@ mod tests { assert!(path.len() == 1); assert_eq!(button_sz, sz); - let s = StateHandle::::new(cx.view_id(&path)); + let s = StateHandle::::new(cx.view_id(&path), &mut cx); assert!(!*s.get(&cx)); let events = [ diff --git a/src/views/drag.rs b/src/views/drag.rs index 72c4aac5..f763a0ee 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -268,7 +268,7 @@ mod tests { assert_eq!(path.len(), 1); assert_eq!(rect_sz, sz); - let s = StateHandle::>::new(cx.view_id(&path)); + let s = StateHandle::>::new(cx.view_id(&path), &mut cx); assert_eq!(cx[s], vec![]); let events = [ diff --git a/src/views/knob.rs b/src/views/knob.rs index 8491981c..97bcf36d 100644 --- a/src/views/knob.rs +++ b/src/views/knob.rs @@ -59,7 +59,7 @@ mod tests { ); assert_eq!(knob_sz, sz); - let s = StateHandle::::new(cx.view_id(&path)); + let s = StateHandle::::new(cx.view_id(&path), &mut cx); assert_eq!(*s.get(&cx), 0.0); let events = [ diff --git a/src/views/map.rs b/src/views/map.rs index 0ed78b38..f395015e 100644 --- a/src/views/map.rs +++ b/src/views/map.rs @@ -23,7 +23,7 @@ where ) { let id = cx.view_id(path); cx.set_state(id, self.value.clone()); - let s = StateHandle::new(id); + let s = StateHandle::new(id, cx); path.push(0); (self.func)(s, cx).process(event, path, cx, actions); path.pop(); @@ -38,7 +38,7 @@ where let id = args.cx.view_id(path); args.cx.set_state(id, self.value.clone()); path.push(0); - (self.func)(StateHandle::new(id), args.cx).draw(path, args); + (self.func)(StateHandle::new(id, args.cx), args.cx).draw(path, args); path.pop(); } @@ -47,7 +47,7 @@ where args.cx.set_state(id, self.value.clone()); path.push(0); - let sz = (self.func)(StateHandle::new(id), args.cx).layout(path, args); + let sz = (self.func)(StateHandle::new(id, args.cx), args.cx).layout(path, args); path.pop(); sz } @@ -56,7 +56,7 @@ where let id = cx.view_id(path); cx.set_state(id, self.value.clone()); path.push(0); - (self.func)(StateHandle::new(id), cx).dirty(path, xform, cx); + (self.func)(StateHandle::new(id, cx), cx).dirty(path, xform, cx); path.pop(); } @@ -64,7 +64,7 @@ where let id = cx.view_id(path); cx.set_state(id, self.value.clone()); path.push(0); - let hit_id = (self.func)(StateHandle::new(id), cx).hittest(path, pt, cx); + let hit_id = (self.func)(StateHandle::new(id, cx), cx).hittest(path, pt, cx); path.pop(); hit_id } @@ -73,7 +73,7 @@ where let id = cx.view_id(path); cx.set_state(id, self.value.clone()); path.push(0); - (self.func)(StateHandle::new(id), cx).commands(path, cx, cmds); + (self.func)(StateHandle::new(id, cx), cx).commands(path, cx, cmds); path.pop(); } @@ -82,7 +82,7 @@ where cx.set_state(id, self.value.clone()); map.push(id); path.push(0); - (self.func)(StateHandle::new(id), cx).gc(path, cx, map); + (self.func)(StateHandle::new(id, cx), cx).gc(path, cx, map); path.pop(); } @@ -95,7 +95,7 @@ where let id = cx.view_id(path); cx.set_state(id, self.value.clone()); path.push(0); - let node_id = (self.func)(StateHandle::new(id), cx).access(path, cx, nodes); + let node_id = (self.func)(StateHandle::new(id, cx), cx).access(path, cx, nodes); path.pop(); node_id } diff --git a/src/views/state.rs b/src/views/state.rs index 13f4dd33..0ea7d88b 100644 --- a/src/views/state.rs +++ b/src/views/state.rs @@ -7,6 +7,7 @@ use std::any::Any; /// to all event handlers, and functions passed to `state`. pub struct StateHandle { pub(crate) id: ViewId, + pub(crate) cx: *mut Context, phantom: std::marker::PhantomData, } @@ -19,9 +20,10 @@ impl Clone for StateHandle { } impl StateHandle { - pub fn new(id: ViewId) -> Self { + pub fn new(id: ViewId, cx: &mut Context) -> Self { Self { id, + cx, phantom: Default::default(), } } @@ -64,7 +66,7 @@ where let id = cx.view_id(path); cx.init_state(id, &self.default); path.push(0); - (self.func)(StateHandle::new(id), cx).process(event, path, cx, actions); + (self.func)(StateHandle::new(id, cx), cx).process(event, path, cx, actions); path.pop(); } @@ -72,7 +74,7 @@ where let id = args.cx.view_id(path); args.cx.init_state(id, &self.default); path.push(0); - (self.func)(StateHandle::new(id), args.cx).draw(path, args); + (self.func)(StateHandle::new(id, args.cx), args.cx).draw(path, args); path.pop(); } @@ -100,7 +102,7 @@ where if compute_layout { args.cx.id_stack.push(id); - let view = (self.func)(StateHandle::new(id), args.cx); + let view = (self.func)(StateHandle::new(id, args.cx), args.cx); path.push(0); let child_size = view.layout(path, args); @@ -147,7 +149,7 @@ where cx.dirty_region.add_rect(WorldRect::from_points(world_pts)); } else { path.push(0); - (self.func)(StateHandle::new(id), cx).dirty(path, xform, cx); + (self.func)(StateHandle::new(id, cx), cx).dirty(path, xform, cx); path.pop(); } } @@ -156,7 +158,7 @@ where let id = cx.view_id(path); cx.init_state(id, &self.default); path.push(0); - let hit_id = (self.func)(StateHandle::new(id), cx).hittest(path, pt, cx); + let hit_id = (self.func)(StateHandle::new(id, cx), cx).hittest(path, pt, cx); path.pop(); hit_id } @@ -165,7 +167,7 @@ where let id = cx.view_id(path); cx.init_state(id, &self.default); path.push(0); - (self.func)(StateHandle::new(id), cx).commands(path, cx, cmds); + (self.func)(StateHandle::new(id, cx), cx).commands(path, cx, cmds); path.pop(); } @@ -174,7 +176,7 @@ where cx.init_state(id, &self.default); map.push(id); path.push(0); - (self.func)(StateHandle::new(id), cx).gc(path, cx, map); + (self.func)(StateHandle::new(id, cx), cx).gc(path, cx, map); path.pop(); } @@ -187,7 +189,7 @@ where let id = cx.view_id(path); cx.init_state(id, &self.default); path.push(0); - let node_id = (self.func)(StateHandle::new(id), cx).access(path, cx, nodes); + let node_id = (self.func)(StateHandle::new(id, cx), cx).access(path, cx, nodes); path.pop(); node_id } diff --git a/src/views/toggle.rs b/src/views/toggle.rs index af27f947..49beaffe 100644 --- a/src/views/toggle.rs +++ b/src/views/toggle.rs @@ -48,7 +48,7 @@ mod tests { ); assert_eq!(knob_sz, sz); - let s = StateHandle::::new(cx.view_id(&path)); + let s = StateHandle::::new(cx.view_id(&path), &mut cx); assert_eq!(*s.get(&cx), false); let events = [