From 0d7d3665ee6cbf8b658dc276ce26dbaef6395785 Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Fri, 4 Oct 2024 15:52:10 +0900 Subject: [PATCH] feat: try to support scale --- iced_layershell/src/application/state.rs | 57 ++++++++++++++++++----- iced_layershell/src/event.rs | 1 - iced_layershell/src/multi_window/state.rs | 10 ++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/iced_layershell/src/application/state.rs b/iced_layershell/src/application/state.rs index 2a3ece3..8074fc4 100644 --- a/iced_layershell/src/application/state.rs +++ b/iced_layershell/src/application/state.rs @@ -11,6 +11,8 @@ where A::Theme: DefaultStyle, { scale_factor: f64, + window_size: iced::Size, + window_scale_factor: u32, viewport: Viewport, viewport_version: usize, theme: A::Theme, @@ -28,13 +30,23 @@ where let theme = application.theme(); let appearance = application.style(&theme); - let viewport = { + let window_scale_factor = 120; + let (window_size, viewport) = { let (width, height) = window.main_window().get_size(); - Viewport::with_physical_size(iced_core::Size::new(width, height), 1. * scale_factor) + 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, + ), + ) }; Self { scale_factor, + window_size, + window_scale_factor, viewport, viewport_version: 0, theme, @@ -48,11 +60,24 @@ where self.modifiers } + pub fn current_wayland_scale(&self) -> f64 { + self.window_scale_factor as f64 / 120. + } + 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( - iced_core::Size::new(width, height), - 1. * self.scale_factor(), - ) + self.window_size(), + 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 { @@ -67,10 +92,6 @@ where self.viewport.logical_size() } - pub fn scale_factor(&self) -> f64 { - self.viewport.scale_factor() - } - pub fn text_color(&self) -> Color { self.appearance.text_color } @@ -100,6 +121,18 @@ where WindowEvent::ModifiersChanged(modifiers) => { self.modifiers = *modifiers; } + WindowEvent::ScaleFactorChanged { + scale_float, + scale_u32, + } => { + self.viewport = Viewport::with_physical_size( + self.window_size(), + self.scale_factor * scale_float, + ); + + self.viewport_version = self.viewport_version.wrapping_add(1); + self.window_scale_factor = *scale_u32; + } _ => {} } } @@ -107,8 +140,10 @@ where pub fn synchronize(&mut self, application: &A) { let new_scale_factor = application.scale_factor(); if self.scale_factor != new_scale_factor { - self.viewport = - Viewport::with_physical_size(self.physical_size(), 1. * new_scale_factor); + self.viewport = Viewport::with_physical_size( + self.window_size(), + self.current_wayland_scale() * new_scale_factor, + ); self.viewport_version = self.viewport_version.wrapping_add(1); self.scale_factor = new_scale_factor; } diff --git a/iced_layershell/src/event.rs b/iced_layershell/src/event.rs index ef4356a..1d8dfec 100644 --- a/iced_layershell/src/event.rs +++ b/iced_layershell/src/event.rs @@ -188,7 +188,6 @@ impl From<&DispatchMessage> for IcedLayerEvent { self.modifiers = *modifiers; } + WindowEvent::ScaleFactorChanged { + scale_float, + scale_u32: _, + } => { + let size = self.viewport.physical_size(); + + self.viewport = Viewport::with_physical_size(size, scale_float * self.scale_factor); + + self.viewport_version = self.viewport_version.wrapping_add(1); + } _ => {} } }