mirror of
https://github.com/rust-windowing/winit.git
synced 2026-09-02 06:40:06 -04:00
wayland: implement resize increments
This commit is contained in:
@@ -140,6 +140,7 @@ pub struct WindowState {
|
||||
/// Min size.
|
||||
min_surface_size: LogicalSize<u32>,
|
||||
max_surface_size: Option<LogicalSize<u32>>,
|
||||
resize_increments: Option<LogicalSize<u32>>,
|
||||
|
||||
/// The size of the window when no states were applied to it. The primary use for it
|
||||
/// is to fallback to original window size, before it was maximized, if the compositor
|
||||
@@ -223,6 +224,7 @@ impl WindowState {
|
||||
last_configure: None,
|
||||
max_surface_size: None,
|
||||
min_surface_size: MIN_WINDOW_SIZE,
|
||||
resize_increments: None,
|
||||
pointer_constraints,
|
||||
pointers: Default::default(),
|
||||
queue_handle: queue_handle.clone(),
|
||||
@@ -361,6 +363,42 @@ impl WindowState {
|
||||
.unwrap_or(new_size.height);
|
||||
}
|
||||
|
||||
// Apply size increments.
|
||||
//
|
||||
// We conditionally apply increments to avoid conflicts with the compositor's layout rules:
|
||||
// 1. If the window is floating (constrain == true), we snap to increments to ensure the
|
||||
// app's grid alignment.
|
||||
// 2. If the user is interactively resizing (is_resizing), we snap the size to provide
|
||||
// feedback.
|
||||
//
|
||||
// However, we MUST NOT snap if the compositor enforces a specific size (constrain == false,
|
||||
// or states like Maximized/Tiled). Snapping in these cases (e.g. corner tiling) would
|
||||
// shrink the window below the allocated area, creating visible gaps between valid
|
||||
// windows or screen edges.
|
||||
if (constrain || configure.is_resizing())
|
||||
&& !configure.is_maximized()
|
||||
&& !configure.is_fullscreen()
|
||||
&& !configure.is_tiled()
|
||||
{
|
||||
if let Some(increments) = self.resize_increments {
|
||||
// We use min size as a base size for the increments, similar to how X11 does it.
|
||||
//
|
||||
// This ensures that we can always reach the min size and the increments are
|
||||
// calculated from it.
|
||||
let (delta_width, delta_height) = (
|
||||
new_size.width.saturating_sub(self.min_surface_size.width),
|
||||
new_size.height.saturating_sub(self.min_surface_size.height),
|
||||
);
|
||||
|
||||
let width = self.min_surface_size.width
|
||||
+ (delta_width / increments.width) * increments.width;
|
||||
let height = self.min_surface_size.height
|
||||
+ (delta_height / increments.height) * increments.height;
|
||||
|
||||
new_size = (width, height).into();
|
||||
}
|
||||
}
|
||||
|
||||
let new_state = configure.state;
|
||||
let old_state = self.last_configure.as_ref().map(|configure| configure.state);
|
||||
|
||||
@@ -749,6 +787,18 @@ impl WindowState {
|
||||
self.selected_cursor = SelectedCursor::Custom(cursor);
|
||||
}
|
||||
|
||||
/// Set the resize increments of the window.
|
||||
pub fn set_resize_increments(&mut self, increments: Option<LogicalSize<u32>>) {
|
||||
self.resize_increments = increments;
|
||||
// NOTE: We don't update the window size here, because it will be done on the next resize
|
||||
// or configure event.
|
||||
}
|
||||
|
||||
/// Get the resize increments of the window.
|
||||
pub fn resize_increments(&self) -> Option<LogicalSize<u32>> {
|
||||
self.resize_increments
|
||||
}
|
||||
|
||||
fn apply_custom_cursor(&self, cursor: &CustomCursor) {
|
||||
self.apply_on_pointer(|pointer, data| {
|
||||
let surface = pointer.surface();
|
||||
|
||||
Reference in New Issue
Block a user