Skip to content

Commit 1fa3bd6

Browse files
committed
Add example for customized window decorations
Signed-off-by: Yureka <[email protected]>
1 parent b62afd2 commit 1fa3bd6

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use cosmic::iced::{Color, Rectangle, Size};
2+
use cosmic::widget;
3+
use cosmic::widget::canvas;
4+
use cosmic_comp::hooks::{Decorations, Hooks};
5+
use cosmic_comp::shell::element::stack::{
6+
DefaultDecorations as DefaultStackDecorations, TAB_HEIGHT,
7+
};
8+
use cosmic_comp::shell::element::window::{
9+
DefaultDecorations as DefaultWindowDecorations, SSD_HEIGHT,
10+
};
11+
use std::sync::Arc;
12+
13+
/// The struct implementing Decorations hook
14+
#[derive(Debug)]
15+
struct AddIndicator<Lower> {
16+
lower: Lower,
17+
height: i32,
18+
}
19+
20+
/// An iced program drawing a circle, which we'll add to the window decorations
21+
struct Circle {
22+
radius: f32,
23+
color: Color,
24+
}
25+
26+
impl<Message, Theme, Renderer: cosmic::iced_renderer::geometry::Renderer>
27+
canvas::Program<Message, Theme, Renderer> for Circle
28+
{
29+
type State = ();
30+
31+
fn draw(
32+
&self,
33+
_state: &(),
34+
renderer: &Renderer,
35+
_theme: &Theme,
36+
bounds: Rectangle,
37+
_cursor: cosmic::iced::mouse::Cursor,
38+
) -> Vec<Renderer::Geometry> {
39+
let bounds = bounds.size();
40+
let min = bounds.height.min(bounds.width);
41+
let mut frame = canvas::Frame::new(renderer, Size::new(min, min));
42+
let circle = canvas::Path::circle(frame.center(), self.radius);
43+
frame.fill(&circle, self.color);
44+
vec![frame.into_geometry()]
45+
}
46+
}
47+
48+
impl<Internal, Message: std::clone::Clone + 'static, Lower: Decorations<Internal, Message>>
49+
Decorations<Internal, Message> for AddIndicator<Lower>
50+
{
51+
fn view(&self, window: &Internal) -> cosmic::Element<'_, Message> {
52+
let orig = self.lower.view(window);
53+
widget::row()
54+
.push(
55+
widget::column()
56+
.push(canvas(Circle {
57+
radius: (self.height as f32 / 2.) * 0.8,
58+
color: Color::new(1.0, 0.0, 0.0, 1.0),
59+
}))
60+
.width(self.height as f32),
61+
)
62+
.push(orig)
63+
.into()
64+
}
65+
}
66+
67+
/// The customized cosmic-comp entrypoint
68+
fn main() -> Result<(), Box<dyn std::error::Error>> {
69+
cosmic_comp::run(Hooks {
70+
window_decorations: Some(Arc::new(AddIndicator {
71+
height: SSD_HEIGHT,
72+
lower: DefaultWindowDecorations,
73+
})),
74+
stack_decorations: Some(Arc::new(AddIndicator {
75+
height: TAB_HEIGHT,
76+
lower: DefaultStackDecorations,
77+
})),
78+
})
79+
}

0 commit comments

Comments
 (0)