From 16c8315a53aec448b8e94b1644199796004f8f7e Mon Sep 17 00:00:00 2001 From: drendog Date: Thu, 11 Dec 2025 07:26:33 +0100 Subject: [PATCH] feat: add builder pattern for surface reconfiguration --- .../wayland/event_handling/app_dispatcher.rs | 6 +- .../event_handling/event_dispatcher.rs | 1 - crates/composition/src/lib.rs | 8 +- crates/composition/src/shell.rs | 7 +- crates/composition/src/system.rs | 86 ++++++++++++++++++- examples/runtime-surface-config/README.md | 10 ++- examples/runtime-surface-config/src/main.rs | 20 ++--- 7 files changed, 116 insertions(+), 22 deletions(-) diff --git a/crates/adapters/src/wayland/event_handling/app_dispatcher.rs b/crates/adapters/src/wayland/event_handling/app_dispatcher.rs index 5e51558..a0f8254 100644 --- a/crates/adapters/src/wayland/event_handling/app_dispatcher.rs +++ b/crates/adapters/src/wayland/event_handling/app_dispatcher.rs @@ -49,6 +49,10 @@ impl Dispatch for AppState { width, height, } => { + info!( + "Layer surface configured with compositor size: {}x{}", + width, height + ); let layer_surface_id = layer_surface.id(); let Some(surface) = state.get_output_by_layer_surface_mut(&layer_surface_id) else { info!( @@ -198,8 +202,6 @@ impl Dispatch for AppState { surface_x, surface_y, } => { - info!("Pointer entered surface {:?}", surface.id()); - let surface_id = surface.id(); if let Some(key) = state.get_key_by_surface(&surface_id).cloned() { diff --git a/crates/adapters/src/wayland/event_handling/event_dispatcher.rs b/crates/adapters/src/wayland/event_handling/event_dispatcher.rs index d51871d..1685891 100644 --- a/crates/adapters/src/wayland/event_handling/event_dispatcher.rs +++ b/crates/adapters/src/wayland/event_handling/event_dispatcher.rs @@ -96,7 +96,6 @@ impl SurfaceState { surface_x: f64, surface_y: f64, ) { - info!("Pointer entered surface {:?}", surface.id()); self.set_last_pointer_serial(serial); self.set_current_pointer_position(surface_x, surface_y); diff --git a/crates/composition/src/lib.rs b/crates/composition/src/lib.rs index faa4af6..f700f7e 100644 --- a/crates/composition/src/lib.rs +++ b/crates/composition/src/lib.rs @@ -3,8 +3,8 @@ mod event_loop; mod layer_surface; mod popup_builder; -mod selector; mod selection; +mod selector; mod shell; mod shell_config; mod shell_runtime; @@ -34,10 +34,12 @@ pub use layer_shika_domain::value_objects::popup_request::{ }; pub use layer_surface::{LayerSurfaceHandle, ShellSurfaceConfigHandler}; pub use popup_builder::PopupBuilder; -pub use selector::{Output, Selector, Surface, SurfaceInfo}; pub use selection::Selection; +pub use selector::{Output, Selector, Surface, SurfaceInfo}; pub use shell_runtime::{DEFAULT_SURFACE_NAME, ShellRuntime}; -pub use system::{EventDispatchContext, ShellControl, SurfaceControlHandle}; +pub use system::{ + EventDispatchContext, RuntimeSurfaceConfigBuilder, ShellControl, SurfaceControlHandle, +}; pub use value_conversion::IntoValue; pub use shell::{ diff --git a/crates/composition/src/shell.rs b/crates/composition/src/shell.rs index 87c589c..7274dfa 100644 --- a/crates/composition/src/shell.rs +++ b/crates/composition/src/shell.rs @@ -622,7 +622,7 @@ impl Shell { } SurfaceCommand::ApplyConfig { name, config } => { log::debug!("Surface command: ApplyConfig '{}'", name); - for surface in ctx.surfaces_by_name(&name) { + for surface in ctx.surfaces_by_name_mut(&name) { let handle = LayerSurfaceHandle::from_window_state(surface); handle.set_size(config.dimensions.width(), config.dimensions.height()); @@ -632,6 +632,11 @@ impl Shell { handle.set_layer(config.layer); handle.set_keyboard_interactivity(config.keyboard_interactivity); handle.commit(); + + surface.update_size_with_compositor_logic( + config.dimensions.width(), + config.dimensions.height(), + ); } } } diff --git a/crates/composition/src/system.rs b/crates/composition/src/system.rs index 2aef61e..d0e2ab7 100644 --- a/crates/composition/src/system.rs +++ b/crates/composition/src/system.rs @@ -13,7 +13,7 @@ use layer_shika_domain::errors::DomainError; use layer_shika_domain::prelude::{ AnchorEdges, KeyboardInteractivity, Layer, Margins, OutputPolicy, ScaleFactor, }; -use layer_shika_domain::value_objects::dimensions::PopupDimensions; +use layer_shika_domain::value_objects::dimensions::{PopupDimensions, SurfaceDimension}; use layer_shika_domain::value_objects::output_handle::OutputHandle; use layer_shika_domain::value_objects::output_info::OutputInfo; use layer_shika_domain::value_objects::popup_positioning_mode::PopupPositioningMode; @@ -307,6 +307,90 @@ impl SurfaceControlHandle { }) }) } + + pub fn configure(self) -> RuntimeSurfaceConfigBuilder { + RuntimeSurfaceConfigBuilder { + handle: self, + config: SurfaceConfig::new(), + } + } +} + +pub struct RuntimeSurfaceConfigBuilder { + handle: SurfaceControlHandle, + config: SurfaceConfig, +} + +impl RuntimeSurfaceConfigBuilder { + #[must_use] + pub fn size(mut self, width: u32, height: u32) -> Self { + self.config.dimensions = SurfaceDimension::from_raw(width, height); + self + } + + #[must_use] + pub fn width(mut self, width: u32) -> Self { + self.config.dimensions = SurfaceDimension::from_raw(width, self.config.dimensions.height()); + self + } + + #[must_use] + pub fn height(mut self, height: u32) -> Self { + self.config.dimensions = SurfaceDimension::from_raw(self.config.dimensions.width(), height); + self + } + + #[must_use] + pub const fn layer(mut self, layer: Layer) -> Self { + self.config.layer = layer; + self + } + + #[must_use] + pub fn margins(mut self, margins: impl Into) -> Self { + self.config.margin = margins.into(); + self + } + + #[must_use] + pub const fn anchor(mut self, anchor: AnchorEdges) -> Self { + self.config.anchor = anchor; + self + } + + #[must_use] + pub const fn exclusive_zone(mut self, zone: i32) -> Self { + self.config.exclusive_zone = zone; + self + } + + #[must_use] + pub fn namespace(mut self, namespace: impl Into) -> Self { + self.config.namespace = namespace.into(); + self + } + + #[must_use] + pub const fn keyboard_interactivity(mut self, mode: KeyboardInteractivity) -> Self { + self.config.keyboard_interactivity = mode; + self + } + + #[must_use] + pub fn output_policy(mut self, policy: OutputPolicy) -> Self { + self.config.output_policy = policy; + self + } + + #[must_use] + pub fn scale_factor(mut self, sf: impl TryInto) -> Self { + self.config.scale_factor = sf.try_into().unwrap_or_default(); + self + } + + pub fn apply(self) -> Result<()> { + self.handle.apply_config(self.config) + } } pub struct EventDispatchContext<'a> { diff --git a/examples/runtime-surface-config/README.md b/examples/runtime-surface-config/README.md index 273c01c..22b6ceb 100644 --- a/examples/runtime-surface-config/README.md +++ b/examples/runtime-surface-config/README.md @@ -28,9 +28,13 @@ cargo run -p runtime-surface-config ```rust shell.on("Bar", "toggle-size", move |control| { - let bar = control.surface("Bar"); - bar.resize(width, height)?; - bar.set_exclusive_zone(new_size)?; + control.surface("Bar") + .configure() + .size(0, new_size) + .exclusive_zone(new_size as i32) + .margins(0, 0, 0, 0) + .apply()?; + Value::Struct(Struct::from_iter([("expanded".into(), is_expanded.into())])) })?; ``` diff --git a/examples/runtime-surface-config/src/main.rs b/examples/runtime-surface-config/src/main.rs index d18a624..6f5202d 100644 --- a/examples/runtime-surface-config/src/main.rs +++ b/examples/runtime-surface-config/src/main.rs @@ -96,17 +96,15 @@ fn setup_toggle_size_callback( } }; - let bar = control.surface("Bar"); - if let Err(e) = bar.resize(width, height) { - log::error!("Failed to resize bar: {}", e); - } - - if let Err(e) = bar.set_exclusive_zone(new_size.try_into().unwrap_or(32)) { - log::error!("Failed to set exclusive zone: {}", e); - } - - if let Err(e) = control.surface("Bar").set_margins((0, 0, 0, 0)) { - log::error!("Failed to set margins: {}", e); + if let Err(e) = control + .surface("Bar") + .configure() + .size(width, height) + .exclusive_zone(new_size.try_into().unwrap_or(32)) + .margins((0, 0, 0, 0)) + .apply() + { + log::error!("Failed to apply configuration: {}", e); } log::info!(