Skip to content

Commit

Permalink
#51 . avoid cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent 5d9b795 commit af486f7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 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(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);
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(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))
Expand Down
2 changes: 1 addition & 1 deletion 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(path.clone()).or_default().rect
cx.layout.get(path).map(|b| b.rect).unwrap_or_default()
}

pub fn new(child: V) -> Self {
Expand Down
8 changes: 4 additions & 4 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(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();
}
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(path.clone()).or_default().offset;
let offset = args.cx.layout.get(path).map(|b| b.offset).unwrap_or_default();

args.vger.save();

Expand Down Expand Up @@ -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();
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(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)
Expand Down
4 changes: 2 additions & 2 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(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)
}
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(path.clone()).or_default().rect
cx.layout.get(path).map(|b| b.rect).unwrap_or_default()
}

/// Sets the fill color for the rectangle.
Expand Down
8 changes: 4 additions & 4 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(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;
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(path.clone()).or_default();
let layout_box = args.cx.layout.get(path).map(|b| b.clone()).unwrap_or_default();

args.vger.save();

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

0 comments on commit af486f7

Please sign in to comment.