1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Debug inspection properties.

use std::{cell::RefCell, fmt, rc::Rc};

use zng_ext_input::{
    focus::WidgetInfoFocusExt as _,
    mouse::{MOUSE_HOVERED_EVENT, MOUSE_MOVE_EVENT},
};
use zng_ext_window::WINDOW_Ext as _;
use zng_layout::unit::Orientation2D;
use zng_view_api::display_list::FrameValue;
use zng_wgt::prelude::*;

/// Target of inspection properties.
#[derive(Default, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum InspectMode {
    /// Just the widget where the inspector property is set.
    Widget,
    /// The widget where the inspector property is set and all descendants.
    ///
    /// The `true` value converts to this.
    WidgetAndDescendants,
    /// Disable inspection.
    ///
    /// The `false` value converts to this.
    #[default]
    Disabled,
}
impl fmt::Debug for InspectMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            write!(f, "InspectMode::")?;
        }
        match self {
            Self::Widget => write!(f, "Widget"),
            Self::WidgetAndDescendants => write!(f, "WidgetAndDescendants"),
            Self::Disabled => write!(f, "Disabled"),
        }
    }
}
impl_from_and_into_var! {
    fn from(widget_and_descendants: bool) -> InspectMode {
        if widget_and_descendants {
            InspectMode::WidgetAndDescendants
        } else {
            InspectMode::Disabled
        }
    }
}

/// Draws a debug dot in target widget's [center point].
///
/// [center point]: zng_wgt::prelude::WidgetInfo::center
#[property(CONTEXT, default(false))]
pub fn show_center_points(child: impl UiNode, mode: impl IntoVar<InspectMode>) -> impl UiNode {
    show_widget_tree(
        child,
        |_, wgt, frame| {
            frame.push_debug_dot(wgt.center(), colors::GREEN);
        },
        mode,
    )
}

/// Draws a border for every target widget's outer and inner bounds.
///
/// The outer bounds is drawn dotted and in pink, the inner bounds is drawn solid and in blue.
#[property(CONTEXT, default(false))]
pub fn show_bounds(child: impl UiNode, mode: impl IntoVar<InspectMode>) -> impl UiNode {
    show_widget_tree(
        child,
        |_, wgt, frame| {
            let p = Dip::new(1).to_px(frame.scale_factor());

            let outer_bounds = wgt.outer_bounds();
            let inner_bounds = wgt.inner_bounds();

            if outer_bounds != inner_bounds && !outer_bounds.is_empty() {
                frame.push_border(
                    wgt.outer_bounds(),
                    PxSideOffsets::new_all_same(p),
                    BorderSides::dotted(web_colors::PINK),
                    PxCornerRadius::zero(),
                );
            }

            if !inner_bounds.size.is_empty() {
                frame.push_border(
                    inner_bounds,
                    PxSideOffsets::new_all_same(p),
                    BorderSides::solid(web_colors::ROYAL_BLUE),
                    PxCornerRadius::zero(),
                );
            }
        },
        mode,
    )
}

/// Draws a border over every inlined widget row in the window.
#[property(CONTEXT, default(false))]
pub fn show_rows(child: impl UiNode, mode: impl IntoVar<InspectMode>) -> impl UiNode {
    let spatial_id = SpatialFrameId::new_unique();
    show_widget_tree(
        child,
        move |i, wgt, frame| {
            let p = Dip::new(1).to_px(frame.scale_factor());

            let wgt = wgt.bounds_info();
            let transform = wgt.inner_transform();
            if let Some(inline) = wgt.inline() {
                frame.push_reference_frame((spatial_id, i as u32).into(), FrameValue::Value(transform), false, false, |frame| {
                    for row in &inline.rows {
                        if !row.size.is_empty() {
                            frame.push_border(
                                *row,
                                PxSideOffsets::new_all_same(p),
                                BorderSides::dotted(web_colors::LIGHT_SALMON),
                                PxCornerRadius::zero(),
                            );
                        }
                    }
                })
            };
        },
        mode,
    )
}

