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
// Copyright 2019 The Druid Authors.
//
// 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 at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Common types for representing mouse events and state

use crate::backend;
use crate::kurbo::{Point, Vec2};
use crate::piet::ImageBuf;
use crate::Modifiers;

/// Information about the mouse event.
///
/// Every mouse event can have a new position. There is no guarantee of
/// receiving a move event before another mouse event.
#[derive(Debug, Clone, PartialEq)]
pub struct MouseEvent {
    /// The location of the mouse in [display points] in relation to the current window.
    ///
    /// [display points]: struct.Scale.html
    pub pos: Point,
    /// Mouse buttons being held down during a move or after a click event.
    /// Thus it will contain the `button` that triggered a mouse-down event,
    /// and it will not contain the `button` that triggered a mouse-up event.
    pub buttons: MouseButtons,
    /// Keyboard modifiers at the time of the event.
    pub mods: Modifiers,
    /// The number of mouse clicks associated with this event. This will always
    /// be `0` for a mouse-up and mouse-move events.
    pub count: u8,
    /// Focus is `true` on macOS when the mouse-down event (or its companion mouse-up event)
    /// with `MouseButton::Left` was the event that caused the window to gain focus.
    pub focus: bool,
    /// The button that was pressed down in the case of mouse-down,
    /// or the button that was released in the case of mouse-up.
    /// This will always be `MouseButton::None` in the case of mouse-move.
    pub button: MouseButton,
    /// The wheel movement.
    ///
    /// The polarity is the amount to be added to the scroll position,
    /// in other words the opposite of the direction the content should
    /// move on scrolling. This polarity is consistent with the
    /// deltaX and deltaY values in a web [WheelEvent].
    ///
    /// [WheelEvent]: https://w3c.github.io/uievents/#event-type-wheel
    pub wheel_delta: Vec2,
}

/// An indicator of which mouse button was pressed.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[repr(u8)]
pub enum MouseButton {
    /// No mouse button.
    // MUST BE FIRST (== 0)
    None,
    /// Left mouse button.
    Left,
    /// Right mouse button.
    Right,
    /// Middle mouse button.
    Middle,
    /// First X button.
    X1,
    /// Second X button.
    X2,
}

impl MouseButton {
    /// Returns `true` if this is [`MouseButton::Left`].
    ///
    /// [`MouseButton::Left`]: #variant.Left
    #[inline]
    pub fn is_left(self) -> bool {
        self == MouseButton::Left
    }

    /// Returns `true` if this is [`MouseButton::Right`].
    ///
    /// [`MouseButton::Right`]: #variant.Right
    #[inline]
    pub fn is_right(self) -> bool {
        self == MouseButton::Right
    }

    /// Returns `true` if this is [`MouseButton::Middle`].
    ///
    /// [`MouseButton::Middle`]: #variant.Middle
    #[inline]
    pub fn is_middle(self) -> bool {
        self == MouseButton::Middle
    }

    /// Returns `true` if this is [`MouseButton::X1`].
    ///
    /// [`MouseButton::X1`]: #variant.X1
    #[inline]
    pub fn is_x1(self) -> bool {
        self == MouseButton::X1
    }

    /// Returns `true` if this is [`MouseButton::X2`].
    ///
    /// [`MouseButton::X2`]: #variant.X2
    #[inline]
    pub fn is_x2(self) -> bool {
        self == MouseButton::X2
    }
}

/// A set of [`MouseButton`]s.
///
/// [`MouseButton`]: enum.MouseButton.html
#[derive(PartialEq, Eq, Clone, Copy, Default)]
pub struct MouseButtons(u8);

impl MouseButtons {
    /// Create a new empty set.
    #[inline]
    pub fn new() -> MouseButtons {
        MouseButtons(0)
    }

    /// Add the `button` to the set.
    #[inline]
    pub fn insert(&mut self, button: MouseButton) {
        self.0 |= 1.min(button as u8) << button as u8;
    }

    /// Remove the `button` from the set.
    #[inline]
    pub fn remove(&mut self, button: MouseButton) {
        self.0 &= !(1.min(button as u8) << button as u8);
    }

    /// Builder-style method for adding the `button` to the set.
    #[inline]
    pub fn with(mut self, button: MouseButton) -> MouseButtons {
        self.0 |= 1.min(button as u8) << button as u8;
        self
    }

    /// Builder-style method for removing the `button` from the set.
    #[inline]
    pub fn without(mut self, button: MouseButton) -> MouseButtons {
        self.0 &= !(1.min(button as u8) << button as u8);
        self
    }

    /// Returns `true` if the `button` is in the set.
    #[inline]
    pub fn contains(self, button: MouseButton) -> bool {
        (self.0 & (1.min(button as u8) << button as u8)) != 0
    }

