Skip to content

Commit

Permalink
#53 Add pointer to context
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Dec 13, 2023
1 parent dd6062f commit ec4d8dc
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 24 deletions.
6 changes: 4 additions & 2 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ use std::{
time::Duration,
};

// TODO: fixme

fn main() {
rui(state(
|| "task not started".to_string(),
|s, cx| {
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]),
Expand Down
2 changes: 1 addition & 1 deletion src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {});

Expand Down
2 changes: 1 addition & 1 deletion src/views/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod tests {
assert!(path.len() == 1);

assert_eq!(button_sz, sz);
let s = StateHandle::<bool>::new(cx.view_id(&path));
let s = StateHandle::<bool>::new(cx.view_id(&path), &mut cx);
assert!(!*s.get(&cx));

let events = [
Expand Down
2 changes: 1 addition & 1 deletion src/views/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ mod tests {
assert_eq!(path.len(), 1);

assert_eq!(rect_sz, sz);
let s = StateHandle::<Vec<GestureState>>::new(cx.view_id(&path));
let s = StateHandle::<Vec<GestureState>>::new(cx.view_id(&path), &mut cx);
assert_eq!(cx[s], vec![]);

let events = [
Expand Down
2 changes: 1 addition & 1 deletion src/views/knob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod tests {
);

assert_eq!(knob_sz, sz);
let s = StateHandle::<f32>::new(cx.view_id(&path));
let s = StateHandle::<f32>::new(cx.view_id(&path), &mut cx);
assert_eq!(*s.get(&cx), 0.0);

let events = [
Expand Down
16 changes: 8 additions & 8 deletions src/views/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}

Expand All @@ -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
}
Expand All @@ -56,15 +56,15 @@ 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();
}

fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
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
}
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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
}
Expand Down
20 changes: 11 additions & 9 deletions src/views/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::any::Any;
/// to all event handlers, and functions passed to `state`.
pub struct StateHandle<S> {
pub(crate) id: ViewId,
pub(crate) cx: *mut Context,
phantom: std::marker::PhantomData<S>,
}

Expand All @@ -19,9 +20,10 @@ impl<S> Clone for StateHandle<S> {
}

impl<S: 'static> StateHandle<S> {
pub fn new(id: ViewId) -> Self {
pub fn new(id: ViewId, cx: &mut Context) -> Self {
Self {
id,
cx,
phantom: Default::default(),
}
}
Expand Down Expand Up @@ -64,15 +66,15 @@ 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();
}

fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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
}
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod tests {
);

assert_eq!(knob_sz, sz);
let s = StateHandle::<bool>::new(cx.view_id(&path));
let s = StateHandle::<bool>::new(cx.view_id(&path), &mut cx);
assert_eq!(*s.get(&cx), false);

let events = [
Expand Down

0 comments on commit ec4d8dc

Please sign in to comment.