From af486f7931c6bb45a47fa2f436ec2e2fadda64cf Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Fri, 18 Aug 2023 20:30:37 -0700 Subject: [PATCH] #51 . avoid cloning --- src/views/canvas.rs | 4 ++-- src/views/clip.rs | 2 +- src/views/list.rs | 8 ++++---- src/views/shapes.rs | 4 ++-- src/views/stack.rs | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/views/canvas.rs b/src/views/canvas.rs index 65c3bb5..7ba6ae4 100644 --- a/src/views/canvas.rs +++ b/src/views/canvas.rs @@ -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(path.clone()).or_default().rect; + let rect = args.cx.layout.get(path).map(|b| b.rect).unwrap_or_default(); args.vger.save(); (self.func)(args.cx, rect, args.vger); @@ -30,7 +30,7 @@ where } fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - let rect = cx.layout.entry(path.clone()).or_default().rect; + let rect = cx.layout.get(path).map(|b| b.rect).unwrap_or_default(); if rect.contains(pt) { Some(hash(path)) diff --git a/src/views/clip.rs b/src/views/clip.rs index e4a9bd0..7b56547 100644 --- a/src/views/clip.rs +++ b/src/views/clip.rs @@ -10,7 +10,7 @@ where V: View, { fn geom(&self, path: &IdPath, cx: &mut Context) -> LocalRect { - cx.layout.entry(path.clone()).or_default().rect + cx.layout.get(path).map(|b| b.rect).unwrap_or_default() } pub fn new(child: V) -> Self { diff --git a/src/views/list.rs b/src/views/list.rs index bd9b0e0..c9fc88f 100644 --- a/src/views/list.rs +++ b/src/views/list.rs @@ -29,7 +29,7 @@ where ) { for child in self.ids.iter().rev() { path.push(hh(child)); - let offset = cx.layout.entry(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); ((self.func)(child)).process(&event.offset(-offset), path, cx, actions); path.pop(); } @@ -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(path.clone()).or_default().offset; + let offset = args.cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); args.vger.save(); @@ -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(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); let xf = xform.pre_translate(offset); ((self.func)(child)).dirty(path, xf, cx); path.pop(); @@ -166,7 +166,7 @@ where let mut hit = None; for child in &self.ids { path.push(hh(child)); - let offset = cx.layout.entry(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); if let Some(h) = ((self.func)(child)).hittest(path, pt - offset, cx) { hit = Some(h) diff --git a/src/views/shapes.rs b/src/views/shapes.rs index c1e827c..a594971 100644 --- a/src/views/shapes.rs +++ b/src/views/shapes.rs @@ -8,7 +8,7 @@ pub struct Circle { impl Circle { fn geom(&self, path: &IdPath, cx: &mut Context) -> (LocalPoint, f32) { - let rect = cx.layout.entry(path.clone()).or_default().rect; + let rect = cx.layout.get(path).map(|b| b.rect).unwrap_or_default(); (rect.center(), rect.size.width.min(rect.size.height) / 2.0) } @@ -73,7 +73,7 @@ pub struct Rectangle { impl Rectangle { fn geom(&self, path: &IdPath, cx: &mut Context) -> LocalRect { - cx.layout.entry(path.clone()).or_default().rect + cx.layout.get(path).map(|b| b.rect).unwrap_or_default() } /// Sets the fill color for the rectangle. diff --git a/src/views/stack.rs b/src/views/stack.rs index 25001f4..21583ab 100644 --- a/src/views/stack.rs +++ b/src/views/stack.rs @@ -45,7 +45,7 @@ impl View for Stack 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(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); (*child).process(&event.offset(-offset), path, cx, actions); path.pop(); c -= 1; @@ -56,7 +56,7 @@ impl View for Stack let mut c = 0; self.children.foreach_view(&mut |child| { path.push(c); - let layout_box = *args.cx.layout.entry(path.clone()).or_default(); + let layout_box = args.cx.layout.get(path).map(|b| b.clone()).unwrap_or_default(); args.vger.save(); @@ -206,7 +206,7 @@ impl View for Stack let mut c = 0; self.children.foreach_view(&mut |child| { path.push(c); - let offset = cx.layout.entry(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); let xf = xform.pre_translate(offset); child.dirty(path, xf, cx); path.pop(); @@ -219,7 +219,7 @@ impl View for Stack let mut hit = None; self.children.foreach_view(&mut |child| { path.push(c); - let offset = cx.layout.entry(path.clone()).or_default().offset; + let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default(); if let Some(h) = child.hittest(path, pt - offset, cx) { hit = Some(h)