    /// Returns `true` if the set is empty.
    #[inline]
    pub fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Returns `true` if all the `buttons` are in the set.
    #[inline]
    pub fn is_superset(self, buttons: MouseButtons) -> bool {
        self.0 & buttons.0 == buttons.0
    }

    /// Returns `true` if [`MouseButton::Left`] is in the set.
    ///
    /// [`MouseButton::Left`]: enum.MouseButton.html#variant.Left
    #[inline]
    pub fn has_left(self) -> bool {
        self.contains(MouseButton::Left)
    }

    /// Returns `true` if [`MouseButton::Right`] is in the set.
    ///
    /// [`MouseButton::Right`]: enum.MouseButton.html#variant.Right
    #[inline]
    pub fn has_right(self) -> bool {
        self.contains(MouseButton::Right)
    }

    /// Returns `true` if [`MouseButton::Middle`] is in the set.
    ///
    /// [`MouseButton::Middle`]: enum.MouseButton.html#variant.Middle
    #[inline]
    pub fn has_middle(self) -> bool {
        self.contains(MouseButton::Middle)
    }

    /// Returns `true` if [`MouseButton::X1`] is in the set.
    ///
    /// [`MouseButton::X1`]: enum.MouseButton.html#variant.X1
    #[inline]
    pub fn has_x1(self) -> bool {
        self.contains(MouseButton::X1)
    }

    /// Returns `true` if [`MouseButton::X2`] is in the set.
    ///
    /// [`MouseButton::X2`]: enum.MouseButton.html#variant.X2
    #[inline]
    pub fn has_x2(self) -> bool {
        self.contains(MouseButton::X2)
    }

    /// Adds all the `buttons` to the set.
    pub fn extend(&mut self, buttons: MouseButtons) {
        self.0 |= buttons.0;
    }

    /// Returns a union of the values in `self` and `other`.
    #[inline]
    pub fn union(mut self, other: MouseButtons) -> MouseButtons {
        self.0 |= other.0;
        self
    }

    /// Clear the set.
    #[inline]
    pub fn clear(&mut self) {
        self.0 = 0;
    }

    /// Count the number of pressed buttons in the set.
    #[inline]
    pub fn count(self) -> u32 {
        self.0.count_ones()
    }
}

impl std::fmt::Debug for MouseButtons {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "MouseButtons({:05b})", self.0 >> 1)
    }
}

//NOTE: this currently only contains cursors that are included by default on
//both Windows and macOS. We may want to provide polyfills for various additional cursors.
/// Mouse cursors.
#[derive(Clone, PartialEq)]
pub enum Cursor {
    /// The default arrow cursor.
    Arrow,
    /// A vertical I-beam, for indicating insertion points in text.
    IBeam,
    Pointer,
    Crosshair,

    #[deprecated(note = "this will be removed in future because it is not available on windows")]
    OpenHand,
    NotAllowed,
    ResizeLeftRight,
    ResizeUpDown,
    // The platform cursor should be small. Any image data that it uses should be shared (i.e.
    // behind an `Arc` or using a platform API that does the sharing).
    Custom(backend::window::CustomCursor),
}

/// A platform-independent description of a custom cursor.
#[derive(Clone)]
pub struct CursorDesc {
    pub(crate) image: ImageBuf,
    pub(crate) hot: Point,
}

impl CursorDesc {
    /// Creates a new `CursorDesc`.
    ///
    /// `hot` is the "hot spot" of the cursor, measured in terms of the pixels in `image` with
    /// `(0, 0)` at the top left. The hot spot is the logical position of the mouse cursor within
    /// the image. For example, if the image is a picture of a arrow, the hot spot might be the
    /// coordinates of the arrow's tip.
    pub fn new(image: ImageBuf, hot: impl Into<Point>) -> CursorDesc {
        CursorDesc {
            image,
            hot: hot.into(),
        }
    }
}

impl std::fmt::Debug for Cursor {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        #[allow(deprecated)]
        match self {
            Cursor::Arrow => write!(f, "Cursor::Arrow"),
            Cursor::IBeam => write!(f, "Cursor::IBeam"),
            Cursor::Pointer => write!(f, "Cursor::Pointer"),
            Cursor::Crosshair => write!(f, "Cursor::Crosshair"),
            Cursor::OpenHand => write!(f, "Cursor::OpenHand"),
            Cursor::NotAllowed => write!(f, "Cursor::NotAllowed"),
            Cursor::ResizeLeftRight => write!(f, "Cursor::ResizeLeftRight"),
            Cursor::ResizeUpDown => write!(f, "Cursor::ResizeUpDown"),
            Cursor::Custom(_) => write!(f, "Cursor::Custom"),
        }
    }
}