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, mut event: PluginEvent, handle: PluginHandle) {
65        for plugin in self.plugins.borrow_mut().iter_mut() {
66            plugin.on_event(&mut event, handle.clone())
67        }
68    }
69}
70
71/// Event emitted to Plugins.
72pub enum PluginEvent<'a> {
73    /// A runner just got created.
74    RunnerCreated {
75        runner: &'a mut Runner,
76    },
77    /// A Window just got created.
78    WindowCreated {
79        window: &'a Window,
80        font_collection: &'a FontCollection,
81        tree: &'a Tree,
82        animation_clock: &'a AnimationClock,
83        runner: &'a mut Runner,
84    },
85
86    /// A Window just got closed.
87    WindowClosed {
88        window: &'a Window,
89        tree: &'a Tree,
90    },
91
92    /// After having rendered, presented and everything else.
93    AfterRedraw {
94        window: &'a Window,
95        font_collection: &'a FontCollection,
96        tree: &'a Tree,
97    },
98
99    /// Before presenting the canvas to the window.
100    BeforePresenting {
101        window: &'a Window,
102        font_collection: &'a FontCollection,
103        tree: &'a Tree,
104    },
105
106    /// After presenting the canvas to the window.
107    AfterPresenting {
108        window: &'a Window,
109        font_collection: &'a FontCollection,
110        tree: &'a Tree,
111    },
112
113    /// Before starting to render the app to the Canvas.
114    BeforeRender {
115        window: &'a Window,
116        canvas: &'a Canvas,
117        font_collection: &'a FontCollection,
118        tree: &'a Tree,
119    },
120
121    /// After rendering the app to the Canvas.
122    AfterRender {
123        window: &'a Window,
124        canvas: &'a Canvas,
125        font_collection: &'a FontCollection,
126        tree: &'a Tree,
127        animation_clock: &'a AnimationClock,
128    },
129
130    /// Before starting to measure the layout.
131    StartedMeasuringLayout {
132        window: &'a Window,
133        tree: &'a Tree,
134    },
135
136    /// After measuringg the layout.
137    FinishedMeasuringLayout {
138        window: &'a Window,
139        tree: &'a Tree,
140    },
141
142    /// Before starting to process the queued events.
143    StartedMeasuringEvents {
144        window: &'a Window,
145        tree: &'a Tree,
146    },
147
148    /// After processing the queued events.
149    FinishedMeasuringEvents {
150        window: &'a Window,
151        tree: &'a Tree,
152    },
153
154    StartedUpdatingTree {
155        window: &'a Window,
156        tree: &'a Tree,
157    },
158
159    FinishedUpdatingTree {
160        window: &'a Window,
161        tree: &'a Tree,
162    },
163
164    BeforeAccessibility {
165        window: &'a Window,
166        font_collection: &'a FontCollection,
167        tree: &'a Tree,
168    },
169
170    AfterAccessibility {
171        window: &'a Window,
172        font_collection: &'a FontCollection,
173        tree: &'a Tree,
174    },
175}
176
177/// Skeleton for Freya plugins.
178pub trait FreyaPlugin {
179    /// React on events emitted by Freya.
180    fn on_event(&mut self, event: &mut PluginEvent, handle: PluginHandle);
181}