fn show_widget_tree(
    child: impl UiNode,
    mut render: impl FnMut(usize, WidgetInfo, &mut FrameBuilder) + Send + 'static,
    mode: impl IntoVar<InspectMode>,
) -> impl UiNode {
    let mode = mode.into_var();
    let cancel_space = SpatialFrameId::new_unique();
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render(&mode);
        }
        UiNodeOp::Render { frame } => {
            child.render(frame);

            let mut r = |render: &mut dyn FnMut(WidgetInfo, &mut FrameBuilder)| {
                let tree = WINDOW.info();
                if let Some(wgt) = tree.get(WIDGET.id()) {
                    if WIDGET.parent_id().is_none() {
                        render(wgt, frame);
                    } else if let Some(t) = frame.transform().inverse() {
                        // cancel current transform
                        frame.push_reference_frame(cancel_space.into(), t.into(), false, false, |frame| {
                            render(wgt, frame);
                        })
                    } else {
                        tracing::error!("cannot inspect from `{:?}`, non-invertible transform", WIDGET.id())
                    }
                }
            };

            match mode.get() {
                InspectMode::Widget => {
                    r(&mut |wgt, frame| {
                        render(0, wgt, frame);
                    });
                }
                InspectMode::WidgetAndDescendants => {
                    r(&mut |wgt, frame| {
                        for (i, wgt) in wgt.self_and_descendants().enumerate() {
                            render(i, wgt, frame);
                        }
                    });
                }
                InspectMode::Disabled => {}
            }
        }
        _ => {}
    })
}

/// Draws the inner bounds that where tested for the mouse point.
///
/// # Window Only
///
/// This property only works if set in a window, if set in another widget it will log an error and not render anything.
#[property(CONTEXT, default(false))]
pub fn show_hit_test(child: impl UiNode, enabled: impl IntoVar<bool>) -> impl UiNode {
    let enabled = enabled.into_var();
    let mut handles = EventHandles::default();
    let mut valid = false;
    let mut fails = vec![];
    let mut hits = vec![];

    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            valid = WIDGET.parent_id().is_none();
            if valid {
                WIDGET.sub_var(&enabled);

                if enabled.get() {
                    let id = WIDGET.id();
                    handles = [MOUSE_MOVE_EVENT.subscribe(id), MOUSE_HOVERED_EVENT.subscribe(id)].into();
                } else {
                    handles.clear();
                }
            } else {
                tracing::error!("property `show_hit_test` is only valid in a window");
            }
        }
        UiNodeOp::Deinit => {
            handles.clear();
        }
        UiNodeOp::Event { update } => {
            if let Some(args) = MOUSE_MOVE_EVENT.on(update) {
                if valid && enabled.get() {
                    let factor = WINDOW.vars().scale_factor().get();
                    let pt = args.position.to_px(factor);

                    let new_fails = Rc::new(RefCell::new(vec![]));
                    let new_hits = Rc::new(RefCell::new(vec![]));

                    let tree = WINDOW.info();
                    let _ = tree
                        .root()
                        .spatial_iter(clmv!(new_fails, new_hits, |w| {
                            let bounds = w.inner_bounds();
                            let hit = bounds.contains(pt);
                            if hit {
                                new_hits.borrow_mut().push(bounds);
                            } else {
                                new_fails.borrow_mut().push(bounds);
                            }
                            hit
                        }))
                        .count();

                    let new_fails = Rc::try_unwrap(new_fails).unwrap().into_inner();
                    let new_hits = Rc::try_unwrap(new_hits).unwrap().into_inner();

                    if fails != new_fails || hits != new_hits {
                        fails = new_fails;
                        hits = new_hits;

                        WIDGET.render();
                    }
                }
            } else if let Some(args) = MOUSE_HOVERED_EVENT.on(update) {
                if args.target.is_none() && !fails.is_empty() && !hits.is_empty() {
                    fails.clear();
                    hits.clear();

                    WIDGET.render();
                }
            }
        }
        UiNodeOp::Update { .. } => {
            if let Some(enabled) = enabled.get_new() {
                if enabled && valid {
                    let id = WIDGET.id();
                    handles = [MOUSE_MOVE_EVENT.subscribe(id), MOUSE_HOVERED_EVENT.subscribe(id)].into();
                } else {
                    handles.clear();
                }
                WIDGET.render();
            }
        }
        UiNodeOp::Render { frame } => {
            child.render(frame);

            if valid && enabled.get() {
                let widths = PxSideOffsets::new_all_same(Px(1));
                let fail_sides = BorderSides::solid(colors::RED);
                let hits_sides = BorderSides::solid(web_colors::LIME_GREEN);

                frame.with_hit_tests_disabled(|frame| {
                    for fail in &fails {
                        if !fail.size.is_empty() {
                            frame.push_border(*fail, widths, fail_sides, PxCornerRadius::zero());
                        }
                    }

                    for hit in &hits {
                        if !hit.size.is_empty() {
                            frame.push_border(*hit, widths, hits_sides, PxCornerRadius::zero());
                        }
                    }
                });
            }
        }
        _ => {}
    })
}

