use crate::backend;
use crate::kurbo::{Point, Vec2};
use crate::piet::ImageBuf;
use crate::Modifiers;
#[derive(Debug, Clone, PartialEq)]
pub struct MouseEvent {
pub pos: Point,
pub buttons: MouseButtons,
pub mods: Modifiers,
pub count: u8,
pub focus: bool,
pub button: MouseButton,
pub wheel_delta: Vec2,
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[repr(u8)]
pub enum MouseButton {
None,
Left,
Right,
Middle,
X1,
X2,
}
impl MouseButton {
#[inline]
pub fn is_left(self) -> bool {
self == MouseButton::Left
}
#[inline]
pub fn is_right(self) -> bool {
self == MouseButton::Right
}
#[inline]
pub fn is_middle(self) -> bool {
self == MouseButton::Middle
}
#[inline]
pub fn is_x1(self) -> bool {
self == MouseButton::X1
}
#[inline]
pub fn is_x2(self) -> bool {
self == MouseButton::X2
}
}
#[derive(PartialEq, Eq, Clone, Copy, Default)]
pub struct MouseButtons(u8);
impl MouseButtons {
#[inline]
pub fn new() -> MouseButtons {
MouseButtons(0)
}
#[inline]
pub fn insert(&mut self, button: MouseButton) {
self.0 |= 1.min(button as u8) << button as u8;
}
#[inline]
pub fn remove(&mut self, button: MouseButton) {
self.0 &= !(1.min(button as u8) << button as u8);
}
#[inline]
pub fn with(mut self, button: MouseButton) -> MouseButtons {
self.0 |= 1.min(button as u8) << button as u8;
self
}
#[inline]
pub fn without(mut self, button: MouseButton) -> MouseButtons {
self.0 &= !(1.min(button as u8) << button as u8);
self
}
#[inline]
pub fn contains(self, button: MouseButton) -> bool {
(self.0 & (1.min(button as u8) << button as u8)) != 0
}
#[inline]
pub fn is_empty(self) -> bool {
self.0 == 0
}
#[inline]
pub fn is_superset(self, buttons: MouseButtons) -> bool {
self.0 & buttons.0 == buttons.0
}
#[inline]
pub fn has_left(self) -> bool {
self.contains(MouseButton::Left)
}
#[inline]
pub fn has_right(self) -> bool {
self.contains(MouseButton::Right)
}
#[inline]
pub fn has_middle(self) -> bool {
self.contains(MouseButton::Middle)
}
#[inline]
pub fn has_x1(self) -> bool {
self.contains(MouseButton::X1)
}
#[inline]
pub fn has_x2(self) -> bool {
self.contains(MouseButton::X2)
}
pub fn extend(&mut self, buttons: MouseButtons) {
self.0 |= buttons.0;
}
#[inline]
pub fn union(mut self, other: MouseButtons) -> MouseButtons {
self.0 |= other.0;
self
}
#[inline]
pub fn clear(&mut self) {
self.0 = 0;
}
#[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)
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum Cursor {
Arrow,
IBeam,
Pointer,
Crosshair,
#[doc(hidden)]
#[deprecated(
since = "0.8.0",
note = "This will be removed because it is not available on Windows."
)]
OpenHand,
NotAllowed,
ResizeLeftRight,
ResizeUpDown,
Custom(backend::window::CustomCursor),
}
#[derive(Clone)]
pub struct CursorDesc {
#[allow(dead_code)] pub(crate) image: ImageBuf,
#[allow(dead_code)] pub(crate) hot: Point,
}
impl CursorDesc {
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"),
}
}
}