Skip to content

Commit

Permalink
Implements RuntimeWindow::update_size (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 15, 2024
1 parent 5993de2 commit 276520f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl ApplicationHandler<Event> 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 {
Expand Down
2 changes: 2 additions & 0 deletions gui/src/rt/window.rs
Original file line number Diff line number Diff line change
@@ -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<u32>) -> Result<(), Box<dyn Error>>;
fn update_scale_factor(&self, v: f64) -> Result<(), Box<dyn Error>>;
fn redraw(&self) -> Result<(), Box<dyn Error>>;
}
41 changes: 36 additions & 5 deletions gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,10 +38,20 @@ impl Window {
}

impl RuntimeWindow for Window {
fn update_size(&self, v: winit::dpi::PhysicalSize<u32>) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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(())
}
Expand Down Expand Up @@ -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();

Expand All @@ -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)
}
Expand Down

0 comments on commit 276520f

Please sign in to comment.