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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::{collections::HashSet, time::Duration};

use zng_app::timer::TIMERS;
use zng_ext_input::{
    gesture::{CLICK_EVENT, GESTURES},
    mouse::{ClickMode, WidgetInfoMouseExt as _, MOUSE_HOVERED_EVENT, MOUSE_INPUT_EVENT},
    pointer_capture::POINTER_CAPTURE_EVENT,
    touch::TOUCHED_EVENT,
};
use zng_view_api::{mouse::ButtonState, touch::TouchPhase};
use zng_wgt::prelude::*;

/// If the mouse pointer is over the widget or a descendant and the widget is disabled.
#[property(EVENT)]
pub fn is_hovered_disabled(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state(child, state, false, MOUSE_HOVERED_EVENT, |args| {
        if args.is_mouse_enter_disabled() {
            Some(true)
        } else if args.is_mouse_leave_disabled() {
            Some(false)
        } else {
            None
        }
    })
}

/// If the mouse pointer is over the widget or a descendant and the widget is enabled.
///
/// This state property does not consider pointer capture, if the pointer is captured by the widget
/// but is not actually over the widget this is `false`, use [`is_cap_hovered`] to include the captured state.
///
/// The value is always `false` when the widget is not [`ENABLED`], use [`is_hovered_disabled`] to implement *disabled hovered* visuals.
///
/// [`is_cap_hovered`]: fn@is_cap_hovered
/// [`ENABLED`]: Interactivity::ENABLED
/// [`is_hovered_disabled`]: fn@is_hovered_disabled
#[property(EVENT)]
pub fn is_hovered(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state(child, state, false, MOUSE_HOVERED_EVENT, |args| {
        if args.is_mouse_enter_enabled() {
            Some(true)
        } else if args.is_mouse_leave_enabled() {
            Some(false)
        } else {
            None
        }
    })
}

/// If the mouse pointer is over the widget, or a descendant, or is captured by the it.
///
/// The value is always `false` when the widget is not [`ENABLED`].
///
/// [`ENABLED`]: Interactivity::ENABLED
#[property(EVENT)]
pub fn is_cap_hovered(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state2(
        child,
        state,
        false,
        MOUSE_HOVERED_EVENT,
        false,
        |hovered_args| {
            if hovered_args.is_mouse_enter_enabled() {
                Some(true)
            } else if hovered_args.is_mouse_leave_enabled() {
                Some(false)
            } else {
                None
            }
        },
        POINTER_CAPTURE_EVENT,
        false,
        |cap_args| {
            if cap_args.is_got(WIDGET.id()) {
                Some(true)
            } else if cap_args.is_lost(WIDGET.id()) {
                Some(false)
            } else {
                None
            }
        },
        |hovered, captured| Some(hovered || captured),
    )
}

/// If the mouse pointer is pressed in the widget and it is enabled.
///
/// This is `true` when the mouse primary button started pressing in the widget
/// and the pointer is over the widget and the primary button is still pressed and
/// the widget is fully [`ENABLED`].
///
/// This state property only considers pointer capture for repeat [click modes](ClickMode), if the pointer is captured by a widget
/// with [`ClickMode::repeat`] `false` and the pointer is not actually over the widget the state is `false`,
/// use [`is_cap_mouse_pressed`] to always include the captured state.
///
/// [`ENABLED`]: Interactivity::ENABLED
/// [`is_cap_mouse_pressed`]: fn@is_cap_mouse_pressed
/// [`ClickMode::repeat`]: zng_ext_input::mouse::ClickMode::repeat
#[property(EVENT)]
pub fn is_mouse_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state3(
        child,
        state,
        false,
        MOUSE_HOVERED_EVENT,
        false,
        |hovered_args| {
            if hovered_args.is_mouse_enter_enabled() {
                Some(true)
            } else if hovered_args.is_mouse_leave_enabled() {
                Some(false)
            } else {
                None
            }
        },
        MOUSE_INPUT_EVENT,
        false,
        |input_args| {
            if input_args.is_primary() {
                match input_args.state {
                    ButtonState::Pressed => {
                        if input_args.capture_allows() {
                            return Some(input_args.is_enabled(WIDGET.id()));
                        }
                    }
                    ButtonState::Released => return Some(false),
                }
            }
            None
        },
        POINTER_CAPTURE_EVENT,
        false,
        |cap_args| {
            if cap_args.is_got(WIDGET.id()) {
                Some(true)
            } else if cap_args.is_lost(WIDGET.id()) {
                Some(false)
            } else {
                None
            }
        },
        {
            let mut info_gen = 0;
            let mut mode = ClickMode::default();

            move |hovered, is_down, is_captured| {
                // cache mode
                let tree = WINDOW.info();
                if info_gen != tree.stats().generation {
                    mode = tree.get(WIDGET.id()).unwrap().click_mode();
                    info_gen = tree.stats().generation;
                }

                if mode.repeat {
                    Some(is_down || is_captured)
                } else {
                    Some(hovered && is_down)
                }
            }
        },
    )
}

