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
use crate::{Id, Rect, Vec2};
/// Visual interaction state; pressing takes precedence over hovering.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WidgetState {
Idle,
Hovered,
Pressed,
Disabled,
}
/// Widget geometry and interaction for the current UI pass.
///
/// Event methods (`clicked`, `double_clicked`, `drag_started`, ...) report one
/// user input exactly once: the pass that follows the input sees it, the next
/// pass does not. A response is plain data and stays `Copy`; it borrows nothing.
#[derive(Clone, Copy, Debug)]
pub struct Response {
pub id: Id,
pub rect: Rect,
pub hovered: bool,
pub pressed: bool,
pub has_focus: bool,
/// Keyboard focus indication. Pointer focus alone does not show a ring.
pub focus_visible: bool,
pub enabled: bool,
pub(super) clicked: bool,
pub(super) changed: bool,
pub(super) submitted: bool,
pub(super) lost_focus: bool,
pub(super) gained_focus: bool,
pub(super) double_clicked: bool,
pub(super) secondary_clicked: bool,
pub(super) drag_started: bool,
pub(super) dragging: bool,
pub(super) drag_stopped: bool,
pub(super) drag_delta: Vec2,
pub(super) menu_selected: Option<Id>,
}
impl Response {
/// Enter confirmed the text during this pass; independent of value changes.
pub fn submitted(self) -> bool {
self.submitted
}
/// The widget lost focus during this pass, including focus changes between redraws.
pub fn lost_focus(self) -> bool {
self.lost_focus
}
/// The widget received keyboard focus (Tab, press or `request_focus`) since the last pass.
pub fn gained_focus(self) -> bool {
self.gained_focus
}
pub fn state(self) -> WidgetState {
if !self.enabled {
WidgetState::Disabled
} else if self.pressed {
WidgetState::Pressed
} else if self.hovered {
WidgetState::Hovered
} else {
WidgetState::Idle
}
}
/// True on release inside the control, or on Enter/Space release when focused.
/// A press that turned into a drag is not a click.
pub fn clicked(self) -> bool {
self.clicked
}
/// The second click of a double click: same widget, within 500 ms and a few
/// pixels of the first. `clicked` is also true on both clicks.
pub fn double_clicked(self) -> bool {
self.double_clicked
}
/// Secondary button pressed and released over this widget. Context menus
/// attached to the widget open on this same signal.
pub fn secondary_clicked(self) -> bool {
self.secondary_clicked
}
/// The pointer moved past the drag threshold while pressed on this widget.
pub fn drag_started(self) -> bool {
self.drag_started
}
/// A drag is in progress, including the passes where it starts and stops.
pub fn dragged(self) -> bool {
self.dragging
}
/// The button was released (or the gesture was cancelled) after a drag.
pub fn drag_stopped(self) -> bool {
self.drag_stopped
}
/// Pointer movement in logical pixels since the previous pass. The first
/// delta of a drag starts at the press position, so the deltas of one drag
/// add up to its total displacement.
pub fn drag_delta(self) -> Vec2 {
self.drag_delta
}
/// For custom widgets: report that this widget changed its value during this pass,
/// like the built-in controls do. `Ui::add` then schedules the follow-up redraw.
pub fn mark_changed(&mut self) {
self.changed = true;
}
/// The context menu attached with [`super::Widget::context_menu`] chose this item.
pub fn menu_selected(self) -> Option<Id> {
self.menu_selected
}
/// True when this pass carries a one-shot event of any kind.
pub(super) fn has_event(self) -> bool {
self.clicked
|| self.changed
|| self.submitted
|| self.lost_focus
|| self.gained_focus
|| self.double_clicked
|| self.secondary_clicked
|| self.drag_started
|| self.drag_stopped
|| self.drag_delta != Vec2::ZERO
|| self.menu_selected.is_some()
}
/// True when this widget changed its bound value during the current UI pass.
pub fn changed(self) -> bool {
self.changed
}
}