Skip to content

Commit

Permalink
#51 . Use IdPaths as keys for layout
Browse files Browse the repository at this point in the history
Some unfortunate cloning
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent c89a886 commit 5d9b795
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct RenderInfo<'a> {
/// shouldn't have to interact with it directly.
pub struct Context {
/// Layout information for all views.
pub(crate) layout: HashMap<ViewId, LayoutBox>,
pub(crate) layout: HashMap<IdPath, LayoutBox>,

/// Which views each touch (or mouse pointer) is interacting with.
pub(crate) touches: [ViewId; 16],
Expand Down Expand Up @@ -178,7 +178,7 @@ impl Context {
assert!(path.len() == 1);
let keep_set = HashSet::<ViewId>::from_iter(keep);
self.state_map.retain(|k, _| keep_set.contains(k));
self.layout.retain(|k, _| keep_set.contains(k));
self.layout.retain(|k, _| keep_set.contains(&hash(k)));

// Get a new accesskit tree.
let mut nodes = vec![];
Expand Down
6 changes: 3 additions & 3 deletions src/views/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where
F: Fn(&mut Context, LocalRect, &mut Vger) + 'static,
{
fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
let rect = args.cx.layout.entry(hash(path)).or_default().rect;
let rect = args.cx.layout.entry(path.clone()).or_default().rect;

args.vger.save();
(self.func)(args.cx, rect, args.vger);
Expand All @@ -20,7 +20,7 @@ where

fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize {
args.cx.layout.insert(
hash(path),
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), args.sz),
offset: LocalOffset::zero(),
Expand All @@ -30,7 +30,7 @@ where
}

fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
let rect = cx.layout.entry(hash(path)).or_default().rect;
let rect = cx.layout.entry(path.clone()).or_default().rect;

if rect.contains(pt) {
Some(hash(path))
Expand Down
4 changes: 2 additions & 2 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
V: View,
{
fn geom(&self, path: &IdPath, cx: &mut Context) -> LocalRect {
cx.layout.entry(hash(path)).or_default().rect
cx.layout.entry(path.clone()).or_default().rect
}

pub fn new(child: V) -> Self {
Expand Down Expand Up @@ -50,7 +50,7 @@ path.pop();
self.child.layout(path, args);
path.pop();
args.cx.layout.insert(
hash(path),
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), args.sz),
offset: LocalOffset::zero(),
Expand Down
4 changes: 2 additions & 2 deletions src/views/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ path.pop();
}

fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
let rect = args.cx.layout[&hash(path)].rect;
let rect = args.cx.layout[path].rect;
(self.func)(args.cx, rect.size, args.vger.current_transform());
path.push(0);
self.child.draw(path, args);
Expand All @@ -38,7 +38,7 @@ path.pop();
path.pop();

args.cx.layout.insert(
hash(path),
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), sz),
offset: LocalOffset::zero(),
Expand Down
12 changes: 6 additions & 6 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
) {
for child in self.ids.iter().rev() {
path.push(hh(child));
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;
((self.func)(child)).process(&event.offset(-offset), path, cx, actions);
path.pop();
}
Expand All @@ -38,7 +38,7 @@ where
fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
for child in &self.ids {
path.push(hh(child));
let offset = args.cx.layout.entry(hash(path)).or_default().offset;
let offset = args.cx.layout.entry(path.clone()).or_default().offset;

args.vger.save();

Expand Down Expand Up @@ -87,7 +87,7 @@ where
VAlignment::Middle,
);

args.cx.layout.entry(hash(path)).or_default().offset = child_offset;
args.cx.layout.entry(path.clone()).or_default().offset = child_offset;

path.pop();

Expand Down Expand Up @@ -133,7 +133,7 @@ where
HAlignment::Center,
);

args.cx.layout.entry(hash(path)).or_default().offset = child_offset;
args.cx.layout.entry(path.clone()).or_default().offset = child_offset;
path.pop();

y -= child_size.height;
Expand All @@ -155,7 +155,7 @@ where
fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) {
for child in &self.ids {
path.push(hh(child));
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;
let xf = xform.pre_translate(offset);
((self.func)(child)).dirty(path, xf, cx);
path.pop();
Expand All @@ -166,7 +166,7 @@ where
let mut hit = None;
for child in &self.ids {
path.push(hh(child));
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;

if let Some(h) = ((self.func)(child)).hittest(path, pt - offset, cx) {
hit = Some(h)
Expand Down
8 changes: 4 additions & 4 deletions src/views/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Circle {

impl Circle {
fn geom(&self, path: &IdPath, cx: &mut Context) -> (LocalPoint, f32) {
let rect = cx.layout.entry(hash(path)).or_default().rect;
let rect = cx.layout.entry(path.clone()).or_default().rect;

(rect.center(), rect.size.width.min(rect.size.height) / 2.0)
}
Expand All @@ -31,7 +31,7 @@ impl View for Circle {

fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize {
args.cx.layout.insert(
hash(path),
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), args.sz),
offset: LocalOffset::zero(),
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Rectangle {

impl Rectangle {
fn geom(&self, path: &IdPath, cx: &mut Context) -> LocalRect {
cx.layout.entry(hash(path)).or_default().rect
cx.layout.entry(path.clone()).or_default().rect
}

/// Sets the fill color for the rectangle.
Expand Down Expand Up @@ -104,7 +104,7 @@ impl View for Rectangle {

fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize {
args.cx.layout.insert(
hash(path),
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), args.sz),
offset: LocalOffset::zero(),
Expand Down
12 changes: 6 additions & 6 deletions src/views/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
let mut c = self.children.len() as i64 - 1;
self.children.foreach_view_rev(&mut |child| {
path.push(c as u64);
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;
(*child).process(&event.offset(-offset), path, cx, actions);
path.pop();
c -= 1;
Expand All @@ -56,7 +56,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
let mut c = 0;
self.children.foreach_view(&mut |child| {
path.push(c);
let layout_box = *args.cx.layout.entry(hash(path)).or_default();
let layout_box = *args.cx.layout.entry(path.clone()).or_default();

args.vger.save();

Expand Down Expand Up @@ -132,7 +132,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
);

path.push(c);
args.cx.layout.entry(hash(path)).or_default().offset = child_offset;
args.cx.layout.entry(path.clone()).or_default().offset = child_offset;
path.pop();
}

Expand Down Expand Up @@ -183,7 +183,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
);

path.push(c);
args.cx.layout.entry(hash(path)).or_default().offset = child_offset;
args.cx.layout.entry(path.clone()).or_default().offset = child_offset;
path.pop();
}

Expand All @@ -206,7 +206,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
let mut c = 0;
self.children.foreach_view(&mut |child| {
path.push(c);
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;
let xf = xform.pre_translate(offset);
child.dirty(path, xf, cx);
path.pop();
Expand All @@ -219,7 +219,7 @@ impl<VT: ViewTuple + 'static, D: StackDirection + 'static> View for Stack<VT, D>
let mut hit = None;
self.children.foreach_view(&mut |child| {
path.push(c);
let offset = cx.layout.entry(hash(path)).or_default().offset;
let offset = cx.layout.entry(path.clone()).or_default().offset;

if let Some(h) = child.hittest(path, pt - offset, cx) {
hit = Some(h)
Expand Down
6 changes: 3 additions & 3 deletions src/views/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
args.cx.deps.insert(id, deps);

args.cx.layout.insert(
id,
path.clone(),
LayoutBox {
rect: LocalRect::new(LocalPoint::zero(), child_size),
offset: LocalOffset::zero(),
Expand All @@ -125,7 +125,7 @@ where
args.cx.id_stack.pop();
}

args.cx.layout[&id].rect.size
args.cx.layout[path].rect.size
}

fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) {
Expand All @@ -138,7 +138,7 @@ where

if holder.dirty {
// Add a region.
let rect = cx.layout[&id].rect;
let rect = cx.layout[path].rect;
let pts: [LocalPoint; 4] = [
rect.min(),
[rect.max_x(), rect.min_y()].into(),
Expand Down

0 comments on commit 5d9b795

Please sign in to comment.