refactor: margins

This commit is contained in:
drendog 2025-10-25 15:24:52 +02:00
parent 1c5bb97782
commit e9f02a8533
Signed by: dwenya
GPG key ID: 8DD77074645332D0
3 changed files with 55 additions and 35 deletions

View file

@ -6,10 +6,10 @@ use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{
use crate::errors::{LayerShikaError, Result}; use crate::errors::{LayerShikaError, Result};
use super::{config::WindowConfig, WindowingSystem}; use super::{config::{Margins, WindowConfig}, WindowingSystem};
pub struct WindowingSystemBuilder { pub struct WindowingSystemBuilder {
config: WindowConfig, config: Option<WindowConfig>,
} }
impl Default for WindowingSystemBuilder { impl Default for WindowingSystemBuilder {
@ -21,33 +21,41 @@ impl Default for WindowingSystemBuilder {
impl WindowingSystemBuilder { impl WindowingSystemBuilder {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
config: WindowConfig::default(), config: None,
} }
} }
#[must_use] #[must_use]
pub const fn with_height(mut self, height: u32) -> Self { 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 self
} }
#[must_use] #[must_use]
pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self { 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 self
} }
#[must_use] #[must_use]
pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { 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 self
} }
#[must_use] #[must_use]
pub const fn with_anchor(mut self, anchor: Anchor) -> Self { 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 self
} }
@ -56,41 +64,47 @@ impl WindowingSystemBuilder {
mut self, mut self,
interactivity: KeyboardInteractivity, interactivity: KeyboardInteractivity,
) -> Self { ) -> Self {
self.config.keyboard_interactivity = interactivity; if let Some(ref mut config) = self.config {
config.keyboard_interactivity = interactivity;
}
self self
} }
#[must_use] #[must_use]
pub const fn with_exclusive_zone(mut self, zone: i32) -> Self { 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 self
} }
#[must_use] #[must_use]
pub fn with_namespace(mut self, namespace: String) -> Self { 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 self
} }
#[must_use] #[must_use]
pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self { 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 self
} }
#[must_use] #[must_use]
pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self { pub fn with_component_definition(mut self, component: ComponentDefinition) -> Self {
self.config.component_definition = Some(component); self.config = Some(WindowConfig::new(component));
self self
} }
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]
pub fn build(&mut self) -> Result<WindowingSystem> { pub fn build(self) -> Result<WindowingSystem> {
match self.config.component_definition { let config = self.config.as_ref().ok_or_else(|| {
Some(_) => WindowingSystem::new(&mut self.config), LayerShikaError::InvalidInput("Slint component not set".into())
None => Err(LayerShikaError::InvalidInput( })?;
"Slint component not set".into(), WindowingSystem::new(config)
)),
}
} }
} }

View file

@ -4,30 +4,39 @@ use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{
zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity}, 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 struct WindowConfig {
pub height: u32, pub height: u32,
pub layer: zwlr_layer_shell_v1::Layer, pub layer: zwlr_layer_shell_v1::Layer,
pub margin: (i32, i32, i32, i32), pub margin: Margins,
pub anchor: Anchor, pub anchor: Anchor,
pub keyboard_interactivity: KeyboardInteractivity, pub keyboard_interactivity: KeyboardInteractivity,
pub exclusive_zone: i32, pub exclusive_zone: i32,
pub scale_factor: f32, pub scale_factor: f32,
pub namespace: String, pub namespace: String,
pub component_definition: Option<ComponentDefinition>, pub component_definition: ComponentDefinition,
} }
impl Default for WindowConfig { impl WindowConfig {
fn default() -> Self { pub fn new(component_definition: ComponentDefinition) -> Self {
Self { Self {
height: 30, height: 30,
layer: zwlr_layer_shell_v1::Layer::Top, layer: zwlr_layer_shell_v1::Layer::Top,
margin: (0, 0, 0, 0), margin: Margins::default(),
anchor: Anchor::Top | Anchor::Left | Anchor::Right, anchor: Anchor::Top | Anchor::Left | Anchor::Right,
keyboard_interactivity: KeyboardInteractivity::OnDemand, keyboard_interactivity: KeyboardInteractivity::OnDemand,
exclusive_zone: -1, exclusive_zone: -1,
namespace: "layer-shika".to_owned(), namespace: "layer-shika".to_owned(),
scale_factor: 1.0, scale_factor: 1.0,
component_definition: None, component_definition,
} }
} }
} }

View file

@ -64,7 +64,7 @@ pub struct WindowingSystem {
} }
impl WindowingSystem { impl WindowingSystem {
fn new(config: &mut WindowConfig) -> Result<Self> { fn new(config: &WindowConfig) -> Result<Self> {
info!("Initializing WindowingSystem"); info!("Initializing WindowingSystem");
let connection = let connection =
Rc::new(Connection::connect_to_env().map_err(LayerShikaError::WaylandConnection)?); 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 pointer = Rc::new(seat.get_pointer(&event_queue.handle(), ()));
let window = Self::initialize_renderer(&surface, &connection.display(), config) let window = Self::initialize_renderer(&surface, &connection.display(), config)
.map_err(|e| LayerShikaError::EGLContextCreation(e.to_string()))?; .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() let mut builder = WindowStateBuilder::new()
.with_component_definition(component_definition) .with_component_definition(config.component_definition.clone())
.with_surface(Rc::clone(&surface)) .with_surface(Rc::clone(&surface))
.with_layer_surface(Rc::clone(&layer_surface)) .with_layer_surface(Rc::clone(&layer_surface))
.with_pointer(Rc::clone(&pointer)) .with_pointer(Rc::clone(&pointer))
@ -210,10 +207,10 @@ impl WindowingSystem {
) { ) {
layer_surface.set_anchor(config.anchor); layer_surface.set_anchor(config.anchor);
layer_surface.set_margin( layer_surface.set_margin(
config.margin.0, config.margin.top,
config.margin.1, config.margin.right,
config.margin.2, config.margin.bottom,
config.margin.3, config.margin.left,
); );
layer_surface.set_exclusive_zone(config.exclusive_zone); layer_surface.set_exclusive_zone(config.exclusive_zone);