Extract rules.apply_{min,max}_size()

This commit is contained in:
Ivan Molodetskikh
2024-12-09 09:32:00 +03:00
parent 9824321fc9
commit 40fa82275c
2 changed files with 41 additions and 27 deletions
+4 -27
View File
@@ -1,5 +1,4 @@
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::cmp::{max, min};
use std::time::Duration; use std::time::Duration;
use niri_config::{Color, CornerRadius, GradientInterpolation, WindowRule}; use niri_config::{Color, CornerRadius, GradientInterpolation, WindowRule};
@@ -535,43 +534,21 @@ impl LayoutElement for Mapped {
} }
fn min_size(&self) -> Size<i32, Logical> { fn min_size(&self) -> Size<i32, Logical> {
let mut size = with_states(self.toplevel().wl_surface(), |state| { let min_size = with_states(self.toplevel().wl_surface(), |state| {
let mut guard = state.cached_state.get::<SurfaceCachedState>(); let mut guard = state.cached_state.get::<SurfaceCachedState>();
guard.current().min_size guard.current().min_size
}); });
if let Some(x) = self.rules.min_width { self.rules.apply_min_size(min_size)
size.w = max(size.w, i32::from(x));
}
if let Some(x) = self.rules.min_height {
size.h = max(size.h, i32::from(x));
}
size
} }
fn max_size(&self) -> Size<i32, Logical> { fn max_size(&self) -> Size<i32, Logical> {
let mut size = with_states(self.toplevel().wl_surface(), |state| { let max_size = with_states(self.toplevel().wl_surface(), |state| {
let mut guard = state.cached_state.get::<SurfaceCachedState>(); let mut guard = state.cached_state.get::<SurfaceCachedState>();
guard.current().max_size guard.current().max_size
}); });
if let Some(x) = self.rules.max_width { self.rules.apply_max_size(max_size)
if size.w == 0 {
size.w = i32::from(x);
} else if x > 0 {
size.w = min(size.w, i32::from(x));
}
}
if let Some(x) = self.rules.max_height {
if size.h == 0 {
size.h = i32::from(x);
} else if x > 0 {
size.h = min(size.h, i32::from(x));
}
}
size
} }
fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool { fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool {
+37
View File
@@ -1,5 +1,8 @@
use std::cmp::{max, min};
use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, WindowRule}; use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, WindowRule};
use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
use smithay::utils::{Logical, Size};
use smithay::wayland::shell::xdg::{ToplevelSurface, XdgToplevelSurfaceRoleAttributes}; use smithay::wayland::shell::xdg::{ToplevelSurface, XdgToplevelSurfaceRoleAttributes};
use crate::layout::scrolling::ColumnWidth; use crate::layout::scrolling::ColumnWidth;
@@ -236,6 +239,40 @@ impl ResolvedWindowRules {
resolved resolved
} }
pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> {
let mut size = min_size;
if let Some(x) = self.min_width {
size.w = max(size.w, i32::from(x));
}
if let Some(x) = self.min_height {
size.h = max(size.h, i32::from(x));
}
size
}
pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> {
let mut size = max_size;
if let Some(x) = self.max_width {
if size.w == 0 {
size.w = i32::from(x);
} else if x > 0 {
size.w = min(size.w, i32::from(x));
}
}
if let Some(x) = self.max_height {
if size.h == 0 {
size.h = i32::from(x);
} else if x > 0 {
size.h = min(size.h, i32::from(x));
}
}
size
}
} }
fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {