iced_graphics/shell.rs
1//! Control the windowing runtime from a renderer.
2use std::sync::Arc;
3
4/// A windowing shell.
5#[derive(Clone)]
6pub struct Shell(Arc<dyn Notifier>);
7
8impl Shell {
9 /// Creates a new [`Shell`].
10 pub fn new(notifier: impl Notifier) -> Self {
11 Self(Arc::new(notifier))
12 }
13
14 /// Creates a headless [`Shell`].
15 pub fn headless() -> Self {
16 struct Headless;
17
18 impl Notifier for Headless {
19 fn tick(&self) {}
20 fn request_redraw(&self) {}
21 fn invalidate_layout(&self) {}
22 }
23
24 Self::new(Headless)
25 }
26
27 /// Requests for [`Renderer::tick`](crate::core::Renderer::tick) to
28 /// be called by the [`Shell`].
29 pub fn tick(&self) {
30 self.0.tick();
31 }
32
33 /// Requests for all windows of the [`Shell`] to be redrawn.
34 pub fn request_redraw(&self) {
35 self.0.request_redraw();
36 }
37
38 /// Requests for all layouts of the [`Shell`] to be recomputed.
39 pub fn invalidate_layout(&self) {
40 self.0.invalidate_layout();
41 }
42}
43
44/// A type that can notify a shell of certain events.
45pub trait Notifier: Send + Sync + 'static {
46 /// Requests for [`Renderer::tick`](crate::core::Renderer::tick) to
47 /// be called by the [`Shell`].
48 fn tick(&self);
49
50 /// Requests for all windows of the [`Shell`] to be redrawn.
51 fn request_redraw(&self);
52
53 /// Requests for all layouts of the [`Shell`] to be recomputed.
54 fn invalidate_layout(&self);
55}