From 990f2b310bfecf2e04a8a462f6939833080c62bf Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sat, 19 Oct 2024 18:20:09 +1300 Subject: Complete rewrite of Phosphor The previous version of the library passed events to an implementation of a WindowController trait by calling the trait method associated with each event, and received requests by calling different trait methods and reading the returned values. This had the downside of requiring that any data received from one event had to be stored in the type so that it could be passed back to Phosphor when a request method was called. The new library structure uses a single handle_event method on a trait, which is passed data representing any single event when it is called. Data is returned via a passed mutable reference to an EventQueue, meaning that any number of responses for any event can be immediately returned to Phosphor without having to wait in storage. --- src/events.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/events.rs (limited to 'src/events.rs') diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..b738e5b --- /dev/null +++ b/src/events.rs @@ -0,0 +1,115 @@ +use crate::*; + +use winit::dpi::PhysicalSize; +use winit::event::ElementState; +use winit::keyboard::KeyCode; +use winit::window::CursorIcon; + +use std::path::PathBuf; + + +pub enum Request { + SetTitle(String), + SetSize(Dimensions), + SetSizeBounds(SizeBounds), + SetResizable(bool), + SetFullscreen(bool), + SetVisible(bool), + SetPixelScale(u32), + SetCursor(Option), + Redraw, + CreateWindow(WindowBuilder), + CloseWindow, +} + + +#[derive(Debug)] +pub enum Event { + Initialise, + CloseRequest, + Close, + Resize(Dimensions), + FocusChange(bool), + CursorEnter, + CursorExit, + CursorMove(Position), + ScrollLines { axis: Axis, distance: f32 }, + ScrollPixels { axis: Axis, distance: f32 }, + MouseButton { button: MouseButton, action: Action }, + KeyboardInput { key: KeyCode, action: Action }, + CharacterInput(char), + ModifierChange(ModifiersState), + FileDrop(PathBuf), +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Action { + Pressed, + Released +} + +impl Action { + pub fn is_pressed(self) -> bool { + self == Action::Pressed + } +} + +impl From for Action { + fn from(value: ElementState) -> Self { + match value { + ElementState::Pressed => Action::Pressed, + ElementState::Released => Action::Released, + } + } +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Axis { + Horizontal, + Vertical, +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum MouseButton { + Left, + Middle, + Right, +} + + +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct SizeBounds { + pub min_width: Option, + pub max_width: Option, + pub min_height: Option, + pub max_height: Option, +} + +impl SizeBounds { + pub fn as_min_max_size(&self, scale: u32) -> (PhysicalSize, PhysicalSize) { + ( + PhysicalSize { + width: self.min_width.unwrap_or(0).saturating_mul(scale), + height: self.min_height.unwrap_or(0).saturating_mul(scale), + }, + PhysicalSize { + width: self.max_width.unwrap_or(u32::MAX).saturating_mul(scale), + height: self.max_height.unwrap_or(u32::MAX).saturating_mul(scale), + }, + ) + } +} + +impl Default for SizeBounds { + fn default() -> Self { + Self { + min_width: None, + max_width: None, + min_height: None, + max_height: None, + } + } +} -- cgit v1.2.3-70-g09d2