app.rs

   1use crate::{
   2    elements::ElementBox,
   3    executor::{self, Task},
   4    keymap::{self, Keystroke},
   5    platform::{self, CursorStyle, Platform, PromptLevel, 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;
  13use platform::Event;
  14use postage::{mpsc, oneshot, sink::Sink as _, stream::Stream as _};
  15use smol::prelude::*;
  16use std::{
  17    any::{type_name, Any, TypeId},
  18    cell::RefCell,
  19    collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque},
  20    fmt::{self, Debug},
  21    hash::{Hash, Hasher},
  22    marker::PhantomData,
  23    mem,
  24    ops::{Deref, DerefMut},
  25    path::{Path, PathBuf},
  26    pin::Pin,
  27    rc::{self, Rc},
  28    sync::{
  29        atomic::{AtomicUsize, Ordering::SeqCst},
  30        Arc, Weak,
  31    },
  32    time::Duration,
  33};
  34
  35pub trait Entity: 'static {
  36    type Event;
  37
  38    fn release(&mut self, _: &mut MutableAppContext) {}
  39    fn app_will_quit(
  40        &mut self,
  41        _: &mut MutableAppContext,
  42    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
  43        None
  44    }
  45}
  46
  47pub trait View: Entity + Sized {
  48    fn ui_name() -> &'static str;
  49    fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox;
  50    fn on_focus(&mut self, _: &mut ViewContext<Self>) {}
  51    fn on_blur(&mut self, _: &mut ViewContext<Self>) {}
  52    fn keymap_context(&self, _: &AppContext) -> keymap::Context {
  53        Self::default_keymap_context()
  54    }
  55    fn default_keymap_context() -> keymap::Context {
  56        let mut cx = keymap::Context::default();
  57        cx.set.insert(Self::ui_name().into());
  58        cx
  59    }
  60}
  61
  62pub trait ReadModel {
  63    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T;
  64}
  65
  66pub trait ReadModelWith {
  67    fn read_model_with<E: Entity, T>(
  68        &self,
  69        handle: &ModelHandle<E>,
  70        read: &mut dyn FnMut(&E, &AppContext) -> T,
  71    ) -> T;
  72}
  73
  74pub trait UpdateModel {
  75    fn update_model<T: Entity, O>(
  76        &mut self,
  77        handle: &ModelHandle<T>,
  78        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
  79    ) -> O;
  80}
  81
  82pub trait UpgradeModelHandle {
  83    fn upgrade_model_handle<T: Entity>(
  84        &self,
  85        handle: &WeakModelHandle<T>,
  86    ) -> Option<ModelHandle<T>>;
  87
  88    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool;
  89
  90    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle>;
  91}
  92
  93pub trait UpgradeViewHandle {
  94    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>>;
  95}
  96
  97pub trait ReadView {
  98    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
  99}
 100
 101pub trait ReadViewWith {
 102    fn read_view_with<V, T>(
 103        &self,
 104        handle: &ViewHandle<V>,
 105        read: &mut dyn FnMut(&V, &AppContext) -> T,
 106    ) -> T
 107    where
 108        V: View;
 109}
 110
 111pub trait UpdateView {
 112    fn update_view<T, S>(
 113        &mut self,
 114        handle: &ViewHandle<T>,
 115        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 116    ) -> S
 117    where
 118        T: View;
 119}
 120
 121pub trait ElementStateContext: DerefMut<Target = MutableAppContext> {
 122    fn current_view_id(&self) -> usize;
 123
 124    fn element_state<Tag: 'static, T: 'static + Default>(
 125        &mut self,
 126        element_id: usize,
 127    ) -> ElementStateHandle<T> {
 128        let id = ElementStateId {
 129            view_id: self.current_view_id(),
 130            element_id,
 131            tag: TypeId::of::<Tag>(),
 132        };
 133        self.cx
 134            .element_states
 135            .entry(id)
 136            .or_insert_with(|| Box::new(T::default()));
 137        ElementStateHandle::new(id, self.frame_count, &self.cx.ref_counts)
 138    }
 139}
 140
 141pub trait Action: 'static + AnyAction {
 142    type Argument: 'static + Clone;
 143}
 144
 145pub trait AnyAction {
 146    fn id(&self) -> TypeId;
 147    fn name(&self) -> &'static str;
 148    fn as_any(&self) -> &dyn Any;
 149    fn boxed_clone(&self) -> Box<dyn AnyAction>;
 150    fn boxed_clone_as_any(&self) -> Box<dyn Any>;
 151}
 152
 153#[macro_export]
 154macro_rules! action {
 155    ($name:ident, $arg:ty) => {
 156        #[derive(Clone)]
 157        pub struct $name(pub $arg);
 158
 159        impl $crate::Action for $name {
 160            type Argument = $arg;
 161        }
 162
 163        impl $crate::AnyAction for $name {
 164            fn id(&self) -> std::any::TypeId {
 165                std::any::TypeId::of::<$name>()
 166            }
 167
 168            fn name(&self) -> &'static str {
 169                stringify!($name)
 170            }
 171
 172            fn as_any(&self) -> &dyn std::any::Any {
 173                self
 174            }
 175
 176            fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
 177                Box::new(self.clone())
 178            }
 179
 180            fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
 181                Box::new(self.clone())
 182            }
 183        }
 184    };
 185
 186    ($name:ident) => {
 187        #[derive(Clone, Debug, Eq, PartialEq)]
 188        pub struct $name;
 189
 190        impl $crate::Action for $name {
 191            type Argument = ();
 192        }
 193
 194        impl $crate::AnyAction for $name {
 195            fn id(&self) -> std::any::TypeId {
 196                std::any::TypeId::of::<$name>()
 197            }
 198
 199            fn name(&self) -> &'static str {
 200                stringify!($name)
 201            }
 202
 203            fn as_any(&self) -> &dyn std::any::Any {
 204                self
 205            }
 206
 207            fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
 208                Box::new(self.clone())
 209            }
 210
 211            fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
 212                Box::new(self.clone())
 213            }
 214        }
 215    };
 216}
 217
 218pub struct Menu<'a> {
 219    pub name: &'a str,
 220    pub items: Vec<MenuItem<'a>>,
 221}
 222
 223pub enum MenuItem<'a> {
 224    Action {
 225        name: &'a str,
 226        keystroke: Option<&'a str>,
 227        action: Box<dyn AnyAction>,
 228    },
 229    Separator,
 230}
 231
 232#[derive(Clone)]
 233pub struct App(Rc<RefCell<MutableAppContext>>);
 234
 235#[derive(Clone)]
 236pub struct AsyncAppContext(Rc<RefCell<MutableAppContext>>);
 237
 238#[derive(Clone)]
 239pub struct TestAppContext {
 240    cx: Rc<RefCell<MutableAppContext>>,
 241    foreground_platform: Rc<platform::test::ForegroundPlatform>,
 242}
 243
 244impl App {
 245    pub fn new(asset_source: impl AssetSource) -> Result<Self> {
 246        let platform = platform::current::platform();
 247        let foreground_platform = platform::current::foreground_platform();
 248        let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
 249        let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
 250            foreground,
 251            Arc::new(executor::Background::new()),
 252            platform.clone(),
 253            foreground_platform.clone(),
 254            Arc::new(FontCache::new(platform.fonts())),
 255            asset_source,
 256        ))));
 257
 258        foreground_platform.on_quit(Box::new({
 259            let cx = app.0.clone();
 260            move || {
 261                cx.borrow_mut().quit();
 262            }
 263        }));
 264        foreground_platform.on_menu_command(Box::new({
 265            let cx = app.0.clone();
 266            move |action| {
 267                let mut cx = cx.borrow_mut();
 268                if let Some(key_window_id) = cx.cx.platform.key_window_id() {
 269                    if let Some((presenter, _)) =
 270                        cx.presenters_and_platform_windows.get(&key_window_id)
 271                    {
 272                        let presenter = presenter.clone();
 273                        let path = presenter.borrow().dispatch_path(cx.as_ref());
 274                        cx.dispatch_action_any(key_window_id, &path, action);
 275                    } else {
 276                        cx.dispatch_global_action_any(action);
 277                    }
 278                } else {
 279                    cx.dispatch_global_action_any(action);
 280                }
 281            }
 282        }));
 283
 284        app.0.borrow_mut().weak_self = Some(Rc::downgrade(&app.0));
 285        Ok(app)
 286    }
 287
 288    pub fn background(&self) -> Arc<executor::Background> {
 289        self.0.borrow().background().clone()
 290    }
 291
 292    pub fn on_become_active<F>(self, mut callback: F) -> Self
 293    where
 294        F: 'static + FnMut(&mut MutableAppContext),
 295    {
 296        let cx = self.0.clone();
 297        self.0
 298            .borrow_mut()
 299            .foreground_platform
 300            .on_become_active(Box::new(move || callback(&mut *cx.borrow_mut())));
 301        self
 302    }
 303
 304    pub fn on_resign_active<F>(self, mut callback: F) -> Self
 305    where
 306        F: 'static + FnMut(&mut MutableAppContext),
 307    {
 308        let cx = self.0.clone();
 309        self.0
 310            .borrow_mut()
 311            .foreground_platform
 312            .on_resign_active(Box::new(move || callback(&mut *cx.borrow_mut())));
 313        self
 314    }
 315
 316    pub fn on_quit<F>(self, mut callback: F) -> Self
 317    where
 318        F: 'static + FnMut(&mut MutableAppContext),
 319    {
 320        let cx = self.0.clone();
 321        self.0
 322            .borrow_mut()
 323            .foreground_platform
 324            .on_quit(Box::new(move || callback(&mut *cx.borrow_mut())));
 325        self
 326    }
 327
 328    pub fn on_event<F>(self, mut callback: F) -> Self
 329    where
 330        F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
 331    {
 332        let cx = self.0.clone();
 333        self.0
 334            .borrow_mut()
 335            .foreground_platform
 336            .on_event(Box::new(move |event| {
 337                callback(event, &mut *cx.borrow_mut())
 338            }));
 339        self
 340    }
 341
 342    pub fn on_open_files<F>(self, mut callback: F) -> Self
 343    where
 344        F: 'static + FnMut(Vec<PathBuf>, &mut MutableAppContext),
 345    {
 346        let cx = self.0.clone();
 347        self.0
 348            .borrow_mut()
 349            .foreground_platform
 350            .on_open_files(Box::new(move |paths| {
 351                callback(paths, &mut *cx.borrow_mut())
 352            }));
 353        self
 354    }
 355
 356    pub fn run<F>(self, on_finish_launching: F)
 357    where
 358        F: 'static + FnOnce(&mut MutableAppContext),
 359    {
 360        let platform = self.0.borrow().foreground_platform.clone();
 361        platform.run(Box::new(move || {
 362            let mut cx = self.0.borrow_mut();
 363            let cx = &mut *cx;
 364            crate::views::init(cx);
 365            on_finish_launching(cx);
 366        }))
 367    }
 368
 369    pub fn platform(&self) -> Arc<dyn Platform> {
 370        self.0.borrow().platform()
 371    }
 372
 373    pub fn font_cache(&self) -> Arc<FontCache> {
 374        self.0.borrow().cx.font_cache.clone()
 375    }
 376
 377    fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 378        let mut state = self.0.borrow_mut();
 379        let result = state.update(callback);
 380        state.pending_notifications.clear();
 381        result
 382    }
 383}
 384
 385impl TestAppContext {
 386    pub fn new(
 387        foreground_platform: Rc<platform::test::ForegroundPlatform>,
 388        platform: Arc<dyn Platform>,
 389        foreground: Rc<executor::Foreground>,
 390        background: Arc<executor::Background>,
 391        font_cache: Arc<FontCache>,
 392        first_entity_id: usize,
 393    ) -> Self {
 394        let mut cx = MutableAppContext::new(
 395            foreground.clone(),
 396            background,
 397            platform,
 398            foreground_platform.clone(),
 399            font_cache,
 400            (),
 401        );
 402        cx.next_entity_id = first_entity_id;
 403        let cx = TestAppContext {
 404            cx: Rc::new(RefCell::new(cx)),
 405            foreground_platform,
 406        };
 407        cx.cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx.cx));
 408        cx
 409    }
 410
 411    pub fn dispatch_action<A: Action>(
 412        &self,
 413        window_id: usize,
 414        responder_chain: Vec<usize>,
 415        action: A,
 416    ) {
 417        self.cx
 418            .borrow_mut()
 419            .dispatch_action_any(window_id, &responder_chain, &action);
 420    }
 421
 422    pub fn dispatch_global_action<A: Action>(&self, action: A) {
 423        self.cx.borrow_mut().dispatch_global_action(action);
 424    }
 425
 426    pub fn dispatch_keystroke(
 427        &self,
 428        window_id: usize,
 429        responder_chain: Vec<usize>,
 430        keystroke: &Keystroke,
 431    ) -> Result<bool> {
 432        let mut state = self.cx.borrow_mut();
 433        state.dispatch_keystroke(window_id, responder_chain, keystroke)
 434    }
 435
 436    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
 437    where
 438        T: Entity,
 439        F: FnOnce(&mut ModelContext<T>) -> T,
 440    {
 441        self.cx.borrow_mut().add_model(build_model)
 442    }
 443
 444    pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
 445    where
 446        T: View,
 447        F: FnOnce(&mut ViewContext<T>) -> T,
 448    {
 449        self.cx
 450            .borrow_mut()
 451            .add_window(Default::default(), build_root_view)
 452    }
 453
 454    pub fn window_ids(&self) -> Vec<usize> {
 455        self.cx.borrow().window_ids().collect()
 456    }
 457
 458    pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
 459        self.cx.borrow().root_view(window_id)
 460    }
 461
 462    pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
 463    where
 464        T: View,
 465        F: FnOnce(&mut ViewContext<T>) -> T,
 466    {
 467        self.cx.borrow_mut().add_view(window_id, build_view)
 468    }
 469
 470    pub fn add_option_view<T, F>(
 471        &mut self,
 472        window_id: usize,
 473        build_view: F,
 474    ) -> Option<ViewHandle<T>>
 475    where
 476        T: View,
 477        F: FnOnce(&mut ViewContext<T>) -> Option<T>,
 478    {
 479        self.cx.borrow_mut().add_option_view(window_id, build_view)
 480    }
 481
 482    pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
 483        callback(self.cx.borrow().as_ref())
 484    }
 485
 486    pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 487        let mut state = self.cx.borrow_mut();
 488        // Don't increment pending flushes in order to effects to be flushed before the callback
 489        // completes, which is helpful in tests.
 490        let result = callback(&mut *state);
 491        // Flush effects after the callback just in case there are any. This can happen in edge
 492        // cases such as the closure dropping handles.
 493        state.flush_effects();
 494        result
 495    }
 496
 497    pub fn to_async(&self) -> AsyncAppContext {
 498        AsyncAppContext(self.cx.clone())
 499    }
 500
 501    pub fn font_cache(&self) -> Arc<FontCache> {
 502        self.cx.borrow().cx.font_cache.clone()
 503    }
 504
 505    pub fn foreground_platform(&self) -> Rc<platform::test::ForegroundPlatform> {
 506        self.foreground_platform.clone()
 507    }
 508
 509    pub fn platform(&self) -> Arc<dyn platform::Platform> {
 510        self.cx.borrow().cx.platform.clone()
 511    }
 512
 513    pub fn foreground(&self) -> Rc<executor::Foreground> {
 514        self.cx.borrow().foreground().clone()
 515    }
 516
 517    pub fn background(&self) -> Arc<executor::Background> {
 518        self.cx.borrow().background().clone()
 519    }
 520
 521    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
 522    where
 523        F: FnOnce(AsyncAppContext) -> Fut,
 524        Fut: 'static + Future<Output = T>,
 525        T: 'static,
 526    {
 527        self.cx.borrow_mut().spawn(f)
 528    }
 529
 530    pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
 531        self.foreground_platform.simulate_new_path_selection(result);
 532    }
 533
 534    pub fn did_prompt_for_new_path(&self) -> bool {
 535        self.foreground_platform.as_ref().did_prompt_for_new_path()
 536    }
 537
 538    pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) {
 539        let mut state = self.cx.borrow_mut();
 540        let (_, window) = state
 541            .presenters_and_platform_windows
 542            .get_mut(&window_id)
 543            .unwrap();
 544        let test_window = window
 545            .as_any_mut()
 546            .downcast_mut::<platform::test::Window>()
 547            .unwrap();
 548        let mut done_tx = test_window
 549            .last_prompt
 550            .take()
 551            .expect("prompt was not called");
 552        let _ = done_tx.try_send(answer);
 553    }
 554}
 555
 556impl AsyncAppContext {
 557    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
 558    where
 559        F: FnOnce(AsyncAppContext) -> Fut,
 560        Fut: 'static + Future<Output = T>,
 561        T: 'static,
 562    {
 563        self.0.borrow().foreground.spawn(f(self.clone()))
 564    }
 565
 566    pub fn read<T, F: FnOnce(&AppContext) -> T>(&mut self, callback: F) -> T {
 567        callback(self.0.borrow().as_ref())
 568    }
 569
 570    pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 571        self.0.borrow_mut().update(callback)
 572    }
 573
 574    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
 575    where
 576        T: Entity,
 577        F: FnOnce(&mut ModelContext<T>) -> T,
 578    {
 579        self.update(|cx| cx.add_model(build_model))
 580    }
 581
 582    pub fn platform(&self) -> Arc<dyn Platform> {
 583        self.0.borrow().platform()
 584    }
 585
 586    pub fn foreground(&self) -> Rc<executor::Foreground> {
 587        self.0.borrow().foreground.clone()
 588    }
 589
 590    pub fn background(&self) -> Arc<executor::Background> {
 591        self.0.borrow().cx.background.clone()
 592    }
 593}
 594
 595impl UpdateModel for AsyncAppContext {
 596    fn update_model<E: Entity, O>(
 597        &mut self,
 598        handle: &ModelHandle<E>,
 599        update: &mut dyn FnMut(&mut E, &mut ModelContext<E>) -> O,
 600    ) -> O {
 601        self.0.borrow_mut().update_model(handle, update)
 602    }
 603}
 604
 605impl UpgradeModelHandle for AsyncAppContext {
 606    fn upgrade_model_handle<T: Entity>(
 607        &self,
 608        handle: &WeakModelHandle<T>,
 609    ) -> Option<ModelHandle<T>> {
 610        self.0.borrow().upgrade_model_handle(handle)
 611    }
 612
 613    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
 614        self.0.borrow().model_handle_is_upgradable(handle)
 615    }
 616
 617    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
 618        self.0.borrow().upgrade_any_model_handle(handle)
 619    }
 620}
 621
 622impl UpgradeViewHandle for AsyncAppContext {
 623    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
 624        self.0.borrow_mut().upgrade_view_handle(handle)
 625    }
 626}
 627
 628impl ReadModelWith for AsyncAppContext {
 629    fn read_model_with<E: Entity, T>(
 630        &self,
 631        handle: &ModelHandle<E>,
 632        read: &mut dyn FnMut(&E, &AppContext) -> T,
 633    ) -> T {
 634        let cx = self.0.borrow();
 635        let cx = cx.as_ref();
 636        read(handle.read(cx), cx)
 637    }
 638}
 639
 640impl UpdateView for AsyncAppContext {
 641    fn update_view<T, S>(
 642        &mut self,
 643        handle: &ViewHandle<T>,
 644        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 645    ) -> S
 646    where
 647        T: View,
 648    {
 649        self.0.borrow_mut().update_view(handle, update)
 650    }
 651}
 652
 653impl ReadViewWith for AsyncAppContext {
 654    fn read_view_with<V, T>(
 655        &self,
 656        handle: &ViewHandle<V>,
 657        read: &mut dyn FnMut(&V, &AppContext) -> T,
 658    ) -> T
 659    where
 660        V: View,
 661    {
 662        let cx = self.0.borrow();
 663        let cx = cx.as_ref();
 664        read(handle.read(cx), cx)
 665    }
 666}
 667
 668impl UpdateModel for TestAppContext {
 669    fn update_model<T: Entity, O>(
 670        &mut self,
 671        handle: &ModelHandle<T>,
 672        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
 673    ) -> O {
 674        self.cx.borrow_mut().update_model(handle, update)
 675    }
 676}
 677
 678impl ReadModelWith for TestAppContext {
 679    fn read_model_with<E: Entity, T>(
 680        &self,
 681        handle: &ModelHandle<E>,
 682        read: &mut dyn FnMut(&E, &AppContext) -> T,
 683    ) -> T {
 684        let cx = self.cx.borrow();
 685        let cx = cx.as_ref();
 686        read(handle.read(cx), cx)
 687    }
 688}
 689
 690impl UpdateView for TestAppContext {
 691    fn update_view<T, S>(
 692        &mut self,
 693        handle: &ViewHandle<T>,
 694        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 695    ) -> S
 696    where
 697        T: View,
 698    {
 699        self.cx.borrow_mut().update_view(handle, update)
 700    }
 701}
 702
 703impl ReadViewWith for TestAppContext {
 704    fn read_view_with<V, T>(
 705        &self,
 706        handle: &ViewHandle<V>,
 707        read: &mut dyn FnMut(&V, &AppContext) -> T,
 708    ) -> T
 709    where
 710        V: View,
 711    {
 712        let cx = self.cx.borrow();
 713        let cx = cx.as_ref();
 714        read(handle.read(cx), cx)
 715    }
 716}
 717
 718type ActionCallback =
 719    dyn FnMut(&mut dyn AnyView, &dyn AnyAction, &mut MutableAppContext, usize, usize);
 720type GlobalActionCallback = dyn FnMut(&dyn AnyAction, &mut MutableAppContext);
 721
 722type SubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext) -> bool>;
 723type ObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
 724type ReleaseObservationCallback = Box<dyn FnMut(&mut MutableAppContext)>;
 725
 726pub struct MutableAppContext {
 727    weak_self: Option<rc::Weak<RefCell<Self>>>,
 728    foreground_platform: Rc<dyn platform::ForegroundPlatform>,
 729    assets: Arc<AssetCache>,
 730    cx: AppContext,
 731    capture_actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
 732    actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
 733    global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
 734    keystroke_matcher: keymap::Matcher,
 735    next_entity_id: usize,
 736    next_window_id: usize,
 737    next_subscription_id: usize,
 738    frame_count: usize,
 739    subscriptions: Arc<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>,
 740    observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ObservationCallback>>>>,
 741    release_observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>,
 742    presenters_and_platform_windows:
 743        HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
 744    debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
 745    foreground: Rc<executor::Foreground>,
 746    pending_effects: VecDeque<Effect>,
 747    pending_notifications: HashSet<usize>,
 748    pending_flushes: usize,
 749    flushing_effects: bool,
 750    next_cursor_style_handle_id: Arc<AtomicUsize>,
 751    halt_action_dispatch: bool,
 752}
 753
 754impl MutableAppContext {
 755    fn new(
 756        foreground: Rc<executor::Foreground>,
 757        background: Arc<executor::Background>,
 758        platform: Arc<dyn platform::Platform>,
 759        foreground_platform: Rc<dyn platform::ForegroundPlatform>,
 760        font_cache: Arc<FontCache>,
 761        asset_source: impl AssetSource,
 762        // entity_drop_tx:
 763    ) -> Self {
 764        Self {
 765            weak_self: None,
 766            foreground_platform,
 767            assets: Arc::new(AssetCache::new(asset_source)),
 768            cx: AppContext {
 769                models: Default::default(),
 770                views: Default::default(),
 771                windows: Default::default(),
 772                app_states: Default::default(),
 773                element_states: Default::default(),
 774                ref_counts: Arc::new(Mutex::new(RefCounts::default())),
 775                background,
 776                font_cache,
 777                platform,
 778            },
 779            capture_actions: HashMap::new(),
 780            actions: HashMap::new(),
 781            global_actions: HashMap::new(),
 782            keystroke_matcher: keymap::Matcher::default(),
 783            next_entity_id: 0,
 784            next_window_id: 0,
 785            next_subscription_id: 0,
 786            frame_count: 0,
 787            subscriptions: Default::default(),
 788            observations: Default::default(),
 789            release_observations: Default::default(),
 790            presenters_and_platform_windows: HashMap::new(),
 791            debug_elements_callbacks: HashMap::new(),
 792            foreground,
 793            pending_effects: VecDeque::new(),
 794            pending_notifications: HashSet::new(),
 795            pending_flushes: 0,
 796            flushing_effects: false,
 797            next_cursor_style_handle_id: Default::default(),
 798            halt_action_dispatch: false,
 799        }
 800    }
 801
 802    pub fn upgrade(&self) -> App {
 803        App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
 804    }
 805
 806    pub fn quit(&mut self) {
 807        let mut futures = Vec::new();
 808        for model_id in self.cx.models.keys().copied().collect::<Vec<_>>() {
 809            let mut model = self.cx.models.remove(&model_id).unwrap();
 810            futures.extend(model.app_will_quit(self));
 811            self.cx.models.insert(model_id, model);
 812        }
 813
 814        for view_id in self.cx.views.keys().copied().collect::<Vec<_>>() {
 815            let mut view = self.cx.views.remove(&view_id).unwrap();
 816            futures.extend(view.app_will_quit(self));
 817            self.cx.views.insert(view_id, view);
 818        }
 819
 820        self.remove_all_windows();
 821
 822        let futures = futures::future::join_all(futures);
 823        if self
 824            .background
 825            .block_with_timeout(Duration::from_millis(100), futures)
 826            .is_err()
 827        {
 828            log::error!("timed out waiting on app_will_quit");
 829        }
 830    }
 831
 832    fn remove_all_windows(&mut self) {
 833        for (window_id, _) in self.cx.windows.drain() {
 834            self.presenters_and_platform_windows.remove(&window_id);
 835        }
 836        self.remove_dropped_entities();
 837    }
 838
 839    pub fn platform(&self) -> Arc<dyn platform::Platform> {
 840        self.cx.platform.clone()
 841    }
 842
 843    pub fn font_cache(&self) -> &Arc<FontCache> {
 844        &self.cx.font_cache
 845    }
 846
 847    pub fn foreground(&self) -> &Rc<executor::Foreground> {
 848        &self.foreground
 849    }
 850
 851    pub fn background(&self) -> &Arc<executor::Background> {
 852        &self.cx.background
 853    }
 854
 855    pub fn on_debug_elements<F>(&mut self, window_id: usize, callback: F)
 856    where
 857        F: 'static + Fn(&AppContext) -> crate::json::Value,
 858    {
 859        self.debug_elements_callbacks
 860            .insert(window_id, Box::new(callback));
 861    }
 862
 863    pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
 864        self.debug_elements_callbacks
 865            .get(&window_id)
 866            .map(|debug_elements| debug_elements(&self.cx))
 867    }
 868
 869    pub fn add_action<A, V, F>(&mut self, handler: F)
 870    where
 871        A: Action,
 872        V: View,
 873        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 874    {
 875        self.add_action_internal(handler, false)
 876    }
 877
 878    pub fn capture_action<A, V, F>(&mut self, handler: F)
 879    where
 880        A: Action,
 881        V: View,
 882        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 883    {
 884        self.add_action_internal(handler, true)
 885    }
 886
 887    fn add_action_internal<A, V, F>(&mut self, mut handler: F, capture: bool)
 888    where
 889        A: Action,
 890        V: View,
 891        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 892    {
 893        let handler = Box::new(
 894            move |view: &mut dyn AnyView,
 895                  action: &dyn AnyAction,
 896                  cx: &mut MutableAppContext,
 897                  window_id: usize,
 898                  view_id: usize| {
 899                let action = action.as_any().downcast_ref().unwrap();
 900                let mut cx = ViewContext::new(cx, window_id, view_id);
 901                handler(
 902                    view.as_any_mut()
 903                        .downcast_mut()
 904                        .expect("downcast is type safe"),
 905                    action,
 906                    &mut cx,
 907                );
 908            },
 909        );
 910
 911        let actions = if capture {
 912            &mut self.capture_actions
 913        } else {
 914            &mut self.actions
 915        };
 916
 917        actions
 918            .entry(TypeId::of::<V>())
 919            .or_default()
 920            .entry(TypeId::of::<A>())
 921            .or_default()
 922            .push(handler);
 923    }
 924
 925    pub fn add_async_action<A, V, F>(&mut self, mut handler: F)
 926    where
 927        A: Action,
 928        V: View,
 929        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>) -> Option<Task<Result<()>>>,
 930    {
 931        self.add_action(move |view, action, cx| {
 932            handler(view, action, cx).map(|task| task.detach_and_log_err(cx));
 933        })
 934    }
 935
 936    pub fn add_global_action<A, F>(&mut self, mut handler: F)
 937    where
 938        A: Action,
 939        F: 'static + FnMut(&A, &mut MutableAppContext),
 940    {
 941        let handler = Box::new(move |action: &dyn AnyAction, cx: &mut MutableAppContext| {
 942            let action = action.as_any().downcast_ref().unwrap();
 943            handler(action, cx);
 944        });
 945
 946        if self
 947            .global_actions
 948            .insert(TypeId::of::<A>(), handler)
 949            .is_some()
 950        {
 951            panic!("registered multiple global handlers for the same action type");
 952        }
 953    }
 954
 955    pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
 956        self.cx.windows.keys().cloned()
 957    }
 958
 959    pub fn activate_window(&self, window_id: usize) {
 960        if let Some((_, window)) = self.presenters_and_platform_windows.get(&window_id) {
 961            window.activate()
 962        }
 963    }
 964
 965    pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
 966        self.cx
 967            .windows
 968            .get(&window_id)
 969            .and_then(|window| window.root_view.clone().downcast::<T>())
 970    }
 971
 972    pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
 973        self.cx.root_view_id(window_id)
 974    }
 975
 976    pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
 977        self.cx.focused_view_id(window_id)
 978    }
 979
 980    pub fn render_view(
 981        &mut self,
 982        window_id: usize,
 983        view_id: usize,
 984        titlebar_height: f32,
 985        refreshing: bool,
 986    ) -> Result<ElementBox> {
 987        let mut view = self
 988            .cx
 989            .views
 990            .remove(&(window_id, view_id))
 991            .ok_or(anyhow!("view not found"))?;
 992        let element = view.render(window_id, view_id, titlebar_height, refreshing, self);
 993        self.cx.views.insert((window_id, view_id), view);
 994        Ok(element)
 995    }
 996
 997    pub fn render_views(
 998        &mut self,
 999        window_id: usize,
1000        titlebar_height: f32,
1001    ) -> HashMap<usize, ElementBox> {
1002        self.start_frame();
1003        let view_ids = self
1004            .views
1005            .keys()
1006            .filter_map(|(win_id, view_id)| {
1007                if *win_id == window_id {
1008                    Some(*view_id)
1009                } else {
1010                    None
1011                }
1012            })
1013            .collect::<Vec<_>>();
1014        view_ids
1015            .into_iter()
1016            .map(|view_id| {
1017                (
1018                    view_id,
1019                    self.render_view(window_id, view_id, titlebar_height, false)
1020                        .unwrap(),
1021                )
1022            })
1023            .collect()
1024    }
1025
1026    pub(crate) fn start_frame(&mut self) {
1027        self.frame_count += 1;
1028    }
1029
1030    pub fn update<T, F: FnOnce(&mut Self) -> T>(&mut self, callback: F) -> T {
1031        self.pending_flushes += 1;
1032        let result = callback(self);
1033        self.flush_effects();
1034        result
1035    }
1036
1037    pub fn set_menus(&mut self, menus: Vec<Menu>) {
1038        self.foreground_platform.set_menus(menus);
1039    }
1040
1041    fn prompt(
1042        &self,
1043        window_id: usize,
1044        level: PromptLevel,
1045        msg: &str,
1046        answers: &[&str],
1047    ) -> oneshot::Receiver<usize> {
1048        let (_, window) = &self.presenters_and_platform_windows[&window_id];
1049        window.prompt(level, msg, answers)
1050    }
1051
1052    pub fn prompt_for_paths(
1053        &self,
1054        options: PathPromptOptions,
1055    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
1056        self.foreground_platform.prompt_for_paths(options)
1057    }
1058
1059    pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
1060        self.foreground_platform.prompt_for_new_path(directory)
1061    }
1062
1063    pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1064    where
1065        E: Entity,
1066        E::Event: 'static,
1067        H: Handle<E>,
1068        F: 'static + FnMut(H, &E::Event, &mut Self),
1069    {
1070        self.subscribe_internal(handle, move |handle, event, cx| {
1071            callback(handle, event, cx);
1072            true
1073        })
1074    }
1075
1076    pub fn observe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1077    where
1078        E: Entity,
1079        E::Event: 'static,
1080        H: Handle<E>,
1081        F: 'static + FnMut(H, &mut Self),
1082    {
1083        self.observe_internal(handle, move |handle, cx| {
1084            callback(handle, cx);
1085            true
1086        })
1087    }
1088
1089    pub fn subscribe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1090    where
1091        E: Entity,
1092        E::Event: 'static,
1093        H: Handle<E>,
1094        F: 'static + FnMut(H, &E::Event, &mut Self) -> bool,
1095    {
1096        let id = post_inc(&mut self.next_subscription_id);
1097        let emitter = handle.downgrade();
1098        self.subscriptions
1099            .lock()
1100            .entry(handle.id())
1101            .or_default()
1102            .insert(
1103                id,
1104                Box::new(move |payload, cx| {
1105                    if let Some(emitter) = H::upgrade_from(&emitter, cx.as_ref()) {
1106                        let payload = payload.downcast_ref().expect("downcast is type safe");
1107                        callback(emitter, payload, cx)
1108                    } else {
1109                        false
1110                    }
1111                }),
1112            );
1113        Subscription::Subscription {
1114            id,
1115            entity_id: handle.id(),
1116            subscriptions: Some(Arc::downgrade(&self.subscriptions)),
1117        }
1118    }
1119
1120    fn observe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1121    where
1122        E: Entity,
1123        E::Event: 'static,
1124        H: Handle<E>,
1125        F: 'static + FnMut(H, &mut Self) -> bool,
1126    {
1127        let id = post_inc(&mut self.next_subscription_id);
1128        let observed = handle.downgrade();
1129        self.observations
1130            .lock()
1131            .entry(handle.id())
1132            .or_default()
1133            .insert(
1134                id,
1135                Box::new(move |cx| {
1136                    if let Some(observed) = H::upgrade_from(&observed, cx) {
1137                        callback(observed, cx)
1138                    } else {
1139                        false
1140                    }
1141                }),
1142            );
1143        Subscription::Observation {
1144            id,
1145            entity_id: handle.id(),
1146            observations: Some(Arc::downgrade(&self.observations)),
1147        }
1148    }
1149
1150    pub fn observe_release<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1151    where
1152        E: Entity,
1153        E::Event: 'static,
1154        H: Handle<E>,
1155        F: 'static + FnMut(&mut Self),
1156    {
1157        let id = post_inc(&mut self.next_subscription_id);
1158        self.release_observations
1159            .lock()
1160            .entry(handle.id())
1161            .or_default()
1162            .insert(id, Box::new(move |cx| callback(cx)));
1163        Subscription::ReleaseObservation {
1164            id,
1165            entity_id: handle.id(),
1166            observations: Some(Arc::downgrade(&self.release_observations)),
1167        }
1168    }
1169
1170    fn defer(&mut self, callback: Box<dyn FnOnce(&mut MutableAppContext)>) {
1171        self.pending_effects.push_back(Effect::Deferred(callback))
1172    }
1173
1174    pub(crate) fn notify_model(&mut self, model_id: usize) {
1175        if self.pending_notifications.insert(model_id) {
1176            self.pending_effects
1177                .push_back(Effect::ModelNotification { model_id });
1178        }
1179    }
1180
1181    pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
1182        if self.pending_notifications.insert(view_id) {
1183            self.pending_effects
1184                .push_back(Effect::ViewNotification { window_id, view_id });
1185        }
1186    }
1187
1188    pub fn dispatch_action<A: Action>(
1189        &mut self,
1190        window_id: usize,
1191        responder_chain: Vec<usize>,
1192        action: &A,
1193    ) {
1194        self.dispatch_action_any(window_id, &responder_chain, action);
1195    }
1196
1197    pub(crate) fn dispatch_action_any(
1198        &mut self,
1199        window_id: usize,
1200        path: &[usize],
1201        action: &dyn AnyAction,
1202    ) -> bool {
1203        self.update(|this| {
1204            this.halt_action_dispatch = false;
1205            for (capture_phase, view_id) in path
1206                .iter()
1207                .map(|view_id| (true, *view_id))
1208                .chain(path.iter().rev().map(|view_id| (false, *view_id)))
1209            {
1210                if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
1211                    let type_id = view.as_any().type_id();
1212
1213                    if let Some((name, mut handlers)) = this
1214                        .actions_mut(capture_phase)
1215                        .get_mut(&type_id)
1216                        .and_then(|h| h.remove_entry(&action.id()))
1217                    {
1218                        for handler in handlers.iter_mut().rev() {
1219                            this.halt_action_dispatch = true;
1220                            handler(view.as_mut(), action, this, window_id, view_id);
1221                            if this.halt_action_dispatch {
1222                                break;
1223                            }
1224                        }
1225                        this.actions_mut(capture_phase)
1226                            .get_mut(&type_id)
1227                            .unwrap()
1228                            .insert(name, handlers);
1229                    }
1230
1231                    this.cx.views.insert((window_id, view_id), view);
1232
1233                    if this.halt_action_dispatch {
1234                        break;
1235                    }
1236                }
1237            }
1238
1239            if !this.halt_action_dispatch {
1240                this.dispatch_global_action_any(action);
1241            }
1242            this.halt_action_dispatch
1243        })
1244    }
1245
1246    fn actions_mut(
1247        &mut self,
1248        capture_phase: bool,
1249    ) -> &mut HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>> {
1250        if capture_phase {
1251            &mut self.capture_actions
1252        } else {
1253            &mut self.actions
1254        }
1255    }
1256
1257    pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
1258        self.dispatch_global_action_any(&action);
1259    }
1260
1261    fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) -> bool {
1262        self.update(|this| {
1263            if let Some((name, mut handler)) = this.global_actions.remove_entry(&action.id()) {
1264                handler(action, this);
1265                this.global_actions.insert(name, handler);
1266                true
1267            } else {
1268                false
1269            }
1270        })
1271    }
1272
1273    pub fn add_bindings<T: IntoIterator<Item = keymap::Binding>>(&mut self, bindings: T) {
1274        self.keystroke_matcher.add_bindings(bindings);
1275    }
1276
1277    pub fn dispatch_keystroke(
1278        &mut self,
1279        window_id: usize,
1280        responder_chain: Vec<usize>,
1281        keystroke: &Keystroke,
1282    ) -> Result<bool> {
1283        let mut context_chain = Vec::new();
1284        for view_id in &responder_chain {
1285            if let Some(view) = self.cx.views.get(&(window_id, *view_id)) {
1286                context_chain.push(view.keymap_context(self.as_ref()));
1287            } else {
1288                return Err(anyhow!(
1289                    "View {} in responder chain does not exist",
1290                    view_id
1291                ));
1292            }
1293        }
1294
1295        let mut pending = false;
1296        for (i, cx) in context_chain.iter().enumerate().rev() {
1297            match self
1298                .keystroke_matcher
1299                .push_keystroke(keystroke.clone(), responder_chain[i], cx)
1300            {
1301                MatchResult::None => {}
1302                MatchResult::Pending => pending = true,
1303                MatchResult::Action(action) => {
1304                    if self.dispatch_action_any(window_id, &responder_chain[0..=i], action.as_ref())
1305                    {
1306                        self.keystroke_matcher.clear_pending();
1307                        return Ok(true);
1308                    }
1309                }
1310            }
1311        }
1312
1313        Ok(pending)
1314    }
1315
1316    pub fn add_app_state<T: 'static>(&mut self, state: T) {
1317        self.cx
1318            .app_states
1319            .insert(TypeId::of::<T>(), Box::new(state));
1320    }
1321
1322    pub fn update_app_state<T: 'static, F, U>(&mut self, update: F) -> U
1323    where
1324        F: FnOnce(&mut T, &mut MutableAppContext) -> U,
1325    {
1326        let type_id = TypeId::of::<T>();
1327        let mut state = self
1328            .cx
1329            .app_states
1330            .remove(&type_id)
1331            .expect("no app state has been added for this type");
1332        let result = update(state.downcast_mut().unwrap(), self);
1333        self.cx.app_states.insert(type_id, state);
1334        result
1335    }
1336
1337    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
1338    where
1339        T: Entity,
1340        F: FnOnce(&mut ModelContext<T>) -> T,
1341    {
1342        self.update(|this| {
1343            let model_id = post_inc(&mut this.next_entity_id);
1344            let handle = ModelHandle::new(model_id, &this.cx.ref_counts);
1345            let mut cx = ModelContext::new(this, model_id);
1346            let model = build_model(&mut cx);
1347            this.cx.models.insert(model_id, Box::new(model));
1348            handle
1349        })
1350    }
1351
1352    pub fn add_window<T, F>(
1353        &mut self,
1354        window_options: WindowOptions,
1355        build_root_view: F,
1356    ) -> (usize, ViewHandle<T>)
1357    where
1358        T: View,
1359        F: FnOnce(&mut ViewContext<T>) -> T,
1360    {
1361        self.update(|this| {
1362            let window_id = post_inc(&mut this.next_window_id);
1363            let root_view = this.add_view(window_id, build_root_view);
1364
1365            this.cx.windows.insert(
1366                window_id,
1367                Window {
1368                    root_view: root_view.clone().into(),
1369                    focused_view_id: root_view.id(),
1370                    invalidation: None,
1371                },
1372            );
1373            this.open_platform_window(window_id, window_options);
1374            root_view.update(this, |view, cx| {
1375                view.on_focus(cx);
1376                cx.notify();
1377            });
1378
1379            (window_id, root_view)
1380        })
1381    }
1382
1383    pub fn remove_window(&mut self, window_id: usize) {
1384        self.cx.windows.remove(&window_id);
1385        self.presenters_and_platform_windows.remove(&window_id);
1386        self.remove_dropped_entities();
1387        self.flush_effects();
1388    }
1389
1390    fn open_platform_window(&mut self, window_id: usize, window_options: WindowOptions) {
1391        let mut window =
1392            self.cx
1393                .platform
1394                .open_window(window_id, window_options, self.foreground.clone());
1395        let presenter = Rc::new(RefCell::new(
1396            self.build_presenter(window_id, window.titlebar_height()),
1397        ));
1398
1399        {
1400            let mut app = self.upgrade();
1401            let presenter = presenter.clone();
1402            window.on_event(Box::new(move |event| {
1403                app.update(|cx| {
1404                    if let Event::KeyDown { keystroke, .. } = &event {
1405                        if cx
1406                            .dispatch_keystroke(
1407                                window_id,
1408                                presenter.borrow().dispatch_path(cx.as_ref()),
1409                                keystroke,
1410                            )
1411                            .unwrap()
1412                        {
1413                            return;
1414                        }
1415                    }
1416
1417                    presenter.borrow_mut().dispatch_event(event, cx);
1418                })
1419            }));
1420        }
1421
1422        {
1423            let mut app = self.upgrade();
1424            window.on_resize(Box::new(move || {
1425                app.update(|cx| cx.resize_window(window_id))
1426            }));
1427        }
1428
1429        {
1430            let mut app = self.upgrade();
1431            window.on_close(Box::new(move || {
1432                app.update(|cx| cx.remove_window(window_id));
1433            }));
1434        }
1435
1436        self.presenters_and_platform_windows
1437            .insert(window_id, (presenter.clone(), window));
1438
1439        self.on_debug_elements(window_id, move |cx| {
1440            presenter.borrow().debug_elements(cx).unwrap()
1441        });
1442    }
1443
1444    pub fn build_presenter(&mut self, window_id: usize, titlebar_height: f32) -> Presenter {
1445        Presenter::new(
1446            window_id,
1447            titlebar_height,
1448            self.cx.font_cache.clone(),
1449            TextLayoutCache::new(self.cx.platform.fonts()),
1450            self.assets.clone(),
1451            self,
1452        )
1453    }
1454
1455    pub fn build_render_context<V: View>(
1456        &mut self,
1457        window_id: usize,
1458        view_id: usize,
1459        titlebar_height: f32,
1460        refreshing: bool,
1461    ) -> RenderContext<V> {
1462        RenderContext {
1463            app: self,
1464            titlebar_height,
1465            refreshing,
1466            window_id,
1467            view_id,
1468            view_type: PhantomData,
1469        }
1470    }
1471
1472    pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
1473    where
1474        T: View,
1475        F: FnOnce(&mut ViewContext<T>) -> T,
1476    {
1477        self.add_option_view(window_id, |cx| Some(build_view(cx)))
1478            .unwrap()
1479    }
1480
1481    pub fn add_option_view<T, F>(
1482        &mut self,
1483        window_id: usize,
1484        build_view: F,
1485    ) -> Option<ViewHandle<T>>
1486    where
1487        T: View,
1488        F: FnOnce(&mut ViewContext<T>) -> Option<T>,
1489    {
1490        self.update(|this| {
1491            let view_id = post_inc(&mut this.next_entity_id);
1492            let mut cx = ViewContext::new(this, window_id, view_id);
1493            let handle = if let Some(view) = build_view(&mut cx) {
1494                this.cx.views.insert((window_id, view_id), Box::new(view));
1495                if let Some(window) = this.cx.windows.get_mut(&window_id) {
1496                    window
1497                        .invalidation
1498                        .get_or_insert_with(Default::default)
1499                        .updated
1500                        .insert(view_id);
1501                }
1502                Some(ViewHandle::new(window_id, view_id, &this.cx.ref_counts))
1503            } else {
1504                None
1505            };
1506            handle
1507        })
1508    }
1509
1510    fn remove_dropped_entities(&mut self) {
1511        loop {
1512            let (dropped_models, dropped_views, dropped_element_states) =
1513                self.cx.ref_counts.lock().take_dropped();
1514            if dropped_models.is_empty()
1515                && dropped_views.is_empty()
1516                && dropped_element_states.is_empty()
1517            {
1518                break;
1519            }
1520
1521            for model_id in dropped_models {
1522                self.subscriptions.lock().remove(&model_id);
1523                self.observations.lock().remove(&model_id);
1524                let mut model = self.cx.models.remove(&model_id).unwrap();
1525                model.release(self);
1526                self.pending_effects.push_back(Effect::Release {
1527                    entity_id: model_id,
1528                });
1529            }
1530
1531            for (window_id, view_id) in dropped_views {
1532                self.subscriptions.lock().remove(&view_id);
1533                self.observations.lock().remove(&view_id);
1534                let mut view = self.cx.views.remove(&(window_id, view_id)).unwrap();
1535                view.release(self);
1536                let change_focus_to = self.cx.windows.get_mut(&window_id).and_then(|window| {
1537                    window
1538                        .invalidation
1539                        .get_or_insert_with(Default::default)
1540                        .removed
1541                        .push(view_id);
1542                    if window.focused_view_id == view_id {
1543                        Some(window.root_view.id())
1544                    } else {
1545                        None
1546                    }
1547                });
1548
1549                if let Some(view_id) = change_focus_to {
1550                    self.focus(window_id, view_id);
1551                }
1552
1553                self.pending_effects
1554                    .push_back(Effect::Release { entity_id: view_id });
1555            }
1556
1557            for key in dropped_element_states {
1558                self.cx.element_states.remove(&key);
1559            }
1560        }
1561    }
1562
1563    fn flush_effects(&mut self) {
1564        self.pending_flushes = self.pending_flushes.saturating_sub(1);
1565
1566        if !self.flushing_effects && self.pending_flushes == 0 {
1567            self.flushing_effects = true;
1568
1569            let mut refreshing = false;
1570            loop {
1571                if let Some(effect) = self.pending_effects.pop_front() {
1572                    match effect {
1573                        Effect::Event { entity_id, payload } => self.emit_event(entity_id, payload),
1574                        Effect::ModelNotification { model_id } => {
1575                            self.notify_model_observers(model_id)
1576                        }
1577                        Effect::ViewNotification { window_id, view_id } => {
1578                            self.notify_view_observers(window_id, view_id)
1579                        }
1580                        Effect::Deferred(callback) => callback(self),
1581                        Effect::Release { entity_id } => self.notify_release_observers(entity_id),
1582                        Effect::Focus { window_id, view_id } => {
1583                            self.focus(window_id, view_id);
1584                        }
1585                        Effect::ResizeWindow { window_id } => {
1586                            if let Some(window) = self.cx.windows.get_mut(&window_id) {
1587                                window
1588                                    .invalidation
1589                                    .get_or_insert(WindowInvalidation::default());
1590                            }
1591                        }
1592                        Effect::RefreshWindows => {
1593                            refreshing = true;
1594                        }
1595                    }
1596                    self.pending_notifications.clear();
1597                    self.remove_dropped_entities();
1598                } else {
1599                    self.remove_dropped_entities();
1600                    if refreshing {
1601                        self.perform_window_refresh();
1602                    } else {
1603                        self.update_windows();
1604                    }
1605
1606                    if self.pending_effects.is_empty() {
1607                        self.flushing_effects = false;
1608                        self.pending_notifications.clear();
1609                        break;
1610                    } else {
1611                        refreshing = false;
1612                    }
1613                }
1614            }
1615        }
1616    }
1617
1618    fn update_windows(&mut self) {
1619        let mut invalidations = HashMap::new();
1620        for (window_id, window) in &mut self.cx.windows {
1621            if let Some(invalidation) = window.invalidation.take() {
1622                invalidations.insert(*window_id, invalidation);
1623            }
1624        }
1625
1626        for (window_id, invalidation) in invalidations {
1627            if let Some((presenter, mut window)) =
1628                self.presenters_and_platform_windows.remove(&window_id)
1629            {
1630                {
1631                    let mut presenter = presenter.borrow_mut();
1632                    presenter.invalidate(invalidation, self);
1633                    let scene =
1634                        presenter.build_scene(window.size(), window.scale_factor(), false, self);
1635                    window.present_scene(scene);
1636                }
1637                self.presenters_and_platform_windows
1638                    .insert(window_id, (presenter, window));
1639            }
1640        }
1641    }
1642
1643    fn resize_window(&mut self, window_id: usize) {
1644        self.pending_effects
1645            .push_back(Effect::ResizeWindow { window_id });
1646    }
1647
1648    pub fn refresh_windows(&mut self) {
1649        self.pending_effects.push_back(Effect::RefreshWindows);
1650    }
1651
1652    fn perform_window_refresh(&mut self) {
1653        let mut presenters = mem::take(&mut self.presenters_and_platform_windows);
1654        for (window_id, (presenter, window)) in &mut presenters {
1655            let invalidation = self
1656                .cx
1657                .windows
1658                .get_mut(&window_id)
1659                .unwrap()
1660                .invalidation
1661                .take();
1662            let mut presenter = presenter.borrow_mut();
1663            presenter.refresh(invalidation, self);
1664            let scene = presenter.build_scene(window.size(), window.scale_factor(), true, self);
1665            window.present_scene(scene);
1666        }
1667        self.presenters_and_platform_windows = presenters;
1668    }
1669
1670    pub fn set_cursor_style(&mut self, style: CursorStyle) -> CursorStyleHandle {
1671        self.platform.set_cursor_style(style);
1672        let id = self.next_cursor_style_handle_id.fetch_add(1, SeqCst);
1673        CursorStyleHandle {
1674            id,
1675            next_cursor_style_handle_id: self.next_cursor_style_handle_id.clone(),
1676            platform: self.platform(),
1677        }
1678    }
1679
1680    fn emit_event(&mut self, entity_id: usize, payload: Box<dyn Any>) {
1681        let callbacks = self.subscriptions.lock().remove(&entity_id);
1682        if let Some(callbacks) = callbacks {
1683            for (id, mut callback) in callbacks {
1684                let alive = callback(payload.as_ref(), self);
1685                if alive {
1686                    self.subscriptions
1687                        .lock()
1688                        .entry(entity_id)
1689                        .or_default()
1690                        .insert(id, callback);
1691                }
1692            }
1693        }
1694    }
1695
1696    fn notify_model_observers(&mut self, observed_id: usize) {
1697        let callbacks = self.observations.lock().remove(&observed_id);
1698        if let Some(callbacks) = callbacks {
1699            if self.cx.models.contains_key(&observed_id) {
1700                for (id, mut callback) in callbacks {
1701                    let alive = callback(self);
1702                    if alive {
1703                        self.observations
1704                            .lock()
1705                            .entry(observed_id)
1706                            .or_default()
1707                            .insert(id, callback);
1708                    }
1709                }
1710            }
1711        }
1712    }
1713
1714    fn notify_view_observers(&mut self, observed_window_id: usize, observed_view_id: usize) {
1715        if let Some(window) = self.cx.windows.get_mut(&observed_window_id) {
1716            window
1717                .invalidation
1718                .get_or_insert_with(Default::default)
1719                .updated
1720                .insert(observed_view_id);
1721        }
1722
1723        let callbacks = self.observations.lock().remove(&observed_view_id);
1724        if let Some(callbacks) = callbacks {
1725            if self
1726                .cx
1727                .views
1728                .contains_key(&(observed_window_id, observed_view_id))
1729            {
1730                for (id, mut callback) in callbacks {
1731                    let alive = callback(self);
1732                    if alive {
1733                        self.observations
1734                            .lock()
1735                            .entry(observed_view_id)
1736                            .or_default()
1737                            .insert(id, callback);
1738                    }
1739                }
1740            }
1741        }
1742    }
1743
1744    fn notify_release_observers(&mut self, entity_id: usize) {
1745        let callbacks = self.release_observations.lock().remove(&entity_id);
1746        if let Some(callbacks) = callbacks {
1747            for (_, mut callback) in callbacks {
1748                callback(self);
1749            }
1750        }
1751    }
1752
1753    fn focus(&mut self, window_id: usize, focused_id: usize) {
1754        if self
1755            .cx
1756            .windows
1757            .get(&window_id)
1758            .map(|w| w.focused_view_id)
1759            .map_or(false, |cur_focused| cur_focused == focused_id)
1760        {
1761            return;
1762        }
1763
1764        self.update(|this| {
1765            let blurred_id = this.cx.windows.get_mut(&window_id).map(|window| {
1766                let blurred_id = window.focused_view_id;
1767                window.focused_view_id = focused_id;
1768                blurred_id
1769            });
1770
1771            if let Some(blurred_id) = blurred_id {
1772                if let Some(mut blurred_view) = this.cx.views.remove(&(window_id, blurred_id)) {
1773                    blurred_view.on_blur(this, window_id, blurred_id);
1774                    this.cx.views.insert((window_id, blurred_id), blurred_view);
1775                }
1776            }
1777
1778            if let Some(mut focused_view) = this.cx.views.remove(&(window_id, focused_id)) {
1779                focused_view.on_focus(this, window_id, focused_id);
1780                this.cx.views.insert((window_id, focused_id), focused_view);
1781            }
1782        })
1783    }
1784
1785    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
1786    where
1787        F: FnOnce(AsyncAppContext) -> Fut,
1788        Fut: 'static + Future<Output = T>,
1789        T: 'static,
1790    {
1791        let future = f(self.to_async());
1792        let cx = self.to_async();
1793        self.foreground.spawn(async move {
1794            let result = future.await;
1795            cx.0.borrow_mut().flush_effects();
1796            result
1797        })
1798    }
1799
1800    pub fn to_async(&self) -> AsyncAppContext {
1801        AsyncAppContext(self.weak_self.as_ref().unwrap().upgrade().unwrap())
1802    }
1803
1804    pub fn write_to_clipboard(&self, item: ClipboardItem) {
1805        self.cx.platform.write_to_clipboard(item);
1806    }
1807
1808    pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
1809        self.cx.platform.read_from_clipboard()
1810    }
1811}
1812
1813impl ReadModel for MutableAppContext {
1814    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
1815        if let Some(model) = self.cx.models.get(&handle.model_id) {
1816            model
1817                .as_any()
1818                .downcast_ref()
1819                .expect("downcast is type safe")
1820        } else {
1821            panic!("circular model reference");
1822        }
1823    }
1824}
1825
1826impl UpdateModel for MutableAppContext {
1827    fn update_model<T: Entity, V>(
1828        &mut self,
1829        handle: &ModelHandle<T>,
1830        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
1831    ) -> V {
1832        if let Some(mut model) = self.cx.models.remove(&handle.model_id) {
1833            self.update(|this| {
1834                let mut cx = ModelContext::new(this, handle.model_id);
1835                let result = update(
1836                    model
1837                        .as_any_mut()
1838                        .downcast_mut()
1839                        .expect("downcast is type safe"),
1840                    &mut cx,
1841                );
1842                this.cx.models.insert(handle.model_id, model);
1843                result
1844            })
1845        } else {
1846            panic!("circular model update");
1847        }
1848    }
1849}
1850
1851impl UpgradeModelHandle for MutableAppContext {
1852    fn upgrade_model_handle<T: Entity>(
1853        &self,
1854        handle: &WeakModelHandle<T>,
1855    ) -> Option<ModelHandle<T>> {
1856        self.cx.upgrade_model_handle(handle)
1857    }
1858
1859    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
1860        self.cx.model_handle_is_upgradable(handle)
1861    }
1862
1863    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
1864        self.cx.upgrade_any_model_handle(handle)
1865    }
1866}
1867
1868impl UpgradeViewHandle for MutableAppContext {
1869    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
1870        self.cx.upgrade_view_handle(handle)
1871    }
1872}
1873
1874impl ReadView for MutableAppContext {
1875    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
1876        if let Some(view) = self.cx.views.get(&(handle.window_id, handle.view_id)) {
1877            view.as_any().downcast_ref().expect("downcast is type safe")
1878        } else {
1879            panic!("circular view reference");
1880        }
1881    }
1882}
1883
1884impl UpdateView for MutableAppContext {
1885    fn update_view<T, S>(
1886        &mut self,
1887        handle: &ViewHandle<T>,
1888        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
1889    ) -> S
1890    where
1891        T: View,
1892    {
1893        self.update(|this| {
1894            let mut view = this
1895                .cx
1896                .views
1897                .remove(&(handle.window_id, handle.view_id))
1898                .expect("circular view update");
1899
1900            let mut cx = ViewContext::new(this, handle.window_id, handle.view_id);
1901            let result = update(
1902                view.as_any_mut()
1903                    .downcast_mut()
1904                    .expect("downcast is type safe"),
1905                &mut cx,
1906            );
1907            this.cx
1908                .views
1909                .insert((handle.window_id, handle.view_id), view);
1910            result
1911        })
1912    }
1913}
1914
1915impl AsRef<AppContext> for MutableAppContext {
1916    fn as_ref(&self) -> &AppContext {
1917        &self.cx
1918    }
1919}
1920
1921impl Deref for MutableAppContext {
1922    type Target = AppContext;
1923
1924    fn deref(&self) -> &Self::Target {
1925        &self.cx
1926    }
1927}
1928
1929pub struct AppContext {
1930    models: HashMap<usize, Box<dyn AnyModel>>,
1931    views: HashMap<(usize, usize), Box<dyn AnyView>>,
1932    windows: HashMap<usize, Window>,
1933    app_states: HashMap<TypeId, Box<dyn Any>>,
1934    element_states: HashMap<ElementStateId, Box<dyn Any>>,
1935    background: Arc<executor::Background>,
1936    ref_counts: Arc<Mutex<RefCounts>>,
1937    font_cache: Arc<FontCache>,
1938    platform: Arc<dyn Platform>,
1939}
1940
1941impl AppContext {
1942    pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
1943        self.windows
1944            .get(&window_id)
1945            .map(|window| window.root_view.id())
1946    }
1947
1948    pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
1949        self.windows
1950            .get(&window_id)
1951            .map(|window| window.focused_view_id)
1952    }
1953
1954    pub fn background(&self) -> &Arc<executor::Background> {
1955        &self.background
1956    }
1957
1958    pub fn font_cache(&self) -> &Arc<FontCache> {
1959        &self.font_cache
1960    }
1961
1962    pub fn platform(&self) -> &Arc<dyn Platform> {
1963        &self.platform
1964    }
1965
1966    pub fn app_state<T: 'static>(&self) -> &T {
1967        self.app_states
1968            .get(&TypeId::of::<T>())
1969            .expect("no app state has been added for this type")
1970            .downcast_ref()
1971            .unwrap()
1972    }
1973}
1974
1975impl ReadModel for AppContext {
1976    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
1977        if let Some(model) = self.models.get(&handle.model_id) {
1978            model
1979                .as_any()
1980                .downcast_ref()
1981                .expect("downcast should be type safe")
1982        } else {
1983            panic!("circular model reference");
1984        }
1985    }
1986}
1987
1988impl UpgradeModelHandle for AppContext {
1989    fn upgrade_model_handle<T: Entity>(
1990        &self,
1991        handle: &WeakModelHandle<T>,
1992    ) -> Option<ModelHandle<T>> {
1993        if self.models.contains_key(&handle.model_id) {
1994            Some(ModelHandle::new(handle.model_id, &self.ref_counts))
1995        } else {
1996            None
1997        }
1998    }
1999
2000    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
2001        self.models.contains_key(&handle.model_id)
2002    }
2003
2004    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
2005        if self.models.contains_key(&handle.model_id) {
2006            self.ref_counts.lock().inc_model(handle.model_id);
2007            Some(AnyModelHandle {
2008                model_id: handle.model_id,
2009                model_type: handle.model_type,
2010                ref_counts: self.ref_counts.clone(),
2011            })
2012        } else {
2013            None
2014        }
2015    }
2016}
2017
2018impl UpgradeViewHandle for AppContext {
2019    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
2020        if self.ref_counts.lock().is_entity_alive(handle.view_id) {
2021            Some(ViewHandle::new(
2022                handle.window_id,
2023                handle.view_id,
2024                &self.ref_counts,
2025            ))
2026        } else {
2027            None
2028        }
2029    }
2030}
2031
2032impl ReadView for AppContext {
2033    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2034        if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
2035            view.as_any()
2036                .downcast_ref()
2037                .expect("downcast should be type safe")
2038        } else {
2039            panic!("circular view reference");
2040        }
2041    }
2042}
2043
2044struct Window {
2045    root_view: AnyViewHandle,
2046    focused_view_id: usize,
2047    invalidation: Option<WindowInvalidation>,
2048}
2049
2050#[derive(Default, Clone)]
2051pub struct WindowInvalidation {
2052    pub updated: HashSet<usize>,
2053    pub removed: Vec<usize>,
2054}
2055
2056pub enum Effect {
2057    Event {
2058        entity_id: usize,
2059        payload: Box<dyn Any>,
2060    },
2061    ModelNotification {
2062        model_id: usize,
2063    },
2064    ViewNotification {
2065        window_id: usize,
2066        view_id: usize,
2067    },
2068    Deferred(Box<dyn FnOnce(&mut MutableAppContext)>),
2069    Release {
2070        entity_id: usize,
2071    },
2072    Focus {
2073        window_id: usize,
2074        view_id: usize,
2075    },
2076    ResizeWindow {
2077        window_id: usize,
2078    },
2079    RefreshWindows,
2080}
2081
2082impl Debug for Effect {
2083    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2084        match self {
2085            Effect::Event { entity_id, .. } => f
2086                .debug_struct("Effect::Event")
2087                .field("entity_id", entity_id)
2088                .finish(),
2089            Effect::ModelNotification { model_id } => f
2090                .debug_struct("Effect::ModelNotification")
2091                .field("model_id", model_id)
2092                .finish(),
2093            Effect::ViewNotification { window_id, view_id } => f
2094                .debug_struct("Effect::ViewNotification")
2095                .field("window_id", window_id)
2096                .field("view_id", view_id)
2097                .finish(),
2098            Effect::Deferred(_) => f.debug_struct("Effect::Deferred").finish(),
2099            Effect::Release { entity_id } => f
2100                .debug_struct("Effect::Release")
2101                .field("entity_id", entity_id)
2102                .finish(),
2103            Effect::Focus { window_id, view_id } => f
2104                .debug_struct("Effect::Focus")
2105                .field("window_id", window_id)
2106                .field("view_id", view_id)
2107                .finish(),
2108            Effect::ResizeWindow { window_id } => f
2109                .debug_struct("Effect::RefreshWindow")
2110                .field("window_id", window_id)
2111                .finish(),
2112            Effect::RefreshWindows => f.debug_struct("Effect::FullViewRefresh").finish(),
2113        }
2114    }
2115}
2116
2117pub trait AnyModel {
2118    fn as_any(&self) -> &dyn Any;
2119    fn as_any_mut(&mut self) -> &mut dyn Any;
2120    fn release(&mut self, cx: &mut MutableAppContext);
2121    fn app_will_quit(
2122        &mut self,
2123        cx: &mut MutableAppContext,
2124    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
2125}
2126
2127impl<T> AnyModel for T
2128where
2129    T: Entity,
2130{
2131    fn as_any(&self) -> &dyn Any {
2132        self
2133    }
2134
2135    fn as_any_mut(&mut self) -> &mut dyn Any {
2136        self
2137    }
2138
2139    fn release(&mut self, cx: &mut MutableAppContext) {
2140        self.release(cx);
2141    }
2142
2143    fn app_will_quit(
2144        &mut self,
2145        cx: &mut MutableAppContext,
2146    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
2147        self.app_will_quit(cx)
2148    }
2149}
2150
2151pub trait AnyView {
2152    fn as_any(&self) -> &dyn Any;
2153    fn as_any_mut(&mut self) -> &mut dyn Any;
2154    fn release(&mut self, cx: &mut MutableAppContext);
2155    fn app_will_quit(
2156        &mut self,
2157        cx: &mut MutableAppContext,
2158    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
2159    fn ui_name(&self) -> &'static str;
2160    fn render<'a>(
2161        &mut self,
2162        window_id: usize,
2163        view_id: usize,
2164        titlebar_height: f32,
2165        refreshing: bool,
2166        cx: &mut MutableAppContext,
2167    ) -> ElementBox;
2168    fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
2169    fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
2170    fn keymap_context(&self, cx: &AppContext) -> keymap::Context;
2171}
2172
2173impl<T> AnyView for T
2174where
2175    T: View,
2176{
2177    fn as_any(&self) -> &dyn Any {
2178        self
2179    }
2180
2181    fn as_any_mut(&mut self) -> &mut dyn Any {
2182        self
2183    }
2184
2185    fn release(&mut self, cx: &mut MutableAppContext) {
2186        self.release(cx);
2187    }
2188
2189    fn app_will_quit(
2190        &mut self,
2191        cx: &mut MutableAppContext,
2192    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
2193        self.app_will_quit(cx)
2194    }
2195
2196    fn ui_name(&self) -> &'static str {
2197        T::ui_name()
2198    }
2199
2200    fn render<'a>(
2201        &mut self,
2202        window_id: usize,
2203        view_id: usize,
2204        titlebar_height: f32,
2205        refreshing: bool,
2206        cx: &mut MutableAppContext,
2207    ) -> ElementBox {
2208        View::render(
2209            self,
2210            &mut RenderContext {
2211                window_id,
2212                view_id,
2213                app: cx,
2214                view_type: PhantomData::<T>,
2215                titlebar_height,
2216                refreshing,
2217            },
2218        )
2219    }
2220
2221    fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
2222        let mut cx = ViewContext::new(cx, window_id, view_id);
2223        View::on_focus(self, &mut cx);
2224    }
2225
2226    fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
2227        let mut cx = ViewContext::new(cx, window_id, view_id);
2228        View::on_blur(self, &mut cx);
2229    }
2230
2231    fn keymap_context(&self, cx: &AppContext) -> keymap::Context {
2232        View::keymap_context(self, cx)
2233    }
2234}
2235
2236pub struct ModelContext<'a, T: ?Sized> {
2237    app: &'a mut MutableAppContext,
2238    model_id: usize,
2239    model_type: PhantomData<T>,
2240    halt_stream: bool,
2241}
2242
2243impl<'a, T: Entity> ModelContext<'a, T> {
2244    fn new(app: &'a mut MutableAppContext, model_id: usize) -> Self {
2245        Self {
2246            app,
2247            model_id,
2248            model_type: PhantomData,
2249            halt_stream: false,
2250        }
2251    }
2252
2253    pub fn background(&self) -> &Arc<executor::Background> {
2254        &self.app.cx.background
2255    }
2256
2257    pub fn halt_stream(&mut self) {
2258        self.halt_stream = true;
2259    }
2260
2261    pub fn model_id(&self) -> usize {
2262        self.model_id
2263    }
2264
2265    pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
2266    where
2267        S: Entity,
2268        F: FnOnce(&mut ModelContext<S>) -> S,
2269    {
2270        self.app.add_model(build_model)
2271    }
2272
2273    pub fn emit(&mut self, payload: T::Event) {
2274        self.app.pending_effects.push_back(Effect::Event {
2275            entity_id: self.model_id,
2276            payload: Box::new(payload),
2277        });
2278    }
2279
2280    pub fn notify(&mut self) {
2281        self.app.notify_model(self.model_id);
2282    }
2283
2284    pub fn subscribe<S: Entity, F>(
2285        &mut self,
2286        handle: &ModelHandle<S>,
2287        mut callback: F,
2288    ) -> Subscription
2289    where
2290        S::Event: 'static,
2291        F: 'static + FnMut(&mut T, ModelHandle<S>, &S::Event, &mut ModelContext<T>),
2292    {
2293        let subscriber = self.weak_handle();
2294        self.app
2295            .subscribe_internal(handle, move |emitter, event, cx| {
2296                if let Some(subscriber) = subscriber.upgrade(cx) {
2297                    subscriber.update(cx, |subscriber, cx| {
2298                        callback(subscriber, emitter, event, cx);
2299                    });
2300                    true
2301                } else {
2302                    false
2303                }
2304            })
2305    }
2306
2307    pub fn observe<S, F>(&mut self, handle: &ModelHandle<S>, mut callback: F) -> Subscription
2308    where
2309        S: Entity,
2310        F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ModelContext<T>),
2311    {
2312        let observer = self.weak_handle();
2313        self.app.observe_internal(handle, move |observed, cx| {
2314            if let Some(observer) = observer.upgrade(cx) {
2315                observer.update(cx, |observer, cx| {
2316                    callback(observer, observed, cx);
2317                });
2318                true
2319            } else {
2320                false
2321            }
2322        })
2323    }
2324
2325    pub fn observe_release<S, F>(
2326        &mut self,
2327        handle: &ModelHandle<S>,
2328        mut callback: F,
2329    ) -> Subscription
2330    where
2331        S: Entity,
2332        F: 'static + FnMut(&mut T, &mut ModelContext<T>),
2333    {
2334        let observer = self.weak_handle();
2335        self.app.observe_release(handle, move |cx| {
2336            if let Some(observer) = observer.upgrade(cx) {
2337                observer.update(cx, |observer, cx| {
2338                    callback(observer, cx);
2339                });
2340            }
2341        })
2342    }
2343
2344    pub fn handle(&self) -> ModelHandle<T> {
2345        ModelHandle::new(self.model_id, &self.app.cx.ref_counts)
2346    }
2347
2348    pub fn weak_handle(&self) -> WeakModelHandle<T> {
2349        WeakModelHandle::new(self.model_id)
2350    }
2351
2352    pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
2353    where
2354        F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
2355        Fut: 'static + Future<Output = S>,
2356        S: 'static,
2357    {
2358        let handle = self.handle();
2359        self.app.spawn(|cx| f(handle, cx))
2360    }
2361
2362    pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
2363    where
2364        F: FnOnce(WeakModelHandle<T>, AsyncAppContext) -> Fut,
2365        Fut: 'static + Future<Output = S>,
2366        S: 'static,
2367    {
2368        let handle = self.weak_handle();
2369        self.app.spawn(|cx| f(handle, cx))
2370    }
2371}
2372
2373impl<M> AsRef<AppContext> for ModelContext<'_, M> {
2374    fn as_ref(&self) -> &AppContext {
2375        &self.app.cx
2376    }
2377}
2378
2379impl<M> AsMut<MutableAppContext> for ModelContext<'_, M> {
2380    fn as_mut(&mut self) -> &mut MutableAppContext {
2381        self.app
2382    }
2383}
2384
2385impl<M> ReadModel for ModelContext<'_, M> {
2386    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2387        self.app.read_model(handle)
2388    }
2389}
2390
2391impl<M> UpdateModel for ModelContext<'_, M> {
2392    fn update_model<T: Entity, V>(
2393        &mut self,
2394        handle: &ModelHandle<T>,
2395        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
2396    ) -> V {
2397        self.app.update_model(handle, update)
2398    }
2399}
2400
2401impl<M> UpgradeModelHandle for ModelContext<'_, M> {
2402    fn upgrade_model_handle<T: Entity>(
2403        &self,
2404        handle: &WeakModelHandle<T>,
2405    ) -> Option<ModelHandle<T>> {
2406        self.cx.upgrade_model_handle(handle)
2407    }
2408
2409    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
2410        self.cx.model_handle_is_upgradable(handle)
2411    }
2412
2413    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
2414        self.cx.upgrade_any_model_handle(handle)
2415    }
2416}
2417
2418impl<M> Deref for ModelContext<'_, M> {
2419    type Target = MutableAppContext;
2420
2421    fn deref(&self) -> &Self::Target {
2422        &self.app
2423    }
2424}
2425
2426impl<M> DerefMut for ModelContext<'_, M> {
2427    fn deref_mut(&mut self) -> &mut Self::Target {
2428        &mut self.app
2429    }
2430}
2431
2432pub struct ViewContext<'a, T: ?Sized> {
2433    app: &'a mut MutableAppContext,
2434    window_id: usize,
2435    view_id: usize,
2436    view_type: PhantomData<T>,
2437}
2438
2439impl<'a, T: View> ViewContext<'a, T> {
2440    fn new(app: &'a mut MutableAppContext, window_id: usize, view_id: usize) -> Self {
2441        Self {
2442            app,
2443            window_id,
2444            view_id,
2445            view_type: PhantomData,
2446        }
2447    }
2448
2449    pub fn handle(&self) -> ViewHandle<T> {
2450        ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts)
2451    }
2452
2453    pub fn weak_handle(&self) -> WeakViewHandle<T> {
2454        WeakViewHandle::new(self.window_id, self.view_id)
2455    }
2456
2457    pub fn window_id(&self) -> usize {
2458        self.window_id
2459    }
2460
2461    pub fn view_id(&self) -> usize {
2462        self.view_id
2463    }
2464
2465    pub fn foreground(&self) -> &Rc<executor::Foreground> {
2466        self.app.foreground()
2467    }
2468
2469    pub fn background_executor(&self) -> &Arc<executor::Background> {
2470        &self.app.cx.background
2471    }
2472
2473    pub fn platform(&self) -> Arc<dyn Platform> {
2474        self.app.platform()
2475    }
2476
2477    pub fn prompt(
2478        &self,
2479        level: PromptLevel,
2480        msg: &str,
2481        answers: &[&str],
2482    ) -> oneshot::Receiver<usize> {
2483        self.app.prompt(self.window_id, level, msg, answers)
2484    }
2485
2486    pub fn prompt_for_paths(
2487        &self,
2488        options: PathPromptOptions,
2489    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
2490        self.app.prompt_for_paths(options)
2491    }
2492
2493    pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
2494        self.app.prompt_for_new_path(directory)
2495    }
2496
2497    pub fn debug_elements(&self) -> crate::json::Value {
2498        self.app.debug_elements(self.window_id).unwrap()
2499    }
2500
2501    pub fn focus<S>(&mut self, handle: S)
2502    where
2503        S: Into<AnyViewHandle>,
2504    {
2505        let handle = handle.into();
2506        self.app.pending_effects.push_back(Effect::Focus {
2507            window_id: handle.window_id,
2508            view_id: handle.view_id,
2509        });
2510    }
2511
2512    pub fn focus_self(&mut self) {
2513        self.app.pending_effects.push_back(Effect::Focus {
2514            window_id: self.window_id,
2515            view_id: self.view_id,
2516        });
2517    }
2518
2519    pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
2520    where
2521        S: Entity,
2522        F: FnOnce(&mut ModelContext<S>) -> S,
2523    {
2524        self.app.add_model(build_model)
2525    }
2526
2527    pub fn add_view<S, F>(&mut self, build_view: F) -> ViewHandle<S>
2528    where
2529        S: View,
2530        F: FnOnce(&mut ViewContext<S>) -> S,
2531    {
2532        self.app.add_view(self.window_id, build_view)
2533    }
2534
2535    pub fn add_option_view<S, F>(&mut self, build_view: F) -> Option<ViewHandle<S>>
2536    where
2537        S: View,
2538        F: FnOnce(&mut ViewContext<S>) -> Option<S>,
2539    {
2540        self.app.add_option_view(self.window_id, build_view)
2541    }
2542
2543    pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
2544    where
2545        E: Entity,
2546        E::Event: 'static,
2547        H: Handle<E>,
2548        F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext<T>),
2549    {
2550        let subscriber = self.weak_handle();
2551        self.app
2552            .subscribe_internal(handle, move |emitter, event, cx| {
2553                if let Some(subscriber) = subscriber.upgrade(cx) {
2554                    subscriber.update(cx, |subscriber, cx| {
2555                        callback(subscriber, emitter, event, cx);
2556                    });
2557                    true
2558                } else {
2559                    false
2560                }
2561            })
2562    }
2563
2564    pub fn observe<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
2565    where
2566        E: Entity,
2567        H: Handle<E>,
2568        F: 'static + FnMut(&mut T, H, &mut ViewContext<T>),
2569    {
2570        let observer = self.weak_handle();
2571        self.app.observe_internal(handle, move |observed, cx| {
2572            if let Some(observer) = observer.upgrade(cx) {
2573                observer.update(cx, |observer, cx| {
2574                    callback(observer, observed, cx);
2575                });
2576                true
2577            } else {
2578                false
2579            }
2580        })
2581    }
2582
2583    pub fn observe_release<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
2584    where
2585        E: Entity,
2586        H: Handle<E>,
2587        F: 'static + FnMut(&mut T, &mut ViewContext<T>),
2588    {
2589        let observer = self.weak_handle();
2590        self.app.observe_release(handle, move |cx| {
2591            if let Some(observer) = observer.upgrade(cx) {
2592                observer.update(cx, |observer, cx| {
2593                    callback(observer, cx);
2594                });
2595            }
2596        })
2597    }
2598
2599    pub fn emit(&mut self, payload: T::Event) {
2600        self.app.pending_effects.push_back(Effect::Event {
2601            entity_id: self.view_id,
2602            payload: Box::new(payload),
2603        });
2604    }
2605
2606    pub fn notify(&mut self) {
2607        self.app.notify_view(self.window_id, self.view_id);
2608    }
2609
2610    pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>)) {
2611        let handle = self.handle();
2612        self.app.defer(Box::new(move |cx| {
2613            handle.update(cx, |view, cx| {
2614                callback(view, cx);
2615            })
2616        }))
2617    }
2618
2619    pub fn propagate_action(&mut self) {
2620        self.app.halt_action_dispatch = false;
2621    }
2622
2623    pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
2624    where
2625        F: FnOnce(ViewHandle<T>, AsyncAppContext) -> Fut,
2626        Fut: 'static + Future<Output = S>,
2627        S: 'static,
2628    {
2629        let handle = self.handle();
2630        self.app.spawn(|cx| f(handle, cx))
2631    }
2632
2633    pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
2634    where
2635        F: FnOnce(WeakViewHandle<T>, AsyncAppContext) -> Fut,
2636        Fut: 'static + Future<Output = S>,
2637        S: 'static,
2638    {
2639        let handle = self.weak_handle();
2640        self.app.spawn(|cx| f(handle, cx))
2641    }
2642}
2643
2644pub struct RenderContext<'a, T: View> {
2645    pub app: &'a mut MutableAppContext,
2646    pub titlebar_height: f32,
2647    pub refreshing: bool,
2648    window_id: usize,
2649    view_id: usize,
2650    view_type: PhantomData<T>,
2651}
2652
2653impl<'a, T: View> RenderContext<'a, T> {
2654    pub fn handle(&self) -> WeakViewHandle<T> {
2655        WeakViewHandle::new(self.window_id, self.view_id)
2656    }
2657
2658    pub fn view_id(&self) -> usize {
2659        self.view_id
2660    }
2661}
2662
2663impl AsRef<AppContext> for &AppContext {
2664    fn as_ref(&self) -> &AppContext {
2665        self
2666    }
2667}
2668
2669impl<V: View> Deref for RenderContext<'_, V> {
2670    type Target = MutableAppContext;
2671
2672    fn deref(&self) -> &Self::Target {
2673        self.app
2674    }
2675}
2676
2677impl<V: View> DerefMut for RenderContext<'_, V> {
2678    fn deref_mut(&mut self) -> &mut Self::Target {
2679        self.app
2680    }
2681}
2682
2683impl<V: View> ReadModel for RenderContext<'_, V> {
2684    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2685        self.app.read_model(handle)
2686    }
2687}
2688
2689impl<V: View> UpdateModel for RenderContext<'_, V> {
2690    fn update_model<T: Entity, O>(
2691        &mut self,
2692        handle: &ModelHandle<T>,
2693        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
2694    ) -> O {
2695        self.app.update_model(handle, update)
2696    }
2697}
2698
2699impl<V: View> ReadView for RenderContext<'_, V> {
2700    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2701        self.app.read_view(handle)
2702    }
2703}
2704
2705impl<V: View> ElementStateContext for RenderContext<'_, V> {
2706    fn current_view_id(&self) -> usize {
2707        self.view_id
2708    }
2709}
2710
2711impl<M> AsRef<AppContext> for ViewContext<'_, M> {
2712    fn as_ref(&self) -> &AppContext {
2713        &self.app.cx
2714    }
2715}
2716
2717impl<M> Deref for ViewContext<'_, M> {
2718    type Target = MutableAppContext;
2719
2720    fn deref(&self) -> &Self::Target {
2721        &self.app
2722    }
2723}
2724
2725impl<M> DerefMut for ViewContext<'_, M> {
2726    fn deref_mut(&mut self) -> &mut Self::Target {
2727        &mut self.app
2728    }
2729}
2730
2731impl<M> AsMut<MutableAppContext> for ViewContext<'_, M> {
2732    fn as_mut(&mut self) -> &mut MutableAppContext {
2733        self.app
2734    }
2735}
2736
2737impl<V> ReadModel for ViewContext<'_, V> {
2738    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2739        self.app.read_model(handle)
2740    }
2741}
2742
2743impl<V> UpgradeModelHandle for ViewContext<'_, V> {
2744    fn upgrade_model_handle<T: Entity>(
2745        &self,
2746        handle: &WeakModelHandle<T>,
2747    ) -> Option<ModelHandle<T>> {
2748        self.cx.upgrade_model_handle(handle)
2749    }
2750
2751    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
2752        self.cx.model_handle_is_upgradable(handle)
2753    }
2754
2755    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
2756        self.cx.upgrade_any_model_handle(handle)
2757    }
2758}
2759
2760impl<V> UpgradeViewHandle for ViewContext<'_, V> {
2761    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
2762        self.cx.upgrade_view_handle(handle)
2763    }
2764}
2765
2766impl<V: View> UpdateModel for ViewContext<'_, V> {
2767    fn update_model<T: Entity, O>(
2768        &mut self,
2769        handle: &ModelHandle<T>,
2770        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
2771    ) -> O {
2772        self.app.update_model(handle, update)
2773    }
2774}
2775
2776impl<V: View> ReadView for ViewContext<'_, V> {
2777    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2778        self.app.read_view(handle)
2779    }
2780}
2781
2782impl<V: View> UpdateView for ViewContext<'_, V> {
2783    fn update_view<T, S>(
2784        &mut self,
2785        handle: &ViewHandle<T>,
2786        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
2787    ) -> S
2788    where
2789        T: View,
2790    {
2791        self.app.update_view(handle, update)
2792    }
2793}
2794
2795impl<V: View> ElementStateContext for ViewContext<'_, V> {
2796    fn current_view_id(&self) -> usize {
2797        self.view_id
2798    }
2799}
2800
2801pub trait Handle<T> {
2802    type Weak: 'static;
2803    fn id(&self) -> usize;
2804    fn location(&self) -> EntityLocation;
2805    fn downgrade(&self) -> Self::Weak;
2806    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
2807    where
2808        Self: Sized;
2809}
2810
2811#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
2812pub enum EntityLocation {
2813    Model(usize),
2814    View(usize, usize),
2815}
2816
2817pub struct ModelHandle<T> {
2818    model_id: usize,
2819    model_type: PhantomData<T>,
2820    ref_counts: Arc<Mutex<RefCounts>>,
2821}
2822
2823impl<T: Entity> ModelHandle<T> {
2824    fn new(model_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
2825        ref_counts.lock().inc_model(model_id);
2826        Self {
2827            model_id,
2828            model_type: PhantomData,
2829            ref_counts: ref_counts.clone(),
2830        }
2831    }
2832
2833    pub fn downgrade(&self) -> WeakModelHandle<T> {
2834        WeakModelHandle::new(self.model_id)
2835    }
2836
2837    pub fn id(&self) -> usize {
2838        self.model_id
2839    }
2840
2841    pub fn read<'a, C: ReadModel>(&self, cx: &'a C) -> &'a T {
2842        cx.read_model(self)
2843    }
2844
2845    pub fn read_with<'a, C, F, S>(&self, cx: &C, read: F) -> S
2846    where
2847        C: ReadModelWith,
2848        F: FnOnce(&T, &AppContext) -> S,
2849    {
2850        let mut read = Some(read);
2851        cx.read_model_with(self, &mut |model, cx| {
2852            let read = read.take().unwrap();
2853            read(model, cx)
2854        })
2855    }
2856
2857    pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
2858    where
2859        C: UpdateModel,
2860        F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
2861    {
2862        let mut update = Some(update);
2863        cx.update_model(self, &mut |model, cx| {
2864            let update = update.take().unwrap();
2865            update(model, cx)
2866        })
2867    }
2868
2869    pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
2870        let (mut tx, mut rx) = mpsc::channel(1);
2871        let mut cx = cx.cx.borrow_mut();
2872        let subscription = cx.observe(self, move |_, _| {
2873            tx.try_send(()).ok();
2874        });
2875
2876        let duration = if std::env::var("CI").is_ok() {
2877            Duration::from_secs(5)
2878        } else {
2879            Duration::from_secs(1)
2880        };
2881
2882        async move {
2883            let notification = timeout(duration, rx.recv())
2884                .await
2885                .expect("next notification timed out");
2886            drop(subscription);
2887            notification.expect("model dropped while test was waiting for its next notification")
2888        }
2889    }
2890
2891    pub fn next_event(&self, cx: &TestAppContext) -> impl Future<Output = T::Event>
2892    where
2893        T::Event: Clone,
2894    {
2895        let (mut tx, mut rx) = mpsc::channel(1);
2896        let mut cx = cx.cx.borrow_mut();
2897        let subscription = cx.subscribe(self, move |_, event, _| {
2898            tx.blocking_send(event.clone()).ok();
2899        });
2900
2901        let duration = if std::env::var("CI").is_ok() {
2902            Duration::from_secs(5)
2903        } else {
2904            Duration::from_secs(1)
2905        };
2906
2907        async move {
2908            let event = timeout(duration, rx.recv())
2909                .await
2910                .expect("next event timed out");
2911            drop(subscription);
2912            event.expect("model dropped while test was waiting for its next event")
2913        }
2914    }
2915
2916    pub fn condition(
2917        &self,
2918        cx: &TestAppContext,
2919        mut predicate: impl FnMut(&T, &AppContext) -> bool,
2920    ) -> impl Future<Output = ()> {
2921        let (tx, mut rx) = mpsc::channel(1024);
2922
2923        let mut cx = cx.cx.borrow_mut();
2924        let subscriptions = (
2925            cx.observe(self, {
2926                let mut tx = tx.clone();
2927                move |_, _| {
2928                    tx.blocking_send(()).ok();
2929                }
2930            }),
2931            cx.subscribe(self, {
2932                let mut tx = tx.clone();
2933                move |_, _, _| {
2934                    tx.blocking_send(()).ok();
2935                }
2936            }),
2937        );
2938
2939        let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
2940        let handle = self.downgrade();
2941        let duration = if std::env::var("CI").is_ok() {
2942            Duration::from_secs(5)
2943        } else {
2944            Duration::from_secs(1)
2945        };
2946
2947        async move {
2948            timeout(duration, async move {
2949                loop {
2950                    {
2951                        let cx = cx.borrow();
2952                        let cx = cx.as_ref();
2953                        if predicate(
2954                            handle
2955                                .upgrade(cx)
2956                                .expect("model dropped with pending condition")
2957                                .read(cx),
2958                            cx,
2959                        ) {
2960                            break;
2961                        }
2962                    }
2963
2964                    cx.borrow().foreground().start_waiting();
2965                    rx.recv()
2966                        .await
2967                        .expect("model dropped with pending condition");
2968                    cx.borrow().foreground().finish_waiting();
2969                }
2970            })
2971            .await
2972            .expect("condition timed out");
2973            drop(subscriptions);
2974        }
2975    }
2976}
2977
2978impl<T> Clone for ModelHandle<T> {
2979    fn clone(&self) -> Self {
2980        self.ref_counts.lock().inc_model(self.model_id);
2981        Self {
2982            model_id: self.model_id,
2983            model_type: PhantomData,
2984            ref_counts: self.ref_counts.clone(),
2985        }
2986    }
2987}
2988
2989impl<T> PartialEq for ModelHandle<T> {
2990    fn eq(&self, other: &Self) -> bool {
2991        self.model_id == other.model_id
2992    }
2993}
2994
2995impl<T> Eq for ModelHandle<T> {}
2996
2997impl<T> PartialEq<WeakModelHandle<T>> for ModelHandle<T> {
2998    fn eq(&self, other: &WeakModelHandle<T>) -> bool {
2999        self.model_id == other.model_id
3000    }
3001}
3002
3003impl<T> Hash for ModelHandle<T> {
3004    fn hash<H: Hasher>(&self, state: &mut H) {
3005        self.model_id.hash(state);
3006    }
3007}
3008
3009impl<T> std::borrow::Borrow<usize> for ModelHandle<T> {
3010    fn borrow(&self) -> &usize {
3011        &self.model_id
3012    }
3013}
3014
3015impl<T> Debug for ModelHandle<T> {
3016    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3017        f.debug_tuple(&format!("ModelHandle<{}>", type_name::<T>()))
3018            .field(&self.model_id)
3019            .finish()
3020    }
3021}
3022
3023unsafe impl<T> Send for ModelHandle<T> {}
3024unsafe impl<T> Sync for ModelHandle<T> {}
3025
3026impl<T> Drop for ModelHandle<T> {
3027    fn drop(&mut self) {
3028        self.ref_counts.lock().dec_model(self.model_id);
3029    }
3030}
3031
3032impl<T: Entity> Handle<T> for ModelHandle<T> {
3033    type Weak = WeakModelHandle<T>;
3034
3035    fn id(&self) -> usize {
3036        self.model_id
3037    }
3038
3039    fn location(&self) -> EntityLocation {
3040        EntityLocation::Model(self.model_id)
3041    }
3042
3043    fn downgrade(&self) -> Self::Weak {
3044        self.downgrade()
3045    }
3046
3047    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
3048    where
3049        Self: Sized,
3050    {
3051        weak.upgrade(cx)
3052    }
3053}
3054
3055pub struct WeakModelHandle<T> {
3056    model_id: usize,
3057    model_type: PhantomData<T>,
3058}
3059
3060unsafe impl<T> Send for WeakModelHandle<T> {}
3061unsafe impl<T> Sync for WeakModelHandle<T> {}
3062
3063impl<T: Entity> WeakModelHandle<T> {
3064    fn new(model_id: usize) -> Self {
3065        Self {
3066            model_id,
3067            model_type: PhantomData,
3068        }
3069    }
3070
3071    pub fn id(&self) -> usize {
3072        self.model_id
3073    }
3074
3075    pub fn is_upgradable(&self, cx: &impl UpgradeModelHandle) -> bool {
3076        cx.model_handle_is_upgradable(self)
3077    }
3078
3079    pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
3080        cx.upgrade_model_handle(self)
3081    }
3082}
3083
3084impl<T> Hash for WeakModelHandle<T> {
3085    fn hash<H: Hasher>(&self, state: &mut H) {
3086        self.model_id.hash(state)
3087    }
3088}
3089
3090impl<T> PartialEq for WeakModelHandle<T> {
3091    fn eq(&self, other: &Self) -> bool {
3092        self.model_id == other.model_id
3093    }
3094}
3095
3096impl<T> Eq for WeakModelHandle<T> {}
3097
3098impl<T> Clone for WeakModelHandle<T> {
3099    fn clone(&self) -> Self {
3100        Self {
3101            model_id: self.model_id,
3102            model_type: PhantomData,
3103        }
3104    }
3105}
3106
3107impl<T> Copy for WeakModelHandle<T> {}
3108
3109pub struct ViewHandle<T> {
3110    window_id: usize,
3111    view_id: usize,
3112    view_type: PhantomData<T>,
3113    ref_counts: Arc<Mutex<RefCounts>>,
3114}
3115
3116impl<T: View> ViewHandle<T> {
3117    fn new(window_id: usize, view_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
3118        ref_counts.lock().inc_view(window_id, view_id);
3119        Self {
3120            window_id,
3121            view_id,
3122            view_type: PhantomData,
3123            ref_counts: ref_counts.clone(),
3124        }
3125    }
3126
3127    pub fn downgrade(&self) -> WeakViewHandle<T> {
3128        WeakViewHandle::new(self.window_id, self.view_id)
3129    }
3130
3131    pub fn window_id(&self) -> usize {
3132        self.window_id
3133    }
3134
3135    pub fn id(&self) -> usize {
3136        self.view_id
3137    }
3138
3139    pub fn read<'a, C: ReadView>(&self, cx: &'a C) -> &'a T {
3140        cx.read_view(self)
3141    }
3142
3143    pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
3144    where
3145        C: ReadViewWith,
3146        F: FnOnce(&T, &AppContext) -> S,
3147    {
3148        let mut read = Some(read);
3149        cx.read_view_with(self, &mut |view, cx| {
3150            let read = read.take().unwrap();
3151            read(view, cx)
3152        })
3153    }
3154
3155    pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
3156    where
3157        C: UpdateView,
3158        F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
3159    {
3160        let mut update = Some(update);
3161        cx.update_view(self, &mut |view, cx| {
3162            let update = update.take().unwrap();
3163            update(view, cx)
3164        })
3165    }
3166
3167    pub fn defer<C, F>(&self, cx: &mut C, update: F)
3168    where
3169        C: AsMut<MutableAppContext>,
3170        F: 'static + FnOnce(&mut T, &mut ViewContext<T>),
3171    {
3172        let this = self.clone();
3173        cx.as_mut().defer(Box::new(move |cx| {
3174            this.update(cx, |view, cx| update(view, cx));
3175        }));
3176    }
3177
3178    pub fn is_focused(&self, cx: &AppContext) -> bool {
3179        cx.focused_view_id(self.window_id)
3180            .map_or(false, |focused_id| focused_id == self.view_id)
3181    }
3182
3183    pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
3184        let (mut tx, mut rx) = mpsc::channel(1);
3185        let mut cx = cx.cx.borrow_mut();
3186        let subscription = cx.observe(self, move |_, _| {
3187            tx.try_send(()).ok();
3188        });
3189
3190        let duration = if std::env::var("CI").is_ok() {
3191            Duration::from_secs(5)
3192        } else {
3193            Duration::from_secs(1)
3194        };
3195
3196        async move {
3197            let notification = timeout(duration, rx.recv())
3198                .await
3199                .expect("next notification timed out");
3200            drop(subscription);
3201            notification.expect("model dropped while test was waiting for its next notification")
3202        }
3203    }
3204
3205    pub fn condition(
3206        &self,
3207        cx: &TestAppContext,
3208        mut predicate: impl FnMut(&T, &AppContext) -> bool,
3209    ) -> impl Future<Output = ()> {
3210        let (tx, mut rx) = mpsc::channel(1024);
3211
3212        let mut cx = cx.cx.borrow_mut();
3213        let subscriptions = self.update(&mut *cx, |_, cx| {
3214            (
3215                cx.observe(self, {
3216                    let mut tx = tx.clone();
3217                    move |_, _, _| {
3218                        tx.blocking_send(()).ok();
3219                    }
3220                }),
3221                cx.subscribe(self, {
3222                    let mut tx = tx.clone();
3223                    move |_, _, _, _| {
3224                        tx.blocking_send(()).ok();
3225                    }
3226                }),
3227            )
3228        });
3229
3230        let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
3231        let handle = self.downgrade();
3232        let duration = if std::env::var("CI").is_ok() {
3233            Duration::from_secs(2)
3234        } else {
3235            Duration::from_millis(500)
3236        };
3237
3238        async move {
3239            timeout(duration, async move {
3240                loop {
3241                    {
3242                        let cx = cx.borrow();
3243                        let cx = cx.as_ref();
3244                        if predicate(
3245                            handle
3246                                .upgrade(cx)
3247                                .expect("view dropped with pending condition")
3248                                .read(cx),
3249                            cx,
3250                        ) {
3251                            break;
3252                        }
3253                    }
3254
3255                    cx.borrow().foreground().start_waiting();
3256                    rx.recv()
3257                        .await
3258                        .expect("view dropped with pending condition");
3259                    cx.borrow().foreground().finish_waiting();
3260                }
3261            })
3262            .await
3263            .expect("condition timed out");
3264            drop(subscriptions);
3265        }
3266    }
3267}
3268
3269impl<T> Clone for ViewHandle<T> {
3270    fn clone(&self) -> Self {
3271        self.ref_counts
3272            .lock()
3273            .inc_view(self.window_id, self.view_id);
3274        Self {
3275            window_id: self.window_id,
3276            view_id: self.view_id,
3277            view_type: PhantomData,
3278            ref_counts: self.ref_counts.clone(),
3279        }
3280    }
3281}
3282
3283impl<T> PartialEq for ViewHandle<T> {
3284    fn eq(&self, other: &Self) -> bool {
3285        self.window_id == other.window_id && self.view_id == other.view_id
3286    }
3287}
3288
3289impl<T> Eq for ViewHandle<T> {}
3290
3291impl<T> Debug for ViewHandle<T> {
3292    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3293        f.debug_struct(&format!("ViewHandle<{}>", type_name::<T>()))
3294            .field("window_id", &self.window_id)
3295            .field("view_id", &self.view_id)
3296            .finish()
3297    }
3298}
3299
3300impl<T> Drop for ViewHandle<T> {
3301    fn drop(&mut self) {
3302        self.ref_counts
3303            .lock()
3304            .dec_view(self.window_id, self.view_id);
3305    }
3306}
3307
3308impl<T: View> Handle<T> for ViewHandle<T> {
3309    type Weak = WeakViewHandle<T>;
3310
3311    fn id(&self) -> usize {
3312        self.view_id
3313    }
3314
3315    fn location(&self) -> EntityLocation {
3316        EntityLocation::View(self.window_id, self.view_id)
3317    }
3318
3319    fn downgrade(&self) -> Self::Weak {
3320        self.downgrade()
3321    }
3322
3323    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
3324    where
3325        Self: Sized,
3326    {
3327        weak.upgrade(cx)
3328    }
3329}
3330
3331pub struct AnyViewHandle {
3332    window_id: usize,
3333    view_id: usize,
3334    view_type: TypeId,
3335    ref_counts: Arc<Mutex<RefCounts>>,
3336}
3337
3338impl AnyViewHandle {
3339    pub fn id(&self) -> usize {
3340        self.view_id
3341    }
3342
3343    pub fn is<T: 'static>(&self) -> bool {
3344        TypeId::of::<T>() == self.view_type
3345    }
3346
3347    pub fn is_focused(&self, cx: &AppContext) -> bool {
3348        cx.focused_view_id(self.window_id)
3349            .map_or(false, |focused_id| focused_id == self.view_id)
3350    }
3351
3352    pub fn downcast<T: View>(self) -> Option<ViewHandle<T>> {
3353        if self.is::<T>() {
3354            let result = Some(ViewHandle {
3355                window_id: self.window_id,
3356                view_id: self.view_id,
3357                ref_counts: self.ref_counts.clone(),
3358                view_type: PhantomData,
3359            });
3360            unsafe {
3361                Arc::decrement_strong_count(&self.ref_counts);
3362            }
3363            std::mem::forget(self);
3364            result
3365        } else {
3366            None
3367        }
3368    }
3369}
3370
3371impl Clone for AnyViewHandle {
3372    fn clone(&self) -> Self {
3373        self.ref_counts
3374            .lock()
3375            .inc_view(self.window_id, self.view_id);
3376        Self {
3377            window_id: self.window_id,
3378            view_id: self.view_id,
3379            view_type: self.view_type,
3380            ref_counts: self.ref_counts.clone(),
3381        }
3382    }
3383}
3384
3385impl From<&AnyViewHandle> for AnyViewHandle {
3386    fn from(handle: &AnyViewHandle) -> Self {
3387        handle.clone()
3388    }
3389}
3390
3391impl<T: View> From<&ViewHandle<T>> for AnyViewHandle {
3392    fn from(handle: &ViewHandle<T>) -> Self {
3393        handle
3394            .ref_counts
3395            .lock()
3396            .inc_view(handle.window_id, handle.view_id);
3397        AnyViewHandle {
3398            window_id: handle.window_id,
3399            view_id: handle.view_id,
3400            view_type: TypeId::of::<T>(),
3401            ref_counts: handle.ref_counts.clone(),
3402        }
3403    }
3404}
3405
3406impl<T: View> From<ViewHandle<T>> for AnyViewHandle {
3407    fn from(handle: ViewHandle<T>) -> Self {
3408        let any_handle = AnyViewHandle {
3409            window_id: handle.window_id,
3410            view_id: handle.view_id,
3411            view_type: TypeId::of::<T>(),
3412            ref_counts: handle.ref_counts.clone(),
3413        };
3414        unsafe {
3415            Arc::decrement_strong_count(&handle.ref_counts);
3416        }
3417        std::mem::forget(handle);
3418        any_handle
3419    }
3420}
3421
3422impl Drop for AnyViewHandle {
3423    fn drop(&mut self) {
3424        self.ref_counts
3425            .lock()
3426            .dec_view(self.window_id, self.view_id);
3427    }
3428}
3429
3430pub struct AnyModelHandle {
3431    model_id: usize,
3432    model_type: TypeId,
3433    ref_counts: Arc<Mutex<RefCounts>>,
3434}
3435
3436impl AnyModelHandle {
3437    pub fn downcast<T: Entity>(self) -> Option<ModelHandle<T>> {
3438        if self.is::<T>() {
3439            let result = Some(ModelHandle {
3440                model_id: self.model_id,
3441                model_type: PhantomData,
3442                ref_counts: self.ref_counts.clone(),
3443            });
3444            unsafe {
3445                Arc::decrement_strong_count(&self.ref_counts);
3446            }
3447            std::mem::forget(self);
3448            result
3449        } else {
3450            None
3451        }
3452    }
3453
3454    pub fn downgrade(&self) -> AnyWeakModelHandle {
3455        AnyWeakModelHandle {
3456            model_id: self.model_id,
3457            model_type: self.model_type,
3458        }
3459    }
3460
3461    pub fn is<T: Entity>(&self) -> bool {
3462        self.model_type == TypeId::of::<T>()
3463    }
3464}
3465
3466impl<T: Entity> From<ModelHandle<T>> for AnyModelHandle {
3467    fn from(handle: ModelHandle<T>) -> Self {
3468        handle.ref_counts.lock().inc_model(handle.model_id);
3469        Self {
3470            model_id: handle.model_id,
3471            model_type: TypeId::of::<T>(),
3472            ref_counts: handle.ref_counts.clone(),
3473        }
3474    }
3475}
3476
3477impl Clone for AnyModelHandle {
3478    fn clone(&self) -> Self {
3479        self.ref_counts.lock().inc_model(self.model_id);
3480        Self {
3481            model_id: self.model_id,
3482            model_type: self.model_type,
3483            ref_counts: self.ref_counts.clone(),
3484        }
3485    }
3486}
3487
3488impl Drop for AnyModelHandle {
3489    fn drop(&mut self) {
3490        self.ref_counts.lock().dec_model(self.model_id);
3491    }
3492}
3493
3494pub struct AnyWeakModelHandle {
3495    model_id: usize,
3496    model_type: TypeId,
3497}
3498
3499impl AnyWeakModelHandle {
3500    pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<AnyModelHandle> {
3501        cx.upgrade_any_model_handle(self)
3502    }
3503}
3504
3505pub struct WeakViewHandle<T> {
3506    window_id: usize,
3507    view_id: usize,
3508    view_type: PhantomData<T>,
3509}
3510
3511impl<T: View> WeakViewHandle<T> {
3512    fn new(window_id: usize, view_id: usize) -> Self {
3513        Self {
3514            window_id,
3515            view_id,
3516            view_type: PhantomData,
3517        }
3518    }
3519
3520    pub fn id(&self) -> usize {
3521        self.view_id
3522    }
3523
3524    pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<ViewHandle<T>> {
3525        cx.upgrade_view_handle(self)
3526    }
3527}
3528
3529impl<T> Clone for WeakViewHandle<T> {
3530    fn clone(&self) -> Self {
3531        Self {
3532            window_id: self.window_id,
3533            view_id: self.view_id,
3534            view_type: PhantomData,
3535        }
3536    }
3537}
3538
3539impl<T> PartialEq for WeakViewHandle<T> {
3540    fn eq(&self, other: &Self) -> bool {
3541        self.window_id == other.window_id && self.view_id == other.view_id
3542    }
3543}
3544
3545impl<T> Eq for WeakViewHandle<T> {}
3546
3547impl<T> Hash for WeakViewHandle<T> {
3548    fn hash<H: Hasher>(&self, state: &mut H) {
3549        self.window_id.hash(state);
3550        self.view_id.hash(state);
3551    }
3552}
3553
3554#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3555pub struct ElementStateId {
3556    view_id: usize,
3557    element_id: usize,
3558    tag: TypeId,
3559}
3560
3561pub struct ElementStateHandle<T> {
3562    value_type: PhantomData<T>,
3563    id: ElementStateId,
3564    ref_counts: Weak<Mutex<RefCounts>>,
3565}
3566
3567impl<T: 'static> ElementStateHandle<T> {
3568    fn new(id: ElementStateId, frame_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
3569        ref_counts.lock().inc_element_state(id, frame_id);
3570        Self {
3571            value_type: PhantomData,
3572            id,
3573            ref_counts: Arc::downgrade(ref_counts),
3574        }
3575    }
3576
3577    pub fn read<'a>(&self, cx: &'a AppContext) -> &'a T {
3578        cx.element_states
3579            .get(&self.id)
3580            .unwrap()
3581            .downcast_ref()
3582            .unwrap()
3583    }
3584
3585    pub fn update<C, R>(&self, cx: &mut C, f: impl FnOnce(&mut T, &mut C) -> R) -> R
3586    where
3587        C: DerefMut<Target = MutableAppContext>,
3588    {
3589        let mut element_state = cx.deref_mut().cx.element_states.remove(&self.id).unwrap();
3590        let result = f(element_state.downcast_mut().unwrap(), cx);
3591        cx.deref_mut()
3592            .cx
3593            .element_states
3594            .insert(self.id, element_state);
3595        result
3596    }
3597}
3598
3599impl<T> Drop for ElementStateHandle<T> {
3600    fn drop(&mut self) {
3601        if let Some(ref_counts) = self.ref_counts.upgrade() {
3602            ref_counts.lock().dec_element_state(self.id);
3603        }
3604    }
3605}
3606
3607pub struct CursorStyleHandle {
3608    id: usize,
3609    next_cursor_style_handle_id: Arc<AtomicUsize>,
3610    platform: Arc<dyn Platform>,
3611}
3612
3613impl Drop for CursorStyleHandle {
3614    fn drop(&mut self) {
3615        if self.id + 1 == self.next_cursor_style_handle_id.load(SeqCst) {
3616            self.platform.set_cursor_style(CursorStyle::Arrow);
3617        }
3618    }
3619}
3620
3621#[must_use]
3622pub enum Subscription {
3623    Subscription {
3624        id: usize,
3625        entity_id: usize,
3626        subscriptions: Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>>,
3627    },
3628    Observation {
3629        id: usize,
3630        entity_id: usize,
3631        observations: Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, ObservationCallback>>>>>,
3632    },
3633    ReleaseObservation {
3634        id: usize,
3635        entity_id: usize,
3636        observations:
3637            Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>>,
3638    },
3639}
3640
3641impl Subscription {
3642    pub fn detach(&mut self) {
3643        match self {
3644            Subscription::Subscription { subscriptions, .. } => {
3645                subscriptions.take();
3646            }
3647            Subscription::Observation { observations, .. } => {
3648                observations.take();
3649            }
3650            Subscription::ReleaseObservation { observations, .. } => {
3651                observations.take();
3652            }
3653        }
3654    }
3655}
3656
3657impl Drop for Subscription {
3658    fn drop(&mut self) {
3659        match self {
3660            Subscription::Observation {
3661                id,
3662                entity_id,
3663                observations,
3664            } => {
3665                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
3666                    if let Some(observations) = observations.lock().get_mut(entity_id) {
3667                        observations.remove(id);
3668                    }
3669                }
3670            }
3671            Subscription::ReleaseObservation {
3672                id,
3673                entity_id,
3674                observations,
3675            } => {
3676                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
3677                    if let Some(observations) = observations.lock().get_mut(entity_id) {
3678                        observations.remove(id);
3679                    }
3680                }
3681            }
3682            Subscription::Subscription {
3683                id,
3684                entity_id,
3685                subscriptions,
3686            } => {
3687                if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
3688                    if let Some(subscriptions) = subscriptions.lock().get_mut(entity_id) {
3689                        subscriptions.remove(id);
3690                    }
3691                }
3692            }
3693        }
3694    }
3695}
3696
3697#[derive(Default)]
3698struct RefCounts {
3699    entity_counts: HashMap<usize, usize>,
3700    element_state_counts: HashMap<ElementStateId, ElementStateRefCount>,
3701    dropped_models: HashSet<usize>,
3702    dropped_views: HashSet<(usize, usize)>,
3703    dropped_element_states: HashSet<ElementStateId>,
3704}
3705
3706struct ElementStateRefCount {
3707    ref_count: usize,
3708    frame_id: usize,
3709}
3710
3711impl RefCounts {
3712    fn inc_model(&mut self, model_id: usize) {
3713        match self.entity_counts.entry(model_id) {
3714            Entry::Occupied(mut entry) => {
3715                *entry.get_mut() += 1;
3716            }
3717            Entry::Vacant(entry) => {
3718                entry.insert(1);
3719                self.dropped_models.remove(&model_id);
3720            }
3721        }
3722    }
3723
3724    fn inc_view(&mut self, window_id: usize, view_id: usize) {
3725        match self.entity_counts.entry(view_id) {
3726            Entry::Occupied(mut entry) => *entry.get_mut() += 1,
3727            Entry::Vacant(entry) => {
3728                entry.insert(1);
3729                self.dropped_views.remove(&(window_id, view_id));
3730            }
3731        }
3732    }
3733
3734    fn inc_element_state(&mut self, id: ElementStateId, frame_id: usize) {
3735        match self.element_state_counts.entry(id) {
3736            Entry::Occupied(mut entry) => {
3737                let entry = entry.get_mut();
3738                if entry.frame_id == frame_id || entry.ref_count >= 2 {
3739                    panic!("used the same element state more than once in the same frame");
3740                }
3741                entry.ref_count += 1;
3742                entry.frame_id = frame_id;
3743            }
3744            Entry::Vacant(entry) => {
3745                entry.insert(ElementStateRefCount {
3746                    ref_count: 1,
3747                    frame_id,
3748                });
3749                self.dropped_element_states.remove(&id);
3750            }
3751        }
3752    }
3753
3754    fn dec_model(&mut self, model_id: usize) {
3755        let count = self.entity_counts.get_mut(&model_id).unwrap();
3756        *count -= 1;
3757        if *count == 0 {
3758            self.entity_counts.remove(&model_id);
3759            self.dropped_models.insert(model_id);
3760        }
3761    }
3762
3763    fn dec_view(&mut self, window_id: usize, view_id: usize) {
3764        let count = self.entity_counts.get_mut(&view_id).unwrap();
3765        *count -= 1;
3766        if *count == 0 {
3767            self.entity_counts.remove(&view_id);
3768            self.dropped_views.insert((window_id, view_id));
3769        }
3770    }
3771
3772    fn dec_element_state(&mut self, id: ElementStateId) {
3773        let entry = self.element_state_counts.get_mut(&id).unwrap();
3774        entry.ref_count -= 1;
3775        if entry.ref_count == 0 {
3776            self.element_state_counts.remove(&id);
3777            self.dropped_element_states.insert(id);
3778        }
3779    }
3780
3781    fn is_entity_alive(&self, entity_id: usize) -> bool {
3782        self.entity_counts.contains_key(&entity_id)
3783    }
3784
3785    fn take_dropped(
3786        &mut self,
3787    ) -> (
3788        HashSet<usize>,
3789        HashSet<(usize, usize)>,
3790        HashSet<ElementStateId>,
3791    ) {
3792        (
3793            std::mem::take(&mut self.dropped_models),
3794            std::mem::take(&mut self.dropped_views),
3795            std::mem::take(&mut self.dropped_element_states),
3796        )
3797    }
3798}
3799
3800#[cfg(test)]
3801mod tests {
3802    use super::*;
3803    use crate::elements::*;
3804    use smol::future::poll_once;
3805    use std::{
3806        cell::Cell,
3807        sync::atomic::{AtomicUsize, Ordering::SeqCst},
3808    };
3809
3810    #[crate::test(self)]
3811    fn test_model_handles(cx: &mut MutableAppContext) {
3812        struct Model {
3813            other: Option<ModelHandle<Model>>,
3814            events: Vec<String>,
3815        }
3816
3817        impl Entity for Model {
3818            type Event = usize;
3819        }
3820
3821        impl Model {
3822            fn new(other: Option<ModelHandle<Self>>, cx: &mut ModelContext<Self>) -> Self {
3823                if let Some(other) = other.as_ref() {
3824                    cx.observe(other, |me, _, _| {
3825                        me.events.push("notified".into());
3826                    })
3827                    .detach();
3828                    cx.subscribe(other, |me, _, event, _| {
3829                        me.events.push(format!("observed event {}", event));
3830                    })
3831                    .detach();
3832                }
3833
3834                Self {
3835                    other,
3836                    events: Vec::new(),
3837                }
3838            }
3839        }
3840
3841        let handle_1 = cx.add_model(|cx| Model::new(None, cx));
3842        let handle_2 = cx.add_model(|cx| Model::new(Some(handle_1.clone()), cx));
3843        assert_eq!(cx.cx.models.len(), 2);
3844
3845        handle_1.update(cx, |model, cx| {
3846            model.events.push("updated".into());
3847            cx.emit(1);
3848            cx.notify();
3849            cx.emit(2);
3850        });
3851        assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
3852        assert_eq!(
3853            handle_2.read(cx).events,
3854            vec![
3855                "observed event 1".to_string(),
3856                "notified".to_string(),
3857                "observed event 2".to_string(),
3858            ]
3859        );
3860
3861        handle_2.update(cx, |model, _| {
3862            drop(handle_1);
3863            model.other.take();
3864        });
3865
3866        assert_eq!(cx.cx.models.len(), 1);
3867        assert!(cx.subscriptions.lock().is_empty());
3868        assert!(cx.observations.lock().is_empty());
3869    }
3870
3871    #[crate::test(self)]
3872    fn test_subscribe_and_emit_from_model(cx: &mut MutableAppContext) {
3873        #[derive(Default)]
3874        struct Model {
3875            events: Vec<usize>,
3876        }
3877
3878        impl Entity for Model {
3879            type Event = usize;
3880        }
3881
3882        let handle_1 = cx.add_model(|_| Model::default());
3883        let handle_2 = cx.add_model(|_| Model::default());
3884        let handle_2b = handle_2.clone();
3885
3886        handle_1.update(cx, |_, c| {
3887            c.subscribe(&handle_2, move |model: &mut Model, _, event, c| {
3888                model.events.push(*event);
3889
3890                c.subscribe(&handle_2b, |model, _, event, _| {
3891                    model.events.push(*event * 2);
3892                })
3893                .detach();
3894            })
3895            .detach();
3896        });
3897
3898        handle_2.update(cx, |_, c| c.emit(7));
3899        assert_eq!(handle_1.read(cx).events, vec![7]);
3900
3901        handle_2.update(cx, |_, c| c.emit(5));
3902        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
3903    }
3904
3905    #[crate::test(self)]
3906    fn test_observe_and_notify_from_model(cx: &mut MutableAppContext) {
3907        #[derive(Default)]
3908        struct Model {
3909            count: usize,
3910            events: Vec<usize>,
3911        }
3912
3913        impl Entity for Model {
3914            type Event = ();
3915        }
3916
3917        let handle_1 = cx.add_model(|_| Model::default());
3918        let handle_2 = cx.add_model(|_| Model::default());
3919        let handle_2b = handle_2.clone();
3920
3921        handle_1.update(cx, |_, c| {
3922            c.observe(&handle_2, move |model, observed, c| {
3923                model.events.push(observed.read(c).count);
3924                c.observe(&handle_2b, |model, observed, c| {
3925                    model.events.push(observed.read(c).count * 2);
3926                })
3927                .detach();
3928            })
3929            .detach();
3930        });
3931
3932        handle_2.update(cx, |model, c| {
3933            model.count = 7;
3934            c.notify()
3935        });
3936        assert_eq!(handle_1.read(cx).events, vec![7]);
3937
3938        handle_2.update(cx, |model, c| {
3939            model.count = 5;
3940            c.notify()
3941        });
3942        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10])
3943    }
3944
3945    #[crate::test(self)]
3946    fn test_view_handles(cx: &mut MutableAppContext) {
3947        struct View {
3948            other: Option<ViewHandle<View>>,
3949            events: Vec<String>,
3950        }
3951
3952        impl Entity for View {
3953            type Event = usize;
3954        }
3955
3956        impl super::View for View {
3957            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
3958                Empty::new().boxed()
3959            }
3960
3961            fn ui_name() -> &'static str {
3962                "View"
3963            }
3964        }
3965
3966        impl View {
3967            fn new(other: Option<ViewHandle<View>>, cx: &mut ViewContext<Self>) -> Self {
3968                if let Some(other) = other.as_ref() {
3969                    cx.subscribe(other, |me, _, event, _| {
3970                        me.events.push(format!("observed event {}", event));
3971                    })
3972                    .detach();
3973                }
3974                Self {
3975                    other,
3976                    events: Vec::new(),
3977                }
3978            }
3979        }
3980
3981        let (window_id, _) = cx.add_window(Default::default(), |cx| View::new(None, cx));
3982        let handle_1 = cx.add_view(window_id, |cx| View::new(None, cx));
3983        let handle_2 = cx.add_view(window_id, |cx| View::new(Some(handle_1.clone()), cx));
3984        assert_eq!(cx.cx.views.len(), 3);
3985
3986        handle_1.update(cx, |view, cx| {
3987            view.events.push("updated".into());
3988            cx.emit(1);
3989            cx.emit(2);
3990        });
3991        assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
3992        assert_eq!(
3993            handle_2.read(cx).events,
3994            vec![
3995                "observed event 1".to_string(),
3996                "observed event 2".to_string(),
3997            ]
3998        );
3999
4000        handle_2.update(cx, |view, _| {
4001            drop(handle_1);
4002            view.other.take();
4003        });
4004
4005        assert_eq!(cx.cx.views.len(), 2);
4006        assert!(cx.subscriptions.lock().is_empty());
4007        assert!(cx.observations.lock().is_empty());
4008    }
4009
4010    #[crate::test(self)]
4011    fn test_add_window(cx: &mut MutableAppContext) {
4012        struct View {
4013            mouse_down_count: Arc<AtomicUsize>,
4014        }
4015
4016        impl Entity for View {
4017            type Event = ();
4018        }
4019
4020        impl super::View for View {
4021            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4022                let mouse_down_count = self.mouse_down_count.clone();
4023                EventHandler::new(Empty::new().boxed())
4024                    .on_mouse_down(move |_| {
4025                        mouse_down_count.fetch_add(1, SeqCst);
4026                        true
4027                    })
4028                    .boxed()
4029            }
4030
4031            fn ui_name() -> &'static str {
4032                "View"
4033            }
4034        }
4035
4036        let mouse_down_count = Arc::new(AtomicUsize::new(0));
4037        let (window_id, _) = cx.add_window(Default::default(), |_| View {
4038            mouse_down_count: mouse_down_count.clone(),
4039        });
4040        let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
4041        // Ensure window's root element is in a valid lifecycle state.
4042        presenter.borrow_mut().dispatch_event(
4043            Event::LeftMouseDown {
4044                position: Default::default(),
4045                ctrl: false,
4046                alt: false,
4047                shift: false,
4048                cmd: false,
4049                click_count: 1,
4050            },
4051            cx,
4052        );
4053        assert_eq!(mouse_down_count.load(SeqCst), 1);
4054    }
4055
4056    #[crate::test(self)]
4057    fn test_entity_release_hooks(cx: &mut MutableAppContext) {
4058        struct Model {
4059            released: Rc<Cell<bool>>,
4060        }
4061
4062        struct View {
4063            released: Rc<Cell<bool>>,
4064        }
4065
4066        impl Entity for Model {
4067            type Event = ();
4068
4069            fn release(&mut self, _: &mut MutableAppContext) {
4070                self.released.set(true);
4071            }
4072        }
4073
4074        impl Entity for View {
4075            type Event = ();
4076
4077            fn release(&mut self, _: &mut MutableAppContext) {
4078                self.released.set(true);
4079            }
4080        }
4081
4082        impl super::View for View {
4083            fn ui_name() -> &'static str {
4084                "View"
4085            }
4086
4087            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4088                Empty::new().boxed()
4089            }
4090        }
4091
4092        let model_released = Rc::new(Cell::new(false));
4093        let model_release_observed = Rc::new(Cell::new(false));
4094        let view_released = Rc::new(Cell::new(false));
4095        let view_release_observed = Rc::new(Cell::new(false));
4096
4097        let model = cx.add_model(|_| Model {
4098            released: model_released.clone(),
4099        });
4100        let (window_id, view) = cx.add_window(Default::default(), |_| View {
4101            released: view_released.clone(),
4102        });
4103        assert!(!model_released.get());
4104        assert!(!view_released.get());
4105
4106        cx.observe_release(&model, {
4107            let model_release_observed = model_release_observed.clone();
4108            move |_| model_release_observed.set(true)
4109        })
4110        .detach();
4111        cx.observe_release(&view, {
4112            let view_release_observed = view_release_observed.clone();
4113            move |_| view_release_observed.set(true)
4114        })
4115        .detach();
4116
4117        cx.update(move |_| {
4118            drop(model);
4119        });
4120        assert!(model_released.get());
4121        assert!(model_release_observed.get());
4122
4123        drop(view);
4124        cx.remove_window(window_id);
4125        assert!(view_released.get());
4126        assert!(view_release_observed.get());
4127    }
4128
4129    #[crate::test(self)]
4130    fn test_subscribe_and_emit_from_view(cx: &mut MutableAppContext) {
4131        #[derive(Default)]
4132        struct View {
4133            events: Vec<usize>,
4134        }
4135
4136        impl Entity for View {
4137            type Event = usize;
4138        }
4139
4140        impl super::View for View {
4141            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4142                Empty::new().boxed()
4143            }
4144
4145            fn ui_name() -> &'static str {
4146                "View"
4147            }
4148        }
4149
4150        struct Model;
4151
4152        impl Entity for Model {
4153            type Event = usize;
4154        }
4155
4156        let (window_id, handle_1) = cx.add_window(Default::default(), |_| View::default());
4157        let handle_2 = cx.add_view(window_id, |_| View::default());
4158        let handle_2b = handle_2.clone();
4159        let handle_3 = cx.add_model(|_| Model);
4160
4161        handle_1.update(cx, |_, c| {
4162            c.subscribe(&handle_2, move |me, _, event, c| {
4163                me.events.push(*event);
4164
4165                c.subscribe(&handle_2b, |me, _, event, _| {
4166                    me.events.push(*event * 2);
4167                })
4168                .detach();
4169            })
4170            .detach();
4171
4172            c.subscribe(&handle_3, |me, _, event, _| {
4173                me.events.push(*event);
4174            })
4175            .detach();
4176        });
4177
4178        handle_2.update(cx, |_, c| c.emit(7));
4179        assert_eq!(handle_1.read(cx).events, vec![7]);
4180
4181        handle_2.update(cx, |_, c| c.emit(5));
4182        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
4183
4184        handle_3.update(cx, |_, c| c.emit(9));
4185        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10, 9]);
4186    }
4187
4188    #[crate::test(self)]
4189    fn test_dropping_subscribers(cx: &mut MutableAppContext) {
4190        struct View;
4191
4192        impl Entity for View {
4193            type Event = ();
4194        }
4195
4196        impl super::View for View {
4197            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4198                Empty::new().boxed()
4199            }
4200
4201            fn ui_name() -> &'static str {
4202                "View"
4203            }
4204        }
4205
4206        struct Model;
4207
4208        impl Entity for Model {
4209            type Event = ();
4210        }
4211
4212        let (window_id, _) = cx.add_window(Default::default(), |_| View);
4213        let observing_view = cx.add_view(window_id, |_| View);
4214        let emitting_view = cx.add_view(window_id, |_| View);
4215        let observing_model = cx.add_model(|_| Model);
4216        let observed_model = cx.add_model(|_| Model);
4217
4218        observing_view.update(cx, |_, cx| {
4219            cx.subscribe(&emitting_view, |_, _, _, _| {}).detach();
4220            cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
4221        });
4222        observing_model.update(cx, |_, cx| {
4223            cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
4224        });
4225
4226        cx.update(|_| {
4227            drop(observing_view);
4228            drop(observing_model);
4229        });
4230
4231        emitting_view.update(cx, |_, cx| cx.emit(()));
4232        observed_model.update(cx, |_, cx| cx.emit(()));
4233    }
4234
4235    #[crate::test(self)]
4236    fn test_observe_and_notify_from_view(cx: &mut MutableAppContext) {
4237        #[derive(Default)]
4238        struct View {
4239            events: Vec<usize>,
4240        }
4241
4242        impl Entity for View {
4243            type Event = usize;
4244        }
4245
4246        impl super::View for View {
4247            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4248                Empty::new().boxed()
4249            }
4250
4251            fn ui_name() -> &'static str {
4252                "View"
4253            }
4254        }
4255
4256        #[derive(Default)]
4257        struct Model {
4258            count: usize,
4259        }
4260
4261        impl Entity for Model {
4262            type Event = ();
4263        }
4264
4265        let (_, view) = cx.add_window(Default::default(), |_| View::default());
4266        let model = cx.add_model(|_| Model::default());
4267
4268        view.update(cx, |_, c| {
4269            c.observe(&model, |me, observed, c| {
4270                me.events.push(observed.read(c).count)
4271            })
4272            .detach();
4273        });
4274
4275        model.update(cx, |model, c| {
4276            model.count = 11;
4277            c.notify();
4278        });
4279        assert_eq!(view.read(cx).events, vec![11]);
4280    }
4281
4282    #[crate::test(self)]
4283    fn test_dropping_observers(cx: &mut MutableAppContext) {
4284        struct View;
4285
4286        impl Entity for View {
4287            type Event = ();
4288        }
4289
4290        impl super::View for View {
4291            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4292                Empty::new().boxed()
4293            }
4294
4295            fn ui_name() -> &'static str {
4296                "View"
4297            }
4298        }
4299
4300        struct Model;
4301
4302        impl Entity for Model {
4303            type Event = ();
4304        }
4305
4306        let (window_id, _) = cx.add_window(Default::default(), |_| View);
4307        let observing_view = cx.add_view(window_id, |_| View);
4308        let observing_model = cx.add_model(|_| Model);
4309        let observed_model = cx.add_model(|_| Model);
4310
4311        observing_view.update(cx, |_, cx| {
4312            cx.observe(&observed_model, |_, _, _| {}).detach();
4313        });
4314        observing_model.update(cx, |_, cx| {
4315            cx.observe(&observed_model, |_, _, _| {}).detach();
4316        });
4317
4318        cx.update(|_| {
4319            drop(observing_view);
4320            drop(observing_model);
4321        });
4322
4323        observed_model.update(cx, |_, cx| cx.notify());
4324    }
4325
4326    #[crate::test(self)]
4327    fn test_focus(cx: &mut MutableAppContext) {
4328        struct View {
4329            name: String,
4330            events: Arc<Mutex<Vec<String>>>,
4331        }
4332
4333        impl Entity for View {
4334            type Event = ();
4335        }
4336
4337        impl super::View for View {
4338            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4339                Empty::new().boxed()
4340            }
4341
4342            fn ui_name() -> &'static str {
4343                "View"
4344            }
4345
4346            fn on_focus(&mut self, _: &mut ViewContext<Self>) {
4347                self.events.lock().push(format!("{} focused", &self.name));
4348            }
4349
4350            fn on_blur(&mut self, _: &mut ViewContext<Self>) {
4351                self.events.lock().push(format!("{} blurred", &self.name));
4352            }
4353        }
4354
4355        let events: Arc<Mutex<Vec<String>>> = Default::default();
4356        let (window_id, view_1) = cx.add_window(Default::default(), |_| View {
4357            events: events.clone(),
4358            name: "view 1".to_string(),
4359        });
4360        let view_2 = cx.add_view(window_id, |_| View {
4361            events: events.clone(),
4362            name: "view 2".to_string(),
4363        });
4364
4365        view_1.update(cx, |_, cx| cx.focus(&view_2));
4366        view_1.update(cx, |_, cx| cx.focus(&view_1));
4367        view_1.update(cx, |_, cx| cx.focus(&view_2));
4368        view_1.update(cx, |_, _| drop(view_2));
4369
4370        assert_eq!(
4371            *events.lock(),
4372            [
4373                "view 1 focused".to_string(),
4374                "view 1 blurred".to_string(),
4375                "view 2 focused".to_string(),
4376                "view 2 blurred".to_string(),
4377                "view 1 focused".to_string(),
4378                "view 1 blurred".to_string(),
4379                "view 2 focused".to_string(),
4380                "view 1 focused".to_string(),
4381            ],
4382        );
4383    }
4384
4385    #[crate::test(self)]
4386    fn test_dispatch_action(cx: &mut MutableAppContext) {
4387        struct ViewA {
4388            id: usize,
4389        }
4390
4391        impl Entity for ViewA {
4392            type Event = ();
4393        }
4394
4395        impl View for ViewA {
4396            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4397                Empty::new().boxed()
4398            }
4399
4400            fn ui_name() -> &'static str {
4401                "View"
4402            }
4403        }
4404
4405        struct ViewB {
4406            id: usize,
4407        }
4408
4409        impl Entity for ViewB {
4410            type Event = ();
4411        }
4412
4413        impl View for ViewB {
4414            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4415                Empty::new().boxed()
4416            }
4417
4418            fn ui_name() -> &'static str {
4419                "View"
4420            }
4421        }
4422
4423        action!(Action, &'static str);
4424
4425        let actions = Rc::new(RefCell::new(Vec::new()));
4426
4427        {
4428            let actions = actions.clone();
4429            cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
4430                actions.borrow_mut().push("global".to_string());
4431            });
4432        }
4433
4434        {
4435            let actions = actions.clone();
4436            cx.add_action(move |view: &mut ViewA, action: &Action, cx| {
4437                assert_eq!(action.0, "bar");
4438                cx.propagate_action();
4439                actions.borrow_mut().push(format!("{} a", view.id));
4440            });
4441        }
4442
4443        {
4444            let actions = actions.clone();
4445            cx.add_action(move |view: &mut ViewA, _: &Action, cx| {
4446                if view.id != 1 {
4447                    cx.add_view(|cx| {
4448                        cx.propagate_action(); // Still works on a nested ViewContext
4449                        ViewB { id: 5 }
4450                    });
4451                }
4452                actions.borrow_mut().push(format!("{} b", view.id));
4453            });
4454        }
4455
4456        {
4457            let actions = actions.clone();
4458            cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
4459                cx.propagate_action();
4460                actions.borrow_mut().push(format!("{} c", view.id));
4461            });
4462        }
4463
4464        {
4465            let actions = actions.clone();
4466            cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
4467                cx.propagate_action();
4468                actions.borrow_mut().push(format!("{} d", view.id));
4469            });
4470        }
4471
4472        {
4473            let actions = actions.clone();
4474            cx.capture_action(move |view: &mut ViewA, _: &Action, cx| {
4475                cx.propagate_action();
4476                actions.borrow_mut().push(format!("{} capture", view.id));
4477            });
4478        }
4479
4480        let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
4481        let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 });
4482        let view_3 = cx.add_view(window_id, |_| ViewA { id: 3 });
4483        let view_4 = cx.add_view(window_id, |_| ViewB { id: 4 });
4484
4485        cx.dispatch_action(
4486            window_id,
4487            vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()],
4488            &Action("bar"),
4489        );
4490
4491        assert_eq!(
4492            *actions.borrow(),
4493            vec![
4494                "1 capture",
4495                "3 capture",
4496                "4 d",
4497                "4 c",
4498                "3 b",
4499                "3 a",
4500                "2 d",
4501                "2 c",
4502                "1 b"
4503            ]
4504        );
4505
4506        // Remove view_1, which doesn't propagate the action
4507        actions.borrow_mut().clear();
4508        cx.dispatch_action(
4509            window_id,
4510            vec![view_2.id(), view_3.id(), view_4.id()],
4511            &Action("bar"),
4512        );
4513
4514        assert_eq!(
4515            *actions.borrow(),
4516            vec![
4517                "3 capture",
4518                "4 d",
4519                "4 c",
4520                "3 b",
4521                "3 a",
4522                "2 d",
4523                "2 c",
4524                "global"
4525            ]
4526        );
4527    }
4528
4529    #[crate::test(self)]
4530    fn test_dispatch_keystroke(cx: &mut MutableAppContext) {
4531        use std::cell::Cell;
4532
4533        action!(Action, &'static str);
4534
4535        struct View {
4536            id: usize,
4537            keymap_context: keymap::Context,
4538        }
4539
4540        impl Entity for View {
4541            type Event = ();
4542        }
4543
4544        impl super::View for View {
4545            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4546                Empty::new().boxed()
4547            }
4548
4549            fn ui_name() -> &'static str {
4550                "View"
4551            }
4552
4553            fn keymap_context(&self, _: &AppContext) -> keymap::Context {
4554                self.keymap_context.clone()
4555            }
4556        }
4557
4558        impl View {
4559            fn new(id: usize) -> Self {
4560                View {
4561                    id,
4562                    keymap_context: keymap::Context::default(),
4563                }
4564            }
4565        }
4566
4567        let mut view_1 = View::new(1);
4568        let mut view_2 = View::new(2);
4569        let mut view_3 = View::new(3);
4570        view_1.keymap_context.set.insert("a".into());
4571        view_2.keymap_context.set.insert("a".into());
4572        view_2.keymap_context.set.insert("b".into());
4573        view_3.keymap_context.set.insert("a".into());
4574        view_3.keymap_context.set.insert("b".into());
4575        view_3.keymap_context.set.insert("c".into());
4576
4577        let (window_id, view_1) = cx.add_window(Default::default(), |_| view_1);
4578        let view_2 = cx.add_view(window_id, |_| view_2);
4579        let view_3 = cx.add_view(window_id, |_| view_3);
4580
4581        // This keymap's only binding dispatches an action on view 2 because that view will have
4582        // "a" and "b" in its context, but not "c".
4583        cx.add_bindings(vec![keymap::Binding::new(
4584            "a",
4585            Action("a"),
4586            Some("a && b && !c"),
4587        )]);
4588
4589        let handled_action = Rc::new(Cell::new(false));
4590        let handled_action_clone = handled_action.clone();
4591        cx.add_action(move |view: &mut View, action: &Action, _| {
4592            handled_action_clone.set(true);
4593            assert_eq!(view.id, 2);
4594            assert_eq!(action.0, "a");
4595        });
4596
4597        cx.dispatch_keystroke(
4598            window_id,
4599            vec![view_1.id(), view_2.id(), view_3.id()],
4600            &Keystroke::parse("a").unwrap(),
4601        )
4602        .unwrap();
4603
4604        assert!(handled_action.get());
4605    }
4606
4607    #[crate::test(self)]
4608    async fn test_model_condition(mut cx: TestAppContext) {
4609        struct Counter(usize);
4610
4611        impl super::Entity for Counter {
4612            type Event = ();
4613        }
4614
4615        impl Counter {
4616            fn inc(&mut self, cx: &mut ModelContext<Self>) {
4617                self.0 += 1;
4618                cx.notify();
4619            }
4620        }
4621
4622        let model = cx.add_model(|_| Counter(0));
4623
4624        let condition1 = model.condition(&cx, |model, _| model.0 == 2);
4625        let condition2 = model.condition(&cx, |model, _| model.0 == 3);
4626        smol::pin!(condition1, condition2);
4627
4628        model.update(&mut cx, |model, cx| model.inc(cx));
4629        assert_eq!(poll_once(&mut condition1).await, None);
4630        assert_eq!(poll_once(&mut condition2).await, None);
4631
4632        model.update(&mut cx, |model, cx| model.inc(cx));
4633        assert_eq!(poll_once(&mut condition1).await, Some(()));
4634        assert_eq!(poll_once(&mut condition2).await, None);
4635
4636        model.update(&mut cx, |model, cx| model.inc(cx));
4637        assert_eq!(poll_once(&mut condition2).await, Some(()));
4638
4639        model.update(&mut cx, |_, cx| cx.notify());
4640    }
4641
4642    #[crate::test(self)]
4643    #[should_panic]
4644    async fn test_model_condition_timeout(mut cx: TestAppContext) {
4645        struct Model;
4646
4647        impl super::Entity for Model {
4648            type Event = ();
4649        }
4650
4651        let model = cx.add_model(|_| Model);
4652        model.condition(&cx, |_, _| false).await;
4653    }
4654
4655    #[crate::test(self)]
4656    #[should_panic(expected = "model dropped with pending condition")]
4657    async fn test_model_condition_panic_on_drop(mut cx: TestAppContext) {
4658        struct Model;
4659
4660        impl super::Entity for Model {
4661            type Event = ();
4662        }
4663
4664        let model = cx.add_model(|_| Model);
4665        let condition = model.condition(&cx, |_, _| false);
4666        cx.update(|_| drop(model));
4667        condition.await;
4668    }
4669
4670    #[crate::test(self)]
4671    async fn test_view_condition(mut cx: TestAppContext) {
4672        struct Counter(usize);
4673
4674        impl super::Entity for Counter {
4675            type Event = ();
4676        }
4677
4678        impl super::View for Counter {
4679            fn ui_name() -> &'static str {
4680                "test view"
4681            }
4682
4683            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4684                Empty::new().boxed()
4685            }
4686        }
4687
4688        impl Counter {
4689            fn inc(&mut self, cx: &mut ViewContext<Self>) {
4690                self.0 += 1;
4691                cx.notify();
4692            }
4693        }
4694
4695        let (_, view) = cx.add_window(|_| Counter(0));
4696
4697        let condition1 = view.condition(&cx, |view, _| view.0 == 2);
4698        let condition2 = view.condition(&cx, |view, _| view.0 == 3);
4699        smol::pin!(condition1, condition2);
4700
4701        view.update(&mut cx, |view, cx| view.inc(cx));
4702        assert_eq!(poll_once(&mut condition1).await, None);
4703        assert_eq!(poll_once(&mut condition2).await, None);
4704
4705        view.update(&mut cx, |view, cx| view.inc(cx));
4706        assert_eq!(poll_once(&mut condition1).await, Some(()));
4707        assert_eq!(poll_once(&mut condition2).await, None);
4708
4709        view.update(&mut cx, |view, cx| view.inc(cx));
4710        assert_eq!(poll_once(&mut condition2).await, Some(()));
4711        view.update(&mut cx, |_, cx| cx.notify());
4712    }
4713
4714    #[crate::test(self)]
4715    #[should_panic]
4716    async fn test_view_condition_timeout(mut cx: TestAppContext) {
4717        struct View;
4718
4719        impl super::Entity for View {
4720            type Event = ();
4721        }
4722
4723        impl super::View for View {
4724            fn ui_name() -> &'static str {
4725                "test view"
4726            }
4727
4728            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4729                Empty::new().boxed()
4730            }
4731        }
4732
4733        let (_, view) = cx.add_window(|_| View);
4734        view.condition(&cx, |_, _| false).await;
4735    }
4736
4737    #[crate::test(self)]
4738    #[should_panic(expected = "view dropped with pending condition")]
4739    async fn test_view_condition_panic_on_drop(mut cx: TestAppContext) {
4740        struct View;
4741
4742        impl super::Entity for View {
4743            type Event = ();
4744        }
4745
4746        impl super::View for View {
4747            fn ui_name() -> &'static str {
4748                "test view"
4749            }
4750
4751            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
4752                Empty::new().boxed()
4753            }
4754        }
4755
4756        let window_id = cx.add_window(|_| View).0;
4757        let view = cx.add_view(window_id, |_| View);
4758
4759        let condition = view.condition(&cx, |_, _| false);
4760        cx.update(|_| drop(view));
4761        condition.await;
4762    }
4763}