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 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 pub fn send_event_loop_event(&self, event: NativeEvent) {
54 self.proxy.send_event(event).ok();
55 }
56}
57
58#[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
76pub enum PluginEvent<'a> {
78 RunnerCreated {
80 runner: &'a mut Runner,
81 },
82 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 WindowClosed {
93 window: &'a Window,
94 tree: &'a Tree,
95 },
96
97 AfterRedraw {
99 window: &'a Window,
100 font_collection: &'a FontCollection,
101 tree: &'a Tree,
102 },
103
104 BeforePresenting {
106 window: &'a Window,
107 font_collection: &'a FontCollection,
108 tree: &'a Tree,
109 },
110
111 AfterPresenting {
113 window: &'a Window,
114 font_collection: &'a FontCollection,
115 tree: &'a Tree,
116 },
117
118 BeforeRender {
120 window: &'a Window,
121 canvas: &'a Canvas,
122 font_collection: &'a FontCollection,
123 tree: &'a Tree,
124 },
125
126 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 StartedMeasuringLayout {
137 window: &'a Window,
138 tree: &'a Tree,
139 },
140
141 FinishedMeasuringLayout {
143 window: &'a Window,
144 tree: &'a Tree,
145 },
146
147 StartedMeasuringEvents {
149 window: &'a Window,
150 tree: &'a Tree,
151 },
152
153 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 KeyboardInput {
183 window: &'a Window,
184 key: Key,
185 code: Code,
186 modifiers: Modifiers,
187 is_pressed: bool,
188 },
189}
190
191pub trait FreyaPlugin {
193 fn on_event(&mut self, event: &mut PluginEvent, handle: PluginHandle);
195}