Skip to content

Commit

Permalink
#51. cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Aug 19, 2023
1 parent e395953 commit a7efb61
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 27 deletions.
9 changes: 8 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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<IdPath, LayoutBox>,
layout: HashMap<IdPath, LayoutBox>,

/// Which views each touch (or mouse pointer) is interacting with.
pub(crate) touches: [ViewId; 16],
Expand Down Expand Up @@ -346,6 +346,13 @@ impl Context {
}
}

pub(crate) fn get_layout(&self, path: &IdPath) -> LayoutBox {
match self.layout.get(path) {
Some(b) => *b,
None => LayoutBox::default(),
}
}

pub(crate) fn update_layout(&mut self, path: &IdPath, layout_box: LayoutBox) {
match self.layout.get_mut(path) {
Some(bref) => *bref = layout_box,
Expand Down
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.get(path).map(|b| b.rect).unwrap_or_default();
let rect = args.cx.get_layout(path).rect;

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.get(path).map(|b| b.rect).unwrap_or_default();
let rect = cx.get_layout(path).rect;

if rect.contains(pt) {
Some(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 @@ -10,7 +10,7 @@ where
V: View,
{
fn geom(&self, path: &IdPath, cx: &mut Context) -> LocalRect {
cx.layout.get(path).map(|b| b.rect).unwrap_or_default()
cx.get_layout(path).rect
}

pub fn new(child: V) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/views/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
}

fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) {
let rect = args.cx.layout[path].rect;
let rect = args.cx.get_layout(path).rect;
(self.func)(args.cx, rect.size, args.vger.current_transform());
path.push(0);
self.child.draw(path, args);
Expand Down
13 changes: 4 additions & 9 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.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;
((self.func)(child)).process(&event.offset(-offset), path, cx, actions);
path.pop();
}
Expand All @@ -38,12 +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
.get(path)
.map(|b| b.offset)
.unwrap_or_default();
let offset = args.cx.get_layout(path).offset;

args.vger.save();

Expand Down Expand Up @@ -160,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.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;
let xf = xform.pre_translate(offset);
((self.func)(child)).dirty(path, xf, cx);
path.pop();
Expand All @@ -171,7 +166,7 @@ where
let mut hit = None;
for child in &self.ids {
path.push(hh(child));
let offset = cx.layout.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;

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.get(path).map(|b| b.rect).unwrap_or_default();
let rect = cx.get_layout(path).rect;

(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.get(path).map(|b| b.rect).unwrap_or_default()
cx.get_layout(path).rect
}

/// Sets the fill color for the rectangle.
Expand Down
13 changes: 4 additions & 9 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.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;
(*child).process(&event.offset(-offset), path, cx, actions);
path.pop();
c -= 1;
Expand All @@ -56,12 +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
.get(path)
.map(|b| b.clone())
.unwrap_or_default();
let layout_box = args.cx.get_layout(path);

args.vger.save();

Expand Down Expand Up @@ -210,7 +205,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.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;
let xf = xform.pre_translate(offset);
child.dirty(path, xf, cx);
path.pop();
Expand All @@ -223,7 +218,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.get(path).map(|b| b.offset).unwrap_or_default();
let offset = cx.get_layout(path).offset;

if let Some(h) = child.hittest(path, pt - offset, cx) {
hit = Some(h)
Expand Down
4 changes: 2 additions & 2 deletions src/views/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
args.cx.id_stack.pop();
}

args.cx.layout[path].rect.size
args.cx.get_layout(path).rect.size
}

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

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

0 comments on commit a7efb61

Please sign in to comment.