From 98c506dfd2ac724f7996aad03737789fe5b34cc5 Mon Sep 17 00:00:00 2001 From: drendog Date: Sat, 25 Oct 2025 19:15:23 +0200 Subject: [PATCH] refactor: type-state builder pattern for windowing system --- src/windowing/builder.rs | 89 +++++++++++++++------------------------- 1 file changed, 33 insertions(+), 56 deletions(-) diff --git a/src/windowing/builder.rs b/src/windowing/builder.rs index d364fa4..f5aec75 100644 --- a/src/windowing/builder.rs +++ b/src/windowing/builder.rs @@ -1,67 +1,61 @@ +use super::{ + config::{Margins, WindowConfig}, + WindowingSystem, +}; +use crate::errors::Result; use slint_interpreter::ComponentDefinition; use smithay_client_toolkit::reexports::protocols_wlr::layer_shell::v1::client::{ zwlr_layer_shell_v1::{self}, zwlr_layer_surface_v1::{Anchor, KeyboardInteractivity}, }; +use std::marker::PhantomData; -use crate::errors::{LayerShikaError, Result}; +pub struct NeedsComponent; +pub struct HasComponent; -use super::{ - config::{Margins, WindowConfig}, - WindowingSystem, -}; - -pub struct WindowingSystemBuilder { - config: Option, +pub struct WindowingSystemBuilder { + config: WindowConfig, + _state: PhantomData, } -impl Default for WindowingSystemBuilder { - fn default() -> Self { - Self::new() - } -} - -impl WindowingSystemBuilder { +impl WindowingSystemBuilder { #[inline] #[must_use] - pub const fn new() -> Self { - Self { config: None } + pub fn new(component_definition: ComponentDefinition) -> WindowingSystemBuilder { + WindowingSystemBuilder { + config: WindowConfig::new(component_definition), + _state: PhantomData, + } } +} +impl WindowingSystemBuilder { #[must_use] pub const fn with_height(mut self, height: u32) -> Self { - if let Some(ref mut config) = self.config { - config.height = height; - } + self.config.height = height; self } #[must_use] pub const fn with_layer(mut self, layer: zwlr_layer_shell_v1::Layer) -> Self { - if let Some(ref mut config) = self.config { - config.layer = layer; - } + self.config.layer = layer; self } #[must_use] pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { - if let Some(ref mut config) = self.config { - config.margin = Margins { - top, - right, - bottom, - left, - }; - } + self.config.margin = Margins { + top, + right, + bottom, + left, + }; self } #[must_use] pub const fn with_anchor(mut self, anchor: Anchor) -> Self { - if let Some(ref mut config) = self.config { - config.anchor = anchor; - } + self.config.anchor = anchor; self } @@ -70,47 +64,30 @@ impl WindowingSystemBuilder { mut self, interactivity: KeyboardInteractivity, ) -> Self { - if let Some(ref mut config) = self.config { - config.keyboard_interactivity = interactivity; - } + self.config.keyboard_interactivity = interactivity; self } #[must_use] pub const fn with_exclusive_zone(mut self, zone: i32) -> Self { - if let Some(ref mut config) = self.config { - config.exclusive_zone = zone; - } + self.config.exclusive_zone = zone; self } #[must_use] pub fn with_namespace(mut self, namespace: String) -> Self { - if let Some(ref mut config) = self.config { - config.namespace = namespace; - } + self.config.namespace = namespace; self } #[must_use] pub const fn with_scale_factor(mut self, scale_factor: f32) -> Self { - 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 = Some(WindowConfig::new(component)); + self.config.scale_factor = scale_factor; self } #[allow(clippy::missing_errors_doc)] pub fn build(self) -> Result { - let config = self - .config - .ok_or_else(|| LayerShikaError::InvalidInput("Slint component not set".into()))?; - WindowingSystem::new(config) + WindowingSystem::new(self.config) } }