From c3ceabd7b9c0adc5fe4383fd7c41d16992fd386a Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Mon, 16 Dec 2024 02:16:32 +0700 Subject: [PATCH] Implements RuntimeWindow::update_size --- gui/src/rt/mod.rs | 1 + gui/src/rt/window.rs | 2 ++ gui/src/ui/backend/window.rs | 41 +++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/gui/src/rt/mod.rs b/gui/src/rt/mod.rs index d4bf56c23..daf56334d 100644 --- a/gui/src/rt/mod.rs +++ b/gui/src/rt/mod.rs @@ -153,6 +153,7 @@ impl ApplicationHandler for Runtime { use winit::event::WindowEvent; match event { + WindowEvent::Resized(v) => self.dispatch_window(el, id, move |w| w.update_size(v)), WindowEvent::CloseRequested => self.on_close.raise(id, ()), WindowEvent::Destroyed => drop(self.windows.remove(&id)), WindowEvent::ScaleFactorChanged { diff --git a/gui/src/rt/window.rs b/gui/src/rt/window.rs index a34409479..43fb52b01 100644 --- a/gui/src/rt/window.rs +++ b/gui/src/rt/window.rs @@ -1,7 +1,9 @@ use std::error::Error; +use winit::dpi::PhysicalSize; /// Encapsulates winit window with application-specific logic. pub trait RuntimeWindow { + fn update_size(&self, v: PhysicalSize) -> Result<(), Box>; fn update_scale_factor(&self, v: f64) -> Result<(), Box>; fn redraw(&self) -> Result<(), Box>; } diff --git a/gui/src/ui/backend/window.rs b/gui/src/ui/backend/window.rs index 33e244368..5d171e233 100644 --- a/gui/src/ui/backend/window.rs +++ b/gui/src/ui/backend/window.rs @@ -2,8 +2,8 @@ use crate::rt::RuntimeWindow; use i_slint_core::window::WindowAdapterInternal; use i_slint_core::InternalToken; use i_slint_renderer_skia::SkiaRenderer; -use slint::platform::{Renderer, WindowAdapter, WindowEvent}; -use slint::{PhysicalSize, PlatformError}; +use slint::platform::{Renderer, WindowAdapter, WindowEvent, WindowProperties}; +use slint::{LogicalSize, PhysicalSize, PlatformError, WindowSize}; use std::any::Any; use std::cell::Cell; use std::error::Error; @@ -38,10 +38,20 @@ impl Window { } impl RuntimeWindow for Window { + fn update_size(&self, v: winit::dpi::PhysicalSize) -> Result<(), Box> { + let size = PhysicalSize::new(v.width, v.height); + let size = LogicalSize::from_physical(size, self.winit.scale_factor() as f32); + + self.slint.dispatch_event(WindowEvent::Resized { size }); + + Ok(()) + } + fn update_scale_factor(&self, v: f64) -> Result<(), Box> { - self.slint.dispatch_event(WindowEvent::ScaleFactorChanged { - scale_factor: v as f32, - }); + let scale_factor = v as f32; + + self.slint + .dispatch_event(WindowEvent::ScaleFactorChanged { scale_factor }); Ok(()) } @@ -78,6 +88,10 @@ impl WindowAdapter for Window { Ok(()) } + fn set_size(&self, size: WindowSize) { + todo!() + } + fn size(&self) -> PhysicalSize { let s = self.winit.inner_size(); @@ -88,6 +102,23 @@ impl WindowAdapter for Window { &self.renderer } + fn update_window_properties(&self, properties: WindowProperties) { + // Set window size. + let size = properties.layout_constraints(); + let scale = self.winit.scale_factor() as f32; + let map = move |v: LogicalSize| { + let v = v.to_physical(scale); + let v = winit::dpi::PhysicalSize::new(v.width, v.height); + + winit::dpi::Size::from(v) + }; + + self.winit.set_min_inner_size(size.min.map(&map)); + self.winit.set_max_inner_size(size.max.map(&map)); + + let _ = self.winit.request_inner_size(map(size.preferred)); + } + fn internal(&self, _: InternalToken) -> Option<&dyn WindowAdapterInternal> { Some(self) }