/// If the mouse pointer is pressed or captured by the widget and it is enabled.
///
/// [`ENABLED`]: Interactivity::ENABLED
#[property(EVENT)]
pub fn is_cap_mouse_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state2(
        child,
        state,
        false,
        MOUSE_INPUT_EVENT,
        false,
        |input_args| {
            if input_args.is_primary() {
                match input_args.state {
                    ButtonState::Pressed => {
                        if input_args.capture_allows() {
                            return Some(input_args.is_enabled(WIDGET.id()));
                        }
                    }
                    ButtonState::Released => return Some(false),
                }
            }
            None
        },
        POINTER_CAPTURE_EVENT,
        false,
        |cap_args| {
            if cap_args.is_got(WIDGET.id()) {
                Some(true)
            } else if cap_args.is_lost(WIDGET.id()) {
                Some(false)
            } else {
                None
            }
        },
        |is_down, is_captured| Some(is_down || is_captured),
    )
}

/// If the widget was clicked by shortcut or accessibility event and the [`shortcut_pressed_duration`] has not elapsed.
///
/// [`shortcut_pressed_duration`]: GESTURES::shortcut_pressed_duration
#[property(EVENT)]
pub fn is_shortcut_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    let state = state.into_var();
    let mut shortcut_press = None;

    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            let _ = state.set(false);
            WIDGET.sub_event(&CLICK_EVENT);
        }
        UiNodeOp::Deinit => {
            let _ = state.set(false);
        }
        UiNodeOp::Event { update } => {
            if let Some(args) = CLICK_EVENT.on(update) {
                if (args.is_from_keyboard() || args.is_from_access()) && args.is_enabled(WIDGET.id()) {
                    // if a shortcut click happened, we show pressed for the duration of `shortcut_pressed_duration`
                    // unless we where already doing that, then we just stop showing pressed, this causes
                    // a flickering effect when rapid clicks are happening.
                    if shortcut_press.take().is_none() {
                        let duration = GESTURES.shortcut_pressed_duration().get();
                        if duration != Duration::default() {
                            let dl = TIMERS.deadline(duration);
                            dl.subscribe(UpdateOp::Update, WIDGET.id()).perm();
                            shortcut_press = Some(dl);
                            let _ = state.set(true);
                        }
                    } else {
                        let _ = state.set(false);
                    }
                }
            }
        }
        UiNodeOp::Update { updates } => {
            child.update(updates);

            if let Some(timer) = &shortcut_press {
                if timer.is_new() {
                    shortcut_press = None;
                    let _ = state.set(false);
                }
            }
        }
        _ => {}
    })
}

/// If a touch contact point is over the widget or a descendant and the it is enabled.
///
/// This state property does not consider pointer capture, if the pointer is captured by the widget
/// but is not actually over the widget this is `false`, use [`is_cap_touched`] to include the captured state.
///
/// This state property also does not consider where the touch started, if it started in a different widget
/// and is not over this widget the widget is touched, use [`is_touched_from_start`] to ignore touched that move in.
///
/// The value is always `false` when the widget is not [`ENABLED`].
///
/// [`is_cap_touched`]: fn@is_cap_touched
/// [`is_touched_from_start`]: fn@is_touched_from_start
/// [`ENABLED`]: Interactivity::ENABLED
#[property(EVENT)]
pub fn is_touched(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state(child, state, false, TOUCHED_EVENT, |args| {
        if args.is_touch_enter_enabled() {
            Some(true)
        } else if args.is_touch_leave_enabled() {
            Some(false)
        } else {
            None
        }
    })
}

/// If a touch contact that started over the widget is over it and it is enabled.
///
/// This state property does not consider pointer capture, if the pointer is captured by the widget
/// but is not actually over the widget this is `false`, use [`is_cap_touched_from_start`] to include the captured state.
///
/// The value is always `false` when the widget is not [`ENABLED`].
///
/// [`ENABLED`]: Interactivity::ENABLED
/// [`is_cap_touched_from_start`]: fn@is_cap_touched_from_start
#[property(EVENT)]
pub fn is_touched_from_start(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    #[expect(clippy::mutable_key_type)] // EventPropagationHandle compares pointers, not value
    let mut touches_started = HashSet::new();
    event_state(child, state, false, TOUCHED_EVENT, move |args| {
        if args.is_touch_enter_enabled() {
            match args.phase {
                TouchPhase::Start => {
                    touches_started.retain(|t: &EventPropagationHandle| !t.is_stopped()); // for touches released outside the widget.
                    touches_started.insert(args.touch_propagation.clone());
                    Some(true)
                }
                TouchPhase::Move => Some(touches_started.contains(&args.touch_propagation)),
                TouchPhase::End | TouchPhase::Cancel => Some(false), // weird
            }
        } else if args.is_touch_leave_enabled() {
            if let TouchPhase::End | TouchPhase::Cancel = args.phase {
                touches_started.remove(&args.touch_propagation);
            }
            Some(false)
        } else {
            None
        }
    })
}

/// If a touch contact point is over the widget, or is over a descendant, or is captured by it.
///
/// The value is always `false` when the widget is not [`ENABLED`].
///
/// [`ENABLED`]: Interactivity::ENABLED
#[property(EVENT)]
pub fn is_cap_touched(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    event_state2(
        child,
        state,
        false,
        TOUCHED_EVENT,
        false,
        |hovered_args| {
            if hovered_args.is_touch_enter_enabled() {
                Some(true)
            } else if hovered_args.is_touch_leave_enabled() {
                Some(false)
            } else {
                None
            }
        },
        POINTER_CAPTURE_EVENT,
        false,
        |cap_args| {
            if cap_args.is_got(WIDGET.id()) {
                Some(true)
            } else if cap_args.is_lost(WIDGET.id()) {
                Some(false)
            } else {
                None
            }
        },
        |hovered, captured| Some(hovered || captured),
    )
}

/// If a touch contact point is over the widget, or is over a descendant, or is captured by it.
///
/// The value is always `false` when the widget is not [`ENABLED`].
///
/// [`ENABLED`]: Interactivity::ENABLED
#[property(EVENT)]
pub fn is_cap_touched_from_start(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    #[expect(clippy::mutable_key_type)] // EventPropagationHandle compares pointers, not value
    let mut touches_started = HashSet::new();
    event_state2(
        child,
        state,
        false,
        TOUCHED_EVENT,
        false,
        move |hovered_args| {
            if hovered_args.is_touch_enter_enabled() {
                match hovered_args.phase {
                    TouchPhase::Start => {
                        touches_started.retain(|t: &EventPropagationHandle| !t.is_stopped()); // for touches released outside the widget.
                        touches_started.insert(hovered_args.touch_propagation.clone());
                        Some(true)
                    }
                    TouchPhase::Move => Some(touches_started.contains(&hovered_args.touch_propagation)),
                    TouchPhase::End | TouchPhase::Cancel => Some(false), // weird
                }
            } else if hovered_args.is_touch_leave_enabled() {
                if let TouchPhase::End | TouchPhase::Cancel = hovered_args.phase {
                    touches_started.remove(&hovered_args.touch_propagation);
                }
                Some(false)
            } else {
                None
            }
        },
        POINTER_CAPTURE_EVENT,
        false,
        |cap_args| {
            if cap_args.is_got(WIDGET.id()) {
                Some(true)
            } else if cap_args.is_lost(WIDGET.id()) {
                Some(false)
            } else {
                None
            }
        },
        |hovered, captured| Some(hovered || captured),
    )
}

/// If [`is_mouse_pressed`] or [`is_touched_from_start`].
///
/// Note that [`is_mouse_pressed`] and [`is_touched_from_start`] do not consider pointer capture, use [`is_cap_pointer_pressed`] to
/// include the captured state.
///
/// [`is_mouse_pressed`]: fn@is_mouse_pressed
/// [`is_touched_from_start`]: fn@is_touched_from_start
/// [`is_cap_pointer_pressed`]: fn@is_cap_pointer_pressed
#[property(EVENT)]
pub fn is_pointer_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    let pressed = state_var();
    let child = is_mouse_pressed(child, pressed.clone());

    let touched = state_var();
    let child = is_touched_from_start(child, touched.clone());

    bind_state(child, merge_var!(pressed, touched, |&p, &t| p || t), state)
}

/// If [`is_mouse_pressed`], [`is_touched_from_start`] or [`is_shortcut_pressed`].
///
/// Note that [`is_mouse_pressed`] and [`is_touched_from_start`] do not consider pointer capture, use [`is_cap_pressed`] to
/// include the captured state.
///
/// [`shortcut_pressed_duration`]: Gestures::shortcut_pressed_duration
/// [`is_mouse_pressed`]: fn@is_mouse_pressed
/// [`is_touched_from_start`]: fn@is_touched_from_start
/// [`is_shortcut_pressed`]: fn@is_shortcut_pressed
/// [`is_cap_pressed`]: fn@is_cap_pressed
#[property(EVENT)]
pub fn is_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    let pressed = state_var();
    let child = is_mouse_pressed(child, pressed.clone());

    let touched = state_var();
    let child = is_touched_from_start(child, touched.clone());

    let shortcut_pressed = state_var();
    let child = is_shortcut_pressed(child, shortcut_pressed.clone());

    bind_state(
        child,
        merge_var!(pressed, touched, shortcut_pressed, |&p, &t, &s| p || t || s),
        state,
    )
}

/// If [`is_cap_mouse_pressed`] or [`is_cap_touched_from_start`].
///
/// [`is_cap_mouse_pressed`]: fn@is_cap_mouse_pressed
/// [`is_cap_touched_from_start`]: fn@is_cap_touched_from_start
#[property(EVENT)]
pub fn is_cap_pointer_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    let pressed = state_var();
    let child = is_cap_mouse_pressed(child, pressed.clone());

    let touched = state_var();
    let child = is_cap_touched_from_start(child, touched.clone());

    bind_state(child, merge_var!(pressed, touched, |&p, &t| p || t), state)
}

/// If [`is_cap_mouse_pressed`], [`is_cap_touched_from_start`] or [`is_shortcut_pressed`].
///
/// [`is_cap_mouse_pressed`]: fn@is_cap_mouse_pressed
/// [`is_cap_touched_from_start`]: fn@is_cap_touched_from_start
/// [`is_shortcut_pressed`]: fn@is_shortcut_pressed
#[property(EVENT)]
pub fn is_cap_pressed(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
    let pressed = state_var();
    let child = is_cap_mouse_pressed(child, pressed.clone());

    let touched = state_var();
    let child = is_cap_touched_from_start(child, touched.clone());

    let shortcut_pressed = state_var();
    let child = is_shortcut_pressed(child, pressed.clone());

    bind_state(
        child,
        merge_var!(pressed, touched, shortcut_pressed, |&p, &t, &s| p || t || s),
        state,
    )
}