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