Added window::Id to multi_window application's scale_factor

This commit is contained in:
Bingus 2023-01-18 17:04:11 -08:00
parent 7e9a12a4aa
commit 0a643287de
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
4 changed files with 14 additions and 6 deletions

View file

@ -12,6 +12,7 @@ use iced::{Color, Command, Element, Length, Settings, Size, Subscription};
use iced_lazy::responsive; use iced_lazy::responsive;
use iced_native::{event, subscription, Event}; use iced_native::{event, subscription, Event};
use iced_native::window::Id;
use std::collections::HashMap; use std::collections::HashMap;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -29,6 +30,7 @@ struct Example {
#[derive(Debug)] #[derive(Debug)]
struct Window { struct Window {
title: String, title: String,
scale: f64,
panes: pane_grid::State<Pane>, panes: pane_grid::State<Pane>,
focus: Option<pane_grid::Pane>, focus: Option<pane_grid::Pane>,
} }
@ -69,6 +71,7 @@ impl Application for Example {
panes, panes,
focus: None, focus: None,
title: String::from("Default window"), title: String::from("Default window"),
scale: 1.0,
}; };
( (
@ -178,6 +181,7 @@ impl Application for Example {
panes, panes,
focus: None, focus: None,
title: format!("New window ({})", self.windows.len()), title: format!("New window ({})", self.windows.len()),
scale: 1.0 + (self.windows.len() as f64 / 10.0),
}; };
let window_id = window::Id::new(self.windows.len()); let window_id = window::Id::new(self.windows.len());
@ -342,6 +346,10 @@ impl Application for Example {
fn close_requested(&self, window: window::Id) -> Self::Message { fn close_requested(&self, window: window::Id) -> Self::Message {
Message::Window(window, WindowMessage::CloseWindow) Message::Window(window, WindowMessage::CloseWindow)
} }
fn scale_factor(&self, window: Id) -> f64 {
self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0)
}
} }
const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb(

View file

@ -148,7 +148,7 @@ pub trait Application: Sized {
/// while a scale factor of `0.5` will shrink them to half their size. /// while a scale factor of `0.5` will shrink them to half their size.
/// ///
/// By default, it returns `1.0`. /// By default, it returns `1.0`.
fn scale_factor(&self) -> f64 { fn scale_factor(&self, window: window::Id) -> f64 {
1.0 1.0
} }
@ -239,8 +239,8 @@ where
self.0.subscription() self.0.subscription()
} }
fn scale_factor(&self) -> f64 { fn scale_factor(&self, window: window::Id) -> f64 {
self.0.scale_factor() self.0.scale_factor(window)
} }
fn should_exit(&self) -> bool { fn should_exit(&self) -> bool {

View file

@ -146,7 +146,7 @@ where
/// while a scale factor of `0.5` will shrink them to half their size. /// while a scale factor of `0.5` will shrink them to half their size.
/// ///
/// By default, it returns `1.0`. /// By default, it returns `1.0`.
fn scale_factor(&self) -> f64 { fn scale_factor(&self, window: window::Id) -> f64 {
1.0 1.0
} }

View file

@ -36,7 +36,7 @@ where
window: &Window, window: &Window,
) -> Self { ) -> Self {
let title = application.title(window_id); let title = application.title(window_id);
let scale_factor = application.scale_factor(); let scale_factor = application.scale_factor(window_id);
let theme = application.theme(); let theme = application.theme();
let appearance = theme.appearance(&application.style()); let appearance = theme.appearance(&application.style());
@ -199,7 +199,7 @@ where
} }
// Update scale factor // Update scale factor
let new_scale_factor = application.scale_factor(); let new_scale_factor = application.scale_factor(window_id);
if self.scale_factor != new_scale_factor { if self.scale_factor != new_scale_factor {
let size = window.inner_size(); let size = window.inner_size();