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>
This commit is contained in:
parent
d6e11de1f1
commit
a74b6e3a9b
5 changed files with 119 additions and 46 deletions
47
src/hooks.rs
Normal file
47
src/hooks.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue