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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Event handling: events
#[allow(unused)] use super::{Event, EventState}; // for doc-links
use super::{EventCx, GrabMode, IsUsed, MouseGrab, TouchGrab};
use crate::event::cx::GrabDetails;
use crate::event::{CursorIcon, MouseButton, Unused, Used};
use crate::geom::Coord;
use crate::{Action, Id};
/// Source of `EventChild::Press`
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PressSource {
/// A mouse click
///
/// Arguments: `button, repeats`.
///
/// The `repeats` argument is used for double-clicks and similar. For a
/// single-click, `repeats == 1`; for a double-click it is 2, for a
/// triple-click it is 3, and so on (without upper limit).
///
/// For `PressMove` and `PressEnd` events delivered with a mouse-grab,
/// both arguments are copied from the initiating `PressStart` event.
/// For `CursorMove` delivered without a grab (only possible with pop-ups)
/// a fake `button` value is used and `repeats == 0`.
Mouse(MouseButton, u32),
/// A touch event (with given `id`)
Touch(u64),
}
impl PressSource {
/// Returns true if this represents the left mouse button or a touch event
#[inline]
pub fn is_primary(self) -> bool {
match self {
PressSource::Mouse(button, _) => button == MouseButton::Left,
PressSource::Touch(_) => true,
}
}
/// Returns true if this represents the right mouse button
#[inline]
pub fn is_secondary(self) -> bool {
matches!(self, PressSource::Mouse(MouseButton::Right, _))
}
/// Returns true if this represents the middle mouse button
#[inline]
pub fn is_tertiary(self) -> bool {
matches!(self, PressSource::Mouse(MouseButton::Middle, _))
}
/// Returns true if this represents a touch event
#[inline]
pub fn is_touch(self) -> bool {
matches!(self, PressSource::Touch(_))
}
/// The `repetitions` value
///
/// This is 1 for a single-click and all touch events, 2 for a double-click,
/// 3 for a triple-click, etc. For `CursorMove` without a grab this is 0.
#[inline]
pub fn repetitions(self) -> u32 {
match self {
PressSource::Mouse(_, repetitions) => repetitions,
PressSource::Touch(_) => 1,
}
}
}
/// Details of press events
#[crate::autoimpl(Deref using self.source)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Press {
/// Source
pub source: PressSource,
/// Identifier of current widget
pub id: Option<Id>,
/// Current coordinate
pub coord: Coord,
}
impl Press {
/// Grab pan/move/press-end events for widget `id`
///
/// There are three types of grab ([`GrabMode`]):
///
/// - `Click`: send the corresponding [`Event::PressEnd`] only
/// - `Grab` (the default): send [`Event::PressMove`] and [`Event::PressEnd`]
/// - Pan modes: send [`Event::Pan`] on motion.
/// Note: this is most useful when grabbing multiple touch events.
///
/// Only a single mouse grab is allowed at any one time; requesting a
/// second will cancel the first (sending [`Event::PressEnd`] with
/// `success: false`).
///
/// [`EventState::is_depressed`] will return true for the grabbing widget.
/// Call [`EventState::set_grab_depress`] on `PressMove` to update the
/// grab's depress target. (This is done automatically for
/// [`GrabMode::Click`], and ends automatically when the grab ends.)
///
/// This method uses the builder pattern. On completion, [`Used`]
/// is returned. It is expected that the requested press/pan events are all
/// "used" ([`Used`]).
#[inline]
pub fn grab(&self, id: Id) -> GrabBuilder {
GrabBuilder {
id,
source: self.source,
coord: self.coord,
mode: GrabMode::Grab,
cursor: None,
}
}
}
/// Bulider pattern (see [`Press::grab`])
///
/// Conclude by calling [`Self::with_cx`].
#[must_use]
pub struct GrabBuilder {
id: Id,
source: PressSource,
coord: Coord,
mode: GrabMode,
cursor: Option<CursorIcon>,
}
impl GrabBuilder {
/// Set grab mode (default: [`GrabMode::Grab`])
#[inline]
pub fn with_mode(mut self, mode: GrabMode) -> Self {
self.mode = mode;
self
}
/// Set cursor icon (default: do not set)
#[inline]
pub fn with_icon(self, icon: CursorIcon) -> Self {
self.with_opt_icon(Some(icon))
}
/// Optionally set cursor icon (default: do not set)
#[inline]
pub fn with_opt_icon(mut self, icon: Option<CursorIcon>) -> Self {
self.cursor = icon;
self
}
/// Complete the grab, providing the [`EventCx`]
///
/// In case of an existing grab for the same [`source`](Press::source),
/// - If the [`Id`] differs this fails (returns [`Unused`])
/// - If the [`MouseButton`] differs this fails (technically this is a
/// different `source`, but simultaneous grabs of multiple mouse buttons
/// are not supported).
/// - If one grab is a [pan](GrabMode::is_pan) and the other is not, this fails
/// - [`GrabMode::Click`] may be upgraded to [`GrabMode::Grab`]
/// - Changing from one pan mode to another is an error
/// - Mouse button repetitions may be increased; decreasing is an error
/// - A [`CursorIcon`] may be set
/// - The depress target is re-set to the grabbing widget
///
/// Note: error conditions are only checked in debug builds. These cases
/// may need revision.
pub fn with_cx(self, cx: &mut EventCx) -> IsUsed {
let GrabBuilder {
id,
source,
coord,
mode,
cursor,
} = self;
log::trace!(target: "kas_core::event", "grab_press: start_id={id}, source={source:?}");
match source {
PressSource::Mouse(button, repetitions) => {
let details = match mode {
GrabMode::Click => GrabDetails::Click {
cur_id: Some(id.clone()),
},
GrabMode::Grab => GrabDetails::Grab,
mode => {
assert!(mode.is_pan());
let g = cx.set_pan_on(id.clone(), mode, false, coord);
GrabDetails::Pan(g)
}
};
if let Some(ref mut grab) = cx.mouse_grab {
if grab.start_id != id
|| grab.button != button
|| grab.details.is_pan() != mode.is_pan()
{
return Unused;
}
debug_assert!(repetitions >= grab.repetitions);
grab.repetitions = repetitions;
grab.depress = Some(id.clone());
grab.details = details;
} else {
cx.mouse_grab = Some(MouseGrab {
button,
repetitions,
start_id: id.clone(),
depress: Some(id.clone()),
details,
});
}
if let Some(icon) = cursor {
cx.window.set_cursor_icon(icon);
}
}
PressSource::Touch(touch_id) => {
if let Some(grab) = cx.get_touch(touch_id) {
if grab.mode.is_pan() != mode.is_pan() {
return Unused;
}
grab.depress = Some(id.clone());
grab.cur_id = Some(id.clone());
grab.last_move = coord;
grab.coord = coord;
grab.mode = grab.mode.max(mode);
} else {
let mut pan_grab = (u16::MAX, 0);
if mode.is_pan() {
pan_grab = cx.set_pan_on(id.clone(), mode, true, coord);
}
cx.touch_grab.push(TouchGrab {
id: touch_id,
start_id: id.clone(),
depress: Some(id.clone()),
cur_id: Some(id.clone()),
last_move: coord,
coord,
mode,
pan_grab,
});
}
}
}
cx.action(id, Action::REDRAW);
Used
}
}