1
0
Fork 0

update x11 code

This commit is contained in:
Billy Messenger 2020-10-17 17:36:28 -05:00
parent 688d45c720
commit a91a5a1126
21 changed files with 99 additions and 105 deletions

0
.github/workflows/rust.yml vendored Normal file → Executable file
View file

0
.gitignore vendored Normal file → Executable file
View file

0
.rustfmt.toml Normal file → Executable file
View file

0
Cargo.toml Normal file → Executable file
View file

0
README.md Normal file → Executable file
View file

0
examples/open_window.rs Normal file → Executable file
View file

View file

@ -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(),
}
}
}

0
src/event.rs Normal file → Executable file
View file

0
src/keyboard.rs Normal file → Executable file
View file

4
src/lib.rs Normal file → Executable file
View file

@ -15,17 +15,15 @@ mod macos;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub use macos::*; pub use macos::*;
mod coordinates;
mod event; mod event;
mod keyboard; mod keyboard;
mod mouse_cursor; mod mouse_cursor;
mod window_info; mod window_info;
mod window_open_options; mod window_open_options;
pub use coordinates::*;
pub use event::*; pub use event::*;
pub use keyboard::*; pub use keyboard::*;
pub use mouse_cursor::MouseCursor; pub use mouse_cursor::MouseCursor;
pub use window_info::WindowInfo; pub use window_info::*;
pub use window_open_options::*; pub use window_open_options::*;
#[derive(Debug)] #[derive(Debug)]

0
src/macos/mod.rs Normal file → Executable file
View file

0
src/macos/window.rs Normal file → Executable file
View file

0
src/mouse_cursor.rs Normal file → Executable file
View file

0
src/win/mod.rs Normal file → Executable file
View file

0
src/win/window.rs Normal file → Executable file
View file

94
src/window_info.rs Normal file → Executable file
View file

@ -1,5 +1,3 @@
use crate::{Size, PhySize};
/// The info about the window /// The info about the window
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct WindowInfo { pub struct WindowInfo {
@ -61,4 +59,96 @@ impl WindowInfo {
pub fn scale_recip(&self) -> f64 { pub fn scale_recip(&self) -> f64 {
self.scale_recip 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(),
}
}
} }

0
src/window_open_options.rs Normal file → Executable file
View file

0
src/x11/cursor.rs Normal file → Executable file
View file

0
src/x11/mod.rs Normal file → Executable file
View file

13
src/x11/window.rs Normal file → Executable file
View file

@ -13,7 +13,7 @@ use super::XcbConnection;
use crate::{ use crate::{
Event, KeyboardEvent, MouseButton, MouseCursor, MouseEvent, Parent, ScrollDelta, WindowEvent, Event, KeyboardEvent, MouseButton, MouseCursor, MouseEvent, Parent, ScrollDelta, WindowEvent,
WindowHandle, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult, WindowHandle, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult,
WindowScalePolicy, Size, Point, WindowScalePolicy, PhyPoint, PhySize,
}; };
pub struct Window { pub struct Window {
@ -25,7 +25,7 @@ pub struct Window {
frame_interval: Duration, frame_interval: Duration,
event_loop_running: bool, event_loop_running: bool,
new_physical_size: Option<Size> new_physical_size: Option<PhySize>
} }
impl Window { impl Window {
@ -307,7 +307,7 @@ impl Window {
xcb::CONFIGURE_NOTIFY => { xcb::CONFIGURE_NOTIFY => {
let event = unsafe { xcb::cast_event::<xcb::ConfigureNotifyEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::ConfigureNotifyEvent>(&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() { if self.new_physical_size.is_some() || new_physical_size != self.window_info.physical_size() {
self.new_physical_size = Some(new_physical_size); self.new_physical_size = Some(new_physical_size);
@ -322,14 +322,13 @@ impl Window {
let detail = event.detail(); let detail = event.detail();
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
let physical_pos = Point::new(event.event_x() as f64, event.event_y() as f64); let physical_pos = PhyPoint::new(event.event_x() as i32, event.event_y() as i32);
let logical_pos = self.window_info.physical_to_logical(physical_pos); let logical_pos = physical_pos.to_logical(&self.window_info);
handler.on_event( handler.on_event(
self, self,
Event::Mouse(MouseEvent::CursorMoved { Event::Mouse(MouseEvent::CursorMoved {
logical_pos: logical_pos.into(), position: logical_pos,
physical_pos: physical_pos.into(),
}), }),
); );
} }

0
src/x11/xcb_connection.rs Normal file → Executable file
View file