diff --git a/crates/composition/src/lib.rs b/crates/composition/src/lib.rs index 82b5990..c76d640 100644 --- a/crates/composition/src/lib.rs +++ b/crates/composition/src/lib.rs @@ -6,7 +6,7 @@ mod shell; mod shell_composition; mod shell_runtime; mod system; -mod value_conversion; +pub mod value_conversion; use layer_shika_adapters::errors::LayerShikaError; use layer_shika_domain::errors::DomainError; @@ -30,6 +30,7 @@ pub use layer_shika_domain::value_objects::popup_request::{ pub use popup_builder::PopupBuilder; pub use shell_runtime::{DEFAULT_WINDOW_NAME, ShellRuntime}; pub use system::{EventContext, EventLoopHandle, ShellControl, SingleWindowShell}; +pub use value_conversion::IntoValue; pub use shell::{ LayerSurfaceHandle, Shell, ShellEventContext, ShellEventLoopHandle, ShellWindowConfigHandler, @@ -59,7 +60,7 @@ pub enum Error { pub mod prelude { pub use crate::{ - AnchorEdges, AnchorStrategy, DEFAULT_WINDOW_NAME, EventContext, EventLoopHandle, + AnchorEdges, AnchorStrategy, DEFAULT_WINDOW_NAME, EventContext, EventLoopHandle, IntoValue, KeyboardInteractivity, Layer, LayerShika, OutputGeometry, OutputHandle, OutputInfo, OutputPolicy, OutputRegistry, PopupBuilder, PopupHandle, PopupPlacement, PopupPositioningMode, PopupRequest, PopupSize, PopupWindow, Result, ShellControl, diff --git a/crates/composition/src/system.rs b/crates/composition/src/system.rs index 542525e..1dd48e0 100644 --- a/crates/composition/src/system.rs +++ b/crates/composition/src/system.rs @@ -795,6 +795,60 @@ impl SingleWindowShell { Ok(()) } + pub fn on_for_output( + &self, + output: OutputHandle, + callback_name: &str, + handler: F, + ) -> Result<()> + where + F: Fn(ShellControl) -> R + 'static, + R: IntoValue, + { + let control = self.control(); + self.with_output(output, |instance| { + let control_clone = control.clone(); + if let Err(e) = instance.set_callback(callback_name, move |_args| { + handler(control_clone.clone()).into_value() + }) { + log::error!( + "Failed to register callback '{}' on output {:?}: {}", + callback_name, + output, + e + ); + } + })?; + Ok(()) + } + + pub fn on_for_output_with_args( + &self, + output: OutputHandle, + callback_name: &str, + handler: F, + ) -> Result<()> + where + F: Fn(&[Value], ShellControl) -> R + 'static, + R: IntoValue, + { + let control = self.control(); + self.with_output(output, |instance| { + let control_clone = control.clone(); + if let Err(e) = instance.set_callback(callback_name, move |args| { + handler(args, control_clone.clone()).into_value() + }) { + log::error!( + "Failed to register callback '{}' on output {:?}: {}", + callback_name, + output, + e + ); + } + })?; + Ok(()) + } + #[must_use] pub fn popup(&self, component_name: impl Into) -> PopupBuilder<'_> { PopupBuilder::new(self, component_name.into())