Skip to content

Commit b769eea

Browse files
author
Jakub Hlusička
committed
WIP debugging, typed tree
1 parent 7222725 commit b769eea

File tree

3 files changed

+105
-12
lines changed

3 files changed

+105
-12
lines changed

src/widgets/floating_panes.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,10 @@ typed_layout! {
12201220
parent_type_name: FloatingPanes,
12211221
fn_name: pane_with_index,
12221222
fn_args: [pane_index: usize],
1223-
fn: |parent: Layout<'a>, pane_index: usize| {
1223+
layout_fn: |parent: Layout<'a>, pane_index: usize| {
1224+
parent.children().nth(pane_index).unwrap()
1225+
},
1226+
tree_fn: |parent: &Tree, pane_index: usize| {
12241227
parent.children().nth(pane_index).unwrap()
12251228
},
12261229
},
@@ -1238,8 +1241,17 @@ typed_layout! {
12381241
parent_type_name: FloatingPane,
12391242
fn_name: content,
12401243
fn_args: [],
1241-
fn: |parent: Layout<'a>| {
1242-
parent.children().nth(0).unwrap().children().nth(1).unwrap().children().nth(0).unwrap()
1244+
layout_fn: |parent: Layout<'a>| {
1245+
parent
1246+
.children().nth(0).unwrap()
1247+
.children().nth(1).unwrap()
1248+
.children().nth(0).unwrap()
1249+
},
1250+
tree_fn: |parent: &Tree| {
1251+
parent
1252+
.children().nth(0).unwrap()
1253+
.children().nth(1).unwrap()
1254+
.children().nth(0).unwrap()
12431255
},
12441256
},
12451257
],

src/widgets/layout.rs

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
use iced_core::{Point, Rectangle};
2-
use std::fmt::Debug;
1+
use iced_core::{Point, Rectangle, widget::Tree};
2+
use std::{fmt::Debug, ops::Deref};
33

44
pub trait TypedLayout: Clone + Copy + Debug {
55
fn position(&self) -> Point;
66
fn bounds(&self) -> Rectangle;
77
}
88

9+
pub trait TypedTreeRef<'a>: Debug {
10+
fn tree_ref(&self) -> &Tree;
11+
}
12+
13+
pub trait TypedTreeMut<'a>: TypedTreeRef<'a> {
14+
fn tree_mut(&mut self) -> &mut Tree;
15+
}
16+
917
/// A macro that facilitates type safety for layout traversal.
1018
/// Generates a newtype (wrapper) for the [`::iced_runtime::Layout`] type and functions to access
1119
/// this type from other typed layout types specified in `traverse` and `children_of`.
@@ -20,7 +28,9 @@ macro_rules! typed_layout {
2028
parent_type_name: $traverse_parent_type_name:ident,
2129
fn_name: $traverse_fn_name:ident,
2230
fn_args: [$($traverse_fn_arg_name:ident: $traverse_fn_arg_ty:ty),*$(,)?],
23-
fn: $traverse_fn:expr,
31+
layout_fn: $traverse_layout_fn:expr,
32+
tree_ref_fn: $traverse_tree_ref_fn:expr,
33+
tree_mut_fn: $traverse_tree_mut_fn:expr,
2434
},
2535
)*
2636
],
@@ -36,6 +46,12 @@ macro_rules! typed_layout {
3646
#[derive(Clone, Copy, Debug)]
3747
pub struct [< $type_name Layout >]<'a>(::iced_core::Layout<'a>);
3848

49+
#[derive(Debug)]
50+
pub struct [< $type_name TreeRef >]<'a>(&'a ::iced_core::widget::Tree);
51+
52+
#[derive(Debug)]
53+
pub struct [< $type_name TreeMut >]<'a>(&'a mut ::iced_core::widget::Tree);
54+
3955
impl<'a> TypedLayout for [< $type_name Layout >]<'a> {
4056
fn position(&self) -> ::iced_core::Point {
4157
self.0.position()
@@ -46,6 +62,18 @@ macro_rules! typed_layout {
4662
}
4763
}
4864

65+
impl<'a> TypedTreeRef for [< $type_name TreeRef >]<'a> {
66+
fn tree_ref(&self) -> &Tree {
67+
&self.0
68+
}
69+
}
70+
71+
impl<'a> TypedTreeMut for [< $type_name TreeMut >]<'a> {
72+
fn tree_mut(&mut self) -> &mut Tree {
73+
&mut self.0
74+
}
75+
}
76+
4977
// impl<'a> ::std::ops::Deref for [< $type_name Layout >]<'a> {
5078
// type Target = ::iced_runtime::Layout<'a>;
5179

@@ -66,6 +94,18 @@ macro_rules! typed_layout {
6694
}
6795
}
6896