/// Draw the directional query for closest sibling of the hovered focusable widget.
///
/// # Window Only
///
/// This property only works if set in a window, if set in another widget it will log an error and not render anything.
#[property(CONTEXT, default(None))]
pub fn show_directional_query(child: impl UiNode, orientation: impl IntoVar<Option<Orientation2D>>) -> impl UiNode {
    let orientation = orientation.into_var();
    let mut valid = false;
    let mut search_quads = vec![];
    let mut _mouse_hovered_handle = None;

    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            valid = WIDGET.parent_id().is_none();
            if valid {
                WIDGET.sub_var(&orientation);
                if orientation.get().is_some() {
                    _mouse_hovered_handle = Some(MOUSE_HOVERED_EVENT.subscribe(WIDGET.id()));
                }
            } else {
                tracing::error!("property `show_directional_query` is only valid in a window");
            }
        }
        UiNodeOp::Deinit => {
            _mouse_hovered_handle = None;
        }
        UiNodeOp::Event { update } => {
            if !valid {
                return;
            }
            if let Some(args) = MOUSE_HOVERED_EVENT.on(update) {
                if let Some(orientation) = orientation.get() {
                    let mut none = true;
                    if let Some(target) = &args.target {
                        let tree = WINDOW.info();
                        for w_id in target.widgets_path().iter().rev() {
                            if let Some(w) = tree.get(*w_id) {
                                if let Some(w) = w.into_focusable(true, true) {
                                    let sq: Vec<_> = orientation
                                        .search_bounds(w.info().center(), Px::MAX, tree.spatial_bounds().to_box2d())
                                        .map(|q| q.to_rect())
                                        .collect();

                                    if search_quads != sq {
                                        search_quads = sq;
                                        WIDGET.render();
                                    }

                                    none = false;
                                    break;
                                }
                            }
                        }
                    }

                    if none && !search_quads.is_empty() {
                        search_quads.clear();
                        WIDGET.render();
                    }
                }
            }
        }
        UiNodeOp::Update { .. } => {
            if !valid {
                return;
            }
            if let Some(ori) = orientation.get_new() {
                search_quads.clear();

                if ori.is_some() {
                    _mouse_hovered_handle = Some(MOUSE_HOVERED_EVENT.subscribe(WIDGET.id()));
                } else {
                    _mouse_hovered_handle = None;
                }

                WIDGET.render();
            }
        }
        UiNodeOp::Render { frame } => {
            child.render(frame);

            if valid && orientation.get().is_some() {
                let widths = PxSideOffsets::new_all_same(Px(1));
                let quad_sides = BorderSides::solid(colors::YELLOW);

                frame.with_hit_tests_disabled(|frame| {
                    for quad in &search_quads {
                        if !quad.size.is_empty() {
                            frame.push_border(*quad, widths, quad_sides, PxCornerRadius::zero());
                        }
                    }
                });
            }
        }
        _ => {}
    })
}