Skip to content

Commit

Permalink
Use view_id
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent c5979a3 commit e395953
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::*;
use euclid::*;
use std::any::Any;
use std::any::TypeId;
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::ops;
use std::collections::hash_map::DefaultHasher;

pub type LocalSpace = vger::defs::LocalSpace;
pub type WorldSpace = vger::defs::WorldSpace;
Expand Down
12 changes: 0 additions & 12 deletions src/viewid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ impl ViewId {

pub type IdPath = Vec<u64>;

// Temporary function so we don't have to change too much at once.
// Just adding Id paths for now to the View functions.
pub fn hash(path: &IdPath) -> ViewId {
let mut hasher = DefaultHasher::new();
for id in path {
hasher.write_u64(*id);
}
ViewId {
id: hasher.finish(),
}
}

pub fn hh<H: Hash>(index: &H) -> u64 {
let mut hasher = DefaultHasher::new();
index.hash(&mut hasher);
Expand Down
2 changes: 1 addition & 1 deletion src/views/anim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
path.push(0);
self.child.gc(path, cx, map);
path.pop();
Expand Down
5 changes: 3 additions & 2 deletions 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(hash(&path));
let s = StateHandle::<bool>::new(cx.view_id(&path));
assert!(!*s.get(&cx));

let events = [
Expand All @@ -85,7 +85,8 @@ mod tests {
ui.process(event, &mut path, &mut cx, &mut actions);
}

assert!(cx.state_map.contains_key(&hash(&path)));
let vid = cx.view_id(&path);
assert!(cx.state_map.contains_key(&vid));

// State should have changed.
assert!(*s.get(&cx));
Expand Down
6 changes: 3 additions & 3 deletions src/views/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ where
let rect = cx.layout.get(path).map(|b| b.rect).unwrap_or_default();

if rect.contains(pt) {
Some(hash(path))
Some(cx.view_id(path))
} else {
None
}
}

fn gc(&self, path: &mut IdPath, _cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(cx.view_id(path));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
path.push(0);
self.child.gc(path, cx, map);
path.pop();
Expand Down
6 changes: 3 additions & 3 deletions src/views/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
let vid = hash(path);
let vid = cx.view_id(path);
match &event {
Event::TouchBegin { id, position } => {
if cx.touches[*id].is_default() && self.hittest(path, *position, cx).is_some() {
Expand Down Expand Up @@ -204,7 +204,7 @@ where
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
let vid = hash(path);
let vid = cx.view_id(path);
match &event {
Event::TouchBegin { id, position } => {
if self.hittest(path, *position, cx).is_some() {
Expand Down Expand Up @@ -331,7 +331,7 @@ mod tests {
assert_eq!(path.len(), 1);

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

let events = [
Expand Down
2 changes: 1 addition & 1 deletion src/views/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
path.push(0);
(self.func)(cx.init_env(&S::default), cx).gc(path, cx, map);
path.pop();
Expand Down
16 changes: 8 additions & 8 deletions src/views/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
let vid = hash(path);
let vid = cx.view_id(path);
match &event {
Event::TouchBegin { id: _, position } => {
if self.hittest(path, *position, cx).is_some() {
Expand All @@ -40,44 +40,44 @@ where
}

fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
let id = hash(path);
let id = args.cx.view_id(path);
path.push(0);
(self.func)(Some(id) == args.cx.focused_id).draw(path, args);
path.pop();
}

fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize {
let id = hash(path);
let id = args.cx.view_id(path);
path.push(0);
let sz = (self.func)(Some(id) == args.cx.focused_id).layout(path, args);
path.pop();
sz
}

fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) {
let id = hash(path);
let id = cx.view_id(path);
path.push(0);
(self.func)(Some(id) == cx.focused_id).dirty(path, xform, cx);
path.pop();
}

fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
let id = hash(path);
let id = cx.view_id(path);
path.push(0);
let vid = (self.func)(Some(id) == cx.focused_id).hittest(path, pt, cx);
path.pop();
vid
}

fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec<CommandInfo>) {
let id = hash(path);
let id = cx.view_id(path);
path.push(0);
(self.func)(Some(id) == cx.focused_id).commands(path, cx, cmds);
path.pop();
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
let id = hash(path);
let id = cx.view_id(path);
path.push(0);
(self.func)(Some(id) == cx.focused_id).gc(path, cx, map);
path.pop();
Expand All @@ -89,7 +89,7 @@ where
cx: &mut Context,
nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>,
) -> Option<accesskit::NodeId> {
let id = hash(path);
let id = cx.view_id(path);
path.push(0);
let node_id = (self.func)(Some(id) == cx.focused_id).access(path, cx, nodes);
path.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/views/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
path.push(0);
self.child.gc(path, cx, map);
path.pop();
Expand Down
5 changes: 3 additions & 2 deletions 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(hash(&path));
let s = StateHandle::<f32>::new(cx.view_id(&path));
assert_eq!(*s.get(&cx), 0.0);

let events = [
Expand All @@ -83,7 +83,8 @@ mod tests {
ui.process(event, &mut path, &mut cx, &mut actions);
}

assert!(cx.state_map.contains_key(&hash(&path)));
let vid = cx.view_id(&path);
assert!(cx.state_map.contains_key(&vid));
// State should have changed.
assert_eq!(*s.get(&cx), 0.125);
}
Expand Down
8 changes: 4 additions & 4 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
for child in &self.ids {
path.push(hh(child));
map.push(hash(path));
map.push(cx.view_id(path));
((self.func)(child)).gc(path, cx, map);
path.pop();
}
Expand All @@ -220,10 +220,10 @@ where

builder.set_children(children);
nodes.push((
hash(path).access_id(),
cx.view_id(path).access_id(),
builder.build(&mut cx.access_node_classes),
));
Some(hash(path).access_id())
Some(cx.view_id(path).access_id())
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/views/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
let id = hash(path);
let id = cx.view_id(path);
cx.set_state(id, self.value.clone());
let s = StateHandle::new(id);
path.push(0);
Expand All @@ -35,15 +35,15 @@ where
}

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

fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize {
let id = hash(path);
let id = args.cx.view_id(path);
args.cx.set_state(id, self.value.clone());

path.push(0);
Expand All @@ -53,15 +53,15 @@ where
}

fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) {
let id = hash(path);
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);
path.pop();
}

fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
let id = hash(path);
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);
Expand All @@ -70,15 +70,15 @@ where
}

fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec<CommandInfo>) {
let id = hash(path);
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);
path.pop();
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
let id = hash(path);
let id = cx.view_id(path);
cx.set_state(id, self.value.clone());
map.push(id);
path.push(0);
Expand All @@ -92,7 +92,7 @@ where
cx: &mut Context,
nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>,
) -> Option<accesskit::NodeId> {
let id = hash(path);
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);
Expand Down
2 changes: 1 addition & 1 deletion src/views/modview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
path.push(0);
(self.func)(self.value.clone(), cx).gc(path, cx, map);
path.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/views/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
path.push(0);
let child_aid = self.child.access(path, cx, nodes);
path.pop();
let aid = hash(path).access_id();
let aid = cx.view_id(path).access_id();
let mut builder = accesskit::NodeBuilder::new(self.role);
builder.set_children(match child_aid {
Some(cid) => vec![cid],
Expand Down
12 changes: 6 additions & 6 deletions src/views/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ impl View for Circle {
let (center, radius) = self.geom(path, cx);

if pt.distance_to(center) < radius {
Some(hash(path))
Some(cx.view_id(path))
} else {
None
}
}

fn gc(&self, path: &mut IdPath, _cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(cx.view_id(path));
}
}

Expand Down Expand Up @@ -117,14 +117,14 @@ impl View for Rectangle {
let rect = self.geom(path, cx);

if rect.contains(pt) {
Some(hash(path))
Some(cx.view_id(path))
} else {
None
}
}

fn gc(&self, path: &mut IdPath, _cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(cx.view_id(path));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/views/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(hash(path));
map.push(cx.view_id(path));
let mut c = 0;
self.children.foreach_view(&mut |child| {
path.push(c);
map.push(hash(path));
map.push(cx.view_id(path));
child.gc(path, cx, map);
path.pop();
c += 1;
Expand All @@ -276,7 +276,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
c += 1;
});
builder.set_children(children);
let aid = hash(path).access_id();
let aid = cx.view_id(path).access_id();
nodes.push((aid, builder.build(&mut cx.access_node_classes)));
Some(aid)
}
Expand Down
Loading

0 comments on commit e395953

Please sign in to comment.