use crate::Key;
use std::cell::Cell;
use std::rc::Rc;
#[derive(Clone, Debug)]
pub struct KeyEvent {
key: Key,
shift_key: bool,
ctrl_key: bool,
alt_key: bool,
meta_key: bool,
consumed: Rc<Cell<bool>>,
}
impl KeyEvent {
pub fn new(key: Key, shift_key: bool, ctrl_key: bool, alt_key: bool, meta_key: bool) -> Self {
Self {
key,
shift_key,
ctrl_key,
alt_key,
meta_key,
consumed: Rc::new(Cell::new(false)),
}
}
pub fn key(&self) -> Key {
self.key
}
pub fn shift_key(&self) -> bool {
self.shift_key
}
pub fn ctrl_key(&self) -> bool {
self.ctrl_key
}
pub fn alt_key(&self) -> bool {
self.alt_key
}
pub fn meta_key(&self) -> bool {
self.meta_key
}
pub fn consume(&self) {
self.consumed.set(true);
}
pub fn consumed(&self) -> bool {
self.consumed.get()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FileDrop {
paths: Vec<String>,
}
impl FileDrop {
pub fn new(joined: &str) -> Self {
Self {
paths: joined
.split('\0')
.filter(|path| !path.is_empty())
.map(str::to_owned)
.collect(),
}
}
pub fn paths(&self) -> &[String] {
&self.paths
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RangeRequest {
start: u32,
count: u32,
}
impl RangeRequest {
pub fn new(start: u32, count: u32) -> Self {
Self { start, count }
}
pub fn start(&self) -> usize {
self.start as usize
}
pub fn count(&self) -> usize {
self.count as usize
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PointerKinds(pub u32);
impl PointerKinds {
pub const NONE: Self = Self(0);
pub const MOVE: Self = Self::of(crate::schema::PointerKind::Move);
pub const ENTER: Self = Self::of(crate::schema::PointerKind::Enter);
pub const LEAVE: Self = Self::of(crate::schema::PointerKind::Leave);
pub const DOWN: Self = Self::of(crate::schema::PointerKind::Down);
pub const UP: Self = Self::of(crate::schema::PointerKind::Up);
pub const CANCEL: Self = Self::of(crate::schema::PointerKind::Cancel);
pub const WHEEL: Self = Self::of(crate::schema::PointerKind::Wheel);
pub const CONTEXT_MENU: Self = Self::of(crate::schema::PointerKind::ContextMenu);
pub const ALL: Self = Self(0xff);
pub const fn of(kind: crate::schema::PointerKind) -> Self {
Self(1 << (kind as u32 - 1))
}
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub const fn contains(self, kind: crate::schema::PointerKind) -> bool {
self.0 & Self::of(kind).0 != 0
}
pub const fn bits(self) -> u32 {
self.0
}
}
impl std::ops::BitOr for PointerKinds {
type Output = Self;
fn bitor(self, other: Self) -> Self {
self.union(other)
}
}
pub mod pointer_button {
pub const PRIMARY: u32 = 1;
pub const SECONDARY: u32 = 2;
pub const TERTIARY: u32 = 4;
pub const BACK: u32 = 8;
pub const FORWARD: u32 = 16;
}
#[derive(Clone, Debug)]
pub struct PointerEvent {
kind: crate::schema::PointerKind,
pointer_id: u32,
pointer_type: crate::schema::PointerType,
x: f32,
y: f32,
buttons: u32,
button: u32,
modifiers: u32,
click_count: u32,
delta_x: f32,
delta_y: f32,
delta_mode: crate::schema::WheelDeltaMode,
timestamp_nanos: u64,
consumed: Rc<Cell<bool>>,
}
impl PointerEvent {
#[allow(clippy::too_many_arguments)]
pub fn new(
kind: crate::schema::PointerKind,
pointer_id: u32,
pointer_type: crate::schema::PointerType,
x: f32,
y: f32,
buttons: u32,
button: u32,
modifiers: u32,
click_count: u32,
delta_x: f32,
delta_y: f32,
delta_mode: crate::schema::WheelDeltaMode,
timestamp_nanos: u64,
) -> Self {
Self {
kind,
pointer_id,
pointer_type,
x,
y,
buttons,
button,
modifiers,
click_count,
delta_x,
delta_y,
delta_mode,
timestamp_nanos,
consumed: Rc::new(Cell::new(false)),
}
}
pub fn kind(&self) -> crate::schema::PointerKind {
self.kind
}
pub fn pointer_id(&self) -> u32 {
self.pointer_id
}
pub fn pointer_type(&self) -> crate::schema::PointerType {
self.pointer_type
}
pub fn x(&self) -> f32 {
self.x
}
pub fn y(&self) -> f32 {
self.y
}
pub fn buttons(&self) -> u32 {
self.buttons
}
pub fn button(&self) -> u32 {
self.button
}
pub fn shift_key(&self) -> bool {
self.modifiers & 1 != 0
}
pub fn ctrl_key(&self) -> bool {
self.modifiers & 2 != 0
}
pub fn alt_key(&self) -> bool {
self.modifiers & 4 != 0
}
pub fn meta_key(&self) -> bool {
self.modifiers & 8 != 0
}
pub fn click_count(&self) -> u32 {
self.click_count
}
pub fn delta_x(&self) -> f32 {
self.delta_x
}
pub fn delta_y(&self) -> f32 {
self.delta_y
}
pub fn delta_mode(&self) -> crate::schema::WheelDeltaMode {
self.delta_mode
}
pub fn timestamp_nanos(&self) -> u64 {
self.timestamp_nanos
}
pub fn consume(&self) {
self.consumed.set(true);
}
pub fn consumed(&self) -> bool {
self.consumed.get()
}
}