1use crate::{
2 app::{AppContext, MutableAppContext, WindowInvalidation},
3 elements::Element,
4 fonts::FontCache,
5 platform::Event,
6 text_layout::TextLayoutCache,
7 AssetCache, Scene,
8};
9use pathfinder_geometry::vector::{vec2f, Vector2F};
10use std::{any::Any, collections::HashMap, sync::Arc};
11
12pub struct Presenter {
13 window_id: usize,
14 rendered_views: HashMap<usize, Box<dyn Element>>,
15 parents: HashMap<usize, usize>,
16 font_cache: Arc<FontCache>,
17 text_layout_cache: TextLayoutCache,
18 asset_cache: Arc<AssetCache>,
19}
20
21impl Presenter {
22 pub fn new(
23 window_id: usize,
24 font_cache: Arc<FontCache>,
25 asset_cache: Arc<AssetCache>,
26 app: &MutableAppContext,
27 ) -> Self {
28 Self {
29 window_id,
30 rendered_views: app.render_views(window_id).unwrap(),
31 parents: HashMap::new(),
32 font_cache,
33 text_layout_cache: TextLayoutCache::new(),
34 asset_cache,
35 }
36 }
37
38 pub fn invalidate(&mut self, invalidation: WindowInvalidation, app: &AppContext) {
39 for view_id in invalidation.updated {
40 self.rendered_views
41 .insert(view_id, app.render_view(self.window_id, view_id).unwrap());
42 }
43 for view_id in invalidation.removed {
44 self.rendered_views.remove(&view_id);
45 self.parents.remove(&view_id);
46 }
47 }
48
49 pub fn build_scene(
50 &mut self,
51 window_size: Vector2F,
52 scale_factor: f32,
53 app: &mut MutableAppContext,
54 ) -> Scene {
55 let mut scene = Scene::new(scale_factor);
56
57 if let Some(root_view_id) = app.root_view_id(self.window_id) {
58 self.layout(window_size, app.downgrade());
59 self.after_layout(app);
60 let mut paint_ctx = PaintContext {
61 scene: &mut scene,
62 font_cache: &self.font_cache,
63 text_layout_cache: &self.text_layout_cache,
64 rendered_views: &mut self.rendered_views,
65 };
66 paint_ctx.paint(root_view_id, Vector2F::zero(), app.downgrade());
67 self.text_layout_cache.finish_frame();
68 } else {
69 log::error!("could not find root_view_id for window {}", self.window_id);
70 }
71
72 scene
73 }
74
75 fn layout(&mut self, size: Vector2F, app: &AppContext) {
76 if let Some(root_view_id) = app.root_view_id(self.window_id) {
77 let mut layout_ctx = LayoutContext {
78 rendered_views: &mut self.rendered_views,
79 parents: &mut self.parents,
80 font_cache: &self.font_cache,
81 text_layout_cache: &self.text_layout_cache,
82 asset_cache: &self.asset_cache,
83 view_stack: Vec::new(),
84 };
85 layout_ctx.layout(root_view_id, SizeConstraint::strict(size), app);
86 }
87 }
88
89 fn after_layout(&mut self, app: &mut MutableAppContext) {
90 if let Some(root_view_id) = app.root_view_id(self.window_id) {
91 let mut ctx = AfterLayoutContext {
92 rendered_views: &mut self.rendered_views,
93 font_cache: &self.font_cache,
94 text_layout_cache: &self.text_layout_cache,
95 };
96 ctx.after_layout(root_view_id, app);
97 }
98 }
99
100 pub fn responder_chain(&self, app: &AppContext) -> Option<Vec<usize>> {
101 app.focused_view_id(self.window_id).map(|mut view_id| {
102 let mut chain = vec![view_id];
103 while let Some(parent_id) = self.parents.get(&view_id) {
104 view_id = *parent_id;
105 chain.push(view_id);
106 }
107 chain.reverse();
108 chain
109 })
110 }
111
112 pub fn dispatch_event(
113 &self,
114 event: Event,
115 app: &AppContext,
116 ) -> Vec<(usize, &'static str, Box<dyn Any>)> {
117 let mut event_ctx = EventContext {
118 rendered_views: &self.rendered_views,
119 actions: Vec::new(),
120 font_cache: &self.font_cache,
121 text_layout_cache: &self.text_layout_cache,
122 view_stack: Vec::new(),
123 };
124 if let Some(root_view_id) = app.root_view_id(self.window_id) {
125 event_ctx.dispatch_event_on_view(root_view_id, &event, app);
126 }
127 event_ctx.actions
128 }
129}
130
131pub struct LayoutContext<'a> {
132 rendered_views: &'a mut HashMap<usize, Box<dyn Element>>,
133 parents: &'a mut HashMap<usize, usize>,
134 pub font_cache: &'a FontCache,
135 pub text_layout_cache: &'a TextLayoutCache,
136 pub asset_cache: &'a AssetCache,
137 view_stack: Vec<usize>,
138}
139
140impl<'a> LayoutContext<'a> {
141 fn layout(&mut self, view_id: usize, constraint: SizeConstraint, app: &AppContext) -> Vector2F {
142 if let Some(parent_id) = self.view_stack.last() {
143 self.parents.insert(view_id, *parent_id);
144 }
145 self.view_stack.push(view_id);
146 let mut rendered_view = self.rendered_views.remove(&view_id).unwrap();
147 let size = rendered_view.layout(constraint, self, app);
148 self.rendered_views.insert(view_id, rendered_view);
149 self.view_stack.pop();
150 size
151 }
152}
153
154pub struct AfterLayoutContext<'a> {
155 rendered_views: &'a mut HashMap<usize, Box<dyn Element>>,
156 pub font_cache: &'a FontCache,
157 pub text_layout_cache: &'a TextLayoutCache,
158}
159
160impl<'a> AfterLayoutContext<'a> {
161 fn after_layout(&mut self, view_id: usize, app: &mut MutableAppContext) {
162 if let Some(mut view) = self.rendered_views.remove(&view_id) {
163 view.after_layout(self, app);
164 self.rendered_views.insert(view_id, view);
165 }
166 }
167}
168
169pub struct PaintContext<'a> {
170 rendered_views: &'a mut HashMap<usize, Box<dyn Element>>,
171 pub scene: &'a mut Scene,
172 pub font_cache: &'a FontCache,
173 pub text_layout_cache: &'a TextLayoutCache,
174}
175
176impl<'a> PaintContext<'a> {
177 fn paint(&mut self, view_id: usize, origin: Vector2F, app: &AppContext) {
178 if let Some(mut tree) = self.rendered_views.remove(&view_id) {
179 tree.paint(origin, self, app);
180 self.rendered_views.insert(view_id, tree);
181 }
182 }
183}
184
185pub struct EventContext<'a> {
186 rendered_views: &'a HashMap<usize, Box<dyn Element>>,
187 actions: Vec<(usize, &'static str, Box<dyn Any>)>,
188 pub font_cache: &'a FontCache,
189 pub text_layout_cache: &'a TextLayoutCache,
190 view_stack: Vec<usize>,
191}
192
193impl<'a> EventContext<'a> {
194 pub fn dispatch_event_on_view(
195 &mut self,
196 view_id: usize,
197 event: &Event,
198 app: &AppContext,
199 ) -> bool {
200 if let Some(element) = self.rendered_views.get(&view_id) {
201 self.view_stack.push(view_id);
202 let result = element.dispatch_event(event, self, app);
203 self.view_stack.pop();
204 result
205 } else {
206 false
207 }
208 }
209
210 pub fn dispatch_action<A: 'static + Any>(&mut self, name: &'static str, arg: A) {
211 self.actions
212 .push((*self.view_stack.last().unwrap(), name, Box::new(arg)));
213 }
214}
215
216#[derive(Clone, Copy, Debug, Eq, PartialEq)]
217pub enum Axis {
218 Horizontal,
219 Vertical,
220}
221
222impl Axis {
223 pub fn invert(self) -> Self {
224 match self {
225 Self::Horizontal => Self::Vertical,
226 Self::Vertical => Self::Horizontal,
227 }
228 }
229}
230
231pub trait Vector2FExt {
232 fn along(self, axis: Axis) -> f32;
233}
234
235impl Vector2FExt for Vector2F {
236 fn along(self, axis: Axis) -> f32 {
237 match axis {
238 Axis::Horizontal => self.x(),
239 Axis::Vertical => self.y(),
240 }
241 }
242}
243
244#[derive(Copy, Clone, Debug)]
245pub struct SizeConstraint {
246 pub min: Vector2F,
247 pub max: Vector2F,
248}
249
250impl SizeConstraint {
251 pub fn new(min: Vector2F, max: Vector2F) -> Self {
252 Self { min, max }
253 }
254
255 pub fn strict(size: Vector2F) -> Self {
256 Self {
257 min: size,
258 max: size,
259 }
260 }
261
262 pub fn strict_along(axis: Axis, max: f32) -> Self {
263 match axis {
264 Axis::Horizontal => Self {
265 min: vec2f(max, 0.0),
266 max: vec2f(max, f32::INFINITY),
267 },
268 Axis::Vertical => Self {
269 min: vec2f(0.0, max),
270 max: vec2f(f32::INFINITY, max),
271 },
272 }
273 }
274
275 pub fn max_along(&self, axis: Axis) -> f32 {
276 match axis {
277 Axis::Horizontal => self.max.x(),
278 Axis::Vertical => self.max.y(),
279 }
280 }
281}
282
283pub struct ChildView {
284 view_id: usize,
285 size: Option<Vector2F>,
286 origin: Option<Vector2F>,
287}
288
289impl ChildView {
290 pub fn new(view_id: usize) -> Self {
291 Self {
292 view_id,
293 size: None,
294 origin: None,
295 }
296 }
297}
298
299impl Element for ChildView {
300 fn layout(
301 &mut self,
302 constraint: SizeConstraint,
303 ctx: &mut LayoutContext,
304 app: &AppContext,
305 ) -> Vector2F {
306 let size = ctx.layout(self.view_id, constraint, app);
307 self.size = Some(size);
308 size
309 }
310
311 fn after_layout(&mut self, ctx: &mut AfterLayoutContext, app: &mut MutableAppContext) {
312 ctx.after_layout(self.view_id, app);
313 }
314
315 fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext) {
316 self.origin = Some(origin);
317 ctx.paint(self.view_id, origin, app);
318 }
319
320 fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool {
321 ctx.dispatch_event_on_view(self.view_id, event, app)
322 }
323
324 fn size(&self) -> Option<Vector2F> {
325 self.size
326 }
327}
328
329#[cfg(test)]
330mod tests {
331 // #[test]
332 // fn test_responder_chain() {
333 // let settings = settings_rx(None);
334 // let mut app = App::new().unwrap();
335 // let workspace = app.add_model(|ctx| Workspace::new(Vec::new(), ctx));
336 // let (window_id, workspace_view) =
337 // app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
338
339 // let invalidations = Rc::new(RefCell::new(Vec::new()));
340 // let invalidations_ = invalidations.clone();
341 // app.on_window_invalidated(window_id, move |invalidation, _| {
342 // invalidations_.borrow_mut().push(invalidation)
343 // });
344
345 // let active_pane_id = workspace_view.update(&mut app, |view, ctx| {
346 // ctx.focus(view.active_pane());
347 // view.active_pane().id()
348 // });
349
350 // app.update(|app| {
351 // let mut presenter = Presenter::new(
352 // window_id,
353 // Rc::new(FontCache::new()),
354 // Rc::new(AssetCache::new()),
355 // app,
356 // );
357 // for invalidation in invalidations.borrow().iter().cloned() {
358 // presenter.update(vec2f(1024.0, 768.0), 2.0, Some(invalidation), app);
359 // }
360
361 // assert_eq!(
362 // presenter.responder_chain(app.ctx()).unwrap(),
363 // vec![workspace_view.id(), active_pane_id]
364 // );
365 // });
366 // }
367}