freya_devtools_app/
node.rs1use freya::prelude::*;
2use freya_core::integration::NodeId;
3
4use crate::hooks::use_node_info;
5
6#[derive(PartialEq)]
7pub struct NodeElement {
8 pub node_id: NodeId,
9 pub window_id: u64,
10 pub is_selected: bool,
11 pub is_open: Option<bool>,
12 pub on_selected: EventHandler<()>,
13 pub on_toggle: EventHandler<()>,
14 pub on_expand_all: EventHandler<()>,
15 pub on_collapse_all: EventHandler<()>,
16 pub on_hover: EventHandler<Option<NodeId>>,
17}
18
19impl Component for NodeElement {
20 fn render_key(&self) -> DiffKey
21 where
22 Self: ComponentKey,
23 {
24 DiffKey::from(&(self.node_id, self.window_id))
25 }
26
27 fn render(&self) -> impl IntoElement {
28 let Some(node) = use_node_info(self.node_id, self.window_id) else {
29 return rect().into_element();
30 };
31
32 let margin_left = ((node.height + 1) * 10) as f32 - 18.;
33 let id = self.node_id.0;
34
35 let role = node.state.accessibility.builder.role();
36
37 let on_select = {
38 let on_selected = self.on_selected.clone();
39 move |_| on_selected.call(())
40 };
41
42 let on_open = {
43 let handler = self.on_toggle.clone();
44 let is_open = self.is_open;
45 move |e: Event<PressEventData>| {
46 if is_open.is_some() {
47 handler.call(());
48 e.stop_propagation();
49 }
50 }
51 };
52
53 let on_hover_enter = {
54 let on_hover = self.on_hover.clone();
55 let node_id = self.node_id;
56 move |_| on_hover.call(Some(node_id))
57 };
58
59 let on_hover_leave = {
60 let on_hover = self.on_hover.clone();
61 move |_| on_hover.call(None)
62 };
63
64 let arrow_button = self.is_open.map(|is_open| {
65 let arrow_degrees = if is_open { 0. } else { 270. };
66 Button::new()
67 .corner_radius(99.)
68 .border_fill(Color::TRANSPARENT)
69 .padding(Gaps::new_all(6.))
70 .background(Color::TRANSPARENT)
71 .on_press(on_open)
72 .child(ArrowIcon::new().rotate(arrow_degrees).fill(Color::WHITE))
73 });
74
75 let on_secondary_down = {
76 let on_toggle = self.on_toggle.clone();
77 let on_expand_all = self.on_expand_all.clone();
78 let on_collapse_all = self.on_collapse_all.clone();
79 let is_open = self.is_open;
80 move |e: Event<PressEventData>| {
81 let on_toggle = on_toggle.clone();
82 let on_expand_all = on_expand_all.clone();
83 let on_collapse_all = on_collapse_all.clone();
84 ContextMenu::open_from_event(
85 &e,
86 Menu::new()
87 .child(
88 MenuItem::new()
89 .on_press(move |_| {
90 on_toggle.call(());
91 ContextMenu::close();
92 })
93 .child(if Some(true) == is_open {
94 "Collapse"
95 } else {
96 "Expand"
97 }),
98 )
99 .child(
100 MenuItem::new()
101 .on_press(move |_| {
102 on_expand_all.call(());
103 ContextMenu::close();
104 })
105 .child("Expand All"),
106 )
107 .child(
108 MenuItem::new()
109 .on_press(move |_| {
110 on_collapse_all.call(());
111 ContextMenu::close();
112 })
113 .child("Collapse All"),
114 ),
115 );
116 }
117 };
118
119 let button = Button::new()
120 .corner_radius(99.)
121 .width(Size::fill())
122 .height(Size::px(27.))
123 .border_fill(Color::TRANSPARENT)
124 .background(if self.is_selected {
125 (40, 40, 40).into()
126 } else {
127 Color::TRANSPARENT
128 })
129 .hover_background(if self.is_selected {
130 (40, 40, 40).into()
131 } else {
132 Color::from((45, 45, 45))
133 })
134 .on_press(on_select)
135 .on_secondary_down(on_secondary_down)
136 .child(
137 rect()
138 .offset_x(margin_left)
139 .direction(Direction::Horizontal)
140 .width(Size::fill())
141 .cross_align(Alignment::center())
142 .child(rect().width(Size::px(25.)).maybe_child(arrow_button))
143 .child(
144 paragraph()
145 .max_lines(1)
146 .font_size(14.)
147 .text_overflow(TextOverflow::Ellipsis)
148 .span(
149 Span::new(if node.is_window {
150 "Window".to_string()
151 } else if role == AccessibilityRole::GenericContainer {
152 "rect".to_string()
153 } else {
154 format!("{role:?}")
155 })
156 .color(Color::WHITE),
157 )
158 .span(
159 Span::new(if node.is_window {
160 format!(", id: {}", self.window_id)
161 } else {
162 format!(", id: {}", id)
163 })
164 .color(Color::from_rgb(200, 200, 200)),
165 ),
166 ),
167 );
168
169 rect()
170 .on_pointer_enter(on_hover_enter)
171 .on_pointer_leave(on_hover_leave)
172 .child(button)
173 .into()
174 }
175}