Skip to content

Commit

Permalink
fix: scaling issue.
Browse files Browse the repository at this point in the history
1. set destination of wpviewport in the creation of a state.
2. all width and height from wayland should be logical.
3. move logic of resizing viewport into `resize_viewport`.

Signed-off-by: fortime <palfortime@gmail.com>
  • Loading branch information
fortime committed Jan 17, 2025
1 parent 51f824c commit f046b65
Showing 1 changed file with 53 additions and 55 deletions.
108 changes: 53 additions & 55 deletions iced_layershell/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ where
id: window::Id,
application_scale_factor: f64,
wayland_scale_factor: f64,
real_window_size: Size<u32>,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
Expand All @@ -41,62 +40,38 @@ where
let theme = application.theme();
let appearance = application.style(&theme);

let real_window_size = Size::new(width, height);
let viewport = Viewport::with_physical_size(
real_window_size,
wayland_scale_factor * application_scale_factor,
);
let logical_size = Size::new(width, height);
let viewport = viewport(logical_size, wayland_scale_factor, application_scale_factor);

let wpviewport = window
.viewport
.clone()
.expect("iced_layershell need viewport support to better wayland hidpi");
wpviewport.set_destination(logical_size.width as i32, logical_size.height as i32);
Self {
id,
application_scale_factor,
wayland_scale_factor,
real_window_size,
viewport,
viewport_version: 0,
theme,
appearance,
mouse_position: None,
modifiers: ModifiersState::default(),
wpviewport: window
.viewport
.clone()
.expect("iced_layershell need viewport support to better wayland hidpi"),
wpviewport
}
}
pub fn modifiers(&self) -> ModifiersState {
self.modifiers
}

pub fn current_wayland_scale(&self) -> f64 {
self.wayland_scale_factor
}

pub fn update_view_port(&mut self, width: u32, height: u32, scale: f64) {
let real_window_size = Size::new(width, height);
if self.real_window_size == real_window_size && self.wayland_scale_factor == scale {
let logical_size = Size::new(width, height);
if self.logical_size_u32() == logical_size && self.wayland_scale_factor == scale {
return;
}
self.real_window_size = real_window_size;
self.wayland_scale_factor = scale;
self.viewport = Viewport::with_physical_size(
self.adjusted_physical_size(),
self.current_wayland_scale() * self.application_scale_factor,
);
let logical_size = self.viewport.logical_size();

self.wpviewport.set_destination(
logical_size.width.ceil() as i32,
logical_size.height.ceil() as i32,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
}

fn adjusted_physical_size(&self) -> Size<u32> {
let mut size = self.real_window_size;
let factor = self.wayland_scale_factor * self.application_scale_factor;
size.width = (size.width as f64 * factor).ceil() as u32;
size.height = (size.height as f64 * factor).ceil() as u32;
size
self.resize_viewport(logical_size);
}

pub fn viewport(&self) -> &Viewport {
Expand All @@ -112,7 +87,7 @@ where
}

pub fn scale_factor(&self) -> f64 {
1.
self.viewport.scale_factor()
}

pub fn text_color(&self) -> Color {
Expand Down Expand Up @@ -156,18 +131,7 @@ where
scale_u32: _,
} => {
self.wayland_scale_factor = *scale_float;
self.viewport = Viewport::with_physical_size(
self.adjusted_physical_size(),
self.application_scale_factor * scale_float,
);

self.viewport_version = self.viewport_version.wrapping_add(1);
let logical_size = self.viewport.logical_size();

self.wpviewport.set_destination(
logical_size.width.ceil() as i32,
logical_size.height.ceil() as i32,
);
self.resize_viewport(self.logical_size_u32());
}
_ => {}
}
Expand All @@ -177,13 +141,47 @@ where
let new_scale_factor = application.scale_factor(self.id);
if self.application_scale_factor != new_scale_factor {
self.application_scale_factor = new_scale_factor;
self.viewport = Viewport::with_physical_size(
self.adjusted_physical_size(),
self.current_wayland_scale() * new_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
self.resize_viewport(self.logical_size_u32());
}
self.theme = application.theme();
self.appearance = application.style(&self.theme);
}

fn resize_viewport(&mut self, logical_size: Size<u32>) {
self.viewport = viewport(
logical_size,
self.wayland_scale_factor,
self.application_scale_factor,
);

self.viewport_version = self.viewport_version.wrapping_add(1);

let logical_size = self.logical_size_u32();
self.wpviewport
.set_destination(logical_size.width as i32, logical_size.height as i32);
}

fn logical_size_u32(&self) -> Size<u32> {
// physical_size = (orig_logical_size as f64 * factor).ceil()
// logical_sizea = physical_size as f64 / factor as f32
// logical_size >= orig_logical_size
let logical_size = self.viewport.logical_size();
Size::new(
logical_size.width.floor() as u32,
logical_size.height.floor() as u32,
)
}
}

fn viewport(
logical_size: Size<u32>,
wayland_scale_factor: f64,
application_scale_factor: f64,
) -> Viewport {
let factor = wayland_scale_factor * application_scale_factor;
let physical_size = Size::new(
(logical_size.width as f64 * factor).ceil() as u32,
(logical_size.height as f64 * factor).ceil() as u32,
);
Viewport::with_physical_size(physical_size, factor)
}

0 comments on commit f046b65

Please sign in to comment.