1use crate::{
2 elements::ElementBox,
3 executor,
4 keymap::{self, Keystroke},
5 platform::{self, WindowOptions},
6 presenter::Presenter,
7 util::{post_inc, timeout},
8 AssetCache, AssetSource, ClipboardItem, FontCache, PathPromptOptions, TextLayoutCache,
9};
10use anyhow::{anyhow, Result};
11use keymap::MatchResult;
12use parking_lot::{Mutex, RwLock};
13use pathfinder_geometry::{rect::RectF, vector::vec2f};
14use platform::Event;
15use postage::{mpsc, sink::Sink as _, stream::Stream as _};
16use smol::prelude::*;
17use std::{
18 any::{type_name, Any, TypeId},
19 cell::RefCell,
20 collections::{HashMap, HashSet, VecDeque},
21 fmt::{self, Debug},
22 hash::{Hash, Hasher},
23 marker::PhantomData,
24 path::{Path, PathBuf},
25 rc::{self, Rc},
26 sync::{Arc, Weak},
27 time::Duration,
28};
29
30pub trait Entity: 'static + Send + Sync {
31 type Event;
32}
33
34pub trait View: Entity {
35 fn ui_name() -> &'static str;
36 fn render<'a>(&self, app: &AppContext) -> ElementBox;
37 fn on_focus(&mut self, _ctx: &mut ViewContext<Self>) {}
38 fn on_blur(&mut self, _ctx: &mut ViewContext<Self>) {}
39 fn keymap_context(&self, _: &AppContext) -> keymap::Context {
40 Self::default_keymap_context()
41 }
42 fn default_keymap_context() -> keymap::Context {
43 let mut ctx = keymap::Context::default();
44 ctx.set.insert(Self::ui_name().into());
45 ctx
46 }
47}
48
49pub trait ReadModel {
50 fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T;
51}
52
53pub trait UpdateModel {
54 fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
55 where
56 T: Entity,
57 F: FnOnce(&mut T, &mut ModelContext<T>) -> S;
58}
59
60pub trait ReadView {
61 fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
62}
63
64pub trait UpdateView {
65 fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
66 where
67 T: View,
68 F: FnOnce(&mut T, &mut ViewContext<T>) -> S;
69}
70
71pub struct Menu<'a> {
72 pub name: &'a str,
73 pub items: Vec<MenuItem<'a>>,
74}
75
76pub enum MenuItem<'a> {
77 Action {
78 name: &'a str,
79 keystroke: Option<&'a str>,
80 action: &'a str,
81 arg: Option<Box<dyn Any + 'static>>,
82 },
83 Separator,
84}
85
86#[derive(Clone)]
87pub struct App(Rc<RefCell<MutableAppContext>>);
88
89#[derive(Clone)]
90pub struct TestAppContext(Rc<RefCell<MutableAppContext>>, Rc<platform::test::Platform>);
91
92impl App {
93 pub fn test<T, A: AssetSource, F: FnOnce(&mut MutableAppContext) -> T>(
94 asset_source: A,
95 f: F,
96 ) -> T {
97 let platform = platform::test::platform();
98 let foreground = Rc::new(executor::Foreground::test());
99 let ctx = Rc::new(RefCell::new(MutableAppContext::new(
100 foreground,
101 Rc::new(platform),
102 asset_source,
103 )));
104 ctx.borrow_mut().weak_self = Some(Rc::downgrade(&ctx));
105 let mut ctx = ctx.borrow_mut();
106 f(&mut *ctx)
107 }
108
109 pub fn test_async<T, F, A: AssetSource, Fn>(asset_source: A, f: Fn) -> T
110 where
111 Fn: FnOnce(TestAppContext) -> F,
112 F: Future<Output = T>,
113 {
114 let platform = Rc::new(platform::test::platform());
115 let foreground = Rc::new(executor::Foreground::test());
116 let ctx = TestAppContext(
117 Rc::new(RefCell::new(MutableAppContext::new(
118 foreground.clone(),
119 platform.clone(),
120 asset_source,
121 ))),
122 platform,
123 );
124 ctx.0.borrow_mut().weak_self = Some(Rc::downgrade(&ctx.0));
125
126 let future = f(ctx);
127 smol::block_on(foreground.run(future))
128 }
129
130 pub fn new(asset_source: impl AssetSource) -> Result<Self> {
131 let platform = platform::current::platform();
132 let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
133 let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
134 foreground,
135 platform.clone(),
136 asset_source,
137 ))));
138
139 let ctx = app.0.clone();
140 platform.on_menu_command(Box::new(move |command, arg| {
141 let mut ctx = ctx.borrow_mut();
142 if let Some(key_window_id) = ctx.platform.key_window_id() {
143 if let Some((presenter, _)) =
144 ctx.presenters_and_platform_windows.get(&key_window_id)
145 {
146 let presenter = presenter.clone();
147 let path = presenter.borrow().dispatch_path(ctx.as_ref());
148 ctx.dispatch_action_any(key_window_id, &path, command, arg.unwrap_or(&()));
149 } else {
150 ctx.dispatch_global_action_any(command, arg.unwrap_or(&()));
151 }
152 } else {
153 ctx.dispatch_global_action_any(command, arg.unwrap_or(&()));
154 }
155 }));
156
157 app.0.borrow_mut().weak_self = Some(Rc::downgrade(&app.0));
158 Ok(app)
159 }
160
161 pub fn on_become_active<F>(self, mut callback: F) -> Self
162 where
163 F: 'static + FnMut(&mut MutableAppContext),
164 {
165 let ctx = self.0.clone();
166 self.0
167 .borrow()
168 .platform
169 .on_become_active(Box::new(move || callback(&mut *ctx.borrow_mut())));
170 self
171 }
172
173 pub fn on_resign_active<F>(self, mut callback: F) -> Self
174 where
175 F: 'static + FnMut(&mut MutableAppContext),
176 {
177 let ctx = self.0.clone();
178 self.0
179 .borrow()
180 .platform
181 .on_resign_active(Box::new(move || callback(&mut *ctx.borrow_mut())));
182 self
183 }
184
185 pub fn on_event<F>(self, mut callback: F) -> Self
186 where
187 F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
188 {
189 let ctx = self.0.clone();
190 self.0.borrow().platform.on_event(Box::new(move |event| {
191 callback(event, &mut *ctx.borrow_mut())
192 }));
193 self
194 }
195
196 pub fn on_open_files<F>(self, mut callback: F) -> Self
197 where
198 F: 'static + FnMut(Vec<PathBuf>, &mut MutableAppContext),
199 {
200 let ctx = self.0.clone();
201 self.0
202 .borrow()
203 .platform
204 .on_open_files(Box::new(move |paths| {
205 callback(paths, &mut *ctx.borrow_mut())
206 }));
207 self
208 }
209
210 pub fn run<F>(self, on_finish_launching: F)
211 where
212 F: 'static + FnOnce(&mut MutableAppContext),
213 {
214 let platform = self.0.borrow().platform.clone();
215 platform.run(Box::new(move || {
216 let mut ctx = self.0.borrow_mut();
217 on_finish_launching(&mut *ctx);
218 }))
219 }
220
221 pub fn font_cache(&self) -> Arc<FontCache> {
222 self.0.borrow().ctx.font_cache.clone()
223 }
224
225 fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
226 let mut state = self.0.borrow_mut();
227 state.pending_flushes += 1;
228 let result = callback(&mut *state);
229 state.flush_effects();
230 result
231 }
232}
233
234impl TestAppContext {
235 pub fn dispatch_action<T: 'static + Any>(
236 &self,
237 window_id: usize,
238 responder_chain: Vec<usize>,
239 name: &str,
240 arg: T,
241 ) {
242 self.0.borrow_mut().dispatch_action_any(
243 window_id,
244 &responder_chain,
245 name,
246 Box::new(arg).as_ref(),
247 );
248 }
249
250 pub fn dispatch_keystroke(
251 &self,
252 window_id: usize,
253 responder_chain: Vec<usize>,
254 keystroke: &Keystroke,
255 ) -> Result<bool> {
256 let mut state = self.0.borrow_mut();
257 state.dispatch_keystroke(window_id, responder_chain, keystroke)
258 }
259
260 pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
261 where
262 T: Entity,
263 F: FnOnce(&mut ModelContext<T>) -> T,
264 {
265 let mut state = self.0.borrow_mut();
266 state.pending_flushes += 1;
267 let handle = state.add_model(build_model);
268 state.flush_effects();
269 handle
270 }
271
272 pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
273 where
274 T: View,
275 F: FnOnce(&mut ViewContext<T>) -> T,
276 {
277 self.0.borrow_mut().add_window(build_root_view)
278 }
279
280 pub fn window_ids(&self) -> Vec<usize> {
281 self.0.borrow().window_ids().collect()
282 }
283
284 pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
285 self.0.borrow().root_view(window_id)
286 }
287
288 pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
289 where
290 T: View,
291 F: FnOnce(&mut ViewContext<T>) -> T,
292 {
293 let mut state = self.0.borrow_mut();
294 state.pending_flushes += 1;
295 let handle = state.add_view(window_id, build_view);
296 state.flush_effects();
297 handle
298 }
299
300 pub fn add_option_view<T, F>(
301 &mut self,
302 window_id: usize,
303 build_view: F,
304 ) -> Option<ViewHandle<T>>
305 where
306 T: View,
307 F: FnOnce(&mut ViewContext<T>) -> Option<T>,
308 {
309 let mut state = self.0.borrow_mut();
310 state.pending_flushes += 1;
311 let handle = state.add_option_view(window_id, build_view);
312 state.flush_effects();
313 handle
314 }
315
316 pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
317 callback(self.0.borrow().as_ref())
318 }
319
320 pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
321 let mut state = self.0.borrow_mut();
322 // Don't increment pending flushes in order to effects to be flushed before the callback
323 // completes, which is helpful in tests.
324 let result = callback(&mut *state);
325 // Flush effects after the callback just in case there are any. This can happen in edge
326 // cases such as the closure dropping handles.
327 state.flush_effects();
328 result
329 }
330
331 pub fn font_cache(&self) -> Arc<FontCache> {
332 self.0.borrow().ctx.font_cache.clone()
333 }
334
335 pub fn platform(&self) -> Rc<dyn platform::Platform> {
336 self.0.borrow().platform.clone()
337 }
338
339 pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
340 self.1.as_ref().simulate_new_path_selection(result);
341 }
342
343 pub fn did_prompt_for_new_path(&self) -> bool {
344 self.1.as_ref().did_prompt_for_new_path()
345 }
346}
347
348impl UpdateModel for TestAppContext {
349 fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
350 where
351 T: Entity,
352 F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
353 {
354 let mut state = self.0.borrow_mut();
355 state.pending_flushes += 1;
356 let result = state.update_model(handle, update);
357 state.flush_effects();
358 result
359 }
360}
361
362impl UpdateView for TestAppContext {
363 fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
364 where
365 T: View,
366 F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
367 {
368 let mut state = self.0.borrow_mut();
369 state.pending_flushes += 1;
370 let result = state.update_view(handle, update);
371 state.flush_effects();
372 result
373 }
374}
375
376type ActionCallback =
377 dyn FnMut(&mut dyn AnyView, &dyn Any, &mut MutableAppContext, usize, usize) -> bool;
378
379type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext);
380
381pub struct MutableAppContext {
382 weak_self: Option<rc::Weak<RefCell<Self>>>,
383 platform: Rc<dyn platform::Platform>,
384 assets: Arc<AssetCache>,
385 ctx: AppContext,
386 actions: HashMap<TypeId, HashMap<String, Vec<Box<ActionCallback>>>>,
387 global_actions: HashMap<String, Vec<Box<GlobalActionCallback>>>,
388 keystroke_matcher: keymap::Matcher,
389 next_entity_id: usize,
390 next_window_id: usize,
391 next_task_id: usize,
392 subscriptions: HashMap<usize, Vec<Subscription>>,
393 model_observations: HashMap<usize, Vec<ModelObservation>>,
394 view_observations: HashMap<usize, Vec<ViewObservation>>,
395 window_invalidations: HashMap<usize, WindowInvalidation>,
396 presenters_and_platform_windows:
397 HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
398 debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
399 foreground: Rc<executor::Foreground>,
400 future_handlers: Rc<RefCell<HashMap<usize, FutureHandler>>>,
401 stream_handlers: Rc<RefCell<HashMap<usize, StreamHandler>>>,
402 pending_effects: VecDeque<Effect>,
403 pending_flushes: usize,
404 flushing_effects: bool,
405}
406
407impl MutableAppContext {
408 pub fn new(
409 foreground: Rc<executor::Foreground>,
410 platform: Rc<dyn platform::Platform>,
411 asset_source: impl AssetSource,
412 ) -> Self {
413 let fonts = platform.fonts();
414 Self {
415 weak_self: None,
416 platform,
417 assets: Arc::new(AssetCache::new(asset_source)),
418 ctx: AppContext {
419 models: Default::default(),
420 windows: Default::default(),
421 values: Default::default(),
422 ref_counts: Arc::new(Mutex::new(RefCounts::default())),
423 background: Arc::new(executor::Background::new()),
424 thread_pool: scoped_pool::Pool::new(num_cpus::get(), "app"),
425 font_cache: Arc::new(FontCache::new(fonts)),
426 },
427 actions: HashMap::new(),
428 global_actions: HashMap::new(),
429 keystroke_matcher: keymap::Matcher::default(),
430 next_entity_id: 0,
431 next_window_id: 0,
432 next_task_id: 0,
433 subscriptions: HashMap::new(),
434 model_observations: HashMap::new(),
435 view_observations: HashMap::new(),
436 window_invalidations: HashMap::new(),
437 presenters_and_platform_windows: HashMap::new(),
438 debug_elements_callbacks: HashMap::new(),
439 foreground,
440 future_handlers: Default::default(),
441 stream_handlers: Default::default(),
442 pending_effects: VecDeque::new(),
443 pending_flushes: 0,
444 flushing_effects: false,
445 }
446 }
447
448 pub fn upgrade(&self) -> App {
449 App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
450 }
451
452 pub fn platform(&self) -> Rc<dyn platform::Platform> {
453 self.platform.clone()
454 }
455
456 pub fn font_cache(&self) -> &Arc<FontCache> {
457 &self.ctx.font_cache
458 }
459
460 pub fn foreground_executor(&self) -> &Rc<executor::Foreground> {
461 &self.foreground
462 }
463
464 pub fn background_executor(&self) -> &Arc<executor::Background> {
465 &self.ctx.background
466 }
467
468 pub fn on_debug_elements<F>(&mut self, window_id: usize, callback: F)
469 where
470 F: 'static + Fn(&AppContext) -> crate::json::Value,
471 {
472 self.debug_elements_callbacks
473 .insert(window_id, Box::new(callback));
474 }
475
476 pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
477 self.debug_elements_callbacks
478 .get(&window_id)
479 .map(|debug_elements| debug_elements(&self.ctx))
480 }
481
482 pub fn add_action<S, V, T, F>(&mut self, name: S, mut handler: F)
483 where
484 S: Into<String>,
485 V: View,
486 T: Any,
487 F: 'static + FnMut(&mut V, &T, &mut ViewContext<V>),
488 {
489 let name = name.into();
490 let name_clone = name.clone();
491 let handler = Box::new(
492 move |view: &mut dyn AnyView,
493 arg: &dyn Any,
494 app: &mut MutableAppContext,
495 window_id: usize,
496 view_id: usize| {
497 match arg.downcast_ref() {
498 Some(arg) => {
499 let mut ctx = ViewContext::new(app, window_id, view_id);
500 handler(
501 view.as_any_mut()
502 .downcast_mut()
503 .expect("downcast is type safe"),
504 arg,
505 &mut ctx,
506 );
507 ctx.halt_action_dispatch
508 }
509 None => {
510 log::error!("Could not downcast argument for action {}", name_clone);
511 false
512 }
513 }
514 },
515 );
516
517 self.actions
518 .entry(TypeId::of::<V>())
519 .or_default()
520 .entry(name)
521 .or_default()
522 .push(handler);
523 }
524
525 pub fn add_global_action<S, T, F>(&mut self, name: S, mut handler: F)
526 where
527 S: Into<String>,
528 T: 'static + Any,
529 F: 'static + FnMut(&T, &mut MutableAppContext),
530 {
531 let name = name.into();
532 let name_clone = name.clone();
533 let handler = Box::new(move |arg: &dyn Any, app: &mut MutableAppContext| {
534 if let Some(arg) = arg.downcast_ref() {
535 handler(arg, app);
536 } else {
537 log::error!("Could not downcast argument for action {}", name_clone);
538 }
539 });
540
541 self.global_actions.entry(name).or_default().push(handler);
542 }
543
544 pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
545 self.ctx.windows.keys().cloned()
546 }
547
548 pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
549 self.ctx
550 .windows
551 .get(&window_id)
552 .and_then(|window| window.root_view.as_ref().unwrap().clone().downcast::<T>())
553 }
554
555 pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
556 self.ctx.root_view_id(window_id)
557 }
558
559 pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
560 self.ctx.focused_view_id(window_id)
561 }
562
563 pub fn render_view(&self, window_id: usize, view_id: usize) -> Result<ElementBox> {
564 self.ctx.render_view(window_id, view_id)
565 }
566
567 pub fn render_views(&self, window_id: usize) -> Result<HashMap<usize, ElementBox>> {
568 self.ctx.render_views(window_id)
569 }
570
571 pub fn update<T, F: FnOnce() -> T>(&mut self, callback: F) -> T {
572 self.pending_flushes += 1;
573 let result = callback();
574 self.flush_effects();
575 result
576 }
577
578 pub fn set_menus(&self, menus: Vec<Menu>) {
579 self.platform.set_menus(menus);
580 }
581
582 pub fn prompt_for_paths<F>(&self, options: PathPromptOptions, done_fn: F)
583 where
584 F: 'static + FnOnce(Option<Vec<PathBuf>>, &mut MutableAppContext),
585 {
586 let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
587 let foreground = self.foreground.clone();
588 self.platform().prompt_for_paths(
589 options,
590 Box::new(move |paths| {
591 foreground
592 .spawn(async move { (done_fn)(paths, &mut *app.borrow_mut()) })
593 .detach();
594 }),
595 );
596 }
597
598 pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
599 where
600 F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
601 {
602 let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
603 let foreground = self.foreground.clone();
604 self.platform().prompt_for_new_path(
605 directory,
606 Box::new(move |path| {
607 foreground
608 .spawn(async move { (done_fn)(path, &mut *app.borrow_mut()) })
609 .detach();
610 }),
611 );
612 }
613
614 pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
615 self.pending_effects
616 .push_back(Effect::ViewNotification { window_id, view_id });
617 }
618
619 pub fn dispatch_action<T: 'static + Any>(
620 &mut self,
621 window_id: usize,
622 responder_chain: Vec<usize>,
623 name: &str,
624 arg: T,
625 ) {
626 self.dispatch_action_any(window_id, &responder_chain, name, Box::new(arg).as_ref());
627 }
628
629 pub(crate) fn dispatch_action_any(
630 &mut self,
631 window_id: usize,
632 path: &[usize],
633 name: &str,
634 arg: &dyn Any,
635 ) -> bool {
636 self.pending_flushes += 1;
637 let mut halted_dispatch = false;
638
639 for view_id in path.iter().rev() {
640 if let Some(mut view) = self
641 .ctx
642 .windows
643 .get_mut(&window_id)
644 .and_then(|w| w.views.remove(view_id))
645 {
646 let type_id = view.as_any().type_id();
647
648 if let Some((name, mut handlers)) = self
649 .actions
650 .get_mut(&type_id)
651 .and_then(|h| h.remove_entry(name))
652 {
653 for handler in handlers.iter_mut().rev() {
654 let halt_dispatch = handler(view.as_mut(), arg, self, window_id, *view_id);
655 if halt_dispatch {
656 halted_dispatch = true;
657 break;
658 }
659 }
660 self.actions
661 .get_mut(&type_id)
662 .unwrap()
663 .insert(name, handlers);
664 }
665
666 self.ctx
667 .windows
668 .get_mut(&window_id)
669 .unwrap()
670 .views
671 .insert(*view_id, view);
672
673 if halted_dispatch {
674 break;
675 }
676 }
677 }
678
679 if !halted_dispatch {
680 self.dispatch_global_action_any(name, arg);
681 }
682
683 self.flush_effects();
684 halted_dispatch
685 }
686
687 pub fn dispatch_global_action<T: 'static + Any>(&mut self, name: &str, arg: T) {
688 self.dispatch_global_action_any(name, Box::new(arg).as_ref());
689 }
690
691 fn dispatch_global_action_any(&mut self, name: &str, arg: &dyn Any) {
692 if let Some((name, mut handlers)) = self.global_actions.remove_entry(name) {
693 self.pending_flushes += 1;
694 for handler in handlers.iter_mut().rev() {
695 handler(arg, self);
696 }
697 self.global_actions.insert(name, handlers);
698 self.flush_effects();
699 }
700 }
701
702 pub fn add_bindings<T: IntoIterator<Item = keymap::Binding>>(&mut self, bindings: T) {
703 self.keystroke_matcher.add_bindings(bindings);
704 }
705
706 pub fn dispatch_keystroke(
707 &mut self,
708 window_id: usize,
709 responder_chain: Vec<usize>,
710 keystroke: &Keystroke,
711 ) -> Result<bool> {
712 let mut context_chain = Vec::new();
713 let mut context = keymap::Context::default();
714 for view_id in &responder_chain {
715 if let Some(view) = self
716 .ctx
717 .windows
718 .get(&window_id)
719 .and_then(|w| w.views.get(view_id))
720 {
721 context.extend(view.keymap_context(self.as_ref()));
722 context_chain.push(context.clone());
723 } else {
724 return Err(anyhow!(
725 "View {} in responder chain does not exist",
726 view_id
727 ));
728 }
729 }
730
731 let mut pending = false;
732 for (i, ctx) in context_chain.iter().enumerate().rev() {
733 match self
734 .keystroke_matcher
735 .push_keystroke(keystroke.clone(), responder_chain[i], ctx)
736 {
737 MatchResult::None => {}
738 MatchResult::Pending => pending = true,
739 MatchResult::Action { name, arg } => {
740 if self.dispatch_action_any(
741 window_id,
742 &responder_chain[0..=i],
743 &name,
744 arg.as_ref().map(|arg| arg.as_ref()).unwrap_or(&()),
745 ) {
746 return Ok(true);
747 }
748 }
749 }
750 }
751
752 Ok(pending)
753 }
754
755 pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
756 where
757 T: Entity,
758 F: FnOnce(&mut ModelContext<T>) -> T,
759 {
760 self.pending_flushes += 1;
761 let model_id = post_inc(&mut self.next_entity_id);
762 let mut ctx = ModelContext::new(self, model_id);
763 let model = build_model(&mut ctx);
764 self.ctx.models.insert(model_id, Box::new(model));
765 self.flush_effects();
766 ModelHandle::new(model_id, &self.ctx.ref_counts)
767 }
768
769 pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
770 where
771 T: View,
772 F: FnOnce(&mut ViewContext<T>) -> T,
773 {
774 self.pending_flushes += 1;
775 let window_id = post_inc(&mut self.next_window_id);
776 self.ctx.windows.insert(window_id, Window::default());
777 self.open_platform_window(window_id);
778 let root_handle = self.add_view(window_id, build_root_view);
779 self.ctx.windows.get_mut(&window_id).unwrap().root_view = Some(root_handle.clone().into());
780 self.focus(window_id, root_handle.id());
781 self.flush_effects();
782
783 (window_id, root_handle)
784 }
785
786 fn open_platform_window(&mut self, window_id: usize) {
787 let mut window = self.platform.open_window(
788 window_id,
789 WindowOptions {
790 bounds: RectF::new(vec2f(0., 0.), vec2f(1024., 768.)),
791 title: "Zed".into(),
792 },
793 self.foreground.clone(),
794 );
795 let text_layout_cache = TextLayoutCache::new(self.platform.fonts());
796 let presenter = Rc::new(RefCell::new(Presenter::new(
797 window_id,
798 self.ctx.font_cache.clone(),
799 text_layout_cache,
800 self.assets.clone(),
801 self,
802 )));
803
804 {
805 let mut app = self.upgrade();
806 let presenter = presenter.clone();
807 window.on_event(Box::new(move |event| {
808 app.update(|ctx| {
809 if let Event::KeyDown { keystroke, .. } = &event {
810 if ctx
811 .dispatch_keystroke(
812 window_id,
813 presenter.borrow().dispatch_path(ctx.as_ref()),
814 keystroke,
815 )
816 .unwrap()
817 {
818 return;
819 }
820 }
821
822 presenter.borrow_mut().dispatch_event(event, ctx);
823 })
824 }));
825 }
826
827 {
828 let mut app = self.upgrade();
829 let presenter = presenter.clone();
830 window.on_resize(Box::new(move |window| {
831 app.update(|ctx| {
832 let scene = presenter.borrow_mut().build_scene(
833 window.size(),
834 window.scale_factor(),
835 ctx,
836 );
837 window.present_scene(scene);
838 })
839 }));
840 }
841
842 self.presenters_and_platform_windows
843 .insert(window_id, (presenter.clone(), window));
844
845 self.on_debug_elements(window_id, move |ctx| {
846 presenter.borrow().debug_elements(ctx).unwrap()
847 });
848 }
849
850 pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
851 where
852 T: View,
853 F: FnOnce(&mut ViewContext<T>) -> T,
854 {
855 self.add_option_view(window_id, |ctx| Some(build_view(ctx)))
856 .unwrap()
857 }
858
859 pub fn add_option_view<T, F>(
860 &mut self,
861 window_id: usize,
862 build_view: F,
863 ) -> Option<ViewHandle<T>>
864 where
865 T: View,
866 F: FnOnce(&mut ViewContext<T>) -> Option<T>,
867 {
868 let view_id = post_inc(&mut self.next_entity_id);
869 self.pending_flushes += 1;
870 let mut ctx = ViewContext::new(self, window_id, view_id);
871 let handle = if let Some(view) = build_view(&mut ctx) {
872 if let Some(window) = self.ctx.windows.get_mut(&window_id) {
873 window.views.insert(view_id, Box::new(view));
874 } else {
875 panic!("Window does not exist");
876 }
877 self.window_invalidations
878 .entry(window_id)
879 .or_default()
880 .updated
881 .insert(view_id);
882 Some(ViewHandle::new(window_id, view_id, &self.ctx.ref_counts))
883 } else {
884 None
885 };
886 self.flush_effects();
887 handle
888 }
889
890 fn remove_dropped_entities(&mut self) {
891 loop {
892 let (dropped_models, dropped_views, dropped_values) =
893 self.ctx.ref_counts.lock().take_dropped();
894 if dropped_models.is_empty() && dropped_views.is_empty() && dropped_values.is_empty() {
895 break;
896 }
897
898 for model_id in dropped_models {
899 self.ctx.models.remove(&model_id);
900 self.subscriptions.remove(&model_id);
901 self.model_observations.remove(&model_id);
902 }
903
904 for (window_id, view_id) in dropped_views {
905 self.subscriptions.remove(&view_id);
906 self.model_observations.remove(&view_id);
907 if let Some(window) = self.ctx.windows.get_mut(&window_id) {
908 self.window_invalidations
909 .entry(window_id)
910 .or_default()
911 .removed
912 .push(view_id);
913 window.views.remove(&view_id);
914 }
915 }
916
917 let mut values = self.ctx.values.write();
918 for key in dropped_values {
919 values.remove(&key);
920 }
921 }
922 }
923
924 fn flush_effects(&mut self) {
925 self.pending_flushes = self.pending_flushes.saturating_sub(1);
926
927 if !self.flushing_effects && self.pending_flushes == 0 {
928 self.flushing_effects = true;
929
930 loop {
931 if let Some(effect) = self.pending_effects.pop_front() {
932 match effect {
933 Effect::Event { entity_id, payload } => self.emit_event(entity_id, payload),
934 Effect::ModelNotification { model_id } => {
935 self.notify_model_observers(model_id)
936 }
937 Effect::ViewNotification { window_id, view_id } => {
938 self.notify_view_observers(window_id, view_id)
939 }
940 Effect::Focus { window_id, view_id } => {
941 self.focus(window_id, view_id);
942 }
943 }
944 } else {
945 self.remove_dropped_entities();
946 self.update_windows();
947
948 if self.pending_effects.is_empty() {
949 self.flushing_effects = false;
950 break;
951 }
952 }
953 }
954 }
955 }
956
957 fn update_windows(&mut self) {
958 let mut invalidations = HashMap::new();
959 std::mem::swap(&mut invalidations, &mut self.window_invalidations);
960
961 for (window_id, invalidation) in invalidations {
962 if let Some((presenter, mut window)) =
963 self.presenters_and_platform_windows.remove(&window_id)
964 {
965 {
966 let mut presenter = presenter.borrow_mut();
967 presenter.invalidate(invalidation, self.as_ref());
968 let scene = presenter.build_scene(window.size(), window.scale_factor(), self);
969 window.present_scene(scene);
970 }
971 self.presenters_and_platform_windows
972 .insert(window_id, (presenter, window));
973 }
974 }
975 }
976
977 fn emit_event(&mut self, entity_id: usize, payload: Box<dyn Any>) {
978 if let Some(subscriptions) = self.subscriptions.remove(&entity_id) {
979 for mut subscription in subscriptions {
980 let alive = match &mut subscription {
981 Subscription::FromModel { model_id, callback } => {
982 if let Some(mut model) = self.ctx.models.remove(model_id) {
983 callback(model.as_any_mut(), payload.as_ref(), self, *model_id);
984 self.ctx.models.insert(*model_id, model);
985 true
986 } else {
987 false
988 }
989 }
990 Subscription::FromView {
991 window_id,
992 view_id,
993 callback,
994 } => {
995 if let Some(mut view) = self
996 .ctx
997 .windows
998 .get_mut(&window_id)
999 .and_then(|window| window.views.remove(view_id))
1000 {
1001 callback(
1002 view.as_any_mut(),
1003 payload.as_ref(),
1004 self,
1005 *window_id,
1006 *view_id,
1007 );
1008 self.ctx
1009 .windows
1010 .get_mut(&window_id)
1011 .unwrap()
1012 .views
1013 .insert(*view_id, view);
1014 true
1015 } else {
1016 false
1017 }
1018 }
1019 };
1020
1021 if alive {
1022 self.subscriptions
1023 .entry(entity_id)
1024 .or_default()
1025 .push(subscription);
1026 }
1027 }
1028 }
1029 }
1030
1031 fn notify_model_observers(&mut self, observed_id: usize) {
1032 if let Some(observations) = self.model_observations.remove(&observed_id) {
1033 if self.ctx.models.contains_key(&observed_id) {
1034 for mut observation in observations {
1035 let alive = match &mut observation {
1036 ModelObservation::FromModel { model_id, callback } => {
1037 if let Some(mut model) = self.ctx.models.remove(model_id) {
1038 callback(model.as_any_mut(), observed_id, self, *model_id);
1039 self.ctx.models.insert(*model_id, model);
1040 true
1041 } else {
1042 false
1043 }
1044 }
1045 ModelObservation::FromView {
1046 window_id,
1047 view_id,
1048 callback,
1049 } => {
1050 if let Some(mut view) = self
1051 .ctx
1052 .windows
1053 .get_mut(window_id)
1054 .and_then(|w| w.views.remove(view_id))
1055 {
1056 callback(
1057 view.as_any_mut(),
1058 observed_id,
1059 self,
1060 *window_id,
1061 *view_id,
1062 );
1063 self.ctx
1064 .windows
1065 .get_mut(window_id)
1066 .unwrap()
1067 .views
1068 .insert(*view_id, view);
1069 true
1070 } else {
1071 false
1072 }
1073 }
1074 };
1075
1076 if alive {
1077 self.model_observations
1078 .entry(observed_id)
1079 .or_default()
1080 .push(observation);
1081 }
1082 }
1083 }
1084 }
1085 }
1086
1087 fn notify_view_observers(&mut self, window_id: usize, view_id: usize) {
1088 self.window_invalidations
1089 .entry(window_id)
1090 .or_default()
1091 .updated
1092 .insert(view_id);
1093
1094 if let Some(observations) = self.view_observations.remove(&view_id) {
1095 if self
1096 .ctx
1097 .windows
1098 .get(&window_id)
1099 .map_or(false, |w| w.views.contains_key(&view_id))
1100 {
1101 for mut observation in observations {
1102 let alive = if let Some(mut view) = self
1103 .ctx
1104 .windows
1105 .get_mut(&observation.window_id)
1106 .and_then(|w| w.views.remove(&observation.view_id))
1107 {
1108 (observation.callback)(
1109 view.as_any_mut(),
1110 view_id,
1111 window_id,
1112 self,
1113 observation.window_id,
1114 observation.view_id,
1115 );
1116 self.ctx
1117 .windows
1118 .get_mut(&observation.window_id)
1119 .unwrap()
1120 .views
1121 .insert(observation.view_id, view);
1122 true
1123 } else {
1124 false
1125 };
1126
1127 if alive {
1128 self.view_observations
1129 .entry(view_id)
1130 .or_default()
1131 .push(observation);
1132 }
1133 }
1134 }
1135 }
1136 }
1137
1138 fn focus(&mut self, window_id: usize, focused_id: usize) {
1139 if self
1140 .ctx
1141 .windows
1142 .get(&window_id)
1143 .and_then(|w| w.focused_view)
1144 .map_or(false, |cur_focused| cur_focused == focused_id)
1145 {
1146 return;
1147 }
1148
1149 self.pending_flushes += 1;
1150
1151 if let Some((blurred_id, mut blurred)) =
1152 self.ctx.windows.get_mut(&window_id).and_then(|w| {
1153 let blurred_view = w.focused_view;
1154 w.focused_view = Some(focused_id);
1155 blurred_view.and_then(|id| w.views.remove(&id).map(|view| (id, view)))
1156 })
1157 {
1158 blurred.on_blur(self, window_id, blurred_id);
1159 self.ctx
1160 .windows
1161 .get_mut(&window_id)
1162 .unwrap()
1163 .views
1164 .insert(blurred_id, blurred);
1165 }
1166
1167 if let Some(mut focused) = self
1168 .ctx
1169 .windows
1170 .get_mut(&window_id)
1171 .and_then(|w| w.views.remove(&focused_id))
1172 {
1173 focused.on_focus(self, window_id, focused_id);
1174 self.ctx
1175 .windows
1176 .get_mut(&window_id)
1177 .unwrap()
1178 .views
1179 .insert(focused_id, focused);
1180 }
1181
1182 self.flush_effects();
1183 }
1184
1185 fn spawn<F, T>(&mut self, future: F) -> EntityTask<T>
1186 where
1187 F: 'static + Future,
1188 T: 'static,
1189 {
1190 let task_id = post_inc(&mut self.next_task_id);
1191 let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
1192 let task = {
1193 let app = app.clone();
1194 self.foreground.spawn(async move {
1195 let output = future.await;
1196 *app.borrow_mut()
1197 .handle_future_output(task_id, Box::new(output))
1198 .downcast::<T>()
1199 .unwrap()
1200 })
1201 };
1202 EntityTask::new(
1203 task_id,
1204 task,
1205 TaskHandlerMap::Future(self.future_handlers.clone()),
1206 )
1207 }
1208
1209 fn spawn_stream<F, T>(&mut self, mut stream: F) -> EntityTask<T>
1210 where
1211 F: 'static + Stream + Unpin,
1212 T: 'static,
1213 {
1214 let task_id = post_inc(&mut self.next_task_id);
1215 let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
1216 let task = self.foreground.spawn(async move {
1217 loop {
1218 match stream.next().await {
1219 Some(item) => {
1220 let mut app = app.borrow_mut();
1221 if app.handle_stream_item(task_id, Box::new(item)) {
1222 break;
1223 }
1224 }
1225 None => {
1226 break;
1227 }
1228 }
1229 }
1230
1231 *app.borrow_mut()
1232 .stream_completed(task_id)
1233 .downcast::<T>()
1234 .unwrap()
1235 });
1236
1237 EntityTask::new(
1238 task_id,
1239 task,
1240 TaskHandlerMap::Stream(self.stream_handlers.clone()),
1241 )
1242 }
1243
1244 fn handle_future_output(&mut self, task_id: usize, output: Box<dyn Any>) -> Box<dyn Any> {
1245 self.pending_flushes += 1;
1246 let future_callback = self.future_handlers.borrow_mut().remove(&task_id).unwrap();
1247 let result = future_callback(output, self);
1248 self.flush_effects();
1249 result
1250 }
1251
1252 fn handle_stream_item(&mut self, task_id: usize, output: Box<dyn Any>) -> bool {
1253 self.pending_flushes += 1;
1254
1255 let mut handler = self.stream_handlers.borrow_mut().remove(&task_id).unwrap();
1256 let halt = (handler.item_callback)(output, self);
1257 self.stream_handlers.borrow_mut().insert(task_id, handler);
1258
1259 self.flush_effects();
1260 halt
1261 }
1262
1263 fn stream_completed(&mut self, task_id: usize) -> Box<dyn Any> {
1264 self.pending_flushes += 1;
1265
1266 let handler = self.stream_handlers.borrow_mut().remove(&task_id).unwrap();
1267 let result = (handler.done_callback)(self);
1268
1269 self.flush_effects();
1270 result
1271 }
1272
1273 pub fn write_to_clipboard(&self, item: ClipboardItem) {
1274 self.platform.write_to_clipboard(item);
1275 }
1276
1277 pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
1278 self.platform.read_from_clipboard()
1279 }
1280}
1281
1282impl ReadModel for MutableAppContext {
1283 fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
1284 if let Some(model) = self.ctx.models.get(&handle.model_id) {
1285 model
1286 .as_any()
1287 .downcast_ref()
1288 .expect("Downcast is type safe")
1289 } else {
1290 panic!("Circular model reference");
1291 }
1292 }
1293}
1294
1295impl UpdateModel for MutableAppContext {
1296 fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
1297 where
1298 T: Entity,
1299 F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
1300 {
1301 if let Some(mut model) = self.ctx.models.remove(&handle.model_id) {
1302 self.pending_flushes += 1;
1303 let mut ctx = ModelContext::new(self, handle.model_id);
1304 let result = update(
1305 model
1306 .as_any_mut()
1307 .downcast_mut()
1308 .expect("Downcast is type safe"),
1309 &mut ctx,
1310 );
1311 self.ctx.models.insert(handle.model_id, model);
1312 self.flush_effects();
1313 result
1314 } else {
1315 panic!("Circular model update");
1316 }
1317 }
1318}
1319
1320impl ReadView for MutableAppContext {
1321 fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
1322 if let Some(window) = self.ctx.windows.get(&handle.window_id) {
1323 if let Some(view) = window.views.get(&handle.view_id) {
1324 view.as_any().downcast_ref().expect("Downcast is type safe")
1325 } else {
1326 panic!("Circular view reference");
1327 }
1328 } else {
1329 panic!("Window does not exist");
1330 }
1331 }
1332}
1333
1334impl UpdateView for MutableAppContext {
1335 fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
1336 where
1337 T: View,
1338 F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
1339 {
1340 self.pending_flushes += 1;
1341 let mut view = if let Some(window) = self.ctx.windows.get_mut(&handle.window_id) {
1342 if let Some(view) = window.views.remove(&handle.view_id) {
1343 view
1344 } else {
1345 panic!("Circular view update");
1346 }
1347 } else {
1348 panic!("Window does not exist");
1349 };
1350
1351 let mut ctx = ViewContext::new(self, handle.window_id, handle.view_id);
1352 let result = update(
1353 view.as_any_mut()
1354 .downcast_mut()
1355 .expect("Downcast is type safe"),
1356 &mut ctx,
1357 );
1358 self.ctx
1359 .windows
1360 .get_mut(&handle.window_id)
1361 .unwrap()
1362 .views
1363 .insert(handle.view_id, view);
1364 self.flush_effects();
1365 result
1366 }
1367}
1368
1369impl AsRef<AppContext> for MutableAppContext {
1370 fn as_ref(&self) -> &AppContext {
1371 &self.ctx
1372 }
1373}
1374
1375pub struct AppContext {
1376 models: HashMap<usize, Box<dyn AnyModel>>,
1377 windows: HashMap<usize, Window>,
1378 values: RwLock<HashMap<(TypeId, usize), Box<dyn Any>>>,
1379 background: Arc<executor::Background>,
1380 ref_counts: Arc<Mutex<RefCounts>>,
1381 thread_pool: scoped_pool::Pool,
1382 font_cache: Arc<FontCache>,
1383}
1384
1385impl AppContext {
1386 pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
1387 self.windows
1388 .get(&window_id)
1389 .and_then(|window| window.root_view.as_ref().map(|v| v.id()))
1390 }
1391
1392 pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
1393 self.windows
1394 .get(&window_id)
1395 .and_then(|window| window.focused_view)
1396 }
1397
1398 pub fn render_view(&self, window_id: usize, view_id: usize) -> Result<ElementBox> {
1399 self.windows
1400 .get(&window_id)
1401 .and_then(|w| w.views.get(&view_id))
1402 .map(|v| v.render(self))
1403 .ok_or(anyhow!("view not found"))
1404 }
1405
1406 pub fn render_views(&self, window_id: usize) -> Result<HashMap<usize, ElementBox>> {
1407 self.windows
1408 .get(&window_id)
1409 .map(|w| {
1410 w.views
1411 .iter()
1412 .map(|(id, view)| (*id, view.render(self)))
1413 .collect::<HashMap<_, ElementBox>>()
1414 })
1415 .ok_or(anyhow!("window not found"))
1416 }
1417
1418 pub fn background_executor(&self) -> &Arc<executor::Background> {
1419 &self.background
1420 }
1421
1422 pub fn font_cache(&self) -> &FontCache {
1423 &self.font_cache
1424 }
1425
1426 pub fn thread_pool(&self) -> &scoped_pool::Pool {
1427 &self.thread_pool
1428 }
1429
1430 pub fn value<Tag: 'static, T: 'static + Default>(&self, id: usize) -> ValueHandle<T> {
1431 let key = (TypeId::of::<Tag>(), id);
1432 let mut values = self.values.write();
1433 values.entry(key).or_insert_with(|| Box::new(T::default()));
1434 ValueHandle::new(TypeId::of::<Tag>(), id, &self.ref_counts)
1435 }
1436}
1437
1438impl ReadModel for AppContext {
1439 fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
1440 if let Some(model) = self.models.get(&handle.model_id) {
1441 model
1442 .as_any()
1443 .downcast_ref()
1444 .expect("downcast should be type safe")
1445 } else {
1446 panic!("circular model reference");
1447 }
1448 }
1449}
1450
1451impl ReadView for AppContext {
1452 fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
1453 if let Some(window) = self.windows.get(&handle.window_id) {
1454 if let Some(view) = window.views.get(&handle.view_id) {
1455 view.as_any()
1456 .downcast_ref()
1457 .expect("downcast should be type safe")
1458 } else {
1459 panic!("circular view reference");
1460 }
1461 } else {
1462 panic!("window does not exist");
1463 }
1464 }
1465}
1466
1467#[derive(Default)]
1468struct Window {
1469 views: HashMap<usize, Box<dyn AnyView>>,
1470 root_view: Option<AnyViewHandle>,
1471 focused_view: Option<usize>,
1472}
1473
1474#[derive(Default, Clone)]
1475pub struct WindowInvalidation {
1476 pub updated: HashSet<usize>,
1477 pub removed: Vec<usize>,
1478}
1479
1480pub enum Effect {
1481 Event {
1482 entity_id: usize,
1483 payload: Box<dyn Any>,
1484 },
1485 ModelNotification {
1486 model_id: usize,
1487 },
1488 ViewNotification {
1489 window_id: usize,
1490 view_id: usize,
1491 },
1492 Focus {
1493 window_id: usize,
1494 view_id: usize,
1495 },
1496}
1497
1498pub trait AnyModel: Send + Sync {
1499 fn as_any(&self) -> &dyn Any;
1500 fn as_any_mut(&mut self) -> &mut dyn Any;
1501}
1502
1503impl<T> AnyModel for T
1504where
1505 T: Entity,
1506{
1507 fn as_any(&self) -> &dyn Any {
1508 self
1509 }
1510
1511 fn as_any_mut(&mut self) -> &mut dyn Any {
1512 self
1513 }
1514}
1515
1516pub trait AnyView: Send + Sync {
1517 fn as_any(&self) -> &dyn Any;
1518 fn as_any_mut(&mut self) -> &mut dyn Any;
1519 fn ui_name(&self) -> &'static str;
1520 fn render<'a>(&self, app: &AppContext) -> ElementBox;
1521 fn on_focus(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize);
1522 fn on_blur(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize);
1523 fn keymap_context(&self, app: &AppContext) -> keymap::Context;
1524}
1525
1526impl<T> AnyView for T
1527where
1528 T: View,
1529{
1530 fn as_any(&self) -> &dyn Any {
1531 self
1532 }
1533
1534 fn as_any_mut(&mut self) -> &mut dyn Any {
1535 self
1536 }
1537
1538 fn ui_name(&self) -> &'static str {
1539 T::ui_name()
1540 }
1541
1542 fn render<'a>(&self, app: &AppContext) -> ElementBox {
1543 View::render(self, app)
1544 }
1545
1546 fn on_focus(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize) {
1547 let mut ctx = ViewContext::new(app, window_id, view_id);
1548 View::on_focus(self, &mut ctx);
1549 }
1550
1551 fn on_blur(&mut self, app: &mut MutableAppContext, window_id: usize, view_id: usize) {
1552 let mut ctx = ViewContext::new(app, window_id, view_id);
1553 View::on_blur(self, &mut ctx);
1554 }
1555
1556 fn keymap_context(&self, app: &AppContext) -> keymap::Context {
1557 View::keymap_context(self, app)
1558 }
1559}
1560
1561pub struct ModelContext<'a, T: ?Sized> {
1562 app: &'a mut MutableAppContext,
1563 model_id: usize,
1564 model_type: PhantomData<T>,
1565 halt_stream: bool,
1566}
1567
1568impl<'a, T: Entity> ModelContext<'a, T> {
1569 fn new(app: &'a mut MutableAppContext, model_id: usize) -> Self {
1570 Self {
1571 app,
1572 model_id,
1573 model_type: PhantomData,
1574 halt_stream: false,
1575 }
1576 }
1577
1578 pub fn background_executor(&self) -> &Arc<executor::Background> {
1579 &self.app.ctx.background
1580 }
1581
1582 pub fn thread_pool(&self) -> &scoped_pool::Pool {
1583 &self.app.ctx.thread_pool
1584 }
1585
1586 pub fn halt_stream(&mut self) {
1587 self.halt_stream = true;
1588 }
1589
1590 pub fn model_id(&self) -> usize {
1591 self.model_id
1592 }
1593
1594 pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
1595 where
1596 S: Entity,
1597 F: FnOnce(&mut ModelContext<S>) -> S,
1598 {
1599 self.app.add_model(build_model)
1600 }
1601
1602 pub fn subscribe<S: Entity, F>(&mut self, handle: &ModelHandle<S>, mut callback: F)
1603 where
1604 S::Event: 'static,
1605 F: 'static + FnMut(&mut T, &S::Event, &mut ModelContext<T>),
1606 {
1607 self.app
1608 .subscriptions
1609 .entry(handle.model_id)
1610 .or_default()
1611 .push(Subscription::FromModel {
1612 model_id: self.model_id,
1613 callback: Box::new(move |model, payload, app, model_id| {
1614 let model = model.downcast_mut().expect("downcast is type safe");
1615 let payload = payload.downcast_ref().expect("downcast is type safe");
1616 let mut ctx = ModelContext::new(app, model_id);
1617 callback(model, payload, &mut ctx);
1618 }),
1619 });
1620 }
1621
1622 pub fn emit(&mut self, payload: T::Event) {
1623 self.app.pending_effects.push_back(Effect::Event {
1624 entity_id: self.model_id,
1625 payload: Box::new(payload),
1626 });
1627 }
1628
1629 pub fn observe<S, F>(&mut self, handle: &ModelHandle<S>, mut callback: F)
1630 where
1631 S: Entity,
1632 F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ModelContext<T>),
1633 {
1634 self.app
1635 .model_observations
1636 .entry(handle.model_id)
1637 .or_default()
1638 .push(ModelObservation::FromModel {
1639 model_id: self.model_id,
1640 callback: Box::new(move |model, observed_id, app, model_id| {
1641 let model = model.downcast_mut().expect("downcast is type safe");
1642 let observed = ModelHandle::new(observed_id, &app.ctx.ref_counts);
1643 let mut ctx = ModelContext::new(app, model_id);
1644 callback(model, observed, &mut ctx);
1645 }),
1646 });
1647 }
1648
1649 pub fn notify(&mut self) {
1650 self.app
1651 .pending_effects
1652 .push_back(Effect::ModelNotification {
1653 model_id: self.model_id,
1654 });
1655 }
1656
1657 fn handle(&self) -> ModelHandle<T> {
1658 ModelHandle::new(self.model_id, &self.app.ctx.ref_counts)
1659 }
1660
1661 pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> EntityTask<U>
1662 where
1663 S: 'static + Future,
1664 F: 'static + FnOnce(&mut T, S::Output, &mut ModelContext<T>) -> U,
1665 U: 'static,
1666 {
1667 let handle = self.handle();
1668 let task = self.app.spawn::<S, U>(future);
1669
1670 self.app.future_handlers.borrow_mut().insert(
1671 task.id,
1672 Box::new(move |output, ctx| {
1673 let output = *output.downcast().unwrap();
1674 handle.update(ctx, |model, ctx| Box::new(callback(model, output, ctx)))
1675 }),
1676 );
1677
1678 task
1679 }
1680
1681 pub fn spawn_stream<S, F, G, U>(
1682 &mut self,
1683 stream: S,
1684 mut item_callback: F,
1685 done_callback: G,
1686 ) -> EntityTask<U>
1687 where
1688 S: 'static + Stream + Unpin,
1689 F: 'static + FnMut(&mut T, S::Item, &mut ModelContext<T>),
1690 G: 'static + FnOnce(&mut T, &mut ModelContext<T>) -> U,
1691 U: 'static + Any,
1692 {
1693 let handle = self.handle();
1694 let task = self.app.spawn_stream(stream);
1695
1696 self.app.stream_handlers.borrow_mut().insert(
1697 task.id,
1698 StreamHandler {
1699 item_callback: {
1700 let handle = handle.clone();
1701 Box::new(move |output, app| {
1702 let output = *output.downcast().unwrap();
1703 handle.update(app, |model, ctx| {
1704 item_callback(model, output, ctx);
1705 ctx.halt_stream
1706 })
1707 })
1708 },
1709 done_callback: Box::new(move |app| {
1710 handle.update(app, |model, ctx| Box::new(done_callback(model, ctx)))
1711 }),
1712 },
1713 );
1714
1715 task
1716 }
1717}
1718
1719impl<M> AsRef<AppContext> for ModelContext<'_, M> {
1720 fn as_ref(&self) -> &AppContext {
1721 &self.app.ctx
1722 }
1723}
1724
1725impl<M> AsMut<MutableAppContext> for ModelContext<'_, M> {
1726 fn as_mut(&mut self) -> &mut MutableAppContext {
1727 self.app
1728 }
1729}
1730
1731impl<M> ReadModel for ModelContext<'_, M> {
1732 fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
1733 self.app.read_model(handle)
1734 }
1735}
1736
1737impl<M> UpdateModel for ModelContext<'_, M> {
1738 fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
1739 where
1740 T: Entity,
1741 F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
1742 {
1743 self.app.update_model(handle, update)
1744 }
1745}
1746
1747pub struct ViewContext<'a, T: ?Sized> {
1748 app: &'a mut MutableAppContext,
1749 window_id: usize,
1750 view_id: usize,
1751 view_type: PhantomData<T>,
1752 halt_action_dispatch: bool,
1753 halt_stream: bool,
1754}
1755
1756impl<'a, T: View> ViewContext<'a, T> {
1757 fn new(app: &'a mut MutableAppContext, window_id: usize, view_id: usize) -> Self {
1758 Self {
1759 app,
1760 window_id,
1761 view_id,
1762 view_type: PhantomData,
1763 halt_action_dispatch: true,
1764 halt_stream: false,
1765 }
1766 }
1767
1768 pub fn handle(&self) -> ViewHandle<T> {
1769 ViewHandle::new(self.window_id, self.view_id, &self.app.ctx.ref_counts)
1770 }
1771
1772 pub fn window_id(&self) -> usize {
1773 self.window_id
1774 }
1775
1776 pub fn view_id(&self) -> usize {
1777 self.view_id
1778 }
1779
1780 pub fn foreground(&self) -> &Rc<executor::Foreground> {
1781 self.app.foreground_executor()
1782 }
1783
1784 pub fn background_executor(&self) -> &Arc<executor::Background> {
1785 &self.app.ctx.background
1786 }
1787
1788 pub fn prompt_for_paths<F>(&self, options: PathPromptOptions, done_fn: F)
1789 where
1790 F: 'static + FnOnce(Option<Vec<PathBuf>>, &mut MutableAppContext),
1791 {
1792 self.app.prompt_for_paths(options, done_fn)
1793 }
1794
1795 pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
1796 where
1797 F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
1798 {
1799 self.app.prompt_for_new_path(directory, done_fn)
1800 }
1801
1802 pub fn debug_elements(&self) -> crate::json::Value {
1803 self.app.debug_elements(self.window_id).unwrap()
1804 }
1805
1806 pub fn focus<S>(&mut self, handle: S)
1807 where
1808 S: Into<AnyViewHandle>,
1809 {
1810 let handle = handle.into();
1811 self.app.pending_effects.push_back(Effect::Focus {
1812 window_id: handle.window_id,
1813 view_id: handle.view_id,
1814 });
1815 }
1816
1817 pub fn focus_self(&mut self) {
1818 self.app.pending_effects.push_back(Effect::Focus {
1819 window_id: self.window_id,
1820 view_id: self.view_id,
1821 });
1822 }
1823
1824 pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
1825 where
1826 S: Entity,
1827 F: FnOnce(&mut ModelContext<S>) -> S,
1828 {
1829 self.app.add_model(build_model)
1830 }
1831
1832 pub fn add_view<S, F>(&mut self, build_view: F) -> ViewHandle<S>
1833 where
1834 S: View,
1835 F: FnOnce(&mut ViewContext<S>) -> S,
1836 {
1837 self.app.add_view(self.window_id, build_view)
1838 }
1839
1840 pub fn add_option_view<S, F>(&mut self, build_view: F) -> Option<ViewHandle<S>>
1841 where
1842 S: View,
1843 F: FnOnce(&mut ViewContext<S>) -> Option<S>,
1844 {
1845 self.app.add_option_view(self.window_id, build_view)
1846 }
1847
1848 pub fn subscribe_to_model<E, F>(&mut self, handle: &ModelHandle<E>, mut callback: F)
1849 where
1850 E: Entity,
1851 E::Event: 'static,
1852 F: 'static + FnMut(&mut T, ModelHandle<E>, &E::Event, &mut ViewContext<T>),
1853 {
1854 let emitter_handle = handle.downgrade();
1855 self.subscribe(handle, move |model, payload, ctx| {
1856 if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) {
1857 callback(model, emitter_handle, payload, ctx);
1858 }
1859 });
1860 }
1861
1862 pub fn subscribe_to_view<V, F>(&mut self, handle: &ViewHandle<V>, mut callback: F)
1863 where
1864 V: View,
1865 V::Event: 'static,
1866 F: 'static + FnMut(&mut T, ViewHandle<V>, &V::Event, &mut ViewContext<T>),
1867 {
1868 let emitter_handle = handle.downgrade();
1869 self.subscribe(handle, move |view, payload, ctx| {
1870 if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) {
1871 callback(view, emitter_handle, payload, ctx);
1872 }
1873 });
1874 }
1875
1876 pub fn subscribe<E, F>(&mut self, handle: &impl Handle<E>, mut callback: F)
1877 where
1878 E: Entity,
1879 E::Event: 'static,
1880 F: 'static + FnMut(&mut T, &E::Event, &mut ViewContext<T>),
1881 {
1882 self.app
1883 .subscriptions
1884 .entry(handle.id())
1885 .or_default()
1886 .push(Subscription::FromView {
1887 window_id: self.window_id,
1888 view_id: self.view_id,
1889 callback: Box::new(move |entity, payload, app, window_id, view_id| {
1890 let entity = entity.downcast_mut().expect("downcast is type safe");
1891 let payload = payload.downcast_ref().expect("downcast is type safe");
1892 let mut ctx = ViewContext::new(app, window_id, view_id);
1893 callback(entity, payload, &mut ctx);
1894 }),
1895 });
1896 }
1897
1898 pub fn emit(&mut self, payload: T::Event) {
1899 self.app.pending_effects.push_back(Effect::Event {
1900 entity_id: self.view_id,
1901 payload: Box::new(payload),
1902 });
1903 }
1904
1905 pub fn observe_model<S, F>(&mut self, handle: &ModelHandle<S>, mut callback: F)
1906 where
1907 S: Entity,
1908 F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ViewContext<T>),
1909 {
1910 self.app
1911 .model_observations
1912 .entry(handle.id())
1913 .or_default()
1914 .push(ModelObservation::FromView {
1915 window_id: self.window_id,
1916 view_id: self.view_id,
1917 callback: Box::new(move |view, observed_id, app, window_id, view_id| {
1918 let view = view.downcast_mut().expect("downcast is type safe");
1919 let observed = ModelHandle::new(observed_id, &app.ctx.ref_counts);
1920 let mut ctx = ViewContext::new(app, window_id, view_id);
1921 callback(view, observed, &mut ctx);
1922 }),
1923 });
1924 }
1925
1926 pub fn observe_view<S, F>(&mut self, handle: &ViewHandle<S>, mut callback: F)
1927 where
1928 S: View,
1929 F: 'static + FnMut(&mut T, ViewHandle<S>, &mut ViewContext<T>),
1930 {
1931 self.app
1932 .view_observations
1933 .entry(handle.id())
1934 .or_default()
1935 .push(ViewObservation {
1936 window_id: self.window_id,
1937 view_id: self.view_id,
1938 callback: Box::new(
1939 move |view,
1940 observed_view_id,
1941 observed_window_id,
1942 app,
1943 observing_window_id,
1944 observing_view_id| {
1945 let view = view.downcast_mut().expect("downcast is type safe");
1946 let observed_handle = ViewHandle::new(
1947 observed_view_id,
1948 observed_window_id,
1949 &app.ctx.ref_counts,
1950 );
1951 let mut ctx = ViewContext::new(app, observing_window_id, observing_view_id);
1952 callback(view, observed_handle, &mut ctx);
1953 },
1954 ),
1955 });
1956 }
1957
1958 pub fn notify(&mut self) {
1959 self.app.notify_view(self.window_id, self.view_id);
1960 }
1961
1962 pub fn propagate_action(&mut self) {
1963 self.halt_action_dispatch = false;
1964 }
1965
1966 pub fn halt_stream(&mut self) {
1967 self.halt_stream = true;
1968 }
1969
1970 pub fn spawn<S, F, U>(&mut self, future: S, callback: F) -> EntityTask<U>
1971 where
1972 S: 'static + Future,
1973 F: 'static + FnOnce(&mut T, S::Output, &mut ViewContext<T>) -> U,
1974 U: 'static,
1975 {
1976 let handle = self.handle();
1977 let task = self.app.spawn(future);
1978
1979 self.app.future_handlers.borrow_mut().insert(
1980 task.id,
1981 Box::new(move |output, app| {
1982 let output = *output.downcast().unwrap();
1983 handle.update(app, |view, ctx| Box::new(callback(view, output, ctx)))
1984 }),
1985 );
1986
1987 task
1988 }
1989
1990 pub fn spawn_stream<S, F, G, U>(
1991 &mut self,
1992 stream: S,
1993 mut item_callback: F,
1994 done_callback: G,
1995 ) -> EntityTask<U>
1996 where
1997 S: 'static + Stream + Unpin,
1998 F: 'static + FnMut(&mut T, S::Item, &mut ViewContext<T>),
1999 G: 'static + FnOnce(&mut T, &mut ViewContext<T>) -> U,
2000 U: 'static + Any,
2001 {
2002 let handle = self.handle();
2003 let task = self.app.spawn_stream(stream);
2004 self.app.stream_handlers.borrow_mut().insert(
2005 task.id,
2006 StreamHandler {
2007 item_callback: {
2008 let handle = handle.clone();
2009 Box::new(move |output, app| {
2010 let output = *output.downcast().unwrap();
2011 handle.update(app, |view, ctx| {
2012 item_callback(view, output, ctx);
2013 ctx.halt_stream
2014 })
2015 })
2016 },
2017 done_callback: Box::new(move |app| {
2018 handle.update(app, |view, ctx| Box::new(done_callback(view, ctx)))
2019 }),
2020 },
2021 );
2022 task
2023 }
2024}
2025
2026impl AsRef<AppContext> for &AppContext {
2027 fn as_ref(&self) -> &AppContext {
2028 self
2029 }
2030}
2031
2032impl<M> AsRef<AppContext> for ViewContext<'_, M> {
2033 fn as_ref(&self) -> &AppContext {
2034 &self.app.ctx
2035 }
2036}
2037
2038impl<M> AsMut<MutableAppContext> for ViewContext<'_, M> {
2039 fn as_mut(&mut self) -> &mut MutableAppContext {
2040 self.app
2041 }
2042}
2043
2044impl<V> ReadModel for ViewContext<'_, V> {
2045 fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2046 self.app.read_model(handle)
2047 }
2048}
2049
2050impl<V: View> UpdateModel for ViewContext<'_, V> {
2051 fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
2052 where
2053 T: Entity,
2054 F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
2055 {
2056 self.app.update_model(handle, update)
2057 }
2058}
2059
2060impl<V: View> ReadView for ViewContext<'_, V> {
2061 fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2062 self.app.read_view(handle)
2063 }
2064}
2065
2066impl<V: View> UpdateView for ViewContext<'_, V> {
2067 fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
2068 where
2069 T: View,
2070 F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
2071 {
2072 self.app.update_view(handle, update)
2073 }
2074}
2075
2076pub trait Handle<T> {
2077 fn id(&self) -> usize;
2078 fn location(&self) -> EntityLocation;
2079}
2080
2081#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
2082pub enum EntityLocation {
2083 Model(usize),
2084 View(usize, usize),
2085}
2086
2087pub struct ModelHandle<T> {
2088 model_id: usize,
2089 model_type: PhantomData<T>,
2090 ref_counts: Weak<Mutex<RefCounts>>,
2091}
2092
2093impl<T: Entity> ModelHandle<T> {
2094 fn new(model_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
2095 ref_counts.lock().inc_entity(model_id);
2096 Self {
2097 model_id,
2098 model_type: PhantomData,
2099 ref_counts: Arc::downgrade(ref_counts),
2100 }
2101 }
2102
2103 pub fn downgrade(&self) -> WeakModelHandle<T> {
2104 WeakModelHandle::new(self.model_id)
2105 }
2106
2107 pub fn id(&self) -> usize {
2108 self.model_id
2109 }
2110
2111 pub fn read<'a, A: ReadModel>(&self, app: &'a A) -> &'a T {
2112 app.read_model(self)
2113 }
2114
2115 pub fn update<A, F, S>(&self, app: &mut A, update: F) -> S
2116 where
2117 A: UpdateModel,
2118 F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
2119 {
2120 app.update_model(self, update)
2121 }
2122
2123 pub fn condition(
2124 &self,
2125 ctx: &TestAppContext,
2126 predicate: impl FnMut(&T, &AppContext) -> bool,
2127 ) -> impl Future<Output = ()> {
2128 self.condition_with_duration(Duration::from_millis(100), ctx, predicate)
2129 }
2130
2131 pub fn condition_with_duration(
2132 &self,
2133 duration: Duration,
2134 ctx: &TestAppContext,
2135 mut predicate: impl FnMut(&T, &AppContext) -> bool,
2136 ) -> impl Future<Output = ()> {
2137 let (tx, mut rx) = mpsc::channel(1024);
2138
2139 let mut ctx = ctx.0.borrow_mut();
2140 self.update(&mut *ctx, |_, ctx| {
2141 ctx.observe(self, {
2142 let mut tx = tx.clone();
2143 move |_, _, _| {
2144 tx.blocking_send(()).ok();
2145 }
2146 });
2147 ctx.subscribe(self, {
2148 let mut tx = tx.clone();
2149 move |_, _, _| {
2150 tx.blocking_send(()).ok();
2151 }
2152 })
2153 });
2154
2155 let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap();
2156 let handle = self.downgrade();
2157
2158 async move {
2159 timeout(duration, async move {
2160 loop {
2161 {
2162 let ctx = ctx.borrow();
2163 let ctx = ctx.as_ref();
2164 if predicate(
2165 handle
2166 .upgrade(ctx)
2167 .expect("model dropped with pending condition")
2168 .read(ctx),
2169 ctx,
2170 ) {
2171 break;
2172 }
2173 }
2174
2175 rx.recv()
2176 .await
2177 .expect("model dropped with pending condition");
2178 }
2179 })
2180 .await
2181 .expect("condition timed out");
2182 }
2183 }
2184}
2185
2186impl<T> Clone for ModelHandle<T> {
2187 fn clone(&self) -> Self {
2188 if let Some(ref_counts) = self.ref_counts.upgrade() {
2189 ref_counts.lock().inc_entity(self.model_id);
2190 }
2191
2192 Self {
2193 model_id: self.model_id,
2194 model_type: PhantomData,
2195 ref_counts: self.ref_counts.clone(),
2196 }
2197 }
2198}
2199
2200impl<T> PartialEq for ModelHandle<T> {
2201 fn eq(&self, other: &Self) -> bool {
2202 self.model_id == other.model_id
2203 }
2204}
2205
2206impl<T> Eq for ModelHandle<T> {}
2207
2208impl<T> Hash for ModelHandle<T> {
2209 fn hash<H: Hasher>(&self, state: &mut H) {
2210 self.model_id.hash(state);
2211 }
2212}
2213
2214impl<T> std::borrow::Borrow<usize> for ModelHandle<T> {
2215 fn borrow(&self) -> &usize {
2216 &self.model_id
2217 }
2218}
2219
2220impl<T> Debug for ModelHandle<T> {
2221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2222 f.debug_tuple(&format!("ModelHandle<{}>", type_name::<T>()))
2223 .field(&self.model_id)
2224 .finish()
2225 }
2226}
2227
2228unsafe impl<T> Send for ModelHandle<T> {}
2229unsafe impl<T> Sync for ModelHandle<T> {}
2230
2231impl<T> Drop for ModelHandle<T> {
2232 fn drop(&mut self) {
2233 if let Some(ref_counts) = self.ref_counts.upgrade() {
2234 ref_counts.lock().dec_model(self.model_id);
2235 }
2236 }
2237}
2238
2239impl<T> Handle<T> for ModelHandle<T> {
2240 fn id(&self) -> usize {
2241 self.model_id
2242 }
2243
2244 fn location(&self) -> EntityLocation {
2245 EntityLocation::Model(self.model_id)
2246 }
2247}
2248
2249pub struct WeakModelHandle<T> {
2250 model_id: usize,
2251 model_type: PhantomData<T>,
2252}
2253
2254impl<T: Entity> WeakModelHandle<T> {
2255 fn new(model_id: usize) -> Self {
2256 Self {
2257 model_id,
2258 model_type: PhantomData,
2259 }
2260 }
2261
2262 pub fn upgrade(&self, app: &AppContext) -> Option<ModelHandle<T>> {
2263 if app.models.contains_key(&self.model_id) {
2264 Some(ModelHandle::new(self.model_id, &app.ref_counts))
2265 } else {
2266 None
2267 }
2268 }
2269}
2270
2271impl<T> Clone for WeakModelHandle<T> {
2272 fn clone(&self) -> Self {
2273 Self {
2274 model_id: self.model_id,
2275 model_type: PhantomData,
2276 }
2277 }
2278}
2279
2280pub struct ViewHandle<T> {
2281 window_id: usize,
2282 view_id: usize,
2283 view_type: PhantomData<T>,
2284 ref_counts: Weak<Mutex<RefCounts>>,
2285}
2286
2287impl<T: View> ViewHandle<T> {
2288 fn new(window_id: usize, view_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
2289 ref_counts.lock().inc_entity(view_id);
2290 Self {
2291 window_id,
2292 view_id,
2293 view_type: PhantomData,
2294 ref_counts: Arc::downgrade(ref_counts),
2295 }
2296 }
2297
2298 pub fn downgrade(&self) -> WeakViewHandle<T> {
2299 WeakViewHandle::new(self.window_id, self.view_id)
2300 }
2301
2302 pub fn window_id(&self) -> usize {
2303 self.window_id
2304 }
2305
2306 pub fn id(&self) -> usize {
2307 self.view_id
2308 }
2309
2310 pub fn read<'a, A: ReadView>(&self, app: &'a A) -> &'a T {
2311 app.read_view(self)
2312 }
2313
2314 pub fn update<A, F, S>(&self, app: &mut A, update: F) -> S
2315 where
2316 A: UpdateView,
2317 F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
2318 {
2319 app.update_view(self, update)
2320 }
2321
2322 pub fn is_focused(&self, app: &AppContext) -> bool {
2323 app.focused_view_id(self.window_id)
2324 .map_or(false, |focused_id| focused_id == self.view_id)
2325 }
2326
2327 pub fn condition(
2328 &self,
2329 ctx: &TestAppContext,
2330 predicate: impl FnMut(&T, &AppContext) -> bool,
2331 ) -> impl Future<Output = ()> {
2332 self.condition_with_duration(Duration::from_millis(500), ctx, predicate)
2333 }
2334
2335 pub fn condition_with_duration(
2336 &self,
2337 duration: Duration,
2338 ctx: &TestAppContext,
2339 mut predicate: impl FnMut(&T, &AppContext) -> bool,
2340 ) -> impl Future<Output = ()> {
2341 let (tx, mut rx) = mpsc::channel(1024);
2342
2343 let mut ctx = ctx.0.borrow_mut();
2344 self.update(&mut *ctx, |_, ctx| {
2345 ctx.observe_view(self, {
2346 let mut tx = tx.clone();
2347 move |_, _, _| {
2348 tx.blocking_send(()).ok();
2349 }
2350 });
2351
2352 ctx.subscribe(self, {
2353 let mut tx = tx.clone();
2354 move |_, _, _| {
2355 tx.blocking_send(()).ok();
2356 }
2357 })
2358 });
2359
2360 let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap();
2361 let handle = self.downgrade();
2362
2363 async move {
2364 timeout(duration, async move {
2365 loop {
2366 {
2367 let ctx = ctx.borrow();
2368 let ctx = ctx.as_ref();
2369 if predicate(
2370 handle
2371 .upgrade(ctx)
2372 .expect("view dropped with pending condition")
2373 .read(ctx),
2374 ctx,
2375 ) {
2376 break;
2377 }
2378 }
2379
2380 rx.recv()
2381 .await
2382 .expect("view dropped with pending condition");
2383 }
2384 })
2385 .await
2386 .expect("condition timed out");
2387 }
2388 }
2389}
2390
2391impl<T> Clone for ViewHandle<T> {
2392 fn clone(&self) -> Self {
2393 if let Some(ref_counts) = self.ref_counts.upgrade() {
2394 ref_counts.lock().inc_entity(self.view_id);
2395 }
2396
2397 Self {
2398 window_id: self.window_id,
2399 view_id: self.view_id,
2400 view_type: PhantomData,
2401 ref_counts: self.ref_counts.clone(),
2402 }
2403 }
2404}
2405
2406impl<T> PartialEq for ViewHandle<T> {
2407 fn eq(&self, other: &Self) -> bool {
2408 self.window_id == other.window_id && self.view_id == other.view_id
2409 }
2410}
2411
2412impl<T> Eq for ViewHandle<T> {}
2413
2414impl<T> Debug for ViewHandle<T> {
2415 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2416 f.debug_struct(&format!("ViewHandle<{}>", type_name::<T>()))
2417 .field("window_id", &self.window_id)
2418 .field("view_id", &self.view_id)
2419 .finish()
2420 }
2421}
2422
2423impl<T> Drop for ViewHandle<T> {
2424 fn drop(&mut self) {
2425 if let Some(ref_counts) = self.ref_counts.upgrade() {
2426 ref_counts.lock().dec_view(self.window_id, self.view_id);
2427 }
2428 }
2429}
2430
2431impl<T> Handle<T> for ViewHandle<T> {
2432 fn id(&self) -> usize {
2433 self.view_id
2434 }
2435
2436 fn location(&self) -> EntityLocation {
2437 EntityLocation::View(self.window_id, self.view_id)
2438 }
2439}
2440
2441#[derive(Clone)]
2442pub struct AnyViewHandle {
2443 window_id: usize,
2444 view_id: usize,
2445 view_type: TypeId,
2446 ref_counts: Weak<Mutex<RefCounts>>,
2447}
2448
2449impl AnyViewHandle {
2450 pub fn id(&self) -> usize {
2451 self.view_id
2452 }
2453
2454 pub fn is<T: 'static>(&self) -> bool {
2455 TypeId::of::<T>() == self.view_type
2456 }
2457
2458 pub fn downcast<T: View>(self) -> Option<ViewHandle<T>> {
2459 if self.is::<T>() {
2460 if let Some(ref_counts) = self.ref_counts.upgrade() {
2461 return Some(ViewHandle::new(self.window_id, self.view_id, &ref_counts));
2462 }
2463 }
2464 None
2465 }
2466}
2467
2468impl<T: View> From<&ViewHandle<T>> for AnyViewHandle {
2469 fn from(handle: &ViewHandle<T>) -> Self {
2470 if let Some(ref_counts) = handle.ref_counts.upgrade() {
2471 ref_counts.lock().inc_entity(handle.view_id);
2472 }
2473 AnyViewHandle {
2474 window_id: handle.window_id,
2475 view_id: handle.view_id,
2476 view_type: TypeId::of::<T>(),
2477 ref_counts: handle.ref_counts.clone(),
2478 }
2479 }
2480}
2481
2482impl<T: View> From<ViewHandle<T>> for AnyViewHandle {
2483 fn from(handle: ViewHandle<T>) -> Self {
2484 (&handle).into()
2485 }
2486}
2487
2488pub struct WeakViewHandle<T> {
2489 window_id: usize,
2490 view_id: usize,
2491 view_type: PhantomData<T>,
2492}
2493
2494impl<T: View> WeakViewHandle<T> {
2495 fn new(window_id: usize, view_id: usize) -> Self {
2496 Self {
2497 window_id,
2498 view_id,
2499 view_type: PhantomData,
2500 }
2501 }
2502
2503 pub fn upgrade(&self, ctx: impl AsRef<AppContext>) -> Option<ViewHandle<T>> {
2504 let ctx = ctx.as_ref();
2505 if ctx
2506 .windows
2507 .get(&self.window_id)
2508 .and_then(|w| w.views.get(&self.view_id))
2509 .is_some()
2510 {
2511 Some(ViewHandle::new(
2512 self.window_id,
2513 self.view_id,
2514 &ctx.ref_counts,
2515 ))
2516 } else {
2517 None
2518 }
2519 }
2520}
2521
2522impl<T> Clone for WeakViewHandle<T> {
2523 fn clone(&self) -> Self {
2524 Self {
2525 window_id: self.window_id,
2526 view_id: self.view_id,
2527 view_type: PhantomData,
2528 }
2529 }
2530}
2531
2532pub struct ValueHandle<T> {
2533 value_type: PhantomData<T>,
2534 tag_type_id: TypeId,
2535 id: usize,
2536 ref_counts: Weak<Mutex<RefCounts>>,
2537}
2538
2539impl<T: 'static> ValueHandle<T> {
2540 fn new(tag_type_id: TypeId, id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
2541 ref_counts.lock().inc_value(tag_type_id, id);
2542 Self {
2543 value_type: PhantomData,
2544 tag_type_id,
2545 id,
2546 ref_counts: Arc::downgrade(ref_counts),
2547 }
2548 }
2549
2550 pub fn read<R>(&self, ctx: &AppContext, f: impl FnOnce(&T) -> R) -> R {
2551 f(ctx
2552 .values
2553 .read()
2554 .get(&(self.tag_type_id, self.id))
2555 .unwrap()
2556 .downcast_ref()
2557 .unwrap())
2558 }
2559
2560 pub fn update<R>(&self, ctx: &AppContext, f: impl FnOnce(&mut T) -> R) -> R {
2561 f(ctx
2562 .values
2563 .write()
2564 .get_mut(&(self.tag_type_id, self.id))
2565 .unwrap()
2566 .downcast_mut()
2567 .unwrap())
2568 }
2569}
2570
2571impl<T> Drop for ValueHandle<T> {
2572 fn drop(&mut self) {
2573 if let Some(ref_counts) = self.ref_counts.upgrade() {
2574 ref_counts.lock().dec_value(self.tag_type_id, self.id);
2575 }
2576 }
2577}
2578
2579#[derive(Default)]
2580struct RefCounts {
2581 entity_counts: HashMap<usize, usize>,
2582 value_counts: HashMap<(TypeId, usize), usize>,
2583 dropped_models: HashSet<usize>,
2584 dropped_views: HashSet<(usize, usize)>,
2585 dropped_values: HashSet<(TypeId, usize)>,
2586}
2587
2588impl RefCounts {
2589 fn inc_entity(&mut self, model_id: usize) {
2590 *self.entity_counts.entry(model_id).or_insert(0) += 1;
2591 }
2592
2593 fn inc_value(&mut self, tag_type_id: TypeId, id: usize) {
2594 *self.value_counts.entry((tag_type_id, id)).or_insert(0) += 1;
2595 }
2596
2597 fn dec_model(&mut self, model_id: usize) {
2598 let count = self.entity_counts.get_mut(&model_id).unwrap();
2599 *count -= 1;
2600 if *count == 0 {
2601 self.entity_counts.remove(&model_id);
2602 self.dropped_models.insert(model_id);
2603 }
2604 }
2605
2606 fn dec_view(&mut self, window_id: usize, view_id: usize) {
2607 let count = self.entity_counts.get_mut(&view_id).unwrap();
2608 *count -= 1;
2609 if *count == 0 {
2610 self.entity_counts.remove(&view_id);
2611 self.dropped_views.insert((window_id, view_id));
2612 }
2613 }
2614
2615 fn dec_value(&mut self, tag_type_id: TypeId, id: usize) {
2616 let key = (tag_type_id, id);
2617 let count = self.value_counts.get_mut(&key).unwrap();
2618 *count -= 1;
2619 if *count == 0 {
2620 self.value_counts.remove(&key);
2621 self.dropped_values.insert(key);
2622 }
2623 }
2624
2625 fn take_dropped(
2626 &mut self,
2627 ) -> (
2628 HashSet<usize>,
2629 HashSet<(usize, usize)>,
2630 HashSet<(TypeId, usize)>,
2631 ) {
2632 let mut dropped_models = HashSet::new();
2633 let mut dropped_views = HashSet::new();
2634 let mut dropped_values = HashSet::new();
2635 std::mem::swap(&mut self.dropped_models, &mut dropped_models);
2636 std::mem::swap(&mut self.dropped_views, &mut dropped_views);
2637 std::mem::swap(&mut self.dropped_values, &mut dropped_values);
2638 (dropped_models, dropped_views, dropped_values)
2639 }
2640}
2641
2642enum Subscription {
2643 FromModel {
2644 model_id: usize,
2645 callback: Box<dyn FnMut(&mut dyn Any, &dyn Any, &mut MutableAppContext, usize)>,
2646 },
2647 FromView {
2648 window_id: usize,
2649 view_id: usize,
2650 callback: Box<dyn FnMut(&mut dyn Any, &dyn Any, &mut MutableAppContext, usize, usize)>,
2651 },
2652}
2653
2654enum ModelObservation {
2655 FromModel {
2656 model_id: usize,
2657 callback: Box<dyn FnMut(&mut dyn Any, usize, &mut MutableAppContext, usize)>,
2658 },
2659 FromView {
2660 window_id: usize,
2661 view_id: usize,
2662 callback: Box<dyn FnMut(&mut dyn Any, usize, &mut MutableAppContext, usize, usize)>,
2663 },
2664}
2665
2666struct ViewObservation {
2667 window_id: usize,
2668 view_id: usize,
2669 callback: Box<dyn FnMut(&mut dyn Any, usize, usize, &mut MutableAppContext, usize, usize)>,
2670}
2671
2672type FutureHandler = Box<dyn FnOnce(Box<dyn Any>, &mut MutableAppContext) -> Box<dyn Any>>;
2673
2674struct StreamHandler {
2675 item_callback: Box<dyn FnMut(Box<dyn Any>, &mut MutableAppContext) -> bool>,
2676 done_callback: Box<dyn FnOnce(&mut MutableAppContext) -> Box<dyn Any>>,
2677}
2678
2679#[must_use]
2680pub struct EntityTask<T> {
2681 id: usize,
2682 task: Option<executor::Task<T>>,
2683 handler_map: TaskHandlerMap,
2684}
2685
2686enum TaskHandlerMap {
2687 Detached,
2688 Future(Rc<RefCell<HashMap<usize, FutureHandler>>>),
2689 Stream(Rc<RefCell<HashMap<usize, StreamHandler>>>),
2690}
2691
2692impl<T> EntityTask<T> {
2693 fn new(id: usize, task: executor::Task<T>, handler_map: TaskHandlerMap) -> Self {
2694 Self {
2695 id,
2696 task: Some(task),
2697 handler_map,
2698 }
2699 }
2700
2701 pub fn detach(mut self) {
2702 self.handler_map = TaskHandlerMap::Detached;
2703 self.task.take().unwrap().detach();
2704 }
2705
2706 pub async fn cancel(mut self) -> Option<T> {
2707 let task = self.task.take().unwrap();
2708 task.cancel().await
2709 }
2710}
2711
2712impl<T> Future for EntityTask<T> {
2713 type Output = T;
2714
2715 fn poll(
2716 self: std::pin::Pin<&mut Self>,
2717 ctx: &mut std::task::Context<'_>,
2718 ) -> std::task::Poll<Self::Output> {
2719 let task = unsafe { self.map_unchecked_mut(|task| task.task.as_mut().unwrap()) };
2720 task.poll(ctx)
2721 }
2722}
2723
2724impl<T> Drop for EntityTask<T> {
2725 fn drop(self: &mut Self) {
2726 match &self.handler_map {
2727 TaskHandlerMap::Detached => {
2728 return;
2729 }
2730 TaskHandlerMap::Future(map) => {
2731 map.borrow_mut().remove(&self.id);
2732 }
2733 TaskHandlerMap::Stream(map) => {
2734 map.borrow_mut().remove(&self.id);
2735 }
2736 }
2737 }
2738}
2739
2740#[cfg(test)]
2741mod tests {
2742 use super::*;
2743 use crate::elements::*;
2744 use smol::future::poll_once;
2745
2746 #[test]
2747 fn test_model_handles() {
2748 struct Model {
2749 other: Option<ModelHandle<Model>>,
2750 events: Vec<String>,
2751 }
2752
2753 impl Entity for Model {
2754 type Event = usize;
2755 }
2756
2757 impl Model {
2758 fn new(other: Option<ModelHandle<Self>>, ctx: &mut ModelContext<Self>) -> Self {
2759 if let Some(other) = other.as_ref() {
2760 ctx.observe(other, |me, _, _| {
2761 me.events.push("notified".into());
2762 });
2763 ctx.subscribe(other, |me, event, _| {
2764 me.events.push(format!("observed event {}", event));
2765 });
2766 }
2767
2768 Self {
2769 other,
2770 events: Vec::new(),
2771 }
2772 }
2773 }
2774
2775 App::test((), |app| {
2776 let handle_1 = app.add_model(|ctx| Model::new(None, ctx));
2777 let handle_2 = app.add_model(|ctx| Model::new(Some(handle_1.clone()), ctx));
2778 assert_eq!(app.ctx.models.len(), 2);
2779
2780 handle_1.update(app, |model, ctx| {
2781 model.events.push("updated".into());
2782 ctx.emit(1);
2783 ctx.notify();
2784 ctx.emit(2);
2785 });
2786 assert_eq!(handle_1.read(app).events, vec!["updated".to_string()]);
2787 assert_eq!(
2788 handle_2.read(app).events,
2789 vec![
2790 "observed event 1".to_string(),
2791 "notified".to_string(),
2792 "observed event 2".to_string(),
2793 ]
2794 );
2795
2796 handle_2.update(app, |model, _| {
2797 drop(handle_1);
2798 model.other.take();
2799 });
2800
2801 assert_eq!(app.ctx.models.len(), 1);
2802 assert!(app.subscriptions.is_empty());
2803 assert!(app.model_observations.is_empty());
2804 });
2805 }
2806
2807 #[test]
2808 fn test_subscribe_and_emit_from_model() {
2809 #[derive(Default)]
2810 struct Model {
2811 events: Vec<usize>,
2812 }
2813
2814 impl Entity for Model {
2815 type Event = usize;
2816 }
2817
2818 App::test((), |app| {
2819 let handle_1 = app.add_model(|_| Model::default());
2820 let handle_2 = app.add_model(|_| Model::default());
2821 let handle_2b = handle_2.clone();
2822
2823 handle_1.update(app, |_, c| {
2824 c.subscribe(&handle_2, move |model: &mut Model, event, c| {
2825 model.events.push(*event);
2826
2827 c.subscribe(&handle_2b, |model, event, _| {
2828 model.events.push(*event * 2);
2829 });
2830 });
2831 });
2832
2833 handle_2.update(app, |_, c| c.emit(7));
2834 assert_eq!(handle_1.read(app).events, vec![7]);
2835
2836 handle_2.update(app, |_, c| c.emit(5));
2837 assert_eq!(handle_1.read(app).events, vec![7, 10, 5]);
2838 })
2839 }
2840
2841 #[test]
2842 fn test_observe_and_notify_from_model() {
2843 #[derive(Default)]
2844 struct Model {
2845 count: usize,
2846 events: Vec<usize>,
2847 }
2848
2849 impl Entity for Model {
2850 type Event = ();
2851 }
2852
2853 App::test((), |app| {
2854 let handle_1 = app.add_model(|_| Model::default());
2855 let handle_2 = app.add_model(|_| Model::default());
2856 let handle_2b = handle_2.clone();
2857
2858 handle_1.update(app, |_, c| {
2859 c.observe(&handle_2, move |model, observed, c| {
2860 model.events.push(observed.read(c).count);
2861 c.observe(&handle_2b, |model, observed, c| {
2862 model.events.push(observed.read(c).count * 2);
2863 });
2864 });
2865 });
2866
2867 handle_2.update(app, |model, c| {
2868 model.count = 7;
2869 c.notify()
2870 });
2871 assert_eq!(handle_1.read(app).events, vec![7]);
2872
2873 handle_2.update(app, |model, c| {
2874 model.count = 5;
2875 c.notify()
2876 });
2877 assert_eq!(handle_1.read(app).events, vec![7, 10, 5])
2878 })
2879 }
2880
2881 #[test]
2882 fn test_spawn_from_model() {
2883 #[derive(Default)]
2884 struct Model {
2885 count: usize,
2886 }
2887
2888 impl Entity for Model {
2889 type Event = ();
2890 }
2891
2892 App::test_async((), |mut app| async move {
2893 let handle = app.add_model(|_| Model::default());
2894 handle
2895 .update(&mut app, |_, c| {
2896 c.spawn(async { 7 }, |model, output, _| {
2897 model.count = output;
2898 })
2899 })
2900 .await;
2901 app.read(|ctx| assert_eq!(handle.read(ctx).count, 7));
2902
2903 handle
2904 .update(&mut app, |_, c| {
2905 c.spawn(async { 14 }, |model, output, _| {
2906 model.count = output;
2907 })
2908 })
2909 .await;
2910 app.read(|ctx| assert_eq!(handle.read(ctx).count, 14));
2911 });
2912 }
2913
2914 #[test]
2915 fn test_spawn_stream_local_from_model() {
2916 #[derive(Default)]
2917 struct Model {
2918 events: Vec<Option<usize>>,
2919 }
2920
2921 impl Entity for Model {
2922 type Event = ();
2923 }
2924
2925 App::test_async((), |mut app| async move {
2926 let handle = app.add_model(|_| Model::default());
2927 handle
2928 .update(&mut app, |_, c| {
2929 c.spawn_stream(
2930 smol::stream::iter(vec![1, 2, 3]),
2931 |model, output, _| {
2932 model.events.push(Some(output));
2933 },
2934 |model, _| {
2935 model.events.push(None);
2936 },
2937 )
2938 })
2939 .await;
2940 app.read(|ctx| assert_eq!(handle.read(ctx).events, [Some(1), Some(2), Some(3), None]));
2941 })
2942 }
2943
2944 #[test]
2945 fn test_view_handles() {
2946 struct View {
2947 other: Option<ViewHandle<View>>,
2948 events: Vec<String>,
2949 }
2950
2951 impl Entity for View {
2952 type Event = usize;
2953 }
2954
2955 impl super::View for View {
2956 fn render<'a>(&self, _: &AppContext) -> ElementBox {
2957 Empty::new().boxed()
2958 }
2959
2960 fn ui_name() -> &'static str {
2961 "View"
2962 }
2963 }
2964
2965 impl View {
2966 fn new(other: Option<ViewHandle<View>>, ctx: &mut ViewContext<Self>) -> Self {
2967 if let Some(other) = other.as_ref() {
2968 ctx.subscribe_to_view(other, |me, _, event, _| {
2969 me.events.push(format!("observed event {}", event));
2970 });
2971 }
2972 Self {
2973 other,
2974 events: Vec::new(),
2975 }
2976 }
2977 }
2978
2979 App::test((), |app| {
2980 let (window_id, _) = app.add_window(|ctx| View::new(None, ctx));
2981 let handle_1 = app.add_view(window_id, |ctx| View::new(None, ctx));
2982 let handle_2 = app.add_view(window_id, |ctx| View::new(Some(handle_1.clone()), ctx));
2983 assert_eq!(app.ctx.windows[&window_id].views.len(), 3);
2984
2985 handle_1.update(app, |view, ctx| {
2986 view.events.push("updated".into());
2987 ctx.emit(1);
2988 ctx.emit(2);
2989 });
2990 assert_eq!(handle_1.read(app).events, vec!["updated".to_string()]);
2991 assert_eq!(
2992 handle_2.read(app).events,
2993 vec![
2994 "observed event 1".to_string(),
2995 "observed event 2".to_string(),
2996 ]
2997 );
2998
2999 handle_2.update(app, |view, _| {
3000 drop(handle_1);
3001 view.other.take();
3002 });
3003
3004 assert_eq!(app.ctx.windows[&window_id].views.len(), 2);
3005 assert!(app.subscriptions.is_empty());
3006 assert!(app.model_observations.is_empty());
3007 })
3008 }
3009
3010 #[test]
3011 fn test_subscribe_and_emit_from_view() {
3012 #[derive(Default)]
3013 struct View {
3014 events: Vec<usize>,
3015 }
3016
3017 impl Entity for View {
3018 type Event = usize;
3019 }
3020
3021 impl super::View for View {
3022 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3023 Empty::new().boxed()
3024 }
3025
3026 fn ui_name() -> &'static str {
3027 "View"
3028 }
3029 }
3030
3031 struct Model;
3032
3033 impl Entity for Model {
3034 type Event = usize;
3035 }
3036
3037 App::test((), |app| {
3038 let (window_id, handle_1) = app.add_window(|_| View::default());
3039 let handle_2 = app.add_view(window_id, |_| View::default());
3040 let handle_2b = handle_2.clone();
3041 let handle_3 = app.add_model(|_| Model);
3042
3043 handle_1.update(app, |_, c| {
3044 c.subscribe_to_view(&handle_2, move |me, _, event, c| {
3045 me.events.push(*event);
3046
3047 c.subscribe_to_view(&handle_2b, |me, _, event, _| {
3048 me.events.push(*event * 2);
3049 });
3050 });
3051
3052 c.subscribe_to_model(&handle_3, |me, _, event, _| {
3053 me.events.push(*event);
3054 })
3055 });
3056
3057 handle_2.update(app, |_, c| c.emit(7));
3058 assert_eq!(handle_1.read(app).events, vec![7]);
3059
3060 handle_2.update(app, |_, c| c.emit(5));
3061 assert_eq!(handle_1.read(app).events, vec![7, 10, 5]);
3062
3063 handle_3.update(app, |_, c| c.emit(9));
3064 assert_eq!(handle_1.read(app).events, vec![7, 10, 5, 9]);
3065 })
3066 }
3067
3068 #[test]
3069 fn test_dropping_subscribers() {
3070 struct View;
3071
3072 impl Entity for View {
3073 type Event = ();
3074 }
3075
3076 impl super::View for View {
3077 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3078 Empty::new().boxed()
3079 }
3080
3081 fn ui_name() -> &'static str {
3082 "View"
3083 }
3084 }
3085
3086 struct Model;
3087
3088 impl Entity for Model {
3089 type Event = ();
3090 }
3091
3092 App::test((), |app| {
3093 let (window_id, _) = app.add_window(|_| View);
3094 let observing_view = app.add_view(window_id, |_| View);
3095 let emitting_view = app.add_view(window_id, |_| View);
3096 let observing_model = app.add_model(|_| Model);
3097 let observed_model = app.add_model(|_| Model);
3098
3099 observing_view.update(app, |_, ctx| {
3100 ctx.subscribe_to_view(&emitting_view, |_, _, _, _| {});
3101 ctx.subscribe_to_model(&observed_model, |_, _, _, _| {});
3102 });
3103 observing_model.update(app, |_, ctx| {
3104 ctx.subscribe(&observed_model, |_, _, _| {});
3105 });
3106
3107 app.update(|| {
3108 drop(observing_view);
3109 drop(observing_model);
3110 });
3111
3112 emitting_view.update(app, |_, ctx| ctx.emit(()));
3113 observed_model.update(app, |_, ctx| ctx.emit(()));
3114 })
3115 }
3116
3117 #[test]
3118 fn test_observe_and_notify_from_view() {
3119 #[derive(Default)]
3120 struct View {
3121 events: Vec<usize>,
3122 }
3123
3124 impl Entity for View {
3125 type Event = usize;
3126 }
3127
3128 impl super::View for View {
3129 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3130 Empty::new().boxed()
3131 }
3132
3133 fn ui_name() -> &'static str {
3134 "View"
3135 }
3136 }
3137
3138 #[derive(Default)]
3139 struct Model {
3140 count: usize,
3141 }
3142
3143 impl Entity for Model {
3144 type Event = ();
3145 }
3146
3147 App::test((), |app| {
3148 let (_, view) = app.add_window(|_| View::default());
3149 let model = app.add_model(|_| Model::default());
3150
3151 view.update(app, |_, c| {
3152 c.observe_model(&model, |me, observed, c| {
3153 me.events.push(observed.read(c).count)
3154 });
3155 });
3156
3157 model.update(app, |model, c| {
3158 model.count = 11;
3159 c.notify();
3160 });
3161 assert_eq!(view.read(app).events, vec![11]);
3162 })
3163 }
3164
3165 #[test]
3166 fn test_dropping_observers() {
3167 struct View;
3168
3169 impl Entity for View {
3170 type Event = ();
3171 }
3172
3173 impl super::View for View {
3174 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3175 Empty::new().boxed()
3176 }
3177
3178 fn ui_name() -> &'static str {
3179 "View"
3180 }
3181 }
3182
3183 struct Model;
3184
3185 impl Entity for Model {
3186 type Event = ();
3187 }
3188
3189 App::test((), |app| {
3190 let (window_id, _) = app.add_window(|_| View);
3191 let observing_view = app.add_view(window_id, |_| View);
3192 let observing_model = app.add_model(|_| Model);
3193 let observed_model = app.add_model(|_| Model);
3194
3195 observing_view.update(app, |_, ctx| {
3196 ctx.observe_model(&observed_model, |_, _, _| {});
3197 });
3198 observing_model.update(app, |_, ctx| {
3199 ctx.observe(&observed_model, |_, _, _| {});
3200 });
3201
3202 app.update(|| {
3203 drop(observing_view);
3204 drop(observing_model);
3205 });
3206
3207 observed_model.update(app, |_, ctx| ctx.notify());
3208 })
3209 }
3210
3211 #[test]
3212 fn test_focus() {
3213 #[derive(Default)]
3214 struct View {
3215 events: Vec<String>,
3216 }
3217
3218 impl Entity for View {
3219 type Event = String;
3220 }
3221
3222 impl super::View for View {
3223 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3224 Empty::new().boxed()
3225 }
3226
3227 fn ui_name() -> &'static str {
3228 "View"
3229 }
3230
3231 fn on_focus(&mut self, ctx: &mut ViewContext<Self>) {
3232 self.events.push("self focused".into());
3233 ctx.emit("focused".into());
3234 }
3235
3236 fn on_blur(&mut self, ctx: &mut ViewContext<Self>) {
3237 self.events.push("self blurred".into());
3238 ctx.emit("blurred".into());
3239 }
3240 }
3241
3242 App::test((), |app| {
3243 let (window_id, view_1) = app.add_window(|_| View::default());
3244 let view_2 = app.add_view(window_id, |_| View::default());
3245
3246 view_1.update(app, |_, ctx| {
3247 ctx.subscribe_to_view(&view_2, |view_1, _, event, _| {
3248 view_1.events.push(format!("view 2 {}", event));
3249 });
3250 ctx.focus(&view_2);
3251 });
3252
3253 view_1.update(app, |_, ctx| {
3254 ctx.focus(&view_1);
3255 });
3256
3257 assert_eq!(
3258 view_1.read(app).events,
3259 [
3260 "self focused".to_string(),
3261 "self blurred".to_string(),
3262 "view 2 focused".to_string(),
3263 "self focused".to_string(),
3264 "view 2 blurred".to_string(),
3265 ],
3266 );
3267 })
3268 }
3269
3270 #[test]
3271 fn test_spawn_from_view() {
3272 #[derive(Default)]
3273 struct View {
3274 count: usize,
3275 }
3276
3277 impl Entity for View {
3278 type Event = ();
3279 }
3280
3281 impl super::View for View {
3282 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3283 Empty::new().boxed()
3284 }
3285
3286 fn ui_name() -> &'static str {
3287 "View"
3288 }
3289 }
3290
3291 App::test_async((), |mut app| async move {
3292 let handle = app.add_window(|_| View::default()).1;
3293 handle
3294 .update(&mut app, |_, c| {
3295 c.spawn(async { 7 }, |me, output, _| {
3296 me.count = output;
3297 })
3298 })
3299 .await;
3300 app.read(|ctx| assert_eq!(handle.read(ctx).count, 7));
3301 handle
3302 .update(&mut app, |_, c| {
3303 c.spawn(async { 14 }, |me, output, _| {
3304 me.count = output;
3305 })
3306 })
3307 .await;
3308 app.read(|ctx| assert_eq!(handle.read(ctx).count, 14));
3309 });
3310 }
3311
3312 #[test]
3313 fn test_spawn_stream_local_from_view() {
3314 #[derive(Default)]
3315 struct View {
3316 events: Vec<Option<usize>>,
3317 }
3318
3319 impl Entity for View {
3320 type Event = ();
3321 }
3322
3323 impl super::View for View {
3324 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3325 Empty::new().boxed()
3326 }
3327
3328 fn ui_name() -> &'static str {
3329 "View"
3330 }
3331 }
3332
3333 App::test_async((), |mut app| async move {
3334 let (_, handle) = app.add_window(|_| View::default());
3335 handle
3336 .update(&mut app, |_, c| {
3337 c.spawn_stream(
3338 smol::stream::iter(vec![1_usize, 2, 3]),
3339 |me, output, _| {
3340 me.events.push(Some(output));
3341 },
3342 |me, _| {
3343 me.events.push(None);
3344 },
3345 )
3346 })
3347 .await;
3348
3349 app.read(|ctx| assert_eq!(handle.read(ctx).events, [Some(1), Some(2), Some(3), None]))
3350 });
3351 }
3352
3353 #[test]
3354 fn test_dispatch_action() {
3355 struct ViewA {
3356 id: usize,
3357 }
3358
3359 impl Entity for ViewA {
3360 type Event = ();
3361 }
3362
3363 impl View for ViewA {
3364 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3365 Empty::new().boxed()
3366 }
3367
3368 fn ui_name() -> &'static str {
3369 "View"
3370 }
3371 }
3372
3373 struct ViewB {
3374 id: usize,
3375 }
3376
3377 impl Entity for ViewB {
3378 type Event = ();
3379 }
3380
3381 impl View for ViewB {
3382 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3383 Empty::new().boxed()
3384 }
3385
3386 fn ui_name() -> &'static str {
3387 "View"
3388 }
3389 }
3390
3391 struct ActionArg {
3392 foo: String,
3393 }
3394
3395 App::test((), |app| {
3396 let actions = Rc::new(RefCell::new(Vec::new()));
3397
3398 let actions_clone = actions.clone();
3399 app.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| {
3400 actions_clone.borrow_mut().push("global a".to_string());
3401 });
3402
3403 let actions_clone = actions.clone();
3404 app.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| {
3405 actions_clone.borrow_mut().push("global b".to_string());
3406 });
3407
3408 let actions_clone = actions.clone();
3409 app.add_action("action", move |view: &mut ViewA, arg: &ActionArg, ctx| {
3410 assert_eq!(arg.foo, "bar");
3411 ctx.propagate_action();
3412 actions_clone.borrow_mut().push(format!("{} a", view.id));
3413 });
3414
3415 let actions_clone = actions.clone();
3416 app.add_action("action", move |view: &mut ViewA, _: &ActionArg, ctx| {
3417 if view.id != 1 {
3418 ctx.propagate_action();
3419 }
3420 actions_clone.borrow_mut().push(format!("{} b", view.id));
3421 });
3422
3423 let actions_clone = actions.clone();
3424 app.add_action("action", move |view: &mut ViewB, _: &ActionArg, ctx| {
3425 ctx.propagate_action();
3426 actions_clone.borrow_mut().push(format!("{} c", view.id));
3427 });
3428
3429 let actions_clone = actions.clone();
3430 app.add_action("action", move |view: &mut ViewB, _: &ActionArg, ctx| {
3431 ctx.propagate_action();
3432 actions_clone.borrow_mut().push(format!("{} d", view.id));
3433 });
3434
3435 let (window_id, view_1) = app.add_window(|_| ViewA { id: 1 });
3436 let view_2 = app.add_view(window_id, |_| ViewB { id: 2 });
3437 let view_3 = app.add_view(window_id, |_| ViewA { id: 3 });
3438 let view_4 = app.add_view(window_id, |_| ViewB { id: 4 });
3439
3440 app.dispatch_action(
3441 window_id,
3442 vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()],
3443 "action",
3444 ActionArg { foo: "bar".into() },
3445 );
3446
3447 assert_eq!(
3448 *actions.borrow(),
3449 vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "1 b"]
3450 );
3451
3452 // Remove view_1, which doesn't propagate the action
3453 actions.borrow_mut().clear();
3454 app.dispatch_action(
3455 window_id,
3456 vec![view_2.id(), view_3.id(), view_4.id()],
3457 "action",
3458 ActionArg { foo: "bar".into() },
3459 );
3460
3461 assert_eq!(
3462 *actions.borrow(),
3463 vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global b", "global a"]
3464 );
3465 })
3466 }
3467
3468 #[test]
3469 fn test_dispatch_keystroke() {
3470 use std::cell::Cell;
3471
3472 #[derive(Clone)]
3473 struct ActionArg {
3474 key: String,
3475 }
3476
3477 struct View {
3478 id: usize,
3479 keymap_context: keymap::Context,
3480 }
3481
3482 impl Entity for View {
3483 type Event = ();
3484 }
3485
3486 impl super::View for View {
3487 fn render<'a>(&self, _: &AppContext) -> ElementBox {
3488 Empty::new().boxed()
3489 }
3490
3491 fn ui_name() -> &'static str {
3492 "View"
3493 }
3494
3495 fn keymap_context(&self, _: &AppContext) -> keymap::Context {
3496 self.keymap_context.clone()
3497 }
3498 }
3499
3500 impl View {
3501 fn new(id: usize) -> Self {
3502 View {
3503 id,
3504 keymap_context: keymap::Context::default(),
3505 }
3506 }
3507 }
3508
3509 App::test((), |app| {
3510 let mut view_1 = View::new(1);
3511 let mut view_2 = View::new(2);
3512 let mut view_3 = View::new(3);
3513 view_1.keymap_context.set.insert("a".into());
3514 view_2.keymap_context.set.insert("b".into());
3515 view_3.keymap_context.set.insert("c".into());
3516
3517 let (window_id, view_1) = app.add_window(|_| view_1);
3518 let view_2 = app.add_view(window_id, |_| view_2);
3519 let view_3 = app.add_view(window_id, |_| view_3);
3520
3521 // This keymap's only binding dispatches an action on view 2 because that view will have
3522 // "a" and "b" in its context, but not "c".
3523 let binding = keymap::Binding::new("a", "action", Some("a && b && !c"))
3524 .with_arg(ActionArg { key: "a".into() });
3525 app.add_bindings(vec![binding]);
3526
3527 let handled_action = Rc::new(Cell::new(false));
3528 let handled_action_clone = handled_action.clone();
3529 app.add_action("action", move |view: &mut View, arg: &ActionArg, _ctx| {
3530 handled_action_clone.set(true);
3531 assert_eq!(view.id, 2);
3532 assert_eq!(arg.key, "a");
3533 });
3534
3535 app.dispatch_keystroke(
3536 window_id,
3537 vec![view_1.id(), view_2.id(), view_3.id()],
3538 &Keystroke::parse("a").unwrap(),
3539 )
3540 .unwrap();
3541
3542 assert!(handled_action.get());
3543 });
3544 }
3545
3546 #[test]
3547 fn test_model_condition() {
3548 struct Counter(usize);
3549
3550 impl super::Entity for Counter {
3551 type Event = ();
3552 }
3553
3554 impl Counter {
3555 fn inc(&mut self, ctx: &mut ModelContext<Self>) {
3556 self.0 += 1;
3557 ctx.notify();
3558 }
3559 }
3560
3561 App::test_async((), |mut app| async move {
3562 let model = app.add_model(|_| Counter(0));
3563
3564 let condition1 = model.condition(&app, |model, _| model.0 == 2);
3565 let condition2 = model.condition(&app, |model, _| model.0 == 3);
3566 smol::pin!(condition1, condition2);
3567
3568 model.update(&mut app, |model, ctx| model.inc(ctx));
3569 assert_eq!(poll_once(&mut condition1).await, None);
3570 assert_eq!(poll_once(&mut condition2).await, None);
3571
3572 model.update(&mut app, |model, ctx| model.inc(ctx));
3573 assert_eq!(poll_once(&mut condition1).await, Some(()));
3574 assert_eq!(poll_once(&mut condition2).await, None);
3575
3576 model.update(&mut app, |model, ctx| model.inc(ctx));
3577 assert_eq!(poll_once(&mut condition2).await, Some(()));
3578
3579 model.update(&mut app, |_, ctx| ctx.notify());
3580 });
3581 }
3582
3583 #[test]
3584 #[should_panic]
3585 fn test_model_condition_timeout() {
3586 struct Model;
3587
3588 impl super::Entity for Model {
3589 type Event = ();
3590 }
3591
3592 App::test_async((), |mut app| async move {
3593 let model = app.add_model(|_| Model);
3594 model.condition(&app, |_, _| false).await;
3595 });
3596 }
3597
3598 #[test]
3599 #[should_panic(expected = "model dropped with pending condition")]
3600 fn test_model_condition_panic_on_drop() {
3601 struct Model;
3602
3603 impl super::Entity for Model {
3604 type Event = ();
3605 }
3606
3607 App::test_async((), |mut app| async move {
3608 let model = app.add_model(|_| Model);
3609 let condition = model.condition(&app, |_, _| false);
3610 app.update(|_| drop(model));
3611 condition.await;
3612 });
3613 }
3614
3615 #[test]
3616 fn test_view_condition() {
3617 struct Counter(usize);
3618
3619 impl super::Entity for Counter {
3620 type Event = ();
3621 }
3622
3623 impl super::View for Counter {
3624 fn ui_name() -> &'static str {
3625 "test view"
3626 }
3627
3628 fn render(&self, _: &AppContext) -> ElementBox {
3629 Empty::new().boxed()
3630 }
3631 }
3632
3633 impl Counter {
3634 fn inc(&mut self, ctx: &mut ViewContext<Self>) {
3635 self.0 += 1;
3636 ctx.notify();
3637 }
3638 }
3639
3640 App::test_async((), |mut app| async move {
3641 let (_, view) = app.add_window(|_| Counter(0));
3642
3643 let condition1 = view.condition(&app, |view, _| view.0 == 2);
3644 let condition2 = view.condition(&app, |view, _| view.0 == 3);
3645 smol::pin!(condition1, condition2);
3646
3647 view.update(&mut app, |view, ctx| view.inc(ctx));
3648 assert_eq!(poll_once(&mut condition1).await, None);
3649 assert_eq!(poll_once(&mut condition2).await, None);
3650
3651 view.update(&mut app, |view, ctx| view.inc(ctx));
3652 assert_eq!(poll_once(&mut condition1).await, Some(()));
3653 assert_eq!(poll_once(&mut condition2).await, None);
3654
3655 view.update(&mut app, |view, ctx| view.inc(ctx));
3656 assert_eq!(poll_once(&mut condition2).await, Some(()));
3657 view.update(&mut app, |_, ctx| ctx.notify());
3658 });
3659 }
3660
3661 #[test]
3662 #[should_panic]
3663 fn test_view_condition_timeout() {
3664 struct View;
3665
3666 impl super::Entity for View {
3667 type Event = ();
3668 }
3669
3670 impl super::View for View {
3671 fn ui_name() -> &'static str {
3672 "test view"
3673 }
3674
3675 fn render(&self, _: &AppContext) -> ElementBox {
3676 Empty::new().boxed()
3677 }
3678 }
3679
3680 App::test_async((), |mut app| async move {
3681 let (_, view) = app.add_window(|_| View);
3682 view.condition(&app, |_, _| false).await;
3683 });
3684 }
3685
3686 #[test]
3687 #[should_panic(expected = "view dropped with pending condition")]
3688 fn test_view_condition_panic_on_drop() {
3689 struct View;
3690
3691 impl super::Entity for View {
3692 type Event = ();
3693 }
3694
3695 impl super::View for View {
3696 fn ui_name() -> &'static str {
3697 "test view"
3698 }
3699
3700 fn render(&self, _: &AppContext) -> ElementBox {
3701 Empty::new().boxed()
3702 }
3703 }
3704
3705 App::test_async((), |mut app| async move {
3706 let window_id = app.add_window(|_| View).0;
3707 let view = app.add_view(window_id, |_| View);
3708
3709 let condition = view.condition(&app, |_, _| false);
3710 app.update(|_| drop(view));
3711 condition.await;
3712 });
3713 }
3714
3715 // #[test]
3716 // fn test_ui_and_window_updates() {
3717 // struct View {
3718 // count: usize,
3719 // }
3720
3721 // impl Entity for View {
3722 // type Event = ();
3723 // }
3724
3725 // impl super::View for View {
3726 // fn render<'a>(&self, _: &AppContext) -> ElementBox {
3727 // Empty::new().boxed()
3728 // }
3729
3730 // fn ui_name() -> &'static str {
3731 // "View"
3732 // }
3733 // }
3734
3735 // App::test(|app| async move {
3736 // let (window_id, _) = app.add_window(|_| View { count: 3 });
3737 // let view_1 = app.add_view(window_id, |_| View { count: 1 });
3738 // let view_2 = app.add_view(window_id, |_| View { count: 2 });
3739
3740 // // Ensure that registering for UI updates after mutating the app still gives us all the
3741 // // updates.
3742 // let ui_updates = Rc::new(RefCell::new(Vec::new()));
3743 // let ui_updates_ = ui_updates.clone();
3744 // app.on_ui_update(move |update, _| ui_updates_.borrow_mut().push(update));
3745
3746 // assert_eq!(
3747 // ui_updates.borrow_mut().drain(..).collect::<Vec<_>>(),
3748 // vec![UiUpdate::OpenWindow {
3749 // window_id,
3750 // width: 1024.0,
3751 // height: 768.0,
3752 // }]
3753 // );
3754
3755 // let window_invalidations = Rc::new(RefCell::new(Vec::new()));
3756 // let window_invalidations_ = window_invalidations.clone();
3757 // app.on_window_invalidated(window_id, move |update, _| {
3758 // window_invalidations_.borrow_mut().push(update)
3759 // });
3760
3761 // let view_2_id = view_2.id();
3762 // view_1.update(app, |view, ctx| {
3763 // view.count = 7;
3764 // ctx.notify();
3765 // drop(view_2);
3766 // });
3767
3768 // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap();
3769 // assert_eq!(invalidation.updated.len(), 1);
3770 // assert!(invalidation.updated.contains(&view_1.id()));
3771 // assert_eq!(invalidation.removed, vec![view_2_id]);
3772
3773 // let view_3 = view_1.update(app, |_, ctx| ctx.add_view(|_| View { count: 8 }));
3774
3775 // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap();
3776 // assert_eq!(invalidation.updated.len(), 1);
3777 // assert!(invalidation.updated.contains(&view_3.id()));
3778 // assert!(invalidation.removed.is_empty());
3779
3780 // view_3
3781 // .update(app, |_, ctx| {
3782 // ctx.spawn_local(async { 9 }, |me, output, ctx| {
3783 // me.count = output;
3784 // ctx.notify();
3785 // })
3786 // })
3787 // .await;
3788
3789 // let invalidation = window_invalidations.borrow_mut().drain(..).next().unwrap();
3790 // assert_eq!(invalidation.updated.len(), 1);
3791 // assert!(invalidation.updated.contains(&view_3.id()));
3792 // assert!(invalidation.removed.is_empty());
3793 // });
3794 // }
3795}