Skip to content

Commit

Permalink
chore: viewport support demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Oct 7, 2024
1 parent 9df4de2 commit 97ffb61
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
53 changes: 40 additions & 13 deletions iced_layershell/src/application/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,49 @@ use iced_graphics::Viewport;
use layershellev::keyboard::ModifiersState;

use crate::event::WindowEvent;
use layershellev::reexport::wp_viewport::WpViewport;

pub struct State<A: Application>
where
A::Theme: DefaultStyle,
{
application_scale_factor: f64,
wayland_scale_factor: f64,
real_window_size: Size<u32>,
viewport: Viewport,
viewport_version: usize,
theme: A::Theme,
appearance: Appearance,
mouse_position: Option<Point>,
modifiers: ModifiersState,
wpviewport: WpViewport,
}

impl<A: Application> State<A>
where
A::Theme: DefaultStyle,
{
pub fn new(application: &A, window: &layershellev::WindowStateSimple) -> Self {
let scale_factor = application.scale_factor();
let application_scale_factor = application.scale_factor();
let theme = application.theme();
let appearance = application.style(&theme);

let wayland_scale_factor = 1.;
let (width, height) = window.main_window().get_size();
let viewport = {
Viewport::with_physical_size(iced_core::Size::new(width, height), wayland_scale_factor)
};
let real_window_size = Size::new(width, height);
let wayland_scale_factor = 1.;

let viewport = { Viewport::with_physical_size(real_window_size, wayland_scale_factor) };
Self {
application_scale_factor: scale_factor,
application_scale_factor,
real_window_size,
wayland_scale_factor,
viewport,
viewport_version: 0,
theme,
appearance,
mouse_position: None,
modifiers: ModifiersState::default(),
wpviewport: window.gen_main_wrapper().viewport.unwrap(),
}
}

Expand All @@ -59,14 +64,29 @@ where
}

pub fn update_view_port(&mut self, width: u32, height: u32, scale: f64) {
self.real_window_size = Size::new(width, height);
self.wayland_scale_factor = scale;
self.viewport = Viewport::with_physical_size(
iced::Size::new(width, height),
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
}

pub fn viewport(&self) -> &Viewport {
&self.viewport
}
Expand Down Expand Up @@ -119,12 +139,19 @@ where
scale_float,
scale_u32: _,
} => {
let size = self.physical_size();
self.viewport =
Viewport::with_physical_size(size, self.application_scale_factor * scale_float);
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);
self.wayland_scale_factor = *scale_float;
let logical_size = self.viewport.logical_size();

self.wpviewport.set_destination(
logical_size.width.ceil() as i32,
logical_size.height.ceil() as i32,
);
}
_ => {}
}
Expand All @@ -133,12 +160,12 @@ where
pub fn synchronize(&mut self, application: &A) {
let new_scale_factor = application.scale_factor();
if self.application_scale_factor != new_scale_factor {
self.application_scale_factor = new_scale_factor;
self.viewport = Viewport::with_physical_size(
self.physical_size(),
self.adjusted_physical_size(),
self.current_wayland_scale() * new_scale_factor,
);
self.viewport_version = self.viewport_version.wrapping_add(1);
self.application_scale_factor = new_scale_factor;
}
self.theme = application.theme();
self.appearance = application.style(&self.theme);
Expand Down
6 changes: 6 additions & 0 deletions layershellev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ pub mod reexport {
pub use crate::strtoshape::ShapeName;
pub use wayland_protocols::wp::cursor_shape::v1::client::wp_cursor_shape_device_v1::Shape;
}
pub mod wp_viewport {
pub use wayland_protocols::wp::viewporter::client::wp_viewport::WpViewport;
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -399,6 +402,7 @@ impl<T> WindowStateUnit<T> {
id: self.id,
display: self.display.clone(),
wl_surface: self.wl_surface.clone(),
viewport: self.viewport.clone(),
}
}
}
Expand Down Expand Up @@ -676,6 +680,7 @@ pub struct WindowWrapper {
pub id: id::Id,
display: WlDisplay,
wl_surface: WlSurface,
pub viewport: Option<WpViewport>,
}

/// Define the way layershell program is start
Expand Down Expand Up @@ -744,6 +749,7 @@ impl<T> WindowState<T> {
id: id::Id::MAIN,
display: self.display.as_ref().unwrap().clone(),
wl_surface: self.background_surface.as_ref().unwrap().clone(),
viewport: None,
};
}
self.main_window().gen_wrapper()
Expand Down

0 comments on commit 97ffb61

Please sign in to comment.