freya_winit/
plugins.rs

1use std::{
2    cell::RefCell,
3    rc::Rc,
4};
5
6use freya_core::integration::*;
7use freya_engine::prelude::{
8    Canvas,
9    FontCollection,
10};
11use winit::{
12    event_loop::EventLoopProxy,
13    window::{
14        Window,
15        WindowId,
16    },
17};
18
19use crate::renderer::{
20    NativeEvent,
21    NativeWindowEvent,
22    NativeWindowEventAction,
23};
24
25#[derive(Clone)]
26pub struct PluginHandle {
27    pub proxy: EventLoopProxy<NativeEvent>,
28}
29
30impl PluginHandle {
31    pub fn new(proxy: &EventLoopProxy<NativeEvent>) -> Self {
32        Self {
33            proxy: proxy.clone(),
34        }
35    }
36
37    /// Emit a [PlatformEvent]. Useful to simulate certain events.
38    pub fn send_platform_event(&self, event: PlatformEvent, window_id: WindowId) {
39        self.proxy
40            .send_event(NativeEvent::Window(NativeWindowEvent {
41                window_id,
42                action: NativeWindowEventAction::PlatformEvent(event),
43            }))
44            .ok();
45    }
46
47    /// Emit a [NativeEvent].
48    pub fn send_event_loop_event(&self, event: NativeEvent) {
49        self.proxy.send_event(event).ok();
50    }
51}
52
53/// Manages all loaded plugins.
54#[derive(Default, Clone)]
55pub struct PluginsManager {
56    plugins: Rc<RefCell<Vec<Box<dyn FreyaPlugin>>>>,
57}
58
59impl PluginsManager {
60    pub fn add_plugin(&mut self, plugin: impl FreyaPlugin + 'static) {
61        self.plugins.borrow_mut().push(Box::new(plugin))
62    }
63
64    pub fn send(&mut self, event: PluginEvent, handle: PluginHandle) {
65        for plugin in self.plugins.borrow_mut().iter_mut() {
66            plugin.on_event(&event, handle.clone())
67        }
68    }
69}
70
71/// Event emitted to Plugins.
72pub enum PluginEvent<'a> {
73    /// A Window just got created.
74    WindowCreated {
75        window: &'a Window,
76        font_collection: &'a FontCollection,
77        tree: &'a Tree,
78        animation_clock: &'a AnimationClock,
79    },
80
81    /// A Window just got closed.
82    WindowClosed {
83        window: &'a Window,
84        tree: &'a Tree,
85    },
86
87    /// After having rendered, presented and everything else.
88    AfterRedraw {
89        window: &'a Window,
90        font_collection: &'a FontCollection,
91        tree: &'a Tree,
92    },
93
94    /// Before presenting the canvas to the window.
95    BeforePresenting {
96        window: &'a Window,
97        font_collection: &'a FontCollection,
98        tree: &'a Tree,
99    },
100
101    /// After presenting the canvas to the window.
102    AfterPresenting {
103        window: &'a Window,
104        font_collection: &'a FontCollection,
105        tree: &'a Tree,
106    },
107
108    /// Before starting to render the app to the Canvas.
109    BeforeRender {
110        window: &'a Window,
111        canvas: &'a Canvas,
112        font_collection: &'a FontCollection,
113        tree: &'a Tree,
114    },
115
116    /// After rendering the app to the Canvas.
117    AfterRender {
118        window: &'a Window,
119        canvas: &'a Canvas,
120        font_collection: &'a FontCollection,
121        tree: &'a Tree,
122        animation_clock: &'a AnimationClock,
123    },
124
125    /// Before starting to measure the layout.
126    StartedMeasuringLayout {
127        window: &'a Window,
128        tree: &'a Tree,
129    },
130
131    /// After measuring the layout.
132    FinishedMeasuringLayout {
133        window: &'a Window,
134        tree: &'a Tree,
135    },
136
137    /// Before starting to process the queued events.
138    StartedMeasuringEvents {
139        window: &'a Window,
140        tree: &'a Tree,
141    },
142
143    /// After processing the queued events.
144    FinishedMeasuringEvents {
145        window: &'a Window,
146        tree: &'a Tree,
147    },
148
149    StartedUpdatingTree {
150        window: &'a Window,
151        tree: &'a Tree,
152    },
153
154    FinishedUpdatingTree {
155        window: &'a Window,
156        tree: &'a Tree,
157    },
158
159    BeforeAccessibility {
160        window: &'a Window,
161        font_collection: &'a FontCollection,
162        tree: &'a Tree,
163    },
164
165    AfterAccessibility {
166        window: &'a Window,
167        font_collection: &'a FontCollection,
168        tree: &'a Tree,
169    },
170}
171
172/// Skeleton for Freya plugins.
173pub trait FreyaPlugin {
174    /// React on events emitted by Freya.
175    fn on_event(&mut self, event: &PluginEvent, handle: PluginHandle);
176}