From e9f02a8533af92929ef83b003dfceac8da8e08a1 Mon Sep 17 00:00:00 2001 From: drendog Date: Sat, 25 Oct 2025 15:24:52 +0200 Subject: [PATCH] refactor: margins --- src/windowing/builder.rs | 54 +++++++++++++++++++++++++--------------- src/windowing/config.rs | 21 +++++++++++----- src/windowing/mod.rs | 15 +++++------ 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/windowing/builder.rs b/src/windowing/builder.rs index 96d5c98..284cff6 100644 --- a/src/windowing/builder.rs +++ b/src/windowing/builder.rs @@ -6,10 +6,10 @@ use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{ use crate::errors::{LayerShikaError, Result}; -use super::{config::WindowConfig, WindowingSystem}; +use super::{config::{Margins, WindowConfig}, WindowingSystem}; pub struct WindowingSystemBuilder { - config: WindowConfig, + config: Option, } impl Default for WindowingSystemBuilder { @@ -21,33 +21,41 @@ impl Default for WindowingSystemBuilder { impl WindowingSystemBuilder { #[inline] #[must_use] - pub fn new() -> Self { + pub const fn new() -> Self { Self { - config: WindowConfig::default(), + config: None, } } #[must_use] pub const fn with_height(mut self, height: u32) -> Self { - self.config.height = height; + if let Some(ref mut config) = self.config { + config.height = height; + } self } #[must_use] pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self { - self.config.layer = layer; + if let Some(ref mut config) = self.config { + config.layer = layer; + } self } #[must_use] pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { - self.config.margin = (top, right, bottom, left); + if let Some(ref mut config) = self.config { + config.margin = Margins { top, right, bottom, left }; + } self } #[must_use] pub const fn with_anchor(mut self, anchor: Anchor) -> Self { - self.config.anchor = anchor; + if let Some(ref mut config) = self.config { + config.anchor = anchor; + } self } @@ -56,41 +64,47 @@ impl WindowingSystemBuilder { mut self, interactivity: KeyboardInteractivity, ) -> Self { - self.config.keyboard_interactivity = interactivity; + if let Some(ref mut config) = self.config { + config.keyboard_interactivity = interactivity; + } self } #[must_use] pub const fn with_exclusive_zone(mut self, zone: i32) -> Self { - self.config.exclusive_zone = zone; + if let Some(ref mut config) = self.config { + config.exclusive_zone = zone; + } self } #[must_use] pub fn with_namespace(mut self, namespace: String) -> Self { - self.config.namespace = namespace; + if let Some(ref mut config) = self.config { + config.namespace = namespace; + } self } #[must_use] pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self { - self.config.scale_factor = scale_factor; + if let Some(ref mut config) = self.config { + config.scale_factor = scale_factor; + } self } #[must_use] pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self { - self.config.component_definition = Some(component); + self.config = Some(WindowConfig::new(component)); self } #[allow(clippy::missing_errors_doc)] - pub fn build(&mut self) -> Result { - match self.config.component_definition { - Some(_) => WindowingSystem::new(&mut self.config), - None => Err(LayerShikaError::InvalidInput( - "Slint component not set".into(), - )), - } + pub fn build(self) -> Result { + let config = self.config.as_ref().ok_or_else(|| { + LayerShikaError::InvalidInput("Slint component not set".into()) + })?; + WindowingSystem::new(config) } } diff --git a/src/windowing/config.rs b/src/windowing/config.rs index 3d7618e..ac358a1 100644 --- a/src/windowing/config.rs +++ b/src/windowing/config.rs @@ -4,30 +4,39 @@ use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{ zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity}, }; +#[derive(Debug, Clone, Copy, Default)] +pub struct Margins { + pub top: i32, + pub right: i32, + pub bottom: i32, + pub left: i32, +} + +#[derive(Clone)] pub struct WindowConfig { pub height: u32, pub layer: zwlr_layer_shell_v1::Layer, - pub margin: (i32, i32, i32, i32), + pub margin: Margins, pub anchor: Anchor, pub keyboard_interactivity: KeyboardInteractivity, pub exclusive_zone: i32, pub scale_factor: f32, pub namespace: String, - pub component_definition: Option, + pub component_definition: ComponentDefinition, } -impl Default for WindowConfig { - fn default() -> Self { +impl WindowConfig { + pub fn new(component_definition: ComponentDefinition) -> Self { Self { height: 30, layer: zwlr_layer_shell_v1::Layer::Top, - margin: (0, 0, 0, 0), + margin: Margins::default(), anchor: Anchor::Top | Anchor::Left | Anchor::Right, keyboard_interactivity: KeyboardInteractivity::OnDemand, exclusive_zone: -1, namespace: "layer-shika".to_owned(), scale_factor: 1.0, - component_definition: None, + component_definition, } } } diff --git a/src/windowing/mod.rs b/src/windowing/mod.rs index e841214..b7f05f9 100644 --- a/src/windowing/mod.rs +++ b/src/windowing/mod.rs @@ -64,7 +64,7 @@ pub struct WindowingSystem { } impl WindowingSystem { - fn new(config: &mut WindowConfig) -> Result { + fn new(config: &WindowConfig) -> Result { info!("Initializing WindowingSystem"); let connection = Rc::new(Connection::connect_to_env().map_err(LayerShikaError::WaylandConnection)?); @@ -87,12 +87,9 @@ impl WindowingSystem { let pointer = Rc::new(seat.get_pointer(&event_queue.handle(), ())); let window = Self::initialize_renderer(&surface, &connection.display(), config) .map_err(|e| LayerShikaError::EGLContextCreation(e.to_string()))?; - let component_definition = config.component_definition.take().ok_or_else(|| { - LayerShikaError::WindowConfiguration("Component definition is required".to_string()) - })?; let mut builder = WindowStateBuilder::new() - .with_component_definition(component_definition) + .with_component_definition(config.component_definition.clone()) .with_surface(Rc::clone(&surface)) .with_layer_surface(Rc::clone(&layer_surface)) .with_pointer(Rc::clone(&pointer)) @@ -210,10 +207,10 @@ impl WindowingSystem { ) { layer_surface.set_anchor(config.anchor); layer_surface.set_margin( - config.margin.0, - config.margin.1, - config.margin.2, - config.margin.3, + config.margin.top, + config.margin.right, + config.margin.bottom, + config.margin.left, ); layer_surface.set_exclusive_zone(config.exclusive_zone);