From f3ba68187713c9fa39adb5c282a0e712bf3358f4 Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sat, 5 Sep 2020 14:55:45 -0500 Subject: [PATCH] Replace dpi with scale --- src/event.rs | 11 ++--------- src/x11/window.rs | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/event.rs b/src/event.rs index b78e27b..95875c4 100644 --- a/src/event.rs +++ b/src/event.rs @@ -14,17 +14,10 @@ pub struct MouseScroll { pub y_delta: f64, } -#[derive(Debug, Copy, Clone)] -pub enum MouseClickType { - Single, - Double, - Triple, -} - #[derive(Debug, Copy, Clone)] pub struct MouseClick { pub id: MouseButtonID, - pub click_type: MouseClickType, + pub click_count: usize, pub x: i32, pub y: i32, } @@ -33,7 +26,7 @@ pub struct MouseClick { pub struct WindowInfo { pub width: u32, pub height: u32, - pub dpi: Option, + pub scale: f64, } #[derive(Debug)] diff --git a/src/x11/window.rs b/src/x11/window.rs index fd4682b..d90a8a4 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -11,7 +11,7 @@ use crate::{AppWindow, Event, MouseButtonID, MouseScroll, Parent, WindowInfo, Wi use raw_window_handle::RawWindowHandle; pub struct Window { - scaling: Option, // DPI scale, 96.0 is "default". + scaling: f64, xcb_connection: XcbConnection, app_window: A, app_message_rx: mpsc::Receiver, @@ -131,21 +131,29 @@ impl Window { ..raw_window_handle::unix::XcbHandle::empty() }); + let scaling = { + let maybe_scaling = + get_scaling_xft(&xcb_connection).or(get_scaling_screen_dimensions(&xcb_connection)); + + if let Some(scaling) = maybe_scaling { + scaling + } else { + 1.0 + } + }; + println!("Scale factor: {:?}", scaling); + let mut x11_window = Self { - scaling: None, + scaling, xcb_connection, app_window, app_message_rx, }; - x11_window.scaling = get_scaling_xft(&x11_window.xcb_connection) - .or(get_scaling_screen_dimensions(&x11_window.xcb_connection)); - println!("Scale factor: {:?}", x11_window.scaling); - let window_info = WindowInfo { width: options.width as u32, height: options.height as u32, - dpi: x11_window.scaling, + scale: x11_window.scaling, }; x11_window @@ -291,8 +299,9 @@ fn get_scaling_xft(xcb_connection: &XcbConnection) -> Option { let value_addr: &CStr = CStr::from_ptr(value.addr); value_addr.to_str().ok(); let value_str = value_addr.to_str().ok()?; - let value_f64 = value_str.parse().ok()?; - Some(value_f64) + let value_f64: f64 = value_str.parse().ok()?; + let dpi_to_scale = value_f64 / 96.0; + Some(dpi_to_scale) } else { None }; @@ -329,8 +338,10 @@ fn get_scaling_screen_dimensions(xcb_connection: &XcbConnection) -> Option let _xres = width_px * 25.4 / width_mm; let yres = height_px * 25.4 / height_mm; + let yscale = yres / 96.0; + // TODO: choose between `xres` and `yres`? (probably both are the same?) - Some(yres) + Some(yscale) } fn mouse_id(id: u8) -> MouseButtonID {