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 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 pub fn send_event_loop_event(&self, event: NativeEvent) {
49 self.proxy.send_event(event).ok();
50 }
51}
52
53#[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
71pub enum PluginEvent<'a> {
73 RunnerCreated {
75 runner: &'a mut Runner,
76 },
77 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 WindowClosed {
88 window: &'a Window,
89 tree: &'a Tree,
90 },
91
92 AfterRedraw {
94 window: &'a Window,
95 font_collection: &'a FontCollection,
96 tree: &'a Tree,
97 },
98
99 BeforePresenting {
101 window: &'a Window,
102 font_collection: &'a FontCollection,
103 tree: &'a Tree,
104 },
105
106 AfterPresenting {
108 window: &'a Window,
109 font_collection: &'a FontCollection,
110 tree: &'a Tree,
111 },
112
113 BeforeRender {
115 window: &'a Window,
116 canvas: &'a Canvas,
117 font_collection: &'a FontCollection,
118 tree: &'a Tree,
119 },
120
121 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 StartedMeasuringLayout {
132 window: &'a Window,
133 tree: &'a Tree,
134 },
135
136 FinishedMeasuringLayout {
138 window: &'a Window,
139 tree: &'a Tree,
140 },
141
142 StartedMeasuringEvents {
144 window: &'a Window,
145 tree: &'a Tree,
146 },
147
148 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
177pub trait FreyaPlugin {
179 fn on_event(&mut self, event: &mut PluginEvent, handle: PluginHandle);
181}