From dc9374b76d0d0b64a72735fb61d20efd05e43582 Mon Sep 17 00:00:00 2001 From: drendog Date: Wed, 3 Dec 2025 12:02:13 +0100 Subject: [PATCH] refactor: unify naming patterns between app and shell --- crates/composition/src/lib.rs | 11 ++++-- crates/composition/src/shell.rs | 49 +++++++++++++++++++++++ crates/composition/src/shell_runtime.rs | 20 ++++++++++ crates/composition/src/system.rs | 52 +++++++++++++++++++++++++ src/lib.rs | 2 +- src/prelude.rs | 2 +- 6 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 crates/composition/src/shell_runtime.rs diff --git a/crates/composition/src/lib.rs b/crates/composition/src/lib.rs index 4308ffb..cb515df 100644 --- a/crates/composition/src/lib.rs +++ b/crates/composition/src/lib.rs @@ -4,6 +4,7 @@ mod builder; mod popup_builder; mod shell; mod shell_composition; +mod shell_runtime; mod system; mod value_conversion; @@ -27,6 +28,7 @@ pub use layer_shika_domain::value_objects::popup_request::{ PopupHandle, PopupPlacement, PopupRequest, PopupSize, }; pub use popup_builder::PopupBuilder; +pub use shell_runtime::{DEFAULT_WINDOW_NAME, ShellRuntime}; pub use system::{EventContext, EventLoopHandle, ShellControl, SingleWindowShell}; pub use shell::{ @@ -57,10 +59,11 @@ pub enum Error { pub mod prelude { pub use crate::{ - AnchorEdges, AnchorStrategy, EventContext, EventLoopHandle, KeyboardInteractivity, Layer, - LayerShika, OutputGeometry, OutputHandle, OutputInfo, OutputPolicy, OutputRegistry, - PopupBuilder, PopupHandle, PopupPlacement, PopupPositioningMode, PopupRequest, PopupSize, - PopupWindow, Result, ShellControl, SingleWindowShell, + AnchorEdges, AnchorStrategy, DEFAULT_WINDOW_NAME, EventContext, EventLoopHandle, + KeyboardInteractivity, Layer, LayerShika, OutputGeometry, OutputHandle, OutputInfo, + OutputPolicy, OutputRegistry, PopupBuilder, PopupHandle, PopupPlacement, + PopupPositioningMode, PopupRequest, PopupSize, PopupWindow, Result, ShellControl, + ShellRuntime, SingleWindowShell, }; pub use crate::{ diff --git a/crates/composition/src/shell.rs b/crates/composition/src/shell.rs index 06d7836..dd6f5ff 100644 --- a/crates/composition/src/shell.rs +++ b/crates/composition/src/shell.rs @@ -1,4 +1,5 @@ use crate::shell_composition::ShellWindowDefinition; +use crate::shell_runtime::ShellRuntime; use crate::system::{EventContext, PopupCommand, ShellControl}; use crate::{Error, Result}; use layer_shika_adapters::errors::EventLoopError; @@ -335,6 +336,54 @@ impl Shell { } } +impl ShellRuntime for Shell { + type LoopHandle = ShellEventLoopHandle; + type Context<'a> = ShellEventContext<'a>; + + fn event_loop_handle(&self) -> Self::LoopHandle { + ShellEventLoopHandle { + system: Rc::downgrade(&self.inner), + } + } + + fn with_component(&self, name: &str, mut f: F) + where + F: FnMut(&ComponentInstance), + { + let facade = self.inner.borrow(); + let system = facade.inner_ref(); + + if self.windows.contains_key(name) { + for window in system.app_state().windows_by_shell_name(name) { + f(window.component_instance()); + } + } + } + + fn with_all_components(&self, mut f: F) + where + F: FnMut(&str, &ComponentInstance), + { + let facade = self.inner.borrow(); + let system = facade.inner_ref(); + + for name in self.windows.keys() { + if let Some(window) = system.app_state().primary_output() { + f(name, window.component_instance()); + } + } + } + + fn run(&mut self) -> Result<()> { + log::info!( + "Starting shell event loop with {} windows", + self.windows.len() + ); + self.inner.borrow_mut().run()?; + Ok(()) + } +} + pub struct ShellEventLoopHandle { system: Weak>, } diff --git a/crates/composition/src/shell_runtime.rs b/crates/composition/src/shell_runtime.rs new file mode 100644 index 0000000..f546a2e --- /dev/null +++ b/crates/composition/src/shell_runtime.rs @@ -0,0 +1,20 @@ +use layer_shika_adapters::platform::slint_interpreter::ComponentInstance; + +pub const DEFAULT_WINDOW_NAME: &str = "main"; + +pub trait ShellRuntime { + type LoopHandle; + type Context<'a>; + + fn event_loop_handle(&self) -> Self::LoopHandle; + + fn with_component(&self, name: &str, f: F) + where + F: FnMut(&ComponentInstance); + + fn with_all_components(&self, f: F) + where + F: FnMut(&str, &ComponentInstance); + + fn run(&mut self) -> crate::Result<()>; +} diff --git a/crates/composition/src/system.rs b/crates/composition/src/system.rs index 28e4e12..542525e 100644 --- a/crates/composition/src/system.rs +++ b/crates/composition/src/system.rs @@ -1,4 +1,5 @@ use crate::popup_builder::PopupBuilder; +use crate::shell_runtime::{DEFAULT_WINDOW_NAME, ShellRuntime}; use crate::value_conversion::IntoValue; use crate::{Error, Result}; use layer_shika_adapters::errors::EventLoopError; @@ -646,6 +647,7 @@ impl EventContext<'_> { pub struct SingleWindowShell { inner: Rc>, popup_command_sender: channel::Sender, + window_name: String, } impl SingleWindowShell { @@ -668,6 +670,7 @@ impl SingleWindowShell { let shell = Self { inner: Rc::clone(&inner_rc), popup_command_sender: sender, + window_name: DEFAULT_WINDOW_NAME.to_string(), }; shell.setup_popup_command_handler(receiver)?; @@ -675,6 +678,17 @@ impl SingleWindowShell { Ok(shell) } + #[must_use] + pub fn with_window_name(mut self, name: impl Into) -> Self { + self.window_name = name.into(); + self + } + + #[must_use] + pub fn window_name(&self) -> &str { + &self.window_name + } + fn setup_popup_command_handler(&self, receiver: channel::Channel) -> Result<()> { let loop_handle = self.inner.borrow().inner_ref().event_loop_handle(); let control = self.control(); @@ -857,3 +871,41 @@ impl SingleWindowShell { system.app_state().output_registry().clone() } } + +impl ShellRuntime for SingleWindowShell { + type LoopHandle = EventLoopHandle; + type Context<'a> = EventContext<'a>; + + fn event_loop_handle(&self) -> Self::LoopHandle { + EventLoopHandle { + system: Rc::downgrade(&self.inner), + } + } + + fn with_component(&self, _name: &str, mut f: F) + where + F: FnMut(&ComponentInstance), + { + let facade = self.inner.borrow(); + let system = facade.inner_ref(); + for window in system.app_state().all_outputs() { + f(window.component_instance()); + } + } + + fn with_all_components(&self, mut f: F) + where + F: FnMut(&str, &ComponentInstance), + { + let facade = self.inner.borrow(); + let system = facade.inner_ref(); + for window in system.app_state().all_outputs() { + f(&self.window_name, window.component_instance()); + } + } + + fn run(&mut self) -> Result<()> { + self.inner.borrow_mut().run()?; + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index a8a50c9..ef0a2c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ pub use layer_shika_composition::{ AnchorEdges, AnchorStrategy, Error, EventContext, EventLoopHandle, KeyboardInteractivity, Layer, LayerShika, OutputGeometry, OutputHandle, OutputInfo, OutputPolicy, OutputRegistry, PopupHandle, PopupPlacement, PopupPositioningMode, PopupRequest, PopupSize, PopupWindow, - Result, ShellControl, SingleWindowShell, + Result, ShellControl, ShellRuntime, SingleWindowShell, DEFAULT_WINDOW_NAME, }; pub use layer_shika_composition::{ diff --git a/src/prelude.rs b/src/prelude.rs index 4850c1a..17957d2 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -10,7 +10,7 @@ pub use crate::{ Error, EventContext, EventLoopHandle, LayerShika, PopupWindow, Result, ShellControl, - SingleWindowShell, + ShellRuntime, SingleWindowShell, DEFAULT_WINDOW_NAME, }; pub use crate::{