Skip to main content

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