cosmic-comp/src/hooks.rs
Yureka a74b6e3a9b Add hooks for custom window decorations
This is a first use of the new hooks system, which allows customizing
cosmic-comp at compile-time.
In this case, the view() function of CosmicWindow / CosmicStack is
hooked and the hook can change what is rendered as the header bar.

Signed-off-by: Yureka <yuka@yuka.dev>
2025-10-29 11:27:14 +01:00

47 lines
1.7 KiB
Rust

// SPDX-License-Identifier: GPL-3.0-only
use crate::shell::element::stack::{
CosmicStackInternal, DefaultDecorations as DefaultStackDecorations, Message as StackMessage,
};
use crate::shell::element::window::{
CosmicWindowInternal, DefaultDecorations as DefaultWindowDecorations, Message as WindowMessage,
};
use std::sync::{Arc, OnceLock};
/// An _unstable_ interface to customize cosmic-comp at compile-time by providing
/// hooks to be run in specific code paths.
#[derive(Default, Debug, Clone)]
pub struct Hooks {
pub window_decorations:
Option<Arc<dyn Decorations<CosmicWindowInternal, WindowMessage> + Send + Sync>>,
pub stack_decorations:
Option<Arc<dyn Decorations<CosmicStackInternal, StackMessage> + Send + Sync>>,
}
pub static HOOKS: OnceLock<Hooks> = OnceLock::new();
pub trait Decorations<Internal, Message>: std::fmt::Debug {
fn view(&self, state: &Internal) -> cosmic::Element<'_, Message>;
}
impl Decorations<CosmicWindowInternal, WindowMessage>
for Option<Arc<dyn Decorations<CosmicWindowInternal, WindowMessage> + Send + Sync>>
{
fn view(&self, window: &CosmicWindowInternal) -> cosmic::Element<'_, WindowMessage> {
match self {
None => DefaultWindowDecorations.view(window),
Some(deco) => deco.view(window),
}
}
}
impl Decorations<CosmicStackInternal, StackMessage>
for Option<Arc<dyn Decorations<CosmicStackInternal, StackMessage> + Send + Sync>>
{
fn view(&self, window: &CosmicStackInternal) -> cosmic::Element<'_, StackMessage> {
match self {
None => DefaultStackDecorations.view(window),
Some(deco) => deco.view(window),
}
}
}