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
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) middle_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>,
pub(super) link: Option<super::LinkActivation>,
pub(super) files_hovering: bool,
pub(super) files_dropped: bool,
}
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 middle button was pressed and released over this widget, which senses it
/// ([`Sense::MIDDLE`](super::Sense::MIDDLE)).
pub fn middle_clicked(self) -> bool {
self.middle_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;
}
/// How a link was activated during this pass: a primary click or Enter/Space, or a
/// middle click or Ctrl+click. Reported once per user action; `None` for anything that
/// is not a link. [`Self::clicked`] is true for the primary kind only.
pub fn link_activation(self) -> Option<super::LinkActivation> {
self.link
}
pub(super) fn set_link(&mut self, activation: Option<super::LinkActivation>) {
self.link = activation;
self.clicked = activation == Some(super::LinkActivation::Primary);
}
/// Files from the system are being dragged over this widget, which is a
/// [`DropTarget`](crate::DropTarget) that takes them. Use it to highlight the zone.
pub fn files_hovering(self) -> bool {
self.files_hovering
}
/// Files from the system were dropped on this widget in this pass, once; take them from
/// [`DropOutput::dropped_files`](crate::DropOutput::dropped_files).
pub fn files_dropped(self) -> bool {
self.files_dropped
}
/// 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.middle_clicked
|| self.drag_started
|| self.drag_stopped
|| self.drag_delta != Vec2::ZERO
|| self.menu_selected.is_some()
|| self.link.is_some()
|| self.files_dropped
}
/// True when this widget changed its bound value during the current UI pass.
pub fn changed(self) -> bool {
self.changed
}
}