From a91a5a112626250c7efb3d70aca25eeaa0c436bc Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sat, 17 Oct 2020 17:36:28 -0500 Subject: [PATCH] update x11 code --- .github/workflows/rust.yml | 0 .gitignore | 0 .rustfmt.toml | 0 Cargo.toml | 0 README.md | 0 examples/open_window.rs | 0 src/coordinates.rs | 93 ------------------------------------- src/event.rs | 0 src/keyboard.rs | 0 src/lib.rs | 4 +- src/macos/mod.rs | 0 src/macos/window.rs | 0 src/mouse_cursor.rs | 0 src/win/mod.rs | 0 src/win/window.rs | 0 src/window_info.rs | 94 +++++++++++++++++++++++++++++++++++++- src/window_open_options.rs | 0 src/x11/cursor.rs | 0 src/x11/mod.rs | 0 src/x11/window.rs | 13 +++--- src/x11/xcb_connection.rs | 0 21 files changed, 99 insertions(+), 105 deletions(-) mode change 100644 => 100755 .github/workflows/rust.yml mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .rustfmt.toml mode change 100644 => 100755 Cargo.toml mode change 100644 => 100755 README.md mode change 100644 => 100755 examples/open_window.rs delete mode 100644 src/coordinates.rs mode change 100644 => 100755 src/event.rs mode change 100644 => 100755 src/keyboard.rs mode change 100644 => 100755 src/lib.rs mode change 100644 => 100755 src/macos/mod.rs mode change 100644 => 100755 src/macos/window.rs mode change 100644 => 100755 src/mouse_cursor.rs mode change 100644 => 100755 src/win/mod.rs mode change 100644 => 100755 src/win/window.rs mode change 100644 => 100755 src/window_info.rs mode change 100644 => 100755 src/window_open_options.rs mode change 100644 => 100755 src/x11/cursor.rs mode change 100644 => 100755 src/x11/mod.rs mode change 100644 => 100755 src/x11/window.rs mode change 100644 => 100755 src/x11/xcb_connection.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.rustfmt.toml b/.rustfmt.toml old mode 100644 new mode 100755 diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/examples/open_window.rs b/examples/open_window.rs old mode 100644 new mode 100755 diff --git a/src/coordinates.rs b/src/coordinates.rs deleted file mode 100644 index 286c50f..0000000 --- a/src/coordinates.rs +++ /dev/null @@ -1,93 +0,0 @@ -use crate::WindowInfo; - -/// A point in logical coordinates -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct Point { - pub x: f64, - pub y: f64 -} - -impl Point { - /// Create a new point in logical coordinates - pub fn new(x: f64, y: f64) -> Self { - Self { x, y } - } - - /// Convert to actual physical coordinates - #[inline] - pub fn to_physical(&self, window_info: &WindowInfo) -> PhyPoint { - PhyPoint { - x: (self.x * window_info.scale()).round() as i32, - y: (self.y * window_info.scale()).round() as i32, - } - } -} - -/// A point in actual physical coordinates -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct PhyPoint { - pub x: i32, - pub y: i32 -} - -impl PhyPoint { - /// Create a new point in actual physical coordinates - pub fn new(x: i32, y: i32) -> Self { - Self { x, y } - } - - /// Convert to logical coordinates - #[inline] - pub fn to_logical(&self, window_info: &WindowInfo) -> Point { - Point { - x: f64::from(self.x) * window_info.scale_recip(), - y: f64::from(self.y) * window_info.scale_recip(), - } - } -} - -/// A size in logical coordinates -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct Size { - pub width: f64, - pub height: f64, -} - -impl Size { - /// Create a new size in logical coordinates - pub fn new(width: f64, height: f64) -> Self { - Self { width, height } - } - - /// Convert to actual physical size - #[inline] - pub fn to_physical(&self, window_info: &WindowInfo) -> PhySize { - PhySize { - width: (self.width * window_info.scale()).round() as u32, - height: (self.height * window_info.scale()).round() as u32, - } - } -} - -/// An actual size in physical coordinates -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct PhySize { - pub width: u32, - pub height: u32, -} - -impl PhySize { - /// Create a new size in actual physical coordinates - pub fn new(width: u32, height: u32) -> Self { - Self { width, height } - } - - /// Convert to logical size - #[inline] - pub fn to_logical(&self, window_info: &WindowInfo) -> Size { - Size { - width: f64::from(self.width) * window_info.scale_recip(), - height: f64::from(self.height) * window_info.scale_recip(), - } - } -} \ No newline at end of file diff --git a/src/event.rs b/src/event.rs old mode 100644 new mode 100755 diff --git a/src/keyboard.rs b/src/keyboard.rs old mode 100644 new mode 100755 diff --git a/src/lib.rs b/src/lib.rs old mode 100644 new mode 100755 index 97a1f25..d45e20a --- a/src/lib.rs +++ b/src/lib.rs @@ -15,17 +15,15 @@ mod macos; #[cfg(target_os = "macos")] pub use macos::*; -mod coordinates; mod event; mod keyboard; mod mouse_cursor; mod window_info; mod window_open_options; -pub use coordinates::*; pub use event::*; pub use keyboard::*; pub use mouse_cursor::MouseCursor; -pub use window_info::WindowInfo; +pub use window_info::*; pub use window_open_options::*; #[derive(Debug)] diff --git a/src/macos/mod.rs b/src/macos/mod.rs old mode 100644 new mode 100755 diff --git a/src/macos/window.rs b/src/macos/window.rs old mode 100644 new mode 100755 diff --git a/src/mouse_cursor.rs b/src/mouse_cursor.rs old mode 100644 new mode 100755 diff --git a/src/win/mod.rs b/src/win/mod.rs old mode 100644 new mode 100755 diff --git a/src/win/window.rs b/src/win/window.rs old mode 100644 new mode 100755 diff --git a/src/window_info.rs b/src/window_info.rs old mode 100644 new mode 100755 index e37d52c..1dad5d5 --- a/src/window_info.rs +++ b/src/window_info.rs @@ -1,5 +1,3 @@ -use crate::{Size, PhySize}; - /// The info about the window #[derive(Debug, Copy, Clone)] pub struct WindowInfo { @@ -61,4 +59,96 @@ impl WindowInfo { pub fn scale_recip(&self) -> f64 { self.scale_recip } +} + +/// A point in logical coordinates +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct Point { + pub x: f64, + pub y: f64 +} + +impl Point { + /// Create a new point in logical coordinates + pub fn new(x: f64, y: f64) -> Self { + Self { x, y } + } + + /// Convert to actual physical coordinates + #[inline] + pub fn to_physical(&self, window_info: &WindowInfo) -> PhyPoint { + PhyPoint { + x: (self.x * window_info.scale()).round() as i32, + y: (self.y * window_info.scale()).round() as i32, + } + } +} + +/// A point in actual physical coordinates +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct PhyPoint { + pub x: i32, + pub y: i32 +} + +impl PhyPoint { + /// Create a new point in actual physical coordinates + pub fn new(x: i32, y: i32) -> Self { + Self { x, y } + } + + /// Convert to logical coordinates + #[inline] + pub fn to_logical(&self, window_info: &WindowInfo) -> Point { + Point { + x: f64::from(self.x) * window_info.scale_recip(), + y: f64::from(self.y) * window_info.scale_recip(), + } + } +} + +/// A size in logical coordinates +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct Size { + pub width: f64, + pub height: f64, +} + +impl Size { + /// Create a new size in logical coordinates + pub fn new(width: f64, height: f64) -> Self { + Self { width, height } + } + + /// Convert to actual physical size + #[inline] + pub fn to_physical(&self, window_info: &WindowInfo) -> PhySize { + PhySize { + width: (self.width * window_info.scale()).round() as u32, + height: (self.height * window_info.scale()).round() as u32, + } + } +} + +/// An actual size in physical coordinates +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct PhySize { + pub width: u32, + pub height: u32, +} + +impl PhySize { + /// Create a new size in actual physical coordinates + pub fn new(width: u32, height: u32) -> Self { + Self { width, height } + } + + /// Convert to logical size + #[inline] + pub fn to_logical(&self, window_info: &WindowInfo) -> Size { + Size { + width: f64::from(self.width) * window_info.scale_recip(), + height: f64::from(self.height) * window_info.scale_recip(), + } + } } \ No newline at end of file diff --git a/src/window_open_options.rs b/src/window_open_options.rs old mode 100644 new mode 100755 diff --git a/src/x11/cursor.rs b/src/x11/cursor.rs old mode 100644 new mode 100755 diff --git a/src/x11/mod.rs b/src/x11/mod.rs old mode 100644 new mode 100755 diff --git a/src/x11/window.rs b/src/x11/window.rs old mode 100644 new mode 100755 index 92e5bba..8549255 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -13,7 +13,7 @@ use super::XcbConnection; use crate::{ Event, KeyboardEvent, MouseButton, MouseCursor, MouseEvent, Parent, ScrollDelta, WindowEvent, WindowHandle, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult, - WindowScalePolicy, Size, Point, + WindowScalePolicy, PhyPoint, PhySize, }; pub struct Window { @@ -25,7 +25,7 @@ pub struct Window { frame_interval: Duration, event_loop_running: bool, - new_physical_size: Option + new_physical_size: Option } impl Window { @@ -307,7 +307,7 @@ impl Window { xcb::CONFIGURE_NOTIFY => { let event = unsafe { xcb::cast_event::(&event) }; - let new_physical_size = Size::new(event.width() as u32, event.height() as u32); + let new_physical_size = PhySize::new(event.width() as u32, event.height() as u32); if self.new_physical_size.is_some() || new_physical_size != self.window_info.physical_size() { self.new_physical_size = Some(new_physical_size); @@ -322,14 +322,13 @@ impl Window { let detail = event.detail(); if detail != 4 && detail != 5 { - let physical_pos = Point::new(event.event_x() as f64, event.event_y() as f64); - let logical_pos = self.window_info.physical_to_logical(physical_pos); + let physical_pos = PhyPoint::new(event.event_x() as i32, event.event_y() as i32); + let logical_pos = physical_pos.to_logical(&self.window_info); handler.on_event( self, Event::Mouse(MouseEvent::CursorMoved { - logical_pos: logical_pos.into(), - physical_pos: physical_pos.into(), + position: logical_pos, }), ); } diff --git a/src/x11/xcb_connection.rs b/src/x11/xcb_connection.rs old mode 100644 new mode 100755