From fb0e0f2c8dae3cceb27bdbcd42e1c46a707b3e53 Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Fri, 4 Oct 2024 18:40:13 +0900 Subject: [PATCH] chore: some update --- iced_examples/counter/src/main.rs | 4 +- iced_layershell/src/application.rs | 4 +- iced_layershell/src/application/state.rs | 52 +++++++++-------------- iced_layershell/src/conversion.rs | 40 ++++++++++++----- iced_layershell/src/multi_window.rs | 7 ++- iced_layershell/src/multi_window/state.rs | 17 ++++++-- 6 files changed, 73 insertions(+), 51 deletions(-) diff --git a/iced_examples/counter/src/main.rs b/iced_examples/counter/src/main.rs index 2366f5f..a077470 100644 --- a/iced_examples/counter/src/main.rs +++ b/iced_examples/counter/src/main.rs @@ -79,8 +79,8 @@ impl Application for Counter { fn update(&mut self, message: Message) -> Command { match message { - Message::IcedEvent(event) => { - println!("hello {event:?}"); + Message::IcedEvent(_event) => { + //println!("hello {event:?}"); Command::none() } Message::IncrementPressed => { diff --git a/iced_layershell/src/application.rs b/iced_layershell/src/application.rs index 873ae7f..cbf8633 100644 --- a/iced_layershell/src/application.rs +++ b/iced_layershell/src/application.rs @@ -468,7 +468,9 @@ async fn run_instance( IcedLayerEvent::Window(event) => { state.update(&event); - if let Some(event) = conversion::window_event(main_id, &event, state.modifiers()) { + if let Some(event) = + conversion::window_event(&event, state.scale_factor(), state.modifiers()) + { events.push(event); } } diff --git a/iced_layershell/src/application/state.rs b/iced_layershell/src/application/state.rs index 8074fc4..7199a5e 100644 --- a/iced_layershell/src/application/state.rs +++ b/iced_layershell/src/application/state.rs @@ -11,8 +11,7 @@ where A::Theme: DefaultStyle, { scale_factor: f64, - window_size: iced::Size, - window_scale_factor: u32, + window_scale_factor: f64, viewport: Viewport, viewport_version: usize, theme: A::Theme, @@ -30,22 +29,13 @@ where let theme = application.theme(); let appearance = application.style(&theme); - let window_scale_factor = 120; - let (window_size, viewport) = { - let (width, height) = window.main_window().get_size(); - - let realscale = window_scale_factor as f64 / 120.; - ( - iced::Size::new(width, height), - Viewport::with_physical_size( - iced_core::Size::new(width, height), - realscale * scale_factor, - ), - ) + let window_scale_factor = 1.; + let (width, height) = window.main_window().get_size(); + let viewport = { + Viewport::with_physical_size(iced_core::Size::new(width, height), window_scale_factor) }; Self { scale_factor, - window_size, window_scale_factor, viewport, viewport_version: 0, @@ -60,26 +50,22 @@ where self.modifiers } + pub fn scale_factor(&self) -> f64 { + self.viewport.scale_factor() + } + pub fn current_wayland_scale(&self) -> f64 { - self.window_scale_factor as f64 / 120. + self.window_scale_factor } pub fn update_view_port(&mut self, width: u32, height: u32) { - self.window_size = iced::Size::new(width, height); self.viewport = Viewport::with_physical_size( - self.window_size(), + iced::Size::new(width, height), self.current_wayland_scale() * self.scale_factor, ); self.viewport_version = self.viewport_version.wrapping_add(1); } - fn window_size(&self) -> iced::Size { - let mut window_size = self.window_size; - window_size.width = window_size.width * 140 / self.window_scale_factor; - window_size.height = window_size.height * 140 / self.window_scale_factor; - window_size - } - pub fn viewport(&self) -> &Viewport { &self.viewport } @@ -106,6 +92,10 @@ where pub fn cursor(&self) -> IcedMouse::Cursor { self.mouse_position + .map(|point| Point { + x: point.x / self.scale_factor() as f32, + y: point.y / self.scale_factor() as f32, + }) .map(IcedMouse::Cursor::Available) .unwrap_or(IcedMouse::Cursor::Unavailable) } @@ -123,15 +113,13 @@ where } WindowEvent::ScaleFactorChanged { scale_float, - scale_u32, + scale_u32: _, } => { - self.viewport = Viewport::with_physical_size( - self.window_size(), - self.scale_factor * scale_float, - ); + let size = self.physical_size(); + self.viewport = Viewport::with_physical_size(size, self.scale_factor * scale_float); self.viewport_version = self.viewport_version.wrapping_add(1); - self.window_scale_factor = *scale_u32; + self.window_scale_factor = *scale_float; } _ => {} } @@ -141,7 +129,7 @@ where let new_scale_factor = application.scale_factor(); if self.scale_factor != new_scale_factor { self.viewport = Viewport::with_physical_size( - self.window_size(), + self.physical_size(), self.current_wayland_scale() * new_scale_factor, ); self.viewport_version = self.viewport_version.wrapping_add(1); diff --git a/iced_layershell/src/conversion.rs b/iced_layershell/src/conversion.rs index 34e25ac..e0e24d8 100644 --- a/iced_layershell/src/conversion.rs +++ b/iced_layershell/src/conversion.rs @@ -10,19 +10,33 @@ use layershellev::keyboard::KeyLocation; use layershellev::keyboard::ModifiersState; use layershellev::xkb_keyboard::ElementState; use layershellev::xkb_keyboard::KeyEvent as LayerShellKeyEvent; +use std::ops::Mul; + +fn scale_down((x, y): (T, T), scale_factor: f64) -> (T, T) +where + T: Mul + TryInto + TryFrom, + >::Error: std::fmt::Debug, + >::Error: std::fmt::Debug, +{ + let (mut x, mut y): (f64, f64) = (x.try_into().unwrap(), y.try_into().unwrap()); + x = x / scale_factor; + y = y / scale_factor; + (x.try_into().unwrap(), y.try_into().unwrap()) +} pub fn window_event( - #[allow(unused)] id: iced_core::window::Id, layerevent: &LayerShellEvent, + scale_factor: f64, modifiers: ModifiersState, ) -> Option { match layerevent { LayerShellEvent::CursorLeft => Some(IcedEvent::Mouse(mouse::Event::CursorLeft)), LayerShellEvent::CursorMoved { x, y } => { + let (x, y) = scale_down((*x, *y), scale_factor); Some(IcedEvent::Mouse(mouse::Event::CursorMoved { position: iced_core::Point { - x: *x as f32, - y: *y as f32, + x: x as f32, + y: y as f32, }, })) } @@ -83,38 +97,42 @@ pub fn window_event( } })), LayerShellEvent::TouchDown { id, x, y } => { + let (x, y) = scale_down((*x, *y), scale_factor); Some(IcedEvent::Touch(touch::Event::FingerPressed { id: touch::Finger(*id as u64), position: iced::Point { - x: *x as f32, - y: *y as f32, + x: x as f32, + y: y as f32, }, })) } LayerShellEvent::TouchUp { id, x, y } => { + let (x, y) = scale_down((*x, *y), scale_factor); Some(IcedEvent::Touch(touch::Event::FingerLifted { id: touch::Finger(*id as u64), position: iced::Point { - x: *x as f32, - y: *y as f32, + x: x as f32, + y: y as f32, }, })) } LayerShellEvent::TouchMotion { id, x, y } => { + let (x, y) = scale_down((*x, *y), scale_factor); Some(IcedEvent::Touch(touch::Event::FingerMoved { id: touch::Finger(*id as u64), position: iced::Point { - x: *x as f32, - y: *y as f32, + x: x as f32, + y: y as f32, }, })) } LayerShellEvent::TouchCancel { id, x, y } => { + let (x, y) = scale_down((*x, *y), scale_factor); Some(IcedEvent::Touch(touch::Event::FingerLost { id: touch::Finger(*id as u64), position: iced::Point { - x: *x as f32, - y: *y as f32, + x: x as f32, + y: y as f32, }, })) } diff --git a/iced_layershell/src/multi_window.rs b/iced_layershell/src/multi_window.rs index 6f6e2df..82e19a6 100644 --- a/iced_layershell/src/multi_window.rs +++ b/iced_layershell/src/multi_window.rs @@ -656,8 +656,11 @@ async fn run_instance( continue; }; window.state.update(&event); - if let Some(event) = conversion::window_event(id, &event, window.state.modifiers()) - { + if let Some(event) = conversion::window_event( + &event, + window.state.scale_factor(), + window.state.modifiers(), + ) { events.push((Some(id), event)); } } diff --git a/iced_layershell/src/multi_window/state.rs b/iced_layershell/src/multi_window/state.rs index 6164b5b..f60fada 100644 --- a/iced_layershell/src/multi_window/state.rs +++ b/iced_layershell/src/multi_window/state.rs @@ -13,6 +13,7 @@ where { id: window::Id, scale_factor: f64, + wayland_scale_factor: f64, viewport: Viewport, viewport_version: usize, theme: A::Theme, @@ -30,11 +31,15 @@ where let theme = application.theme(); let appearance = application.style(&theme); - let viewport = - Viewport::with_physical_size(iced_core::Size::new(width, height), 1. * scale_factor); + let wayland_scale_factor = 1.0; + let viewport = Viewport::with_physical_size( + iced_core::Size::new(width, height), + wayland_scale_factor * scale_factor, + ); Self { id, scale_factor, + wayland_scale_factor, viewport, viewport_version: 0, theme, @@ -50,7 +55,8 @@ where self.viewport = Viewport::with_physical_size( iced_core::Size::new(width, height), 1. * self.scale_factor(), - ) + ); + self.viewport_version = self.viewport_version.wrapping_add(1); } pub fn viewport(&self) -> &Viewport { @@ -87,6 +93,10 @@ where pub fn cursor(&self) -> IcedMouse::Cursor { self.mouse_position + .map(|point| Point { + x: point.x / self.scale_factor() as f32, + y: point.y / self.scale_factor() as f32, + }) .map(IcedMouse::Cursor::Available) .unwrap_or(IcedMouse::Cursor::Unavailable) } @@ -106,6 +116,7 @@ where scale_float, scale_u32: _, } => { + self.wayland_scale_factor = *scale_float; let size = self.viewport.physical_size(); self.viewport = Viewport::with_physical_size(size, scale_float * self.scale_factor);