Skip to content

Commit

Permalink
Move pass View methods to context types and AppState (#145)
Browse files Browse the repository at this point in the history
* Remove `update_main`

* Remove `layout_main`

* Remove `cleanup`

* Remove `style_main`

* Remove `compute_layout_main`

* Remove `event_main`

* Remove `paint_main`
  • Loading branch information
Zoxc authored Nov 5, 2023
1 parent cb16a25 commit af7618c
Show file tree
Hide file tree
Showing 15 changed files with 735 additions and 733 deletions.
640 changes: 639 additions & 1 deletion src/context.rs

Large diffs are not rendered by default.

633 changes: 14 additions & 619 deletions src/view.rs

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ impl<V: View> View for Clip<V> {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -72,11 +72,7 @@ impl<V: View> View for Clip<V> {
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
Expand All @@ -93,7 +89,7 @@ impl<V: View> View for Clip<V> {
} else {
cx.clip(&size.to_rect());
}
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
cx.restore();
}
}
12 changes: 4 additions & 8 deletions src/views/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ impl<V: View> View for Container<V> {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -77,14 +77,10 @@ impl<V: View> View for Container<V> {
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
}
}
12 changes: 4 additions & 8 deletions src/views/container_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ impl View for ContainerBox {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -107,14 +107,10 @@ impl View for ContainerBox {
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
}
}
12 changes: 4 additions & 8 deletions src/views/drag_resize_window_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ impl<V: View> View for DragResizeWindowArea<V> {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -92,14 +92,10 @@ impl<V: View> View for DragResizeWindowArea<V> {
id_path: Option<&[Id]>,
event: Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
}
}
20 changes: 8 additions & 12 deletions src/views/drag_window_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use crate::{

use super::Decorators;

pub struct DargWindowArea<V: View> {
pub struct DragWindowArea<V: View> {
id: Id,
child: V,
}

pub fn drag_window_area<V: View>(child: V) -> DargWindowArea<V> {
pub fn drag_window_area<V: View>(child: V) -> DragWindowArea<V> {
let id = Id::next();
DargWindowArea { id, child }
DragWindowArea { id, child }
.on_event(EventListener::PointerDown, |_| {
drag_window();
true
Expand All @@ -27,7 +27,7 @@ pub fn drag_window_area<V: View>(child: V) -> DargWindowArea<V> {
})
}

impl<V: View> View for DargWindowArea<V> {
impl<V: View> View for DragWindowArea<V> {
fn id(&self) -> Id {
self.id
}
Expand Down Expand Up @@ -65,11 +65,11 @@ impl<V: View> View for DargWindowArea<V> {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -78,14 +78,10 @@ impl<V: View> View for DargWindowArea<V> {
id_path: Option<&[Id]>,
event: Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
}
}
12 changes: 4 additions & 8 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ impl<T: 'static> View for DynamicContainer<T> {
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
cx.layout_node(self.id, true, |cx| vec![self.child.layout_main(cx)])
cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)])
}

fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
Some(self.child.compute_layout_main(cx))
Some(cx.compute_view_layout(&mut self.child))
}

fn event(
Expand All @@ -153,14 +153,10 @@ impl<T: 'static> View for DynamicContainer<T> {
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
if cx.should_send(self.child.id(), &event) {
self.child.event_main(cx, id_path, event)
} else {
false
}
cx.view_event(&mut self.child, id_path, event)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
}
}
11 changes: 5 additions & 6 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<V: View + 'static, T> View for List<V, T> {
let nodes = self
.children
.iter_mut()
.filter_map(|child| Some(child.as_mut()?.0.layout_main(cx)))
.filter_map(|child| Some(cx.layout_view(&mut child.as_mut()?.0)))
.collect::<Vec<_>>();
nodes
})
Expand All @@ -162,7 +162,7 @@ impl<V: View + 'static, T> View for List<V, T> {
let mut layout_rect = Rect::ZERO;
for child in &mut self.children {
if let Some((child, _)) = child.as_mut() {
layout_rect = layout_rect.union(child.compute_layout_main(cx));
layout_rect = layout_rect.union(cx.compute_view_layout(child));
}
}
Some(layout_rect)
Expand All @@ -176,8 +176,7 @@ impl<V: View + 'static, T> View for List<V, T> {
) -> bool {
for child in self.children.iter_mut() {
if let Some((child, _)) = child.as_mut() {
let id = child.id();
if cx.should_send(id, &event) && child.event_main(cx, id_path, event.clone()) {
if cx.view_event(child, id_path, event.clone()) {
return true;
}
}
Expand All @@ -188,7 +187,7 @@ impl<V: View + 'static, T> View for List<V, T> {
fn paint(&mut self, cx: &mut crate::context::PaintCx) {
for child in self.children.iter_mut() {
if let Some((child, _)) = child.as_mut() {
child.paint_main(cx);
cx.paint_view(child);
}
}
}
Expand Down Expand Up @@ -325,7 +324,7 @@ fn remove_index<V: View>(
index: usize,
) -> Option<()> {
let (mut view, scope) = std::mem::take(&mut children[index])?;
view.cleanup(app_state);
app_state.remove_view(&mut view);
scope.dispose();
Some(())
}
Expand Down
12 changes: 5 additions & 7 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ impl<V: View> View for Scroll<V> {
self.track_hover_style
.read_style(cx, &track_style.apply_selectors(&[StyleSelector::Hover]));

self.child.style_main(cx);
cx.style_view(&mut self.child);
}

fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node {
Expand All @@ -651,7 +651,7 @@ impl<V: View> View for Scroll<V> {
.combined_style
.clone()
.set(PositionProp, Position::Absolute);
let child_node = self.child.layout_main(cx);
let child_node = cx.layout_view(&mut self.child);

let virtual_style = Style::new()
.width(self.child_size.width)
Expand Down Expand Up @@ -680,7 +680,7 @@ impl<V: View> View for Scroll<V> {
fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option<Rect> {
self.update_size(cx.app_state_mut());
self.clamp_child_viewport(cx.app_state_mut(), self.child_viewport);
self.child.compute_layout_main(cx);
cx.compute_view_layout(&mut self.child);
None
}

Expand Down Expand Up @@ -797,9 +797,7 @@ impl<V: View> View for Scroll<V> {
_ => {}
}

if cx.should_send(self.child.id(), &event)
&& self.child.event_main(cx, id_path, event.clone())
{
if cx.view_event(&mut self.child, id_path, event.clone()) {
return true;
}

Expand Down Expand Up @@ -839,7 +837,7 @@ impl<V: View> View for Scroll<V> {
cx.clip(&self.actual_rect);
}
cx.offset((-self.child_viewport.x0, -self.child_viewport.y0));
self.child.paint_main(cx);
cx.paint_view(&mut self.child);
cx.restore();

if !self.hide {
Expand Down
18 changes: 6 additions & 12 deletions src/views/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<VT: ViewTuple + 'static> View for Stack<VT> {

fn style(&mut self, cx: &mut crate::context::StyleCx) {
self.children.foreach_mut(&mut |view| {
view.style_main(cx);
cx.style_view(view);
false
});
}
Expand All @@ -103,14 +103,8 @@ impl<VT: ViewTuple + 'static> View for Stack<VT> {
) -> bool {
let mut handled = false;
self.children.foreach_rev(&mut |view| {
let id = view.id();
if cx.should_send(id, &event) {
handled = view.event_main(cx, id_path, event.clone());
if handled {
return true;
}
}
false
handled |= cx.view_event(view, id_path, event.clone());
handled
});
handled
}
Expand All @@ -119,7 +113,7 @@ impl<VT: ViewTuple + 'static> View for Stack<VT> {
cx.layout_node(self.id, true, |cx| {
let mut nodes = Vec::new();
self.children.foreach_mut(&mut |view| {
let node = view.layout_main(cx);
let node = cx.layout_view(view);
nodes.push(node);
false
});
Expand All @@ -130,15 +124,15 @@ impl<VT: ViewTuple + 'static> View for Stack<VT> {
fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
let mut layout_rect = Rect::ZERO;
self.children.foreach_mut(&mut |view| {
layout_rect = layout_rect.union(view.compute_layout_main(cx));
layout_rect = layout_rect.union(cx.compute_view_layout(view));
false
});
Some(layout_rect)
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
self.children.foreach_mut(&mut |view| {
view.paint_main(cx);
cx.paint_view(view);
false
});
}
Expand Down
Loading

0 comments on commit af7618c

Please sign in to comment.