97+
impl<'a> From<&'a ::iced_core::widget::Tree> for [< $type_name TreeRef >]<'a> {
98+
fn from(tree: &'a ::iced_core::widget::Tree) -> Self {
99+
Self(tree)
100+
}
101+
}
102+
103+
impl<'a> From<[< $type_name TreeRef >]<'a>> for ::iced_core::widget::Tree {
104+
fn from(tree: [< $type_name Tree >]) -> Self {
105+
tree.0
106+
}
107+
}
108+
69109
$(
70110
$(
71111
impl<'a> [< $traverse_parent_type_name Layout >]<'a> {
@@ -76,10 +116,23 @@ macro_rules! typed_layout {
76116
use ::iced_core::Layout;
77117
// let [< $traverse_parent_type_name Layout >](parent) = self;
78118
let parent = self.into();
79-
let layout = ($traverse_fn)(parent, $($traverse_fn_arg_name, )*);
119+
let layout = ($traverse_layout_fn)(parent, $($traverse_fn_arg_name, )*);
80120
[< $type_name Layout >]::from(layout)
81121
}
82122
}
123+
124+
impl [< $traverse_parent_type_name Tree >] {
125+
pub fn [< $traverse_fn_name >](
126+
self,
127+
$($traverse_fn_arg_name: $traverse_fn_arg_ty, )*
128+
) -> [< $type_name Tree >]<'a> {
129+
use ::iced_core::widget::Tree;
130+
// let [< $traverse_parent_type_name Tree >](parent) = self;
131+
let parent = self.into();
132+
let layout = ($traverse_tree_fn)(parent, $($traverse_fn_arg_name, )*);
133+
[< $type_name Tree >]::from(layout)
134+
}
135+
}
83136
)*
84137
)?
85138

@@ -94,6 +147,17 @@ macro_rules! typed_layout {
94147
})
95148
}
96149
}
150+
151+
impl [< $children_of_parent_type_name Tree >] {
152+
pub fn [< $children_of_fn_name >](
153+
self,
154+
) -> impl Iterator<Item=[< $type_name Tree >]> {
155+
let [< $children_of_parent_type_name Tree >](parent) = self;
156+
parent.children().map(|layout| {
157+
[< $type_name Tree >]::from(layout)
158+
})
159+
}
160+
}
97161
)?
98162
}
99163
}

src/widgets/node.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,15 +768,15 @@ where R: margin::WidgetRenderer
768768
let mut mouse_interaction = mouse::Interaction::default();
769769
let mut primitives = Vec::new();
770770

771-
primitives.extend(panes.children.iter().zip(layout.panes()).map(
772-
|((_child_index, child), layout)| {
771+
primitives.extend(panes.children.iter().zip(layout.panes()).zip(&state.children).map(
772+
|(((_child_index, child), child_layout), child_state)| {
773773
/*let (primitive, new_mouse_interaction) =*/
774774
child.element_tree.as_widget().draw(
775-
state,
775+
child_state,
776776
self,
777777
theme,
778778
style,
779-
layout.into(),
779+
child_layout.into(),
780780
cursor,
781781
viewport,
782782
);
@@ -962,8 +962,25 @@ where R: margin::WidgetRenderer
962962

963963
// Draw connection points
964964
{
965-
for (pane_layout, node_index) in layout.panes().zip(panes.children.keys().copied()) {
965+
for (pane_layout, (node_index, pane)) in layout.panes().zip(&panes.children) {
966966
let node = panes.children.get(&node_index).unwrap();
967+
968+
fn debug_layout(layout: &Layout, depth: usize) {
969+
let prefix = std::iter::repeat_n(' ', depth).collect::<String>();
970+
if layout.children().next().is_none() {
971+
println!("{prefix}{{}}");
972+
} else {
973+
println!("{prefix}{{");
974+
for child in layout.children() {
975+
debug_layout(&child, depth + 1);
976+
}
977+
println!("{prefix}}}");
978+
}
979+
}
980+
981+
print!("Node #{node_index:?} {}", pane.behaviour_data.);
982+
debug_layout(&pane_layout.into(), 0);
983+
967984
let inputs_layout = pane_layout
968985
.content()
969986
.channels_with_direction(ChannelDirection::In)

0 commit comments

Comments
 (0)