pub struct TkAction { /* private fields */ }
Expand description
Action required after processing
This type is returned by many widgets on modification to self and is tracked
internally by event::EventMgr
to determine which updates are needed to
the UI.
Two TkAction
values may be combined via bit-or (a | b
). Bit-or
assignments are supported by both TkAction
and event::EventMgr
.
Users receiving a value of this type from a widget update method should
usually handle with *mgr |= action;
. Before the event loop starts
(toolkit.run()
) or if the widget in question is not part of a UI these
values can be ignored.
Implementations§
source§impl TkAction
impl TkAction
sourcepub const REDRAW: Self = _
pub const REDRAW: Self = _
The whole window requires redrawing
Note that [event::EventMgr::redraw
] can instead be used for more
selective redrawing.
sourcepub const REGION_MOVED: Self = _
pub const REGION_MOVED: Self = _
Some widgets within a region moved
Used when a pop-up is closed or a region adjusted (e.g. scroll or switch tab) to update which widget is under the mouse cursor / touch events. Identifier is that of the parent widget/window encapsulating the region.
Implies window redraw.
sourcepub const THEME_UPDATE: Self = _
pub const THEME_UPDATE: Self = _
Update theme memory
sourcepub const RECONFIGURE: Self = _
pub const RECONFIGURE: Self = _
Reconfigure all widgets of the window
Configuring widgets assigns WidgetId
identifiers and calls
crate::Widget::configure
.
sourcepub const fn empty() -> Self
pub const fn empty() -> Self
Returns an empty set of flags.
Examples found in repository?
More examples
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
pub fn new(config: SharedRc<Config>, scale_factor: f32, dpem: f32) -> Self {
EventState {
config: WindowConfig::new(config, scale_factor, dpem),
disabled: vec![],
window_has_focus: false,
modifiers: ModifiersState::empty(),
char_focus: false,
sel_focus: None,
nav_focus: None,
nav_fallback: None,
hover: None,
hover_icon: CursorIcon::Default,
key_depress: Default::default(),
last_mouse_coord: Coord::ZERO,
last_click_button: FAKE_MOUSE_BUTTON,
last_click_repetitions: 0,
last_click_timeout: Instant::now(), // unimportant value
mouse_grab: None,
touch_grab: Default::default(),
pan_grab: SmallVec::new(),
accel_layers: Default::default(),
popups: Default::default(),
popup_removed: Default::default(),
time_updates: vec![],
pending: Default::default(),
action: TkAction::empty(),
}
}
/// Update scale factor
pub fn set_scale_factor(&mut self, scale_factor: f32, dpem: f32) {
self.config.update(scale_factor, dpem);
}
/// Configure event manager for a widget tree.
///
/// This should be called by the toolkit on the widget tree when the window
/// is created (before or after resizing).
///
/// This method calls [`ConfigMgr::configure`] in order to assign
/// [`WidgetId`] identifiers and call widgets' [`Widget::configure`]
/// method. Additionally, it updates the [`EventState`] to account for
/// renamed and removed widgets.
pub fn full_configure(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
log::debug!(target: "kas_core::event::manager", "full_configure");
self.action.remove(TkAction::RECONFIGURE);
// These are recreated during configure:
self.accel_layers.clear();
self.nav_fallback = None;
self.new_accel_layer(WidgetId::ROOT, false);
shell.size_and_draw_shared(&mut |size, draw_shared| {
let mut mgr = ConfigMgr::new(size, draw_shared, self);
mgr.configure(WidgetId::ROOT, widget);
});
let hover = widget.find_id(self.last_mouse_coord);
self.with(shell, |mgr| mgr.set_hover(hover));
}
/// Update the widgets under the cursor and touch events
pub fn region_moved(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
log::trace!(target: "kas_core::event::manager", "region_moved");
// Note: redraw is already implied.
// Update hovered widget
let hover = widget.find_id(self.last_mouse_coord);
self.with(shell, |mgr| mgr.set_hover(hover));
for grab in self.touch_grab.iter_mut() {
grab.cur_id = widget.find_id(grab.coord);
}
}
/// Get the next resume time
pub fn next_resume(&self) -> Option<Instant> {
self.time_updates.last().map(|time| time.0)
}
/// Construct a [`EventMgr`] referring to this state
///
/// Invokes the given closure on this [`EventMgr`].
#[inline]
pub fn with<F>(&mut self, shell: &mut dyn ShellWindow, f: F)
where
F: FnOnce(&mut EventMgr),
{
let mut mgr = EventMgr {
state: self,
shell,
messages: vec![],
scroll: Scroll::None,
action: TkAction::empty(),
};
f(&mut mgr);
let action = mgr.action;
drop(mgr);
self.send_action(action);
}
/// Update, after receiving all events
#[inline]
pub fn update(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) -> TkAction {
let old_hover_icon = self.hover_icon;
let mut mgr = EventMgr {
state: self,
shell,
messages: vec![],
scroll: Scroll::None,
action: TkAction::empty(),
};
while let Some((parent, wid)) = mgr.popup_removed.pop() {
mgr.send_event(widget, parent, Event::PopupRemoved(wid));
}
if let Some((id, event)) = mgr.mouse_grab().and_then(|g| g.flush_move()) {
mgr.send_event(widget, id, event);
}
for i in 0..mgr.touch_grab.len() {
if let Some((id, event)) = mgr.touch_grab[i].flush_move() {
mgr.send_event(widget, id, event);
}
}
for gi in 0..mgr.pan_grab.len() {
let grab = &mut mgr.pan_grab[gi];
debug_assert!(grab.mode != GrabMode::Grab);
assert!(grab.n > 0);
// Terminology: pi are old coordinates, qi are new coords
let (p1, q1) = (DVec2::conv(grab.coords[0].0), DVec2::conv(grab.coords[0].1));
grab.coords[0].0 = grab.coords[0].1;
let alpha;
let delta;
if grab.mode == GrabMode::PanOnly || grab.n == 1 {
alpha = DVec2(1.0, 0.0);
delta = q1 - p1;
} else {
// We don't use more than two touches: information would be
// redundant (although it could be averaged).
let (p2, q2) = (DVec2::conv(grab.coords[1].0), DVec2::conv(grab.coords[1].1));
grab.coords[1].0 = grab.coords[1].1;
let (pd, qd) = (p2 - p1, q2 - q1);
alpha = match grab.mode {
GrabMode::PanFull => qd.complex_div(pd),
GrabMode::PanScale => DVec2((qd.sum_square() / pd.sum_square()).sqrt(), 0.0),
GrabMode::PanRotate => {
let a = qd.complex_div(pd);
a / a.sum_square().sqrt()
}
_ => unreachable!(),
};
// Average delta from both movements:
delta = (q1 - alpha.complex_mul(p1) + q2 - alpha.complex_mul(p2)) * 0.5;
}
let id = grab.id.clone();
if alpha != DVec2(1.0, 0.0) || delta != DVec2::ZERO {
let event = Event::Pan { alpha, delta };
mgr.send_event(widget, id, event);
}
}
// Warning: infinite loops are possible here if widgets always queue a
// new pending event when evaluating one of these:
while let Some(item) = mgr.pending.pop_front() {
log::trace!(target: "kas_core::event::manager", "update: handling Pending::{item:?}");
let (id, event) = match item {
Pending::SetNavFocus(id, key_focus) => (id, Event::NavFocus(key_focus)),
Pending::MouseHover(id) => (id, Event::MouseHover),
Pending::LostNavFocus(id) => (id, Event::LostNavFocus),
Pending::LostMouseHover(id) => {
mgr.hover_icon = Default::default();
(id, Event::LostMouseHover)
}
Pending::LostCharFocus(id) => (id, Event::LostCharFocus),
Pending::LostSelFocus(id) => (id, Event::LostSelFocus),
};
mgr.send_event(widget, id, event);
}
let action = mgr.action;
drop(mgr);
if self.hover_icon != old_hover_icon && self.mouse_grab.is_none() {
shell.set_cursor_icon(self.hover_icon);
}
let action = action | self.action;
self.action = TkAction::empty();
action
}
sourcepub const fn from_bits(bits: u32) -> Option<Self>
pub const fn from_bits(bits: u32) -> Option<Self>
Convert from underlying bit representation, unless that representation contains bits that do not correspond to a flag.
sourcepub const fn from_bits_truncate(bits: u32) -> Self
pub const fn from_bits_truncate(bits: u32) -> Self
Convert from underlying bit representation, dropping any bits that do not correspond to flags.
sourcepub const unsafe fn from_bits_unchecked(bits: u32) -> Self
pub const unsafe fn from_bits_unchecked(bits: u32) -> Self
Convert from underlying bit representation, preserving all bits (even those not corresponding to a defined flag).
Safety
The caller of the bitflags!
macro can chose to allow or
disallow extra bits for their bitflags type.
The caller of from_bits_unchecked()
has to ensure that
all bits correspond to a defined flag or that extra bits
are valid for this bitflags type.
sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if no flags are currently stored.
Examples found in repository?
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
pub fn scroll_by_event(
&mut self,
mgr: &mut EventMgr,
event: Event,
id: WidgetId,
window_rect: Rect,
) -> (bool, Response) {
let mut moved = false;
match event {
Event::Command(cmd) => {
let offset = match cmd {
Command::Home => Offset::ZERO,
Command::End => self.max_offset,
cmd => {
let delta = match cmd {
Command::Left => LineDelta(-1.0, 0.0),
Command::Right => LineDelta(1.0, 0.0),
Command::Up => LineDelta(0.0, 1.0),
Command::Down => LineDelta(0.0, -1.0),
Command::PageUp => PixelDelta(Offset(0, window_rect.size.1 / 2)),
Command::PageDown => PixelDelta(Offset(0, -(window_rect.size.1 / 2))),
_ => return (false, Response::Unused),
};
let delta = match delta {
LineDelta(x, y) => mgr.config().scroll_distance((x, y)),
PixelDelta(d) => d,
};
self.offset - delta
}
};
let action = self.set_offset(offset);
if !action.is_empty() {
moved = true;
*mgr |= action;
}
mgr.set_scroll(Scroll::Rect(window_rect));
}
Event::Scroll(delta) => {
let delta = match delta {
LineDelta(x, y) => mgr.config().scroll_distance((x, y)),
PixelDelta(d) => d,
};
moved = self.scroll_by_delta(mgr, delta);
}
Event::PressStart { source, coord, .. }
if self.max_offset != Offset::ZERO && mgr.config_enable_pan(source) =>
{
let icon = Some(CursorIcon::Grabbing);
mgr.grab_press_unique(id, source, coord, icon);
}
Event::PressMove { delta, .. } => {
self.glide.move_delta(delta);
moved = self.scroll_by_delta(mgr, delta);
}
Event::PressEnd { .. } => {
if self.glide.opt_start(mgr.config().scroll_flick_timeout()) {
mgr.request_update(id, PAYLOAD_GLIDE, Duration::new(0, 0), true);
}
}
Event::TimerUpdate(pl) if pl == PAYLOAD_GLIDE => {
// Momentum/glide scrolling: update per arbitrary step time until movment stops.
let decay = mgr.config().scroll_flick_decay();
if let Some(delta) = self.glide.step(decay) {
let action = self.set_offset(self.offset - delta);
if !action.is_empty() {
*mgr |= action;
moved = true;
}
if delta == Offset::ZERO || !action.is_empty() {
// Note: when FPS > pixels/sec, delta may be zero while
// still scrolling. Glide returns None when we're done,
// but we're also done if unable to scroll further.
let dur = Duration::from_millis(GLIDE_POLL_MS);
mgr.request_update(id, PAYLOAD_GLIDE, dur, true);
mgr.set_scroll(Scroll::Scrolled);
}
}
}
_ => return (false, Response::Unused),
}
(moved, Response::Used)
}
sourcepub const fn intersects(&self, other: Self) -> bool
pub const fn intersects(&self, other: Self) -> bool
Returns true
if there are flags common to both self
and other
.
sourcepub const fn contains(&self, other: Self) -> bool
pub const fn contains(&self, other: Self) -> bool
Returns true
if all of the flags in other
are contained within self
.
sourcepub fn remove(&mut self, other: Self)
pub fn remove(&mut self, other: Self)
Removes the specified flags in-place.
Examples found in repository?
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
pub fn full_configure(&mut self, shell: &mut dyn ShellWindow, widget: &mut dyn Widget) {
log::debug!(target: "kas_core::event::manager", "full_configure");
self.action.remove(TkAction::RECONFIGURE);
// These are recreated during configure:
self.accel_layers.clear();
self.nav_fallback = None;
self.new_accel_layer(WidgetId::ROOT, false);
shell.size_and_draw_shared(&mut |size, draw_shared| {
let mut mgr = ConfigMgr::new(size, draw_shared, self);
mgr.configure(WidgetId::ROOT, widget);
});
let hover = widget.find_id(self.last_mouse_coord);
self.with(shell, |mgr| mgr.set_hover(hover));
}
sourcepub fn set(&mut self, other: Self, value: bool)
pub fn set(&mut self, other: Self, value: bool)
Inserts or removes the specified flags depending on the passed value.
sourcepub const fn intersection(self, other: Self) -> Self
pub const fn intersection(self, other: Self) -> Self
Returns the intersection between the flags in self
and
other
.
Specifically, the returned set contains only the flags which are
present in both self
and other
.
This is equivalent to using the &
operator (e.g.
ops::BitAnd
), as in flags & other
.
sourcepub const fn union(self, other: Self) -> Self
pub const fn union(self, other: Self) -> Self
Returns the union of between the flags in self
and other
.
Specifically, the returned set contains all flags which are
present in either self
or other
, including any which are
present in both (see Self::symmetric_difference
if that
is undesirable).
This is equivalent to using the |
operator (e.g.
ops::BitOr
), as in flags | other
.
sourcepub const fn difference(self, other: Self) -> Self
pub const fn difference(self, other: Self) -> Self
Returns the difference between the flags in self
and other
.
Specifically, the returned set contains all flags present in
self
, except for the ones present in other
.
It is also conceptually equivalent to the “bit-clear” operation:
flags & !other
(and this syntax is also supported).
This is equivalent to using the -
operator (e.g.
ops::Sub
), as in flags - other
.
sourcepub const fn symmetric_difference(self, other: Self) -> Self
pub const fn symmetric_difference(self, other: Self) -> Self
Returns the symmetric difference between the flags
in self
and other
.
Specifically, the returned set contains the flags present which
are present in self
or other
, but that are not present in
both. Equivalently, it contains the flags present in exactly
one of the sets self
and other
.
This is equivalent to using the ^
operator (e.g.
ops::BitXor
), as in flags ^ other
.
sourcepub const fn complement(self) -> Self
pub const fn complement(self) -> Self
Returns the complement of this set of flags.
Specifically, the returned set contains all the flags which are
not set in self
, but which are allowed for this type.
Alternatively, it can be thought of as the set difference
between Self::all()
and self
(e.g. Self::all() - self
)
This is equivalent to using the !
operator (e.g.
ops::Not
), as in !flags
.
Trait Implementations§
source§impl BitAndAssign<TkAction> for TkAction
impl BitAndAssign<TkAction> for TkAction
source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
Disables all flags disabled in the set.
source§impl<'a> BitOrAssign<TkAction> for ConfigMgr<'a>
impl<'a> BitOrAssign<TkAction> for ConfigMgr<'a>
source§fn bitor_assign(&mut self, action: TkAction)
fn bitor_assign(&mut self, action: TkAction)
|=
operation. Read moresource§impl<'a> BitOrAssign<TkAction> for DrawMgr<'a>
impl<'a> BitOrAssign<TkAction> for DrawMgr<'a>
source§fn bitor_assign(&mut self, action: TkAction)
fn bitor_assign(&mut self, action: TkAction)
|=
operation. Read moresource§impl<'a> BitOrAssign<TkAction> for EventMgr<'a>
impl<'a> BitOrAssign<TkAction> for EventMgr<'a>
source§fn bitor_assign(&mut self, action: TkAction)
fn bitor_assign(&mut self, action: TkAction)
|=
operation. Read moresource§impl BitOrAssign<TkAction> for EventState
impl BitOrAssign<TkAction> for EventState
source§fn bitor_assign(&mut self, action: TkAction)
fn bitor_assign(&mut self, action: TkAction)
|=
operation. Read moresource§impl BitOrAssign<TkAction> for TkAction
impl BitOrAssign<TkAction> for TkAction
source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
Adds the set of flags.
source§impl BitXorAssign<TkAction> for TkAction
impl BitXorAssign<TkAction> for TkAction
source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
Toggles the set of flags.
source§impl Extend<TkAction> for TkAction
impl Extend<TkAction> for TkAction
source§fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
fn extend<T: IntoIterator<Item = Self>>(&mut self, iterator: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl FromIterator<TkAction> for TkAction
impl FromIterator<TkAction> for TkAction
source§fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iterator: T) -> Self
source§impl Ord for TkAction
impl Ord for TkAction
source§impl PartialEq<TkAction> for TkAction
impl PartialEq<TkAction> for TkAction
source§impl PartialOrd<TkAction> for TkAction
impl PartialOrd<TkAction> for TkAction
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<TkAction> for TkAction
impl SubAssign<TkAction> for TkAction
source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Disables all flags enabled in the set.
impl Copy for TkAction
impl Eq for TkAction
impl StructuralEq for TkAction
impl StructuralPartialEq for TkAction
Auto Trait Implementations§
impl RefUnwindSafe for TkAction
impl Send for TkAction
impl Sync for TkAction
impl Unpin for TkAction
impl UnwindSafe for TkAction
Blanket Implementations§
source§impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.