Skip to content

Commit

Permalink
feat: try to support scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Oct 6, 2024
1 parent e1dbaf4 commit 0d7d366
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
57 changes: 46 additions & 11 deletions iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ where
A::Theme: DefaultStyle,
{
scale_factor: f64,
window_size: iced::Size<u32>,
window_scale_factor: u32,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -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,
Expand All @@ -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<u32> {
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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -100,15 +121,29 @@ 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;
}
_ => {}
}
}

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;
}
Expand Down
1 change: 0 additions & 1 deletion iced_layershell/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ impl<Message: 'static, INFO: Clone> From<&DispatchMessage> for IcedLayerEvent<Me
y: *y,
})
}

DispatchMessage::PreferredScale {
scale_u32,
scale_float,
Expand Down
10 changes: 10 additions & 0 deletions iced_layershell/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ where
WindowEvent::ModifiersChanged(modifiers) => {
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);
}
_ => {}
}
}
Expand Down

0 comments on commit 0d7d366

Please sign in to comment.