Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements RuntimeWindow::update_scale_factor #1185

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct Runtime {
}

impl Runtime {
fn dispatch(&mut self, el: &ActiveEventLoop, task: u64) -> bool {
fn dispatch_task(&mut self, el: &ActiveEventLoop, task: u64) -> bool {
// Get target task.
let mut task = match self.tasks.get(task) {
Some(v) => v,
Expand Down Expand Up @@ -102,7 +102,12 @@ impl Runtime {
true
}

fn redraw(&mut self, el: &ActiveEventLoop, win: WindowId) {
fn dispatch_window(
&mut self,
el: &ActiveEventLoop,
win: WindowId,
f: impl FnOnce(&dyn RuntimeWindow) -> Result<(), Box<dyn Error>>,
) {
// Get target window.
let win = match self.windows.get(&win).unwrap().upgrade() {
Some(v) => v,
Expand All @@ -116,8 +121,8 @@ impl Runtime {
on_close: &mut self.on_close,
};

// Redraw.
let e = match cx.run(move || win.redraw()) {
// Dispatch the event.
let e = match cx.run(move || f(win.as_ref())) {
Ok(_) => return,
Err(e) => e,
};
Expand All @@ -128,29 +133,33 @@ impl Runtime {

impl ApplicationHandler<Event> for Runtime {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
assert!(self.dispatch(event_loop, self.main));
assert!(self.dispatch_task(event_loop, self.main));
}

fn user_event(&mut self, event_loop: &ActiveEventLoop, event: Event) {
match event {
Event::TaskReady(task) => {
self.dispatch(event_loop, task);
self.dispatch_task(event_loop, task);
}
}
}

fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
el: &ActiveEventLoop,
id: WindowId,
event: winit::event::WindowEvent,
) {
use winit::event::WindowEvent;

match event {
WindowEvent::CloseRequested => self.on_close.raise(window_id, ()),
WindowEvent::Destroyed => drop(self.windows.remove(&window_id)),
WindowEvent::RedrawRequested => self.redraw(event_loop, window_id),
WindowEvent::CloseRequested => self.on_close.raise(id, ()),
WindowEvent::Destroyed => drop(self.windows.remove(&id)),
WindowEvent::ScaleFactorChanged {
scale_factor,
inner_size_writer: _,
} => self.dispatch_window(el, id, move |w| w.update_scale_factor(scale_factor)),
WindowEvent::RedrawRequested => self.dispatch_window(el, id, |w| w.redraw()),
_ => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions gui/src/rt/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ use std::error::Error;

/// Encapsulates winit window with application-specific logic.
pub trait RuntimeWindow {
fn update_scale_factor(&self, v: f64) -> Result<(), Box<dyn Error>>;
fn redraw(&self) -> Result<(), Box<dyn Error>>;
}
10 changes: 9 additions & 1 deletion gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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};
use slint::platform::{Renderer, WindowAdapter, WindowEvent};
use slint::{PhysicalSize, PlatformError};
use std::any::Any;
use std::cell::Cell;
Expand Down Expand Up @@ -38,6 +38,14 @@ impl Window {
}

impl RuntimeWindow for Window {
fn update_scale_factor(&self, v: f64) -> Result<(), Box<dyn Error>> {
self.slint.dispatch_event(WindowEvent::ScaleFactorChanged {
scale_factor: v as f32,
});

Ok(())
}

fn redraw(&self) -> Result<(), Box<dyn Error>> {
// Wayland will show the window on the first render so we need to check visibility flag
// here.
Expand Down