app.rs

   1pub mod action;
   2
   3use crate::{
   4    elements::ElementBox,
   5    executor::{self, Task},
   6    keymap::{self, Binding, Keystroke},
   7    platform::{self, CursorStyle, Platform, PromptLevel, WindowOptions},
   8    presenter::Presenter,
   9    util::post_inc,
  10    AssetCache, AssetSource, ClipboardItem, FontCache, PathPromptOptions, TextLayoutCache,
  11};
  12pub use action::*;
  13use anyhow::{anyhow, Context, Result};
  14use collections::btree_map;
  15use keymap::MatchResult;
  16use lazy_static::lazy_static;
  17use parking_lot::Mutex;
  18use platform::Event;
  19use postage::oneshot;
  20use smallvec::SmallVec;
  21use smol::prelude::*;
  22use std::{
  23    any::{type_name, Any, TypeId},
  24    cell::RefCell,
  25    collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque},
  26    fmt::{self, Debug},
  27    hash::{Hash, Hasher},
  28    marker::PhantomData,
  29    mem,
  30    ops::{Deref, DerefMut},
  31    path::{Path, PathBuf},
  32    pin::Pin,
  33    rc::{self, Rc},
  34    sync::{
  35        atomic::{AtomicUsize, Ordering::SeqCst},
  36        Arc, Weak,
  37    },
  38    time::Duration,
  39};
  40
  41pub trait Entity: 'static {
  42    type Event;
  43
  44    fn release(&mut self, _: &mut MutableAppContext) {}
  45    fn app_will_quit(
  46        &mut self,
  47        _: &mut MutableAppContext,
  48    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
  49        None
  50    }
  51}
  52
  53pub trait View: Entity + Sized {
  54    fn ui_name() -> &'static str;
  55    fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox;
  56    fn on_focus(&mut self, _: &mut ViewContext<Self>) {}
  57    fn on_blur(&mut self, _: &mut ViewContext<Self>) {}
  58    fn keymap_context(&self, _: &AppContext) -> keymap::Context {
  59        Self::default_keymap_context()
  60    }
  61    fn default_keymap_context() -> keymap::Context {
  62        let mut cx = keymap::Context::default();
  63        cx.set.insert(Self::ui_name().into());
  64        cx
  65    }
  66    fn debug_json(&self, _: &AppContext) -> serde_json::Value {
  67        serde_json::Value::Null
  68    }
  69}
  70
  71pub trait ReadModel {
  72    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T;
  73}
  74
  75pub trait ReadModelWith {
  76    fn read_model_with<E: Entity, T>(
  77        &self,
  78        handle: &ModelHandle<E>,
  79        read: &mut dyn FnMut(&E, &AppContext) -> T,
  80    ) -> T;
  81}
  82
  83pub trait UpdateModel {
  84    fn update_model<T: Entity, O>(
  85        &mut self,
  86        handle: &ModelHandle<T>,
  87        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
  88    ) -> O;
  89}
  90
  91pub trait UpgradeModelHandle {
  92    fn upgrade_model_handle<T: Entity>(
  93        &self,
  94        handle: &WeakModelHandle<T>,
  95    ) -> Option<ModelHandle<T>>;
  96
  97    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool;
  98
  99    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle>;
 100}
 101
 102pub trait UpgradeViewHandle {
 103    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>>;
 104
 105    fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle>;
 106}
 107
 108pub trait ReadView {
 109    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
 110}
 111
 112pub trait ReadViewWith {
 113    fn read_view_with<V, T>(
 114        &self,
 115        handle: &ViewHandle<V>,
 116        read: &mut dyn FnMut(&V, &AppContext) -> T,
 117    ) -> T
 118    where
 119        V: View;
 120}
 121
 122pub trait UpdateView {
 123    fn update_view<T, S>(
 124        &mut self,
 125        handle: &ViewHandle<T>,
 126        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 127    ) -> S
 128    where
 129        T: View;
 130}
 131
 132pub trait ElementStateContext: DerefMut<Target = MutableAppContext> {
 133    fn current_view_id(&self) -> usize;
 134
 135    fn element_state<Tag: 'static, T: 'static + Default>(
 136        &mut self,
 137        element_id: usize,
 138    ) -> ElementStateHandle<T> {
 139        let id = ElementStateId {
 140            view_id: self.current_view_id(),
 141            element_id,
 142            tag: TypeId::of::<Tag>(),
 143        };
 144        self.cx
 145            .element_states
 146            .entry(id)
 147            .or_insert_with(|| Box::new(T::default()));
 148        ElementStateHandle::new(id, self.frame_count, &self.cx.ref_counts)
 149    }
 150}
 151
 152pub struct Menu<'a> {
 153    pub name: &'a str,
 154    pub items: Vec<MenuItem<'a>>,
 155}
 156
 157pub enum MenuItem<'a> {
 158    Action {
 159        name: &'a str,
 160        keystroke: Option<&'a str>,
 161        action: Box<dyn Action>,
 162    },
 163    Separator,
 164}
 165
 166#[derive(Clone)]
 167pub struct App(Rc<RefCell<MutableAppContext>>);
 168
 169#[derive(Clone)]
 170pub struct AsyncAppContext(Rc<RefCell<MutableAppContext>>);
 171
 172#[cfg(any(test, feature = "test-support"))]
 173pub struct TestAppContext {
 174    cx: Rc<RefCell<MutableAppContext>>,
 175    foreground_platform: Rc<platform::test::ForegroundPlatform>,
 176}
 177
 178impl App {
 179    pub fn new(asset_source: impl AssetSource) -> Result<Self> {
 180        let platform = platform::current::platform();
 181        let foreground_platform = platform::current::foreground_platform();
 182        let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
 183        let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
 184            foreground,
 185            Arc::new(executor::Background::new()),
 186            platform.clone(),
 187            foreground_platform.clone(),
 188            Arc::new(FontCache::new(platform.fonts())),
 189            Default::default(),
 190            asset_source,
 191        ))));
 192
 193        foreground_platform.on_quit(Box::new({
 194            let cx = app.0.clone();
 195            move || {
 196                cx.borrow_mut().quit();
 197            }
 198        }));
 199        foreground_platform.on_menu_command(Box::new({
 200            let cx = app.0.clone();
 201            move |action| {
 202                let mut cx = cx.borrow_mut();
 203                if let Some(key_window_id) = cx.cx.platform.key_window_id() {
 204                    if let Some((presenter, _)) =
 205                        cx.presenters_and_platform_windows.get(&key_window_id)
 206                    {
 207                        let presenter = presenter.clone();
 208                        let path = presenter.borrow().dispatch_path(cx.as_ref());
 209                        cx.dispatch_action_any(key_window_id, &path, action);
 210                    } else {
 211                        cx.dispatch_global_action_any(action);
 212                    }
 213                } else {
 214                    cx.dispatch_global_action_any(action);
 215                }
 216            }
 217        }));
 218
 219        app.0.borrow_mut().weak_self = Some(Rc::downgrade(&app.0));
 220        Ok(app)
 221    }
 222
 223    pub fn background(&self) -> Arc<executor::Background> {
 224        self.0.borrow().background().clone()
 225    }
 226
 227    pub fn on_become_active<F>(self, mut callback: F) -> Self
 228    where
 229        F: 'static + FnMut(&mut MutableAppContext),
 230    {
 231        let cx = self.0.clone();
 232        self.0
 233            .borrow_mut()
 234            .foreground_platform
 235            .on_become_active(Box::new(move || callback(&mut *cx.borrow_mut())));
 236        self
 237    }
 238
 239    pub fn on_resign_active<F>(self, mut callback: F) -> Self
 240    where
 241        F: 'static + FnMut(&mut MutableAppContext),
 242    {
 243        let cx = self.0.clone();
 244        self.0
 245            .borrow_mut()
 246            .foreground_platform
 247            .on_resign_active(Box::new(move || callback(&mut *cx.borrow_mut())));
 248        self
 249    }
 250
 251    pub fn on_quit<F>(&mut self, mut callback: F) -> &mut Self
 252    where
 253        F: 'static + FnMut(&mut MutableAppContext),
 254    {
 255        let cx = self.0.clone();
 256        self.0
 257            .borrow_mut()
 258            .foreground_platform
 259            .on_quit(Box::new(move || callback(&mut *cx.borrow_mut())));
 260        self
 261    }
 262
 263    pub fn on_event<F>(&mut self, mut callback: F) -> &mut Self
 264    where
 265        F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
 266    {
 267        let cx = self.0.clone();
 268        self.0
 269            .borrow_mut()
 270            .foreground_platform
 271            .on_event(Box::new(move |event| {
 272                callback(event, &mut *cx.borrow_mut())
 273            }));
 274        self
 275    }
 276
 277    pub fn on_open_urls<F>(&mut self, mut callback: F) -> &mut Self
 278    where
 279        F: 'static + FnMut(Vec<String>, &mut MutableAppContext),
 280    {
 281        let cx = self.0.clone();
 282        self.0
 283            .borrow_mut()
 284            .foreground_platform
 285            .on_open_urls(Box::new(move |paths| {
 286                callback(paths, &mut *cx.borrow_mut())
 287            }));
 288        self
 289    }
 290
 291    pub fn run<F>(self, on_finish_launching: F)
 292    where
 293        F: 'static + FnOnce(&mut MutableAppContext),
 294    {
 295        let platform = self.0.borrow().foreground_platform.clone();
 296        platform.run(Box::new(move || {
 297            let mut cx = self.0.borrow_mut();
 298            let cx = &mut *cx;
 299            crate::views::init(cx);
 300            on_finish_launching(cx);
 301        }))
 302    }
 303
 304    pub fn platform(&self) -> Arc<dyn Platform> {
 305        self.0.borrow().platform()
 306    }
 307
 308    pub fn font_cache(&self) -> Arc<FontCache> {
 309        self.0.borrow().cx.font_cache.clone()
 310    }
 311
 312    fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 313        let mut state = self.0.borrow_mut();
 314        let result = state.update(callback);
 315        state.pending_notifications.clear();
 316        result
 317    }
 318}
 319
 320#[cfg(any(test, feature = "test-support"))]
 321impl TestAppContext {
 322    pub fn new(
 323        foreground_platform: Rc<platform::test::ForegroundPlatform>,
 324        platform: Arc<dyn Platform>,
 325        foreground: Rc<executor::Foreground>,
 326        background: Arc<executor::Background>,
 327        font_cache: Arc<FontCache>,
 328        leak_detector: Arc<Mutex<LeakDetector>>,
 329        first_entity_id: usize,
 330    ) -> Self {
 331        let mut cx = MutableAppContext::new(
 332            foreground.clone(),
 333            background,
 334            platform,
 335            foreground_platform.clone(),
 336            font_cache,
 337            RefCounts {
 338                #[cfg(any(test, feature = "test-support"))]
 339                leak_detector,
 340                ..Default::default()
 341            },
 342            (),
 343        );
 344        cx.next_entity_id = first_entity_id;
 345        let cx = TestAppContext {
 346            cx: Rc::new(RefCell::new(cx)),
 347            foreground_platform,
 348        };
 349        cx.cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx.cx));
 350        cx
 351    }
 352
 353    pub fn dispatch_action<A: Action>(&self, window_id: usize, action: A) {
 354        let mut cx = self.cx.borrow_mut();
 355        let dispatch_path = cx
 356            .presenters_and_platform_windows
 357            .get(&window_id)
 358            .unwrap()
 359            .0
 360            .borrow()
 361            .dispatch_path(cx.as_ref());
 362
 363        cx.dispatch_action_any(window_id, &dispatch_path, &action);
 364    }
 365
 366    pub fn dispatch_global_action<A: Action>(&self, action: A) {
 367        self.cx.borrow_mut().dispatch_global_action(action);
 368    }
 369
 370    pub fn dispatch_keystroke(
 371        &mut self,
 372        window_id: usize,
 373        keystroke: Keystroke,
 374        input: Option<String>,
 375        is_held: bool,
 376    ) {
 377        self.cx.borrow_mut().update(|cx| {
 378            let presenter = cx
 379                .presenters_and_platform_windows
 380                .get(&window_id)
 381                .unwrap()
 382                .0
 383                .clone();
 384            let dispatch_path = presenter.borrow().dispatch_path(cx.as_ref());
 385
 386            if !cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) {
 387                presenter.borrow_mut().dispatch_event(
 388                    Event::KeyDown {
 389                        keystroke,
 390                        input,
 391                        is_held,
 392                    },
 393                    cx,
 394                );
 395            }
 396        });
 397    }
 398
 399    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
 400    where
 401        T: Entity,
 402        F: FnOnce(&mut ModelContext<T>) -> T,
 403    {
 404        self.cx.borrow_mut().add_model(build_model)
 405    }
 406
 407    pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
 408    where
 409        T: View,
 410        F: FnOnce(&mut ViewContext<T>) -> T,
 411    {
 412        self.cx
 413            .borrow_mut()
 414            .add_window(Default::default(), build_root_view)
 415    }
 416
 417    pub fn window_ids(&self) -> Vec<usize> {
 418        self.cx.borrow().window_ids().collect()
 419    }
 420
 421    pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
 422        self.cx.borrow().root_view(window_id)
 423    }
 424
 425    pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
 426    where
 427        T: View,
 428        F: FnOnce(&mut ViewContext<T>) -> T,
 429    {
 430        self.cx.borrow_mut().add_view(window_id, build_view)
 431    }
 432
 433    pub fn add_option_view<T, F>(
 434        &mut self,
 435        window_id: usize,
 436        build_view: F,
 437    ) -> Option<ViewHandle<T>>
 438    where
 439        T: View,
 440        F: FnOnce(&mut ViewContext<T>) -> Option<T>,
 441    {
 442        self.cx.borrow_mut().add_option_view(window_id, build_view)
 443    }
 444
 445    pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
 446        callback(self.cx.borrow().as_ref())
 447    }
 448
 449    pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 450        let mut state = self.cx.borrow_mut();
 451        // Don't increment pending flushes in order for effects to be flushed before the callback
 452        // completes, which is helpful in tests.
 453        let result = callback(&mut *state);
 454        // Flush effects after the callback just in case there are any. This can happen in edge
 455        // cases such as the closure dropping handles.
 456        state.flush_effects();
 457        result
 458    }
 459
 460    pub fn to_async(&self) -> AsyncAppContext {
 461        AsyncAppContext(self.cx.clone())
 462    }
 463
 464    pub fn font_cache(&self) -> Arc<FontCache> {
 465        self.cx.borrow().cx.font_cache.clone()
 466    }
 467
 468    pub fn foreground_platform(&self) -> Rc<platform::test::ForegroundPlatform> {
 469        self.foreground_platform.clone()
 470    }
 471
 472    pub fn platform(&self) -> Arc<dyn platform::Platform> {
 473        self.cx.borrow().cx.platform.clone()
 474    }
 475
 476    pub fn foreground(&self) -> Rc<executor::Foreground> {
 477        self.cx.borrow().foreground().clone()
 478    }
 479
 480    pub fn background(&self) -> Arc<executor::Background> {
 481        self.cx.borrow().background().clone()
 482    }
 483
 484    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
 485    where
 486        F: FnOnce(AsyncAppContext) -> Fut,
 487        Fut: 'static + Future<Output = T>,
 488        T: 'static,
 489    {
 490        self.cx.borrow_mut().spawn(f)
 491    }
 492
 493    pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
 494        self.foreground_platform.simulate_new_path_selection(result);
 495    }
 496
 497    pub fn did_prompt_for_new_path(&self) -> bool {
 498        self.foreground_platform.as_ref().did_prompt_for_new_path()
 499    }
 500
 501    pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) {
 502        use postage::prelude::Sink as _;
 503
 504        let mut state = self.cx.borrow_mut();
 505        let (_, window) = state
 506            .presenters_and_platform_windows
 507            .get_mut(&window_id)
 508            .unwrap();
 509        let test_window = window
 510            .as_any_mut()
 511            .downcast_mut::<platform::test::Window>()
 512            .unwrap();
 513        let mut done_tx = test_window
 514            .last_prompt
 515            .take()
 516            .expect("prompt was not called");
 517        let _ = done_tx.try_send(answer);
 518    }
 519
 520    #[cfg(any(test, feature = "test-support"))]
 521    pub fn leak_detector(&self) -> Arc<Mutex<LeakDetector>> {
 522        self.cx.borrow().leak_detector()
 523    }
 524
 525    #[cfg(any(test, feature = "test-support"))]
 526    pub fn assert_dropped(&self, handle: impl WeakHandle) {
 527        self.cx
 528            .borrow()
 529            .leak_detector()
 530            .lock()
 531            .assert_dropped(handle.id())
 532    }
 533}
 534
 535impl AsyncAppContext {
 536    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
 537    where
 538        F: FnOnce(AsyncAppContext) -> Fut,
 539        Fut: 'static + Future<Output = T>,
 540        T: 'static,
 541    {
 542        self.0.borrow().foreground.spawn(f(self.clone()))
 543    }
 544
 545    pub fn read<T, F: FnOnce(&AppContext) -> T>(&mut self, callback: F) -> T {
 546        callback(self.0.borrow().as_ref())
 547    }
 548
 549    pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
 550        self.0.borrow_mut().update(callback)
 551    }
 552
 553    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
 554    where
 555        T: Entity,
 556        F: FnOnce(&mut ModelContext<T>) -> T,
 557    {
 558        self.update(|cx| cx.add_model(build_model))
 559    }
 560
 561    pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
 562    where
 563        T: View,
 564        F: FnOnce(&mut ViewContext<T>) -> T,
 565    {
 566        self.update(|cx| cx.add_view(window_id, build_view))
 567    }
 568
 569    pub fn platform(&self) -> Arc<dyn Platform> {
 570        self.0.borrow().platform()
 571    }
 572
 573    pub fn foreground(&self) -> Rc<executor::Foreground> {
 574        self.0.borrow().foreground.clone()
 575    }
 576
 577    pub fn background(&self) -> Arc<executor::Background> {
 578        self.0.borrow().cx.background.clone()
 579    }
 580}
 581
 582impl UpdateModel for AsyncAppContext {
 583    fn update_model<E: Entity, O>(
 584        &mut self,
 585        handle: &ModelHandle<E>,
 586        update: &mut dyn FnMut(&mut E, &mut ModelContext<E>) -> O,
 587    ) -> O {
 588        self.0.borrow_mut().update_model(handle, update)
 589    }
 590}
 591
 592impl UpgradeModelHandle for AsyncAppContext {
 593    fn upgrade_model_handle<T: Entity>(
 594        &self,
 595        handle: &WeakModelHandle<T>,
 596    ) -> Option<ModelHandle<T>> {
 597        self.0.borrow().upgrade_model_handle(handle)
 598    }
 599
 600    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
 601        self.0.borrow().model_handle_is_upgradable(handle)
 602    }
 603
 604    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
 605        self.0.borrow().upgrade_any_model_handle(handle)
 606    }
 607}
 608
 609impl UpgradeViewHandle for AsyncAppContext {
 610    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
 611        self.0.borrow_mut().upgrade_view_handle(handle)
 612    }
 613
 614    fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
 615        self.0.borrow_mut().upgrade_any_view_handle(handle)
 616    }
 617}
 618
 619impl ReadModelWith for AsyncAppContext {
 620    fn read_model_with<E: Entity, T>(
 621        &self,
 622        handle: &ModelHandle<E>,
 623        read: &mut dyn FnMut(&E, &AppContext) -> T,
 624    ) -> T {
 625        let cx = self.0.borrow();
 626        let cx = cx.as_ref();
 627        read(handle.read(cx), cx)
 628    }
 629}
 630
 631impl UpdateView for AsyncAppContext {
 632    fn update_view<T, S>(
 633        &mut self,
 634        handle: &ViewHandle<T>,
 635        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 636    ) -> S
 637    where
 638        T: View,
 639    {
 640        self.0.borrow_mut().update_view(handle, update)
 641    }
 642}
 643
 644impl ReadViewWith for AsyncAppContext {
 645    fn read_view_with<V, T>(
 646        &self,
 647        handle: &ViewHandle<V>,
 648        read: &mut dyn FnMut(&V, &AppContext) -> T,
 649    ) -> T
 650    where
 651        V: View,
 652    {
 653        let cx = self.0.borrow();
 654        let cx = cx.as_ref();
 655        read(handle.read(cx), cx)
 656    }
 657}
 658
 659#[cfg(any(test, feature = "test-support"))]
 660impl UpdateModel for TestAppContext {
 661    fn update_model<T: Entity, O>(
 662        &mut self,
 663        handle: &ModelHandle<T>,
 664        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
 665    ) -> O {
 666        self.cx.borrow_mut().update_model(handle, update)
 667    }
 668}
 669
 670#[cfg(any(test, feature = "test-support"))]
 671impl ReadModelWith for TestAppContext {
 672    fn read_model_with<E: Entity, T>(
 673        &self,
 674        handle: &ModelHandle<E>,
 675        read: &mut dyn FnMut(&E, &AppContext) -> T,
 676    ) -> T {
 677        let cx = self.cx.borrow();
 678        let cx = cx.as_ref();
 679        read(handle.read(cx), cx)
 680    }
 681}
 682
 683#[cfg(any(test, feature = "test-support"))]
 684impl UpdateView for TestAppContext {
 685    fn update_view<T, S>(
 686        &mut self,
 687        handle: &ViewHandle<T>,
 688        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
 689    ) -> S
 690    where
 691        T: View,
 692    {
 693        self.cx.borrow_mut().update_view(handle, update)
 694    }
 695}
 696
 697#[cfg(any(test, feature = "test-support"))]
 698impl ReadViewWith for TestAppContext {
 699    fn read_view_with<V, T>(
 700        &self,
 701        handle: &ViewHandle<V>,
 702        read: &mut dyn FnMut(&V, &AppContext) -> T,
 703    ) -> T
 704    where
 705        V: View,
 706    {
 707        let cx = self.cx.borrow();
 708        let cx = cx.as_ref();
 709        read(handle.read(cx), cx)
 710    }
 711}
 712
 713type ActionCallback =
 714    dyn FnMut(&mut dyn AnyView, &dyn Action, &mut MutableAppContext, usize, usize);
 715type GlobalActionCallback = dyn FnMut(&dyn Action, &mut MutableAppContext);
 716
 717type SubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext) -> bool>;
 718type GlobalSubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext)>;
 719type ObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
 720type FocusObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
 721type GlobalObservationCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext)>;
 722type ReleaseObservationCallback = Box<dyn FnOnce(&dyn Any, &mut MutableAppContext)>;
 723type DeserializeActionCallback = fn(json: &str) -> anyhow::Result<Box<dyn Action>>;
 724
 725pub struct MutableAppContext {
 726    weak_self: Option<rc::Weak<RefCell<Self>>>,
 727    foreground_platform: Rc<dyn platform::ForegroundPlatform>,
 728    assets: Arc<AssetCache>,
 729    cx: AppContext,
 730    action_deserializers: HashMap<&'static str, (TypeId, DeserializeActionCallback)>,
 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, Option<SubscriptionCallback>>>>>,
 740    global_subscriptions:
 741        Arc<Mutex<HashMap<TypeId, BTreeMap<usize, Option<GlobalSubscriptionCallback>>>>>,
 742    observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, Option<ObservationCallback>>>>>,
 743    focus_observations:
 744        Arc<Mutex<HashMap<usize, BTreeMap<usize, Option<FocusObservationCallback>>>>>,
 745    global_observations:
 746        Arc<Mutex<HashMap<TypeId, BTreeMap<usize, Option<GlobalObservationCallback>>>>>,
 747    release_observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>,
 748    presenters_and_platform_windows:
 749        HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
 750    foreground: Rc<executor::Foreground>,
 751    pending_effects: VecDeque<Effect>,
 752    pending_focus_index: Option<usize>,
 753    pending_notifications: HashSet<usize>,
 754    pending_global_notifications: HashSet<TypeId>,
 755    pending_flushes: usize,
 756    flushing_effects: bool,
 757    next_cursor_style_handle_id: Arc<AtomicUsize>,
 758    halt_action_dispatch: bool,
 759}
 760
 761impl MutableAppContext {
 762    fn new(
 763        foreground: Rc<executor::Foreground>,
 764        background: Arc<executor::Background>,
 765        platform: Arc<dyn platform::Platform>,
 766        foreground_platform: Rc<dyn platform::ForegroundPlatform>,
 767        font_cache: Arc<FontCache>,
 768        ref_counts: RefCounts,
 769        asset_source: impl AssetSource,
 770    ) -> Self {
 771        Self {
 772            weak_self: None,
 773            foreground_platform,
 774            assets: Arc::new(AssetCache::new(asset_source)),
 775            cx: AppContext {
 776                models: Default::default(),
 777                views: Default::default(),
 778                windows: Default::default(),
 779                globals: Default::default(),
 780                element_states: Default::default(),
 781                ref_counts: Arc::new(Mutex::new(ref_counts)),
 782                background,
 783                font_cache,
 784                platform,
 785            },
 786            action_deserializers: HashMap::new(),
 787            capture_actions: HashMap::new(),
 788            actions: HashMap::new(),
 789            global_actions: HashMap::new(),
 790            keystroke_matcher: keymap::Matcher::default(),
 791            next_entity_id: 0,
 792            next_window_id: 0,
 793            next_subscription_id: 0,
 794            frame_count: 0,
 795            subscriptions: Default::default(),
 796            global_subscriptions: Default::default(),
 797            observations: Default::default(),
 798            focus_observations: Default::default(),
 799            release_observations: Default::default(),
 800            global_observations: Default::default(),
 801            presenters_and_platform_windows: HashMap::new(),
 802            foreground,
 803            pending_effects: VecDeque::new(),
 804            pending_focus_index: None,
 805            pending_notifications: HashSet::new(),
 806            pending_global_notifications: HashSet::new(),
 807            pending_flushes: 0,
 808            flushing_effects: false,
 809            next_cursor_style_handle_id: Default::default(),
 810            halt_action_dispatch: false,
 811        }
 812    }
 813
 814    pub fn upgrade(&self) -> App {
 815        App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
 816    }
 817
 818    pub fn quit(&mut self) {
 819        let mut futures = Vec::new();
 820        for model_id in self.cx.models.keys().copied().collect::<Vec<_>>() {
 821            let mut model = self.cx.models.remove(&model_id).unwrap();
 822            futures.extend(model.app_will_quit(self));
 823            self.cx.models.insert(model_id, model);
 824        }
 825
 826        for view_id in self.cx.views.keys().copied().collect::<Vec<_>>() {
 827            let mut view = self.cx.views.remove(&view_id).unwrap();
 828            futures.extend(view.app_will_quit(self));
 829            self.cx.views.insert(view_id, view);
 830        }
 831
 832        self.remove_all_windows();
 833
 834        let futures = futures::future::join_all(futures);
 835        if self
 836            .background
 837            .block_with_timeout(Duration::from_millis(100), futures)
 838            .is_err()
 839        {
 840            log::error!("timed out waiting on app_will_quit");
 841        }
 842    }
 843
 844    pub fn remove_all_windows(&mut self) {
 845        for (window_id, _) in self.cx.windows.drain() {
 846            self.presenters_and_platform_windows.remove(&window_id);
 847        }
 848        self.flush_effects();
 849    }
 850
 851    pub fn platform(&self) -> Arc<dyn platform::Platform> {
 852        self.cx.platform.clone()
 853    }
 854
 855    pub fn font_cache(&self) -> &Arc<FontCache> {
 856        &self.cx.font_cache
 857    }
 858
 859    pub fn foreground(&self) -> &Rc<executor::Foreground> {
 860        &self.foreground
 861    }
 862
 863    pub fn background(&self) -> &Arc<executor::Background> {
 864        &self.cx.background
 865    }
 866
 867    pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
 868        self.presenters_and_platform_windows
 869            .get(&window_id)
 870            .and_then(|(presenter, _)| presenter.borrow().debug_elements(self))
 871    }
 872
 873    pub fn deserialize_action(
 874        &self,
 875        name: &str,
 876        argument: Option<&str>,
 877    ) -> Result<Box<dyn Action>> {
 878        let callback = self
 879            .action_deserializers
 880            .get(name)
 881            .ok_or_else(|| anyhow!("unknown action {}", name))?
 882            .1;
 883        callback(argument.unwrap_or("{}"))
 884            .with_context(|| format!("invalid data for action {}", name))
 885    }
 886
 887    pub fn add_action<A, V, F>(&mut self, handler: F)
 888    where
 889        A: Action,
 890        V: View,
 891        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 892    {
 893        self.add_action_internal(handler, false)
 894    }
 895
 896    pub fn capture_action<A, V, F>(&mut self, handler: F)
 897    where
 898        A: Action,
 899        V: View,
 900        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 901    {
 902        self.add_action_internal(handler, true)
 903    }
 904
 905    fn add_action_internal<A, V, F>(&mut self, mut handler: F, capture: bool)
 906    where
 907        A: Action,
 908        V: View,
 909        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
 910    {
 911        let handler = Box::new(
 912            move |view: &mut dyn AnyView,
 913                  action: &dyn Action,
 914                  cx: &mut MutableAppContext,
 915                  window_id: usize,
 916                  view_id: usize| {
 917                let action = action.as_any().downcast_ref().unwrap();
 918                let mut cx = ViewContext::new(cx, window_id, view_id);
 919                handler(
 920                    view.as_any_mut()
 921                        .downcast_mut()
 922                        .expect("downcast is type safe"),
 923                    action,
 924                    &mut cx,
 925                );
 926            },
 927        );
 928
 929        self.action_deserializers
 930            .entry(A::qualified_name())
 931            .or_insert((TypeId::of::<A>(), A::from_json_str));
 932
 933        let actions = if capture {
 934            &mut self.capture_actions
 935        } else {
 936            &mut self.actions
 937        };
 938
 939        actions
 940            .entry(TypeId::of::<V>())
 941            .or_default()
 942            .entry(TypeId::of::<A>())
 943            .or_default()
 944            .push(handler);
 945    }
 946
 947    pub fn add_async_action<A, V, F>(&mut self, mut handler: F)
 948    where
 949        A: Action,
 950        V: View,
 951        F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>) -> Option<Task<Result<()>>>,
 952    {
 953        self.add_action(move |view, action, cx| {
 954            handler(view, action, cx).map(|task| task.detach_and_log_err(cx));
 955        })
 956    }
 957
 958    pub fn add_global_action<A, F>(&mut self, mut handler: F)
 959    where
 960        A: Action,
 961        F: 'static + FnMut(&A, &mut MutableAppContext),
 962    {
 963        let handler = Box::new(move |action: &dyn Action, cx: &mut MutableAppContext| {
 964            let action = action.as_any().downcast_ref().unwrap();
 965            handler(action, cx);
 966        });
 967
 968        self.action_deserializers
 969            .entry(A::qualified_name())
 970            .or_insert((TypeId::of::<A>(), A::from_json_str));
 971
 972        if self
 973            .global_actions
 974            .insert(TypeId::of::<A>(), handler)
 975            .is_some()
 976        {
 977            panic!(
 978                "registered multiple global handlers for {}",
 979                type_name::<A>()
 980            );
 981        }
 982    }
 983
 984    pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
 985        self.cx.windows.keys().cloned()
 986    }
 987
 988    pub fn activate_window(&self, window_id: usize) {
 989        if let Some((_, window)) = self.presenters_and_platform_windows.get(&window_id) {
 990            window.activate()
 991        }
 992    }
 993
 994    pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
 995        self.cx
 996            .windows
 997            .get(&window_id)
 998            .and_then(|window| window.root_view.clone().downcast::<T>())
 999    }
1000
1001    pub fn render_view(
1002        &mut self,
1003        window_id: usize,
1004        view_id: usize,
1005        titlebar_height: f32,
1006        refreshing: bool,
1007    ) -> Result<ElementBox> {
1008        let mut view = self
1009            .cx
1010            .views
1011            .remove(&(window_id, view_id))
1012            .ok_or(anyhow!("view not found"))?;
1013        let element = view.render(window_id, view_id, titlebar_height, refreshing, self);
1014        self.cx.views.insert((window_id, view_id), view);
1015        Ok(element)
1016    }
1017
1018    pub fn render_views(
1019        &mut self,
1020        window_id: usize,
1021        titlebar_height: f32,
1022    ) -> HashMap<usize, ElementBox> {
1023        self.start_frame();
1024        let view_ids = self
1025            .views
1026            .keys()
1027            .filter_map(|(win_id, view_id)| {
1028                if *win_id == window_id {
1029                    Some(*view_id)
1030                } else {
1031                    None
1032                }
1033            })
1034            .collect::<Vec<_>>();
1035        view_ids
1036            .into_iter()
1037            .map(|view_id| {
1038                (
1039                    view_id,
1040                    self.render_view(window_id, view_id, titlebar_height, false)
1041                        .unwrap(),
1042                )
1043            })
1044            .collect()
1045    }
1046
1047    pub(crate) fn start_frame(&mut self) {
1048        self.frame_count += 1;
1049    }
1050
1051    pub fn update<T, F: FnOnce(&mut Self) -> T>(&mut self, callback: F) -> T {
1052        self.pending_flushes += 1;
1053        let result = callback(self);
1054        self.flush_effects();
1055        result
1056    }
1057
1058    pub fn set_menus(&mut self, menus: Vec<Menu>) {
1059        self.foreground_platform.set_menus(menus);
1060    }
1061
1062    fn prompt(
1063        &self,
1064        window_id: usize,
1065        level: PromptLevel,
1066        msg: &str,
1067        answers: &[&str],
1068    ) -> oneshot::Receiver<usize> {
1069        let (_, window) = &self.presenters_and_platform_windows[&window_id];
1070        window.prompt(level, msg, answers)
1071    }
1072
1073    pub fn prompt_for_paths(
1074        &self,
1075        options: PathPromptOptions,
1076    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
1077        self.foreground_platform.prompt_for_paths(options)
1078    }
1079
1080    pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
1081        self.foreground_platform.prompt_for_new_path(directory)
1082    }
1083
1084    pub fn emit_global<E: Any>(&mut self, payload: E) {
1085        self.pending_effects.push_back(Effect::GlobalEvent {
1086            payload: Box::new(payload),
1087        });
1088    }
1089
1090    pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1091    where
1092        E: Entity,
1093        E::Event: 'static,
1094        H: Handle<E>,
1095        F: 'static + FnMut(H, &E::Event, &mut Self),
1096    {
1097        self.subscribe_internal(handle, move |handle, event, cx| {
1098            callback(handle, event, cx);
1099            true
1100        })
1101    }
1102
1103    pub fn subscribe_global<E, F>(&mut self, mut callback: F) -> Subscription
1104    where
1105        E: Any,
1106        F: 'static + FnMut(&E, &mut Self),
1107    {
1108        let subscription_id = post_inc(&mut self.next_subscription_id);
1109        let type_id = TypeId::of::<E>();
1110        self.pending_effects.push_back(Effect::GlobalSubscription {
1111            type_id,
1112            subscription_id,
1113            callback: Box::new(move |payload, cx| {
1114                let payload = payload.downcast_ref().expect("downcast is type safe");
1115                callback(payload, cx)
1116            }),
1117        });
1118        Subscription::GlobalSubscription {
1119            id: subscription_id,
1120            type_id,
1121            subscriptions: Some(Arc::downgrade(&self.global_subscriptions)),
1122        }
1123    }
1124
1125    pub fn observe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1126    where
1127        E: Entity,
1128        E::Event: 'static,
1129        H: Handle<E>,
1130        F: 'static + FnMut(H, &mut Self),
1131    {
1132        self.observe_internal(handle, move |handle, cx| {
1133            callback(handle, cx);
1134            true
1135        })
1136    }
1137
1138    pub fn subscribe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1139    where
1140        E: Entity,
1141        E::Event: 'static,
1142        H: Handle<E>,
1143        F: 'static + FnMut(H, &E::Event, &mut Self) -> bool,
1144    {
1145        let subscription_id = post_inc(&mut self.next_subscription_id);
1146        let emitter = handle.downgrade();
1147        self.pending_effects.push_back(Effect::Subscription {
1148            entity_id: handle.id(),
1149            subscription_id,
1150            callback: Box::new(move |payload, cx| {
1151                if let Some(emitter) = H::upgrade_from(&emitter, cx.as_ref()) {
1152                    let payload = payload.downcast_ref().expect("downcast is type safe");
1153                    callback(emitter, payload, cx)
1154                } else {
1155                    false
1156                }
1157            }),
1158        });
1159        Subscription::Subscription {
1160            id: subscription_id,
1161            entity_id: handle.id(),
1162            subscriptions: Some(Arc::downgrade(&self.subscriptions)),
1163        }
1164    }
1165
1166    fn observe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
1167    where
1168        E: Entity,
1169        E::Event: 'static,
1170        H: Handle<E>,
1171        F: 'static + FnMut(H, &mut Self) -> bool,
1172    {
1173        let subscription_id = post_inc(&mut self.next_subscription_id);
1174        let observed = handle.downgrade();
1175        let entity_id = handle.id();
1176        self.pending_effects.push_back(Effect::Observation {
1177            entity_id,
1178            subscription_id,
1179            callback: Box::new(move |cx| {
1180                if let Some(observed) = H::upgrade_from(&observed, cx) {
1181                    callback(observed, cx)
1182                } else {
1183                    false
1184                }
1185            }),
1186        });
1187        Subscription::Observation {
1188            id: subscription_id,
1189            entity_id,
1190            observations: Some(Arc::downgrade(&self.observations)),
1191        }
1192    }
1193
1194    fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> Subscription
1195    where
1196        F: 'static + FnMut(ViewHandle<V>, &mut MutableAppContext) -> bool,
1197        V: View,
1198    {
1199        let subscription_id = post_inc(&mut self.next_subscription_id);
1200        let observed = handle.downgrade();
1201        let view_id = handle.id();
1202        self.pending_effects.push_back(Effect::FocusObservation {
1203            view_id,
1204            subscription_id,
1205            callback: Box::new(move |cx| {
1206                if let Some(observed) = observed.upgrade(cx) {
1207                    callback(observed, cx)
1208                } else {
1209                    false
1210                }
1211            }),
1212        });
1213        Subscription::FocusObservation {
1214            id: subscription_id,
1215            view_id,
1216            observations: Some(Arc::downgrade(&self.focus_observations)),
1217        }
1218    }
1219
1220    pub fn observe_global<G, F>(&mut self, mut observe: F) -> Subscription
1221    where
1222        G: Any,
1223        F: 'static + FnMut(&G, &mut MutableAppContext),
1224    {
1225        let type_id = TypeId::of::<G>();
1226        let id = post_inc(&mut self.next_subscription_id);
1227
1228        self.global_observations
1229            .lock()
1230            .entry(type_id)
1231            .or_default()
1232            .insert(
1233                id,
1234                Some(
1235                    Box::new(move |global: &dyn Any, cx: &mut MutableAppContext| {
1236                        observe(global.downcast_ref().unwrap(), cx)
1237                    }) as GlobalObservationCallback,
1238                ),
1239            );
1240
1241        Subscription::GlobalObservation {
1242            id,
1243            type_id,
1244            observations: Some(Arc::downgrade(&self.global_observations)),
1245        }
1246    }
1247
1248    pub fn observe_release<E, H, F>(&mut self, handle: &H, callback: F) -> Subscription
1249    where
1250        E: Entity,
1251        E::Event: 'static,
1252        H: Handle<E>,
1253        F: 'static + FnOnce(&E, &mut Self),
1254    {
1255        let id = post_inc(&mut self.next_subscription_id);
1256        self.release_observations
1257            .lock()
1258            .entry(handle.id())
1259            .or_default()
1260            .insert(
1261                id,
1262                Box::new(move |released, cx| {
1263                    let released = released.downcast_ref().unwrap();
1264                    callback(released, cx)
1265                }),
1266            );
1267        Subscription::ReleaseObservation {
1268            id,
1269            entity_id: handle.id(),
1270            observations: Some(Arc::downgrade(&self.release_observations)),
1271        }
1272    }
1273
1274    pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut MutableAppContext)) {
1275        self.pending_effects.push_back(Effect::Deferred {
1276            callback: Box::new(callback),
1277            after_window_update: false,
1278        })
1279    }
1280
1281    pub fn after_window_update(&mut self, callback: impl 'static + FnOnce(&mut MutableAppContext)) {
1282        self.pending_effects.push_back(Effect::Deferred {
1283            callback: Box::new(callback),
1284            after_window_update: true,
1285        })
1286    }
1287
1288    pub(crate) fn notify_model(&mut self, model_id: usize) {
1289        if self.pending_notifications.insert(model_id) {
1290            self.pending_effects
1291                .push_back(Effect::ModelNotification { model_id });
1292        }
1293    }
1294
1295    pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
1296        if self.pending_notifications.insert(view_id) {
1297            self.pending_effects
1298                .push_back(Effect::ViewNotification { window_id, view_id });
1299        }
1300    }
1301
1302    pub(crate) fn notify_global(&mut self, type_id: TypeId) {
1303        if self.pending_global_notifications.insert(type_id) {
1304            self.pending_effects
1305                .push_back(Effect::GlobalNotification { type_id });
1306        }
1307    }
1308
1309    pub fn available_actions(
1310        &self,
1311        window_id: usize,
1312        view_id: usize,
1313    ) -> impl Iterator<Item = (&'static str, Box<dyn Action>, SmallVec<[&Binding; 1]>)> {
1314        let mut action_types: HashSet<_> = self.global_actions.keys().copied().collect();
1315
1316        let presenter = self
1317            .presenters_and_platform_windows
1318            .get(&window_id)
1319            .unwrap()
1320            .0
1321            .clone();
1322        let dispatch_path = presenter.borrow().dispatch_path_from(view_id);
1323        for view_id in dispatch_path {
1324            if let Some(view) = self.views.get(&(window_id, view_id)) {
1325                let view_type = view.as_any().type_id();
1326                if let Some(actions) = self.actions.get(&view_type) {
1327                    action_types.extend(actions.keys().copied());
1328                }
1329            }
1330        }
1331
1332        self.action_deserializers
1333            .iter()
1334            .filter_map(move |(name, (type_id, deserialize))| {
1335                if action_types.contains(type_id) {
1336                    Some((
1337                        *name,
1338                        deserialize("{}").ok()?,
1339                        self.keystroke_matcher
1340                            .bindings_for_action_type(*type_id)
1341                            .collect(),
1342                    ))
1343                } else {
1344                    None
1345                }
1346            })
1347    }
1348
1349    pub fn dispatch_action_at(&mut self, window_id: usize, view_id: usize, action: &dyn Action) {
1350        let presenter = self
1351            .presenters_and_platform_windows
1352            .get(&window_id)
1353            .unwrap()
1354            .0
1355            .clone();
1356        let dispatch_path = presenter.borrow().dispatch_path_from(view_id);
1357        self.dispatch_action_any(window_id, &dispatch_path, action);
1358    }
1359
1360    pub fn dispatch_action<A: Action>(
1361        &mut self,
1362        window_id: usize,
1363        dispatch_path: Vec<usize>,
1364        action: &A,
1365    ) {
1366        self.dispatch_action_any(window_id, &dispatch_path, action);
1367    }
1368
1369    pub(crate) fn dispatch_action_any(
1370        &mut self,
1371        window_id: usize,
1372        path: &[usize],
1373        action: &dyn Action,
1374    ) -> bool {
1375        self.update(|this| {
1376            this.halt_action_dispatch = false;
1377            for (capture_phase, view_id) in path
1378                .iter()
1379                .map(|view_id| (true, *view_id))
1380                .chain(path.iter().rev().map(|view_id| (false, *view_id)))
1381            {
1382                if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
1383                    let type_id = view.as_any().type_id();
1384
1385                    if let Some((name, mut handlers)) = this
1386                        .actions_mut(capture_phase)
1387                        .get_mut(&type_id)
1388                        .and_then(|h| h.remove_entry(&action.id()))
1389                    {
1390                        for handler in handlers.iter_mut().rev() {
1391                            this.halt_action_dispatch = true;
1392                            handler(view.as_mut(), action, this, window_id, view_id);
1393                            if this.halt_action_dispatch {
1394                                break;
1395                            }
1396                        }
1397                        this.actions_mut(capture_phase)
1398                            .get_mut(&type_id)
1399                            .unwrap()
1400                            .insert(name, handlers);
1401                    }
1402
1403                    this.cx.views.insert((window_id, view_id), view);
1404
1405                    if this.halt_action_dispatch {
1406                        break;
1407                    }
1408                }
1409            }
1410
1411            if !this.halt_action_dispatch {
1412                this.halt_action_dispatch = this.dispatch_global_action_any(action);
1413            }
1414            this.halt_action_dispatch
1415        })
1416    }
1417
1418    fn actions_mut(
1419        &mut self,
1420        capture_phase: bool,
1421    ) -> &mut HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>> {
1422        if capture_phase {
1423            &mut self.capture_actions
1424        } else {
1425            &mut self.actions
1426        }
1427    }
1428
1429    pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
1430        self.dispatch_global_action_any(&action);
1431    }
1432
1433    fn dispatch_global_action_any(&mut self, action: &dyn Action) -> bool {
1434        self.update(|this| {
1435            if let Some((name, mut handler)) = this.global_actions.remove_entry(&action.id()) {
1436                handler(action, this);
1437                this.global_actions.insert(name, handler);
1438                true
1439            } else {
1440                false
1441            }
1442        })
1443    }
1444
1445    pub fn add_bindings<T: IntoIterator<Item = keymap::Binding>>(&mut self, bindings: T) {
1446        self.keystroke_matcher.add_bindings(bindings);
1447    }
1448
1449    pub fn clear_bindings(&mut self) {
1450        self.keystroke_matcher.clear_bindings();
1451    }
1452
1453    pub fn dispatch_keystroke(
1454        &mut self,
1455        window_id: usize,
1456        dispatch_path: Vec<usize>,
1457        keystroke: &Keystroke,
1458    ) -> bool {
1459        let mut context_chain = Vec::new();
1460        for view_id in &dispatch_path {
1461            let view = self
1462                .cx
1463                .views
1464                .get(&(window_id, *view_id))
1465                .expect("view in responder chain does not exist");
1466            context_chain.push(view.keymap_context(self.as_ref()));
1467        }
1468
1469        let mut pending = false;
1470        for (i, cx) in context_chain.iter().enumerate().rev() {
1471            match self
1472                .keystroke_matcher
1473                .push_keystroke(keystroke.clone(), dispatch_path[i], cx)
1474            {
1475                MatchResult::None => {}
1476                MatchResult::Pending => pending = true,
1477                MatchResult::Action(action) => {
1478                    if self.dispatch_action_any(window_id, &dispatch_path[0..=i], action.as_ref()) {
1479                        self.keystroke_matcher.clear_pending();
1480                        return true;
1481                    }
1482                }
1483            }
1484        }
1485
1486        pending
1487    }
1488
1489    pub fn default_global<T: 'static + Default>(&mut self) -> &T {
1490        let type_id = TypeId::of::<T>();
1491        self.update(|this| {
1492            if !this.globals.contains_key(&type_id) {
1493                this.notify_global(type_id);
1494            }
1495
1496            this.cx
1497                .globals
1498                .entry(type_id)
1499                .or_insert_with(|| Box::new(T::default()));
1500        });
1501        self.globals.get(&type_id).unwrap().downcast_ref().unwrap()
1502    }
1503
1504    pub fn set_global<T: 'static>(&mut self, state: T) {
1505        self.update(|this| {
1506            let type_id = TypeId::of::<T>();
1507            this.cx.globals.insert(type_id, Box::new(state));
1508            this.notify_global(type_id);
1509        });
1510    }
1511
1512    pub fn update_default_global<T, F, U>(&mut self, update: F) -> U
1513    where
1514        T: 'static + Default,
1515        F: FnOnce(&mut T, &mut MutableAppContext) -> U,
1516    {
1517        self.update(|this| {
1518            let type_id = TypeId::of::<T>();
1519            let mut state = this
1520                .cx
1521                .globals
1522                .remove(&type_id)
1523                .unwrap_or_else(|| Box::new(T::default()));
1524            let result = update(state.downcast_mut().unwrap(), this);
1525            this.cx.globals.insert(type_id, state);
1526            this.notify_global(type_id);
1527            result
1528        })
1529    }
1530
1531    pub fn update_global<T, F, U>(&mut self, update: F) -> U
1532    where
1533        T: 'static,
1534        F: FnOnce(&mut T, &mut MutableAppContext) -> U,
1535    {
1536        self.update(|this| {
1537            let type_id = TypeId::of::<T>();
1538            let mut state = this
1539                .cx
1540                .globals
1541                .remove(&type_id)
1542                .expect("no global has been added for this type");
1543            let result = update(state.downcast_mut().unwrap(), this);
1544            this.cx.globals.insert(type_id, state);
1545            this.notify_global(type_id);
1546            result
1547        })
1548    }
1549
1550    pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
1551    where
1552        T: Entity,
1553        F: FnOnce(&mut ModelContext<T>) -> T,
1554    {
1555        self.update(|this| {
1556            let model_id = post_inc(&mut this.next_entity_id);
1557            let handle = ModelHandle::new(model_id, &this.cx.ref_counts);
1558            let mut cx = ModelContext::new(this, model_id);
1559            let model = build_model(&mut cx);
1560            this.cx.models.insert(model_id, Box::new(model));
1561            handle
1562        })
1563    }
1564
1565    pub fn add_window<T, F>(
1566        &mut self,
1567        window_options: WindowOptions,
1568        build_root_view: F,
1569    ) -> (usize, ViewHandle<T>)
1570    where
1571        T: View,
1572        F: FnOnce(&mut ViewContext<T>) -> T,
1573    {
1574        self.update(|this| {
1575            let window_id = post_inc(&mut this.next_window_id);
1576            let root_view = this.add_view(window_id, build_root_view);
1577
1578            this.cx.windows.insert(
1579                window_id,
1580                Window {
1581                    root_view: root_view.clone().into(),
1582                    focused_view_id: Some(root_view.id()),
1583                    invalidation: None,
1584                },
1585            );
1586            root_view.update(this, |view, cx| {
1587                view.on_focus(cx);
1588            });
1589            this.open_platform_window(window_id, window_options);
1590
1591            (window_id, root_view)
1592        })
1593    }
1594
1595    pub fn remove_window(&mut self, window_id: usize) {
1596        self.cx.windows.remove(&window_id);
1597        self.presenters_and_platform_windows.remove(&window_id);
1598        self.flush_effects();
1599    }
1600
1601    fn open_platform_window(&mut self, window_id: usize, window_options: WindowOptions) {
1602        let mut window =
1603            self.cx
1604                .platform
1605                .open_window(window_id, window_options, self.foreground.clone());
1606        let presenter = Rc::new(RefCell::new(
1607            self.build_presenter(window_id, window.titlebar_height()),
1608        ));
1609
1610        {
1611            let mut app = self.upgrade();
1612            let presenter = presenter.clone();
1613            window.on_event(Box::new(move |event| {
1614                app.update(|cx| {
1615                    if let Event::KeyDown { keystroke, .. } = &event {
1616                        if cx.dispatch_keystroke(
1617                            window_id,
1618                            presenter.borrow().dispatch_path(cx.as_ref()),
1619                            keystroke,
1620                        ) {
1621                            return;
1622                        }
1623                    }
1624
1625                    presenter.borrow_mut().dispatch_event(event, cx);
1626                })
1627            }));
1628        }
1629
1630        {
1631            let mut app = self.upgrade();
1632            window.on_resize(Box::new(move || {
1633                app.update(|cx| cx.resize_window(window_id))
1634            }));
1635        }
1636
1637        {
1638            let mut app = self.upgrade();
1639            window.on_close(Box::new(move || {
1640                app.update(|cx| cx.remove_window(window_id));
1641            }));
1642        }
1643
1644        let scene =
1645            presenter
1646                .borrow_mut()
1647                .build_scene(window.size(), window.scale_factor(), false, self);
1648        window.present_scene(scene);
1649        self.presenters_and_platform_windows
1650            .insert(window_id, (presenter.clone(), window));
1651    }
1652
1653    pub fn build_presenter(&mut self, window_id: usize, titlebar_height: f32) -> Presenter {
1654        Presenter::new(
1655            window_id,
1656            titlebar_height,
1657            self.cx.font_cache.clone(),
1658            TextLayoutCache::new(self.cx.platform.fonts()),
1659            self.assets.clone(),
1660            self,
1661        )
1662    }
1663
1664    pub fn build_render_context<V: View>(
1665        &mut self,
1666        window_id: usize,
1667        view_id: usize,
1668        titlebar_height: f32,
1669        refreshing: bool,
1670    ) -> RenderContext<V> {
1671        RenderContext {
1672            app: self,
1673            titlebar_height,
1674            refreshing,
1675            window_id,
1676            view_id,
1677            view_type: PhantomData,
1678        }
1679    }
1680
1681    pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
1682    where
1683        T: View,
1684        F: FnOnce(&mut ViewContext<T>) -> T,
1685    {
1686        self.add_option_view(window_id, |cx| Some(build_view(cx)))
1687            .unwrap()
1688    }
1689
1690    pub fn add_option_view<T, F>(
1691        &mut self,
1692        window_id: usize,
1693        build_view: F,
1694    ) -> Option<ViewHandle<T>>
1695    where
1696        T: View,
1697        F: FnOnce(&mut ViewContext<T>) -> Option<T>,
1698    {
1699        self.update(|this| {
1700            let view_id = post_inc(&mut this.next_entity_id);
1701            let mut cx = ViewContext::new(this, window_id, view_id);
1702            let handle = if let Some(view) = build_view(&mut cx) {
1703                this.cx.views.insert((window_id, view_id), Box::new(view));
1704                if let Some(window) = this.cx.windows.get_mut(&window_id) {
1705                    window
1706                        .invalidation
1707                        .get_or_insert_with(Default::default)
1708                        .updated
1709                        .insert(view_id);
1710                }
1711                Some(ViewHandle::new(window_id, view_id, &this.cx.ref_counts))
1712            } else {
1713                None
1714            };
1715            handle
1716        })
1717    }
1718
1719    fn remove_dropped_entities(&mut self) {
1720        loop {
1721            let (dropped_models, dropped_views, dropped_element_states) =
1722                self.cx.ref_counts.lock().take_dropped();
1723            if dropped_models.is_empty()
1724                && dropped_views.is_empty()
1725                && dropped_element_states.is_empty()
1726            {
1727                break;
1728            }
1729
1730            for model_id in dropped_models {
1731                self.subscriptions.lock().remove(&model_id);
1732                self.observations.lock().remove(&model_id);
1733                let mut model = self.cx.models.remove(&model_id).unwrap();
1734                model.release(self);
1735                self.pending_effects
1736                    .push_back(Effect::ModelRelease { model_id, model });
1737            }
1738
1739            for (window_id, view_id) in dropped_views {
1740                self.subscriptions.lock().remove(&view_id);
1741                self.observations.lock().remove(&view_id);
1742                let mut view = self.cx.views.remove(&(window_id, view_id)).unwrap();
1743                view.release(self);
1744                let change_focus_to = self.cx.windows.get_mut(&window_id).and_then(|window| {
1745                    window
1746                        .invalidation
1747                        .get_or_insert_with(Default::default)
1748                        .removed
1749                        .push(view_id);
1750                    if window.focused_view_id == Some(view_id) {
1751                        Some(window.root_view.id())
1752                    } else {
1753                        None
1754                    }
1755                });
1756
1757                if let Some(view_id) = change_focus_to {
1758                    self.handle_focus_effect(window_id, Some(view_id));
1759                }
1760
1761                self.pending_effects
1762                    .push_back(Effect::ViewRelease { view_id, view });
1763            }
1764
1765            for key in dropped_element_states {
1766                self.cx.element_states.remove(&key);
1767            }
1768        }
1769    }
1770
1771    fn flush_effects(&mut self) {
1772        self.pending_flushes = self.pending_flushes.saturating_sub(1);
1773        let mut after_window_update_callbacks = Vec::new();
1774
1775        if !self.flushing_effects && self.pending_flushes == 0 {
1776            self.flushing_effects = true;
1777
1778            let mut refreshing = false;
1779            loop {
1780                if let Some(effect) = self.pending_effects.pop_front() {
1781                    if let Some(pending_focus_index) = self.pending_focus_index.as_mut() {
1782                        *pending_focus_index = pending_focus_index.saturating_sub(1);
1783                    }
1784                    match effect {
1785                        Effect::Subscription {
1786                            entity_id,
1787                            subscription_id,
1788                            callback,
1789                        } => self.handle_subscription_effect(entity_id, subscription_id, callback),
1790                        Effect::Event { entity_id, payload } => self.emit_event(entity_id, payload),
1791                        Effect::GlobalSubscription {
1792                            type_id,
1793                            subscription_id,
1794                            callback,
1795                        } => self.handle_global_subscription_effect(
1796                            type_id,
1797                            subscription_id,
1798                            callback,
1799                        ),
1800                        Effect::GlobalEvent { payload } => self.emit_global_event(payload),
1801                        Effect::Observation {
1802                            entity_id,
1803                            subscription_id,
1804                            callback,
1805                        } => self.handle_observation_effect(entity_id, subscription_id, callback),
1806                        Effect::ModelNotification { model_id } => {
1807                            self.handle_model_notification_effect(model_id)
1808                        }
1809                        Effect::ViewNotification { window_id, view_id } => {
1810                            self.handle_view_notification_effect(window_id, view_id)
1811                        }
1812                        Effect::GlobalNotification { type_id } => {
1813                            self.handle_global_notification_effect(type_id)
1814                        }
1815                        Effect::Deferred {
1816                            callback,
1817                            after_window_update,
1818                        } => {
1819                            if after_window_update {
1820                                after_window_update_callbacks.push(callback);
1821                            } else {
1822                                callback(self)
1823                            }
1824                        }
1825                        Effect::ModelRelease { model_id, model } => {
1826                            self.handle_entity_release_effect(model_id, model.as_any())
1827                        }
1828                        Effect::ViewRelease { view_id, view } => {
1829                            self.handle_entity_release_effect(view_id, view.as_any())
1830                        }
1831                        Effect::Focus { window_id, view_id } => {
1832                            self.handle_focus_effect(window_id, view_id);
1833                        }
1834                        Effect::FocusObservation {
1835                            view_id,
1836                            subscription_id,
1837                            callback,
1838                        } => {
1839                            self.handle_focus_observation_effect(view_id, subscription_id, callback)
1840                        }
1841                        Effect::ResizeWindow { window_id } => {
1842                            if let Some(window) = self.cx.windows.get_mut(&window_id) {
1843                                window
1844                                    .invalidation
1845                                    .get_or_insert(WindowInvalidation::default());
1846                            }
1847                        }
1848                        Effect::RefreshWindows => {
1849                            refreshing = true;
1850                        }
1851                    }
1852                    self.pending_notifications.clear();
1853                    self.remove_dropped_entities();
1854                } else {
1855                    self.remove_dropped_entities();
1856                    if refreshing {
1857                        self.perform_window_refresh();
1858                    } else {
1859                        self.update_windows();
1860                    }
1861
1862                    if self.pending_effects.is_empty() {
1863                        for callback in after_window_update_callbacks.drain(..) {
1864                            callback(self);
1865                        }
1866
1867                        if self.pending_effects.is_empty() {
1868                            self.flushing_effects = false;
1869                            self.pending_notifications.clear();
1870                            self.pending_global_notifications.clear();
1871                            break;
1872                        }
1873                    }
1874
1875                    refreshing = false;
1876                }
1877            }
1878        }
1879    }
1880
1881    fn update_windows(&mut self) {
1882        let mut invalidations = HashMap::new();
1883        for (window_id, window) in &mut self.cx.windows {
1884            if let Some(invalidation) = window.invalidation.take() {
1885                invalidations.insert(*window_id, invalidation);
1886            }
1887        }
1888
1889        for (window_id, mut invalidation) in invalidations {
1890            if let Some((presenter, mut window)) =
1891                self.presenters_and_platform_windows.remove(&window_id)
1892            {
1893                {
1894                    let mut presenter = presenter.borrow_mut();
1895                    presenter.invalidate(&mut invalidation, self);
1896                    let scene =
1897                        presenter.build_scene(window.size(), window.scale_factor(), false, self);
1898                    window.present_scene(scene);
1899                }
1900                self.presenters_and_platform_windows
1901                    .insert(window_id, (presenter, window));
1902            }
1903        }
1904    }
1905
1906    fn resize_window(&mut self, window_id: usize) {
1907        self.pending_effects
1908            .push_back(Effect::ResizeWindow { window_id });
1909    }
1910
1911    pub fn refresh_windows(&mut self) {
1912        self.pending_effects.push_back(Effect::RefreshWindows);
1913    }
1914
1915    fn perform_window_refresh(&mut self) {
1916        let mut presenters = mem::take(&mut self.presenters_and_platform_windows);
1917        for (window_id, (presenter, window)) in &mut presenters {
1918            let mut invalidation = self
1919                .cx
1920                .windows
1921                .get_mut(&window_id)
1922                .unwrap()
1923                .invalidation
1924                .take();
1925            let mut presenter = presenter.borrow_mut();
1926            presenter.refresh(
1927                invalidation.as_mut().unwrap_or(&mut Default::default()),
1928                self,
1929            );
1930            let scene = presenter.build_scene(window.size(), window.scale_factor(), true, self);
1931            window.present_scene(scene);
1932        }
1933        self.presenters_and_platform_windows = presenters;
1934    }
1935
1936    pub fn set_cursor_style(&mut self, style: CursorStyle) -> CursorStyleHandle {
1937        self.platform.set_cursor_style(style);
1938        let id = self.next_cursor_style_handle_id.fetch_add(1, SeqCst);
1939        CursorStyleHandle {
1940            id,
1941            next_cursor_style_handle_id: self.next_cursor_style_handle_id.clone(),
1942            platform: self.platform(),
1943        }
1944    }
1945
1946    fn handle_subscription_effect(
1947        &mut self,
1948        entity_id: usize,
1949        subscription_id: usize,
1950        callback: SubscriptionCallback,
1951    ) {
1952        match self
1953            .subscriptions
1954            .lock()
1955            .entry(entity_id)
1956            .or_default()
1957            .entry(subscription_id)
1958        {
1959            btree_map::Entry::Vacant(entry) => {
1960                entry.insert(Some(callback));
1961            }
1962            // Subscription was dropped before effect was processed
1963            btree_map::Entry::Occupied(entry) => {
1964                debug_assert!(entry.get().is_none());
1965                entry.remove();
1966            }
1967        }
1968    }
1969
1970    fn emit_event(&mut self, entity_id: usize, payload: Box<dyn Any>) {
1971        let callbacks = self.subscriptions.lock().remove(&entity_id);
1972        if let Some(callbacks) = callbacks {
1973            for (id, callback) in callbacks {
1974                if let Some(mut callback) = callback {
1975                    let alive = callback(payload.as_ref(), self);
1976                    if alive {
1977                        match self
1978                            .subscriptions
1979                            .lock()
1980                            .entry(entity_id)
1981                            .or_default()
1982                            .entry(id)
1983                        {
1984                            btree_map::Entry::Vacant(entry) => {
1985                                entry.insert(Some(callback));
1986                            }
1987                            btree_map::Entry::Occupied(entry) => {
1988                                entry.remove();
1989                            }
1990                        }
1991                    }
1992                }
1993            }
1994        }
1995    }
1996
1997    fn handle_global_subscription_effect(
1998        &mut self,
1999        type_id: TypeId,
2000        subscription_id: usize,
2001        callback: GlobalSubscriptionCallback,
2002    ) {
2003        match self
2004            .global_subscriptions
2005            .lock()
2006            .entry(type_id)
2007            .or_default()
2008            .entry(subscription_id)
2009        {
2010            btree_map::Entry::Vacant(entry) => {
2011                entry.insert(Some(callback));
2012            }
2013            // Subscription was dropped before effect was processed
2014            btree_map::Entry::Occupied(entry) => {
2015                debug_assert!(entry.get().is_none());
2016                entry.remove();
2017            }
2018        }
2019    }
2020
2021    fn emit_global_event(&mut self, payload: Box<dyn Any>) {
2022        let type_id = (&*payload).type_id();
2023        let callbacks = self.global_subscriptions.lock().remove(&type_id);
2024        if let Some(callbacks) = callbacks {
2025            for (id, callback) in callbacks {
2026                if let Some(mut callback) = callback {
2027                    callback(payload.as_ref(), self);
2028                    match self
2029                        .global_subscriptions
2030                        .lock()
2031                        .entry(type_id)
2032                        .or_default()
2033                        .entry(id)
2034                    {
2035                        btree_map::Entry::Vacant(entry) => {
2036                            entry.insert(Some(callback));
2037                        }
2038                        btree_map::Entry::Occupied(entry) => {
2039                            entry.remove();
2040                        }
2041                    }
2042                }
2043            }
2044        }
2045    }
2046
2047    fn handle_observation_effect(
2048        &mut self,
2049        entity_id: usize,
2050        subscription_id: usize,
2051        callback: ObservationCallback,
2052    ) {
2053        match self
2054            .observations
2055            .lock()
2056            .entry(entity_id)
2057            .or_default()
2058            .entry(subscription_id)
2059        {
2060            btree_map::Entry::Vacant(entry) => {
2061                entry.insert(Some(callback));
2062            }
2063            // Observation was dropped before effect was processed
2064            btree_map::Entry::Occupied(entry) => {
2065                debug_assert!(entry.get().is_none());
2066                entry.remove();
2067            }
2068        }
2069    }
2070
2071    fn handle_focus_observation_effect(
2072        &mut self,
2073        view_id: usize,
2074        subscription_id: usize,
2075        callback: FocusObservationCallback,
2076    ) {
2077        match self
2078            .focus_observations
2079            .lock()
2080            .entry(view_id)
2081            .or_default()
2082            .entry(subscription_id)
2083        {
2084            btree_map::Entry::Vacant(entry) => {
2085                entry.insert(Some(callback));
2086            }
2087            // Observation was dropped before effect was processed
2088            btree_map::Entry::Occupied(entry) => {
2089                debug_assert!(entry.get().is_none());
2090                entry.remove();
2091            }
2092        }
2093    }
2094
2095    fn handle_model_notification_effect(&mut self, observed_id: usize) {
2096        let callbacks = self.observations.lock().remove(&observed_id);
2097        if let Some(callbacks) = callbacks {
2098            if self.cx.models.contains_key(&observed_id) {
2099                for (id, callback) in callbacks {
2100                    if let Some(mut callback) = callback {
2101                        let alive = callback(self);
2102                        if alive {
2103                            match self
2104                                .observations
2105                                .lock()
2106                                .entry(observed_id)
2107                                .or_default()
2108                                .entry(id)
2109                            {
2110                                btree_map::Entry::Vacant(entry) => {
2111                                    entry.insert(Some(callback));
2112                                }
2113                                btree_map::Entry::Occupied(entry) => {
2114                                    entry.remove();
2115                                }
2116                            }
2117                        }
2118                    }
2119                }
2120            }
2121        }
2122    }
2123
2124    fn handle_view_notification_effect(
2125        &mut self,
2126        observed_window_id: usize,
2127        observed_view_id: usize,
2128    ) {
2129        if let Some(window) = self.cx.windows.get_mut(&observed_window_id) {
2130            window
2131                .invalidation
2132                .get_or_insert_with(Default::default)
2133                .updated
2134                .insert(observed_view_id);
2135        }
2136
2137        let callbacks = self.observations.lock().remove(&observed_view_id);
2138        if let Some(callbacks) = callbacks {
2139            if self
2140                .cx
2141                .views
2142                .contains_key(&(observed_window_id, observed_view_id))
2143            {
2144                for (id, callback) in callbacks {
2145                    if let Some(mut callback) = callback {
2146                        let alive = callback(self);
2147                        if alive {
2148                            match self
2149                                .observations
2150                                .lock()
2151                                .entry(observed_view_id)
2152                                .or_default()
2153                                .entry(id)
2154                            {
2155                                btree_map::Entry::Vacant(entry) => {
2156                                    entry.insert(Some(callback));
2157                                }
2158                                btree_map::Entry::Occupied(entry) => {
2159                                    entry.remove();
2160                                }
2161                            }
2162                        }
2163                    }
2164                }
2165            }
2166        }
2167    }
2168
2169    fn handle_global_notification_effect(&mut self, observed_type_id: TypeId) {
2170        let callbacks = self.global_observations.lock().remove(&observed_type_id);
2171        if let Some(callbacks) = callbacks {
2172            if let Some(global) = self.cx.globals.remove(&observed_type_id) {
2173                for (id, callback) in callbacks {
2174                    if let Some(mut callback) = callback {
2175                        callback(global.as_ref(), self);
2176                        match self
2177                            .global_observations
2178                            .lock()
2179                            .entry(observed_type_id)
2180                            .or_default()
2181                            .entry(id)
2182                        {
2183                            collections::btree_map::Entry::Vacant(entry) => {
2184                                entry.insert(Some(callback));
2185                            }
2186                            collections::btree_map::Entry::Occupied(entry) => {
2187                                entry.remove();
2188                            }
2189                        }
2190                    }
2191                }
2192                self.cx.globals.insert(observed_type_id, global);
2193            }
2194        }
2195    }
2196
2197    fn handle_entity_release_effect(&mut self, entity_id: usize, entity: &dyn Any) {
2198        let callbacks = self.release_observations.lock().remove(&entity_id);
2199        if let Some(callbacks) = callbacks {
2200            for (_, callback) in callbacks {
2201                callback(entity, self);
2202            }
2203        }
2204    }
2205
2206    fn handle_focus_effect(&mut self, window_id: usize, focused_id: Option<usize>) {
2207        self.pending_focus_index.take();
2208
2209        if self
2210            .cx
2211            .windows
2212            .get(&window_id)
2213            .map(|w| w.focused_view_id)
2214            .map_or(false, |cur_focused| cur_focused == focused_id)
2215        {
2216            return;
2217        }
2218
2219        self.update(|this| {
2220            let blurred_id = this.cx.windows.get_mut(&window_id).and_then(|window| {
2221                let blurred_id = window.focused_view_id;
2222                window.focused_view_id = focused_id;
2223                blurred_id
2224            });
2225
2226            if let Some(blurred_id) = blurred_id {
2227                if let Some(mut blurred_view) = this.cx.views.remove(&(window_id, blurred_id)) {
2228                    blurred_view.on_blur(this, window_id, blurred_id);
2229                    this.cx.views.insert((window_id, blurred_id), blurred_view);
2230                }
2231            }
2232
2233            if let Some(focused_id) = focused_id {
2234                if let Some(mut focused_view) = this.cx.views.remove(&(window_id, focused_id)) {
2235                    focused_view.on_focus(this, window_id, focused_id);
2236                    this.cx.views.insert((window_id, focused_id), focused_view);
2237
2238                    let callbacks = this.focus_observations.lock().remove(&focused_id);
2239                    if let Some(callbacks) = callbacks {
2240                        for (id, callback) in callbacks {
2241                            if let Some(mut callback) = callback {
2242                                let alive = callback(this);
2243                                if alive {
2244                                    match this
2245                                        .focus_observations
2246                                        .lock()
2247                                        .entry(focused_id)
2248                                        .or_default()
2249                                        .entry(id)
2250                                    {
2251                                        btree_map::Entry::Vacant(entry) => {
2252                                            entry.insert(Some(callback));
2253                                        }
2254                                        btree_map::Entry::Occupied(entry) => {
2255                                            entry.remove();
2256                                        }
2257                                    }
2258                                }
2259                            }
2260                        }
2261                    }
2262                }
2263            }
2264        })
2265    }
2266
2267    fn focus(&mut self, window_id: usize, view_id: Option<usize>) {
2268        if let Some(pending_focus_index) = self.pending_focus_index {
2269            self.pending_effects.remove(pending_focus_index);
2270        }
2271        self.pending_focus_index = Some(self.pending_effects.len());
2272        self.pending_effects
2273            .push_back(Effect::Focus { window_id, view_id });
2274    }
2275
2276    pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
2277    where
2278        F: FnOnce(AsyncAppContext) -> Fut,
2279        Fut: 'static + Future<Output = T>,
2280        T: 'static,
2281    {
2282        let future = f(self.to_async());
2283        let cx = self.to_async();
2284        self.foreground.spawn(async move {
2285            let result = future.await;
2286            cx.0.borrow_mut().flush_effects();
2287            result
2288        })
2289    }
2290
2291    pub fn to_async(&self) -> AsyncAppContext {
2292        AsyncAppContext(self.weak_self.as_ref().unwrap().upgrade().unwrap())
2293    }
2294
2295    pub fn write_to_clipboard(&self, item: ClipboardItem) {
2296        self.cx.platform.write_to_clipboard(item);
2297    }
2298
2299    pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
2300        self.cx.platform.read_from_clipboard()
2301    }
2302
2303    #[cfg(any(test, feature = "test-support"))]
2304    pub fn leak_detector(&self) -> Arc<Mutex<LeakDetector>> {
2305        self.cx.ref_counts.lock().leak_detector.clone()
2306    }
2307}
2308
2309impl ReadModel for MutableAppContext {
2310    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2311        if let Some(model) = self.cx.models.get(&handle.model_id) {
2312            model
2313                .as_any()
2314                .downcast_ref()
2315                .expect("downcast is type safe")
2316        } else {
2317            panic!("circular model reference");
2318        }
2319    }
2320}
2321
2322impl UpdateModel for MutableAppContext {
2323    fn update_model<T: Entity, V>(
2324        &mut self,
2325        handle: &ModelHandle<T>,
2326        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
2327    ) -> V {
2328        if let Some(mut model) = self.cx.models.remove(&handle.model_id) {
2329            self.update(|this| {
2330                let mut cx = ModelContext::new(this, handle.model_id);
2331                let result = update(
2332                    model
2333                        .as_any_mut()
2334                        .downcast_mut()
2335                        .expect("downcast is type safe"),
2336                    &mut cx,
2337                );
2338                this.cx.models.insert(handle.model_id, model);
2339                result
2340            })
2341        } else {
2342            panic!("circular model update");
2343        }
2344    }
2345}
2346
2347impl UpgradeModelHandle for MutableAppContext {
2348    fn upgrade_model_handle<T: Entity>(
2349        &self,
2350        handle: &WeakModelHandle<T>,
2351    ) -> Option<ModelHandle<T>> {
2352        self.cx.upgrade_model_handle(handle)
2353    }
2354
2355    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
2356        self.cx.model_handle_is_upgradable(handle)
2357    }
2358
2359    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
2360        self.cx.upgrade_any_model_handle(handle)
2361    }
2362}
2363
2364impl UpgradeViewHandle for MutableAppContext {
2365    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
2366        self.cx.upgrade_view_handle(handle)
2367    }
2368
2369    fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
2370        self.cx.upgrade_any_view_handle(handle)
2371    }
2372}
2373
2374impl ReadView for MutableAppContext {
2375    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2376        if let Some(view) = self.cx.views.get(&(handle.window_id, handle.view_id)) {
2377            view.as_any().downcast_ref().expect("downcast is type safe")
2378        } else {
2379            panic!("circular view reference");
2380        }
2381    }
2382}
2383
2384impl UpdateView for MutableAppContext {
2385    fn update_view<T, S>(
2386        &mut self,
2387        handle: &ViewHandle<T>,
2388        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
2389    ) -> S
2390    where
2391        T: View,
2392    {
2393        self.update(|this| {
2394            let mut view = this
2395                .cx
2396                .views
2397                .remove(&(handle.window_id, handle.view_id))
2398                .expect("circular view update");
2399
2400            let mut cx = ViewContext::new(this, handle.window_id, handle.view_id);
2401            let result = update(
2402                view.as_any_mut()
2403                    .downcast_mut()
2404                    .expect("downcast is type safe"),
2405                &mut cx,
2406            );
2407            this.cx
2408                .views
2409                .insert((handle.window_id, handle.view_id), view);
2410            result
2411        })
2412    }
2413}
2414
2415impl AsRef<AppContext> for MutableAppContext {
2416    fn as_ref(&self) -> &AppContext {
2417        &self.cx
2418    }
2419}
2420
2421impl Deref for MutableAppContext {
2422    type Target = AppContext;
2423
2424    fn deref(&self) -> &Self::Target {
2425        &self.cx
2426    }
2427}
2428
2429pub struct AppContext {
2430    models: HashMap<usize, Box<dyn AnyModel>>,
2431    views: HashMap<(usize, usize), Box<dyn AnyView>>,
2432    windows: HashMap<usize, Window>,
2433    globals: HashMap<TypeId, Box<dyn Any>>,
2434    element_states: HashMap<ElementStateId, Box<dyn Any>>,
2435    background: Arc<executor::Background>,
2436    ref_counts: Arc<Mutex<RefCounts>>,
2437    font_cache: Arc<FontCache>,
2438    platform: Arc<dyn Platform>,
2439}
2440
2441impl AppContext {
2442    pub(crate) fn root_view(&self, window_id: usize) -> Option<AnyViewHandle> {
2443        self.windows
2444            .get(&window_id)
2445            .map(|window| window.root_view.clone())
2446    }
2447
2448    pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
2449        self.windows
2450            .get(&window_id)
2451            .map(|window| window.root_view.id())
2452    }
2453
2454    pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
2455        self.windows
2456            .get(&window_id)
2457            .and_then(|window| window.focused_view_id)
2458    }
2459
2460    pub fn background(&self) -> &Arc<executor::Background> {
2461        &self.background
2462    }
2463
2464    pub fn font_cache(&self) -> &Arc<FontCache> {
2465        &self.font_cache
2466    }
2467
2468    pub fn platform(&self) -> &Arc<dyn Platform> {
2469        &self.platform
2470    }
2471
2472    pub fn has_global<T: 'static>(&self) -> bool {
2473        self.globals.contains_key(&TypeId::of::<T>())
2474    }
2475
2476    pub fn global<T: 'static>(&self) -> &T {
2477        if let Some(global) = self.globals.get(&TypeId::of::<T>()) {
2478            global.downcast_ref().unwrap()
2479        } else {
2480            panic!("no global has been added for {}", type_name::<T>());
2481        }
2482    }
2483}
2484
2485impl ReadModel for AppContext {
2486    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
2487        if let Some(model) = self.models.get(&handle.model_id) {
2488            model
2489                .as_any()
2490                .downcast_ref()
2491                .expect("downcast should be type safe")
2492        } else {
2493            panic!("circular model reference");
2494        }
2495    }
2496}
2497
2498impl UpgradeModelHandle for AppContext {
2499    fn upgrade_model_handle<T: Entity>(
2500        &self,
2501        handle: &WeakModelHandle<T>,
2502    ) -> Option<ModelHandle<T>> {
2503        if self.models.contains_key(&handle.model_id) {
2504            Some(ModelHandle::new(handle.model_id, &self.ref_counts))
2505        } else {
2506            None
2507        }
2508    }
2509
2510    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
2511        self.models.contains_key(&handle.model_id)
2512    }
2513
2514    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
2515        if self.models.contains_key(&handle.model_id) {
2516            Some(AnyModelHandle::new(
2517                handle.model_id,
2518                handle.model_type,
2519                self.ref_counts.clone(),
2520            ))
2521        } else {
2522            None
2523        }
2524    }
2525}
2526
2527impl UpgradeViewHandle for AppContext {
2528    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
2529        if self.ref_counts.lock().is_entity_alive(handle.view_id) {
2530            Some(ViewHandle::new(
2531                handle.window_id,
2532                handle.view_id,
2533                &self.ref_counts,
2534            ))
2535        } else {
2536            None
2537        }
2538    }
2539
2540    fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
2541        if self.ref_counts.lock().is_entity_alive(handle.view_id) {
2542            Some(AnyViewHandle::new(
2543                handle.window_id,
2544                handle.view_id,
2545                handle.view_type,
2546                self.ref_counts.clone(),
2547            ))
2548        } else {
2549            None
2550        }
2551    }
2552}
2553
2554impl ReadView for AppContext {
2555    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
2556        if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
2557            view.as_any()
2558                .downcast_ref()
2559                .expect("downcast should be type safe")
2560        } else {
2561            panic!("circular view reference");
2562        }
2563    }
2564}
2565
2566struct Window {
2567    root_view: AnyViewHandle,
2568    focused_view_id: Option<usize>,
2569    invalidation: Option<WindowInvalidation>,
2570}
2571
2572#[derive(Default, Clone)]
2573pub struct WindowInvalidation {
2574    pub updated: HashSet<usize>,
2575    pub removed: Vec<usize>,
2576}
2577
2578pub enum Effect {
2579    Subscription {
2580        entity_id: usize,
2581        subscription_id: usize,
2582        callback: SubscriptionCallback,
2583    },
2584    Event {
2585        entity_id: usize,
2586        payload: Box<dyn Any>,
2587    },
2588    GlobalSubscription {
2589        type_id: TypeId,
2590        subscription_id: usize,
2591        callback: GlobalSubscriptionCallback,
2592    },
2593    GlobalEvent {
2594        payload: Box<dyn Any>,
2595    },
2596    Observation {
2597        entity_id: usize,
2598        subscription_id: usize,
2599        callback: ObservationCallback,
2600    },
2601    ModelNotification {
2602        model_id: usize,
2603    },
2604    ViewNotification {
2605        window_id: usize,
2606        view_id: usize,
2607    },
2608    Deferred {
2609        callback: Box<dyn FnOnce(&mut MutableAppContext)>,
2610        after_window_update: bool,
2611    },
2612    GlobalNotification {
2613        type_id: TypeId,
2614    },
2615    ModelRelease {
2616        model_id: usize,
2617        model: Box<dyn AnyModel>,
2618    },
2619    ViewRelease {
2620        view_id: usize,
2621        view: Box<dyn AnyView>,
2622    },
2623    Focus {
2624        window_id: usize,
2625        view_id: Option<usize>,
2626    },
2627    FocusObservation {
2628        view_id: usize,
2629        subscription_id: usize,
2630        callback: FocusObservationCallback,
2631    },
2632    ResizeWindow {
2633        window_id: usize,
2634    },
2635    RefreshWindows,
2636}
2637
2638impl Debug for Effect {
2639    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2640        match self {
2641            Effect::Subscription {
2642                entity_id,
2643                subscription_id,
2644                ..
2645            } => f
2646                .debug_struct("Effect::Subscribe")
2647                .field("entity_id", entity_id)
2648                .field("subscription_id", subscription_id)
2649                .finish(),
2650            Effect::Event { entity_id, .. } => f
2651                .debug_struct("Effect::Event")
2652                .field("entity_id", entity_id)
2653                .finish(),
2654            Effect::GlobalSubscription {
2655                type_id,
2656                subscription_id,
2657                ..
2658            } => f
2659                .debug_struct("Effect::Subscribe")
2660                .field("type_id", type_id)
2661                .field("subscription_id", subscription_id)
2662                .finish(),
2663            Effect::GlobalEvent { payload, .. } => f
2664                .debug_struct("Effect::GlobalEvent")
2665                .field("type_id", &(&*payload).type_id())
2666                .finish(),
2667            Effect::Observation {
2668                entity_id,
2669                subscription_id,
2670                ..
2671            } => f
2672                .debug_struct("Effect::Observation")
2673                .field("entity_id", entity_id)
2674                .field("subscription_id", subscription_id)
2675                .finish(),
2676            Effect::ModelNotification { model_id } => f
2677                .debug_struct("Effect::ModelNotification")
2678                .field("model_id", model_id)
2679                .finish(),
2680            Effect::ViewNotification { window_id, view_id } => f
2681                .debug_struct("Effect::ViewNotification")
2682                .field("window_id", window_id)
2683                .field("view_id", view_id)
2684                .finish(),
2685            Effect::GlobalNotification { type_id } => f
2686                .debug_struct("Effect::GlobalNotification")
2687                .field("type_id", type_id)
2688                .finish(),
2689            Effect::Deferred { .. } => f.debug_struct("Effect::Deferred").finish(),
2690            Effect::ModelRelease { model_id, .. } => f
2691                .debug_struct("Effect::ModelRelease")
2692                .field("model_id", model_id)
2693                .finish(),
2694            Effect::ViewRelease { view_id, .. } => f
2695                .debug_struct("Effect::ViewRelease")
2696                .field("view_id", view_id)
2697                .finish(),
2698            Effect::Focus { window_id, view_id } => f
2699                .debug_struct("Effect::Focus")
2700                .field("window_id", window_id)
2701                .field("view_id", view_id)
2702                .finish(),
2703            Effect::FocusObservation {
2704                view_id,
2705                subscription_id,
2706                ..
2707            } => f
2708                .debug_struct("Effect::FocusObservation")
2709                .field("view_id", view_id)
2710                .field("subscription_id", subscription_id)
2711                .finish(),
2712            Effect::ResizeWindow { window_id } => f
2713                .debug_struct("Effect::RefreshWindow")
2714                .field("window_id", window_id)
2715                .finish(),
2716            Effect::RefreshWindows => f.debug_struct("Effect::FullViewRefresh").finish(),
2717        }
2718    }
2719}
2720
2721pub trait AnyModel {
2722    fn as_any(&self) -> &dyn Any;
2723    fn as_any_mut(&mut self) -> &mut dyn Any;
2724    fn release(&mut self, cx: &mut MutableAppContext);
2725    fn app_will_quit(
2726        &mut self,
2727        cx: &mut MutableAppContext,
2728    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
2729}
2730
2731impl<T> AnyModel for T
2732where
2733    T: Entity,
2734{
2735    fn as_any(&self) -> &dyn Any {
2736        self
2737    }
2738
2739    fn as_any_mut(&mut self) -> &mut dyn Any {
2740        self
2741    }
2742
2743    fn release(&mut self, cx: &mut MutableAppContext) {
2744        self.release(cx);
2745    }
2746
2747    fn app_will_quit(
2748        &mut self,
2749        cx: &mut MutableAppContext,
2750    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
2751        self.app_will_quit(cx)
2752    }
2753}
2754
2755pub trait AnyView {
2756    fn as_any(&self) -> &dyn Any;
2757    fn as_any_mut(&mut self) -> &mut dyn Any;
2758    fn release(&mut self, cx: &mut MutableAppContext);
2759    fn app_will_quit(
2760        &mut self,
2761        cx: &mut MutableAppContext,
2762    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
2763    fn ui_name(&self) -> &'static str;
2764    fn render<'a>(
2765        &mut self,
2766        window_id: usize,
2767        view_id: usize,
2768        titlebar_height: f32,
2769        refreshing: bool,
2770        cx: &mut MutableAppContext,
2771    ) -> ElementBox;
2772    fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
2773    fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
2774    fn keymap_context(&self, cx: &AppContext) -> keymap::Context;
2775    fn debug_json(&self, cx: &AppContext) -> serde_json::Value;
2776}
2777
2778impl<T> AnyView for T
2779where
2780    T: View,
2781{
2782    fn as_any(&self) -> &dyn Any {
2783        self
2784    }
2785
2786    fn as_any_mut(&mut self) -> &mut dyn Any {
2787        self
2788    }
2789
2790    fn release(&mut self, cx: &mut MutableAppContext) {
2791        self.release(cx);
2792    }
2793
2794    fn app_will_quit(
2795        &mut self,
2796        cx: &mut MutableAppContext,
2797    ) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
2798        self.app_will_quit(cx)
2799    }
2800
2801    fn ui_name(&self) -> &'static str {
2802        T::ui_name()
2803    }
2804
2805    fn render<'a>(
2806        &mut self,
2807        window_id: usize,
2808        view_id: usize,
2809        titlebar_height: f32,
2810        refreshing: bool,
2811        cx: &mut MutableAppContext,
2812    ) -> ElementBox {
2813        View::render(
2814            self,
2815            &mut RenderContext {
2816                window_id,
2817                view_id,
2818                app: cx,
2819                view_type: PhantomData::<T>,
2820                titlebar_height,
2821                refreshing,
2822            },
2823        )
2824    }
2825
2826    fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
2827        let mut cx = ViewContext::new(cx, window_id, view_id);
2828        View::on_focus(self, &mut cx);
2829    }
2830
2831    fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
2832        let mut cx = ViewContext::new(cx, window_id, view_id);
2833        View::on_blur(self, &mut cx);
2834    }
2835
2836    fn keymap_context(&self, cx: &AppContext) -> keymap::Context {
2837        View::keymap_context(self, cx)
2838    }
2839
2840    fn debug_json(&self, cx: &AppContext) -> serde_json::Value {
2841        View::debug_json(self, cx)
2842    }
2843}
2844
2845pub struct ModelContext<'a, T: ?Sized> {
2846    app: &'a mut MutableAppContext,
2847    model_id: usize,
2848    model_type: PhantomData<T>,
2849    halt_stream: bool,
2850}
2851
2852impl<'a, T: Entity> ModelContext<'a, T> {
2853    fn new(app: &'a mut MutableAppContext, model_id: usize) -> Self {
2854        Self {
2855            app,
2856            model_id,
2857            model_type: PhantomData,
2858            halt_stream: false,
2859        }
2860    }
2861
2862    pub fn background(&self) -> &Arc<executor::Background> {
2863        &self.app.cx.background
2864    }
2865
2866    pub fn halt_stream(&mut self) {
2867        self.halt_stream = true;
2868    }
2869
2870    pub fn model_id(&self) -> usize {
2871        self.model_id
2872    }
2873
2874    pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
2875    where
2876        S: Entity,
2877        F: FnOnce(&mut ModelContext<S>) -> S,
2878    {
2879        self.app.add_model(build_model)
2880    }
2881
2882    pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ModelContext<T>)) {
2883        let handle = self.handle();
2884        self.app.defer(move |cx| {
2885            handle.update(cx, |model, cx| {
2886                callback(model, cx);
2887            })
2888        })
2889    }
2890
2891    pub fn emit(&mut self, payload: T::Event) {
2892        self.app.pending_effects.push_back(Effect::Event {
2893            entity_id: self.model_id,
2894            payload: Box::new(payload),
2895        });
2896    }
2897
2898    pub fn notify(&mut self) {
2899        self.app.notify_model(self.model_id);
2900    }
2901
2902    pub fn subscribe<S: Entity, F>(
2903        &mut self,
2904        handle: &ModelHandle<S>,
2905        mut callback: F,
2906    ) -> Subscription
2907    where
2908        S::Event: 'static,
2909        F: 'static + FnMut(&mut T, ModelHandle<S>, &S::Event, &mut ModelContext<T>),
2910    {
2911        let subscriber = self.weak_handle();
2912        self.app
2913            .subscribe_internal(handle, move |emitter, event, cx| {
2914                if let Some(subscriber) = subscriber.upgrade(cx) {
2915                    subscriber.update(cx, |subscriber, cx| {
2916                        callback(subscriber, emitter, event, cx);
2917                    });
2918                    true
2919                } else {
2920                    false
2921                }
2922            })
2923    }
2924
2925    pub fn observe<S, F>(&mut self, handle: &ModelHandle<S>, mut callback: F) -> Subscription
2926    where
2927        S: Entity,
2928        F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ModelContext<T>),
2929    {
2930        let observer = self.weak_handle();
2931        self.app.observe_internal(handle, move |observed, cx| {
2932            if let Some(observer) = observer.upgrade(cx) {
2933                observer.update(cx, |observer, cx| {
2934                    callback(observer, observed, cx);
2935                });
2936                true
2937            } else {
2938                false
2939            }
2940        })
2941    }
2942
2943    pub fn observe_release<S, F>(
2944        &mut self,
2945        handle: &ModelHandle<S>,
2946        mut callback: F,
2947    ) -> Subscription
2948    where
2949        S: Entity,
2950        F: 'static + FnMut(&mut T, &S, &mut ModelContext<T>),
2951    {
2952        let observer = self.weak_handle();
2953        self.app.observe_release(handle, move |released, cx| {
2954            if let Some(observer) = observer.upgrade(cx) {
2955                observer.update(cx, |observer, cx| {
2956                    callback(observer, released, cx);
2957                });
2958            }
2959        })
2960    }
2961
2962    pub fn handle(&self) -> ModelHandle<T> {
2963        ModelHandle::new(self.model_id, &self.app.cx.ref_counts)
2964    }
2965
2966    pub fn weak_handle(&self) -> WeakModelHandle<T> {
2967        WeakModelHandle::new(self.model_id)
2968    }
2969
2970    pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
2971    where
2972        F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
2973        Fut: 'static + Future<Output = S>,
2974        S: 'static,
2975    {
2976        let handle = self.handle();
2977        self.app.spawn(|cx| f(handle, cx))
2978    }
2979
2980    pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
2981    where
2982        F: FnOnce(WeakModelHandle<T>, AsyncAppContext) -> Fut,
2983        Fut: 'static + Future<Output = S>,
2984        S: 'static,
2985    {
2986        let handle = self.weak_handle();
2987        self.app.spawn(|cx| f(handle, cx))
2988    }
2989}
2990
2991impl<M> AsRef<AppContext> for ModelContext<'_, M> {
2992    fn as_ref(&self) -> &AppContext {
2993        &self.app.cx
2994    }
2995}
2996
2997impl<M> AsMut<MutableAppContext> for ModelContext<'_, M> {
2998    fn as_mut(&mut self) -> &mut MutableAppContext {
2999        self.app
3000    }
3001}
3002
3003impl<M> ReadModel for ModelContext<'_, M> {
3004    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
3005        self.app.read_model(handle)
3006    }
3007}
3008
3009impl<M> UpdateModel for ModelContext<'_, M> {
3010    fn update_model<T: Entity, V>(
3011        &mut self,
3012        handle: &ModelHandle<T>,
3013        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
3014    ) -> V {
3015        self.app.update_model(handle, update)
3016    }
3017}
3018
3019impl<M> UpgradeModelHandle for ModelContext<'_, M> {
3020    fn upgrade_model_handle<T: Entity>(
3021        &self,
3022        handle: &WeakModelHandle<T>,
3023    ) -> Option<ModelHandle<T>> {
3024        self.cx.upgrade_model_handle(handle)
3025    }
3026
3027    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
3028        self.cx.model_handle_is_upgradable(handle)
3029    }
3030
3031    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
3032        self.cx.upgrade_any_model_handle(handle)
3033    }
3034}
3035
3036impl<M> Deref for ModelContext<'_, M> {
3037    type Target = MutableAppContext;
3038
3039    fn deref(&self) -> &Self::Target {
3040        &self.app
3041    }
3042}
3043
3044impl<M> DerefMut for ModelContext<'_, M> {
3045    fn deref_mut(&mut self) -> &mut Self::Target {
3046        &mut self.app
3047    }
3048}
3049
3050pub struct ViewContext<'a, T: ?Sized> {
3051    app: &'a mut MutableAppContext,
3052    window_id: usize,
3053    view_id: usize,
3054    view_type: PhantomData<T>,
3055}
3056
3057impl<'a, T: View> ViewContext<'a, T> {
3058    fn new(app: &'a mut MutableAppContext, window_id: usize, view_id: usize) -> Self {
3059        Self {
3060            app,
3061            window_id,
3062            view_id,
3063            view_type: PhantomData,
3064        }
3065    }
3066
3067    pub fn handle(&self) -> ViewHandle<T> {
3068        ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts)
3069    }
3070
3071    pub fn weak_handle(&self) -> WeakViewHandle<T> {
3072        WeakViewHandle::new(self.window_id, self.view_id)
3073    }
3074
3075    pub fn window_id(&self) -> usize {
3076        self.window_id
3077    }
3078
3079    pub fn view_id(&self) -> usize {
3080        self.view_id
3081    }
3082
3083    pub fn foreground(&self) -> &Rc<executor::Foreground> {
3084        self.app.foreground()
3085    }
3086
3087    pub fn background_executor(&self) -> &Arc<executor::Background> {
3088        &self.app.cx.background
3089    }
3090
3091    pub fn platform(&self) -> Arc<dyn Platform> {
3092        self.app.platform()
3093    }
3094
3095    pub fn prompt(
3096        &self,
3097        level: PromptLevel,
3098        msg: &str,
3099        answers: &[&str],
3100    ) -> oneshot::Receiver<usize> {
3101        self.app.prompt(self.window_id, level, msg, answers)
3102    }
3103
3104    pub fn prompt_for_paths(
3105        &self,
3106        options: PathPromptOptions,
3107    ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
3108        self.app.prompt_for_paths(options)
3109    }
3110
3111    pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
3112        self.app.prompt_for_new_path(directory)
3113    }
3114
3115    pub fn debug_elements(&self) -> crate::json::Value {
3116        self.app.debug_elements(self.window_id).unwrap()
3117    }
3118
3119    pub fn focus<S>(&mut self, handle: S)
3120    where
3121        S: Into<AnyViewHandle>,
3122    {
3123        let handle = handle.into();
3124        self.app.focus(handle.window_id, Some(handle.view_id));
3125    }
3126
3127    pub fn focus_self(&mut self) {
3128        self.app.focus(self.window_id, Some(self.view_id));
3129    }
3130
3131    pub fn blur(&mut self) {
3132        self.app.focus(self.window_id, None);
3133    }
3134
3135    pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
3136    where
3137        S: Entity,
3138        F: FnOnce(&mut ModelContext<S>) -> S,
3139    {
3140        self.app.add_model(build_model)
3141    }
3142
3143    pub fn add_view<S, F>(&mut self, build_view: F) -> ViewHandle<S>
3144    where
3145        S: View,
3146        F: FnOnce(&mut ViewContext<S>) -> S,
3147    {
3148        self.app.add_view(self.window_id, build_view)
3149    }
3150
3151    pub fn add_option_view<S, F>(&mut self, build_view: F) -> Option<ViewHandle<S>>
3152    where
3153        S: View,
3154        F: FnOnce(&mut ViewContext<S>) -> Option<S>,
3155    {
3156        self.app.add_option_view(self.window_id, build_view)
3157    }
3158
3159    pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
3160    where
3161        E: Entity,
3162        E::Event: 'static,
3163        H: Handle<E>,
3164        F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext<T>),
3165    {
3166        let subscriber = self.weak_handle();
3167        self.app
3168            .subscribe_internal(handle, move |emitter, event, cx| {
3169                if let Some(subscriber) = subscriber.upgrade(cx) {
3170                    subscriber.update(cx, |subscriber, cx| {
3171                        callback(subscriber, emitter, event, cx);
3172                    });
3173                    true
3174                } else {
3175                    false
3176                }
3177            })
3178    }
3179
3180    pub fn observe<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
3181    where
3182        E: Entity,
3183        H: Handle<E>,
3184        F: 'static + FnMut(&mut T, H, &mut ViewContext<T>),
3185    {
3186        let observer = self.weak_handle();
3187        self.app.observe_internal(handle, move |observed, cx| {
3188            if let Some(observer) = observer.upgrade(cx) {
3189                observer.update(cx, |observer, cx| {
3190                    callback(observer, observed, cx);
3191                });
3192                true
3193            } else {
3194                false
3195            }
3196        })
3197    }
3198
3199    pub fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> Subscription
3200    where
3201        F: 'static + FnMut(&mut T, ViewHandle<V>, &mut ViewContext<T>),
3202        V: View,
3203    {
3204        let observer = self.weak_handle();
3205        self.app.observe_focus(handle, move |observed, cx| {
3206            if let Some(observer) = observer.upgrade(cx) {
3207                observer.update(cx, |observer, cx| {
3208                    callback(observer, observed, cx);
3209                });
3210                true
3211            } else {
3212                false
3213            }
3214        })
3215    }
3216
3217    pub fn observe_release<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
3218    where
3219        E: Entity,
3220        H: Handle<E>,
3221        F: 'static + FnMut(&mut T, &E, &mut ViewContext<T>),
3222    {
3223        let observer = self.weak_handle();
3224        self.app.observe_release(handle, move |released, cx| {
3225            if let Some(observer) = observer.upgrade(cx) {
3226                observer.update(cx, |observer, cx| {
3227                    callback(observer, released, cx);
3228                });
3229            }
3230        })
3231    }
3232
3233    pub fn emit(&mut self, payload: T::Event) {
3234        self.app.pending_effects.push_back(Effect::Event {
3235            entity_id: self.view_id,
3236            payload: Box::new(payload),
3237        });
3238    }
3239
3240    pub fn notify(&mut self) {
3241        self.app.notify_view(self.window_id, self.view_id);
3242    }
3243
3244    pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>)) {
3245        let handle = self.handle();
3246        self.app.defer(move |cx| {
3247            handle.update(cx, |view, cx| {
3248                callback(view, cx);
3249            })
3250        })
3251    }
3252
3253    pub fn after_window_update(
3254        &mut self,
3255        callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>),
3256    ) {
3257        let handle = self.handle();
3258        self.app.after_window_update(move |cx| {
3259            handle.update(cx, |view, cx| {
3260                callback(view, cx);
3261            })
3262        })
3263    }
3264
3265    pub fn propagate_action(&mut self) {
3266        self.app.halt_action_dispatch = false;
3267    }
3268
3269    pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
3270    where
3271        F: FnOnce(ViewHandle<T>, AsyncAppContext) -> Fut,
3272        Fut: 'static + Future<Output = S>,
3273        S: 'static,
3274    {
3275        let handle = self.handle();
3276        self.app.spawn(|cx| f(handle, cx))
3277    }
3278
3279    pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
3280    where
3281        F: FnOnce(WeakViewHandle<T>, AsyncAppContext) -> Fut,
3282        Fut: 'static + Future<Output = S>,
3283        S: 'static,
3284    {
3285        let handle = self.weak_handle();
3286        self.app.spawn(|cx| f(handle, cx))
3287    }
3288}
3289
3290pub struct RenderContext<'a, T: View> {
3291    pub app: &'a mut MutableAppContext,
3292    pub titlebar_height: f32,
3293    pub refreshing: bool,
3294    window_id: usize,
3295    view_id: usize,
3296    view_type: PhantomData<T>,
3297}
3298
3299impl<'a, T: View> RenderContext<'a, T> {
3300    pub fn handle(&self) -> WeakViewHandle<T> {
3301        WeakViewHandle::new(self.window_id, self.view_id)
3302    }
3303
3304    pub fn view_id(&self) -> usize {
3305        self.view_id
3306    }
3307}
3308
3309impl AsRef<AppContext> for &AppContext {
3310    fn as_ref(&self) -> &AppContext {
3311        self
3312    }
3313}
3314
3315impl<V: View> Deref for RenderContext<'_, V> {
3316    type Target = MutableAppContext;
3317
3318    fn deref(&self) -> &Self::Target {
3319        self.app
3320    }
3321}
3322
3323impl<V: View> DerefMut for RenderContext<'_, V> {
3324    fn deref_mut(&mut self) -> &mut Self::Target {
3325        self.app
3326    }
3327}
3328
3329impl<V: View> ReadModel for RenderContext<'_, V> {
3330    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
3331        self.app.read_model(handle)
3332    }
3333}
3334
3335impl<V: View> UpdateModel for RenderContext<'_, V> {
3336    fn update_model<T: Entity, O>(
3337        &mut self,
3338        handle: &ModelHandle<T>,
3339        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
3340    ) -> O {
3341        self.app.update_model(handle, update)
3342    }
3343}
3344
3345impl<V: View> ReadView for RenderContext<'_, V> {
3346    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
3347        self.app.read_view(handle)
3348    }
3349}
3350
3351impl<V: View> ElementStateContext for RenderContext<'_, V> {
3352    fn current_view_id(&self) -> usize {
3353        self.view_id
3354    }
3355}
3356
3357impl<M> AsRef<AppContext> for ViewContext<'_, M> {
3358    fn as_ref(&self) -> &AppContext {
3359        &self.app.cx
3360    }
3361}
3362
3363impl<M> Deref for ViewContext<'_, M> {
3364    type Target = MutableAppContext;
3365
3366    fn deref(&self) -> &Self::Target {
3367        &self.app
3368    }
3369}
3370
3371impl<M> DerefMut for ViewContext<'_, M> {
3372    fn deref_mut(&mut self) -> &mut Self::Target {
3373        &mut self.app
3374    }
3375}
3376
3377impl<M> AsMut<MutableAppContext> for ViewContext<'_, M> {
3378    fn as_mut(&mut self) -> &mut MutableAppContext {
3379        self.app
3380    }
3381}
3382
3383impl<V> ReadModel for ViewContext<'_, V> {
3384    fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
3385        self.app.read_model(handle)
3386    }
3387}
3388
3389impl<V> UpgradeModelHandle for ViewContext<'_, V> {
3390    fn upgrade_model_handle<T: Entity>(
3391        &self,
3392        handle: &WeakModelHandle<T>,
3393    ) -> Option<ModelHandle<T>> {
3394        self.cx.upgrade_model_handle(handle)
3395    }
3396
3397    fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
3398        self.cx.model_handle_is_upgradable(handle)
3399    }
3400
3401    fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
3402        self.cx.upgrade_any_model_handle(handle)
3403    }
3404}
3405
3406impl<V> UpgradeViewHandle for ViewContext<'_, V> {
3407    fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
3408        self.cx.upgrade_view_handle(handle)
3409    }
3410
3411    fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
3412        self.cx.upgrade_any_view_handle(handle)
3413    }
3414}
3415
3416impl<V: View> UpdateModel for ViewContext<'_, V> {
3417    fn update_model<T: Entity, O>(
3418        &mut self,
3419        handle: &ModelHandle<T>,
3420        update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
3421    ) -> O {
3422        self.app.update_model(handle, update)
3423    }
3424}
3425
3426impl<V: View> ReadView for ViewContext<'_, V> {
3427    fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
3428        self.app.read_view(handle)
3429    }
3430}
3431
3432impl<V: View> UpdateView for ViewContext<'_, V> {
3433    fn update_view<T, S>(
3434        &mut self,
3435        handle: &ViewHandle<T>,
3436        update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
3437    ) -> S
3438    where
3439        T: View,
3440    {
3441        self.app.update_view(handle, update)
3442    }
3443}
3444
3445impl<V: View> ElementStateContext for ViewContext<'_, V> {
3446    fn current_view_id(&self) -> usize {
3447        self.view_id
3448    }
3449}
3450
3451pub trait Handle<T> {
3452    type Weak: 'static;
3453    fn id(&self) -> usize;
3454    fn location(&self) -> EntityLocation;
3455    fn downgrade(&self) -> Self::Weak;
3456    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
3457    where
3458        Self: Sized;
3459}
3460
3461pub trait WeakHandle {
3462    fn id(&self) -> usize;
3463}
3464
3465#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
3466pub enum EntityLocation {
3467    Model(usize),
3468    View(usize, usize),
3469}
3470
3471pub struct ModelHandle<T: Entity> {
3472    model_id: usize,
3473    model_type: PhantomData<T>,
3474    ref_counts: Arc<Mutex<RefCounts>>,
3475
3476    #[cfg(any(test, feature = "test-support"))]
3477    handle_id: usize,
3478}
3479
3480impl<T: Entity> ModelHandle<T> {
3481    fn new(model_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
3482        ref_counts.lock().inc_model(model_id);
3483
3484        #[cfg(any(test, feature = "test-support"))]
3485        let handle_id = ref_counts
3486            .lock()
3487            .leak_detector
3488            .lock()
3489            .handle_created(Some(type_name::<T>()), model_id);
3490
3491        Self {
3492            model_id,
3493            model_type: PhantomData,
3494            ref_counts: ref_counts.clone(),
3495
3496            #[cfg(any(test, feature = "test-support"))]
3497            handle_id,
3498        }
3499    }
3500
3501    pub fn downgrade(&self) -> WeakModelHandle<T> {
3502        WeakModelHandle::new(self.model_id)
3503    }
3504
3505    pub fn id(&self) -> usize {
3506        self.model_id
3507    }
3508
3509    pub fn read<'a, C: ReadModel>(&self, cx: &'a C) -> &'a T {
3510        cx.read_model(self)
3511    }
3512
3513    pub fn read_with<'a, C, F, S>(&self, cx: &C, read: F) -> S
3514    where
3515        C: ReadModelWith,
3516        F: FnOnce(&T, &AppContext) -> S,
3517    {
3518        let mut read = Some(read);
3519        cx.read_model_with(self, &mut |model, cx| {
3520            let read = read.take().unwrap();
3521            read(model, cx)
3522        })
3523    }
3524
3525    pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
3526    where
3527        C: UpdateModel,
3528        F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
3529    {
3530        let mut update = Some(update);
3531        cx.update_model(self, &mut |model, cx| {
3532            let update = update.take().unwrap();
3533            update(model, cx)
3534        })
3535    }
3536
3537    #[cfg(any(test, feature = "test-support"))]
3538    pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
3539        let (tx, mut rx) = futures::channel::mpsc::unbounded();
3540        let mut cx = cx.cx.borrow_mut();
3541        let subscription = cx.observe(self, move |_, _| {
3542            tx.unbounded_send(()).ok();
3543        });
3544
3545        let duration = if std::env::var("CI").is_ok() {
3546            Duration::from_secs(5)
3547        } else {
3548            Duration::from_secs(1)
3549        };
3550
3551        async move {
3552            let notification = crate::util::timeout(duration, rx.next())
3553                .await
3554                .expect("next notification timed out");
3555            drop(subscription);
3556            notification.expect("model dropped while test was waiting for its next notification")
3557        }
3558    }
3559
3560    #[cfg(any(test, feature = "test-support"))]
3561    pub fn next_event(&self, cx: &TestAppContext) -> impl Future<Output = T::Event>
3562    where
3563        T::Event: Clone,
3564    {
3565        let (tx, mut rx) = futures::channel::mpsc::unbounded();
3566        let mut cx = cx.cx.borrow_mut();
3567        let subscription = cx.subscribe(self, move |_, event, _| {
3568            tx.unbounded_send(event.clone()).ok();
3569        });
3570
3571        let duration = if std::env::var("CI").is_ok() {
3572            Duration::from_secs(5)
3573        } else {
3574            Duration::from_secs(1)
3575        };
3576
3577        cx.foreground.start_waiting();
3578        async move {
3579            let event = crate::util::timeout(duration, rx.next())
3580                .await
3581                .expect("next event timed out");
3582            drop(subscription);
3583            event.expect("model dropped while test was waiting for its next event")
3584        }
3585    }
3586
3587    #[cfg(any(test, feature = "test-support"))]
3588    pub fn condition(
3589        &self,
3590        cx: &TestAppContext,
3591        mut predicate: impl FnMut(&T, &AppContext) -> bool,
3592    ) -> impl Future<Output = ()> {
3593        let (tx, mut rx) = futures::channel::mpsc::unbounded();
3594
3595        let mut cx = cx.cx.borrow_mut();
3596        let subscriptions = (
3597            cx.observe(self, {
3598                let tx = tx.clone();
3599                move |_, _| {
3600                    tx.unbounded_send(()).ok();
3601                }
3602            }),
3603            cx.subscribe(self, {
3604                let tx = tx.clone();
3605                move |_, _, _| {
3606                    tx.unbounded_send(()).ok();
3607                }
3608            }),
3609        );
3610
3611        let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
3612        let handle = self.downgrade();
3613        let duration = if std::env::var("CI").is_ok() {
3614            Duration::from_secs(5)
3615        } else {
3616            Duration::from_secs(1)
3617        };
3618
3619        async move {
3620            crate::util::timeout(duration, async move {
3621                loop {
3622                    {
3623                        let cx = cx.borrow();
3624                        let cx = cx.as_ref();
3625                        if predicate(
3626                            handle
3627                                .upgrade(cx)
3628                                .expect("model dropped with pending condition")
3629                                .read(cx),
3630                            cx,
3631                        ) {
3632                            break;
3633                        }
3634                    }
3635
3636                    cx.borrow().foreground().start_waiting();
3637                    rx.next()
3638                        .await
3639                        .expect("model dropped with pending condition");
3640                    cx.borrow().foreground().finish_waiting();
3641                }
3642            })
3643            .await
3644            .expect("condition timed out");
3645            drop(subscriptions);
3646        }
3647    }
3648}
3649
3650impl<T: Entity> Clone for ModelHandle<T> {
3651    fn clone(&self) -> Self {
3652        Self::new(self.model_id, &self.ref_counts)
3653    }
3654}
3655
3656impl<T: Entity> PartialEq for ModelHandle<T> {
3657    fn eq(&self, other: &Self) -> bool {
3658        self.model_id == other.model_id
3659    }
3660}
3661
3662impl<T: Entity> Eq for ModelHandle<T> {}
3663
3664impl<T: Entity> PartialEq<WeakModelHandle<T>> for ModelHandle<T> {
3665    fn eq(&self, other: &WeakModelHandle<T>) -> bool {
3666        self.model_id == other.model_id
3667    }
3668}
3669
3670impl<T: Entity> Hash for ModelHandle<T> {
3671    fn hash<H: Hasher>(&self, state: &mut H) {
3672        self.model_id.hash(state);
3673    }
3674}
3675
3676impl<T: Entity> std::borrow::Borrow<usize> for ModelHandle<T> {
3677    fn borrow(&self) -> &usize {
3678        &self.model_id
3679    }
3680}
3681
3682impl<T: Entity> Debug for ModelHandle<T> {
3683    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3684        f.debug_tuple(&format!("ModelHandle<{}>", type_name::<T>()))
3685            .field(&self.model_id)
3686            .finish()
3687    }
3688}
3689
3690unsafe impl<T: Entity> Send for ModelHandle<T> {}
3691unsafe impl<T: Entity> Sync for ModelHandle<T> {}
3692
3693impl<T: Entity> Drop for ModelHandle<T> {
3694    fn drop(&mut self) {
3695        let mut ref_counts = self.ref_counts.lock();
3696        ref_counts.dec_model(self.model_id);
3697
3698        #[cfg(any(test, feature = "test-support"))]
3699        ref_counts
3700            .leak_detector
3701            .lock()
3702            .handle_dropped(self.model_id, self.handle_id);
3703    }
3704}
3705
3706impl<T: Entity> Handle<T> for ModelHandle<T> {
3707    type Weak = WeakModelHandle<T>;
3708
3709    fn id(&self) -> usize {
3710        self.model_id
3711    }
3712
3713    fn location(&self) -> EntityLocation {
3714        EntityLocation::Model(self.model_id)
3715    }
3716
3717    fn downgrade(&self) -> Self::Weak {
3718        self.downgrade()
3719    }
3720
3721    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
3722    where
3723        Self: Sized,
3724    {
3725        weak.upgrade(cx)
3726    }
3727}
3728
3729pub struct WeakModelHandle<T> {
3730    model_id: usize,
3731    model_type: PhantomData<T>,
3732}
3733
3734impl<T> WeakHandle for WeakModelHandle<T> {
3735    fn id(&self) -> usize {
3736        self.model_id
3737    }
3738}
3739
3740unsafe impl<T> Send for WeakModelHandle<T> {}
3741unsafe impl<T> Sync for WeakModelHandle<T> {}
3742
3743impl<T: Entity> WeakModelHandle<T> {
3744    fn new(model_id: usize) -> Self {
3745        Self {
3746            model_id,
3747            model_type: PhantomData,
3748        }
3749    }
3750
3751    pub fn id(&self) -> usize {
3752        self.model_id
3753    }
3754
3755    pub fn is_upgradable(&self, cx: &impl UpgradeModelHandle) -> bool {
3756        cx.model_handle_is_upgradable(self)
3757    }
3758
3759    pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
3760        cx.upgrade_model_handle(self)
3761    }
3762}
3763
3764impl<T> Hash for WeakModelHandle<T> {
3765    fn hash<H: Hasher>(&self, state: &mut H) {
3766        self.model_id.hash(state)
3767    }
3768}
3769
3770impl<T> PartialEq for WeakModelHandle<T> {
3771    fn eq(&self, other: &Self) -> bool {
3772        self.model_id == other.model_id
3773    }
3774}
3775
3776impl<T> Eq for WeakModelHandle<T> {}
3777
3778impl<T> Clone for WeakModelHandle<T> {
3779    fn clone(&self) -> Self {
3780        Self {
3781            model_id: self.model_id,
3782            model_type: PhantomData,
3783        }
3784    }
3785}
3786
3787impl<T> Copy for WeakModelHandle<T> {}
3788
3789pub struct ViewHandle<T> {
3790    window_id: usize,
3791    view_id: usize,
3792    view_type: PhantomData<T>,
3793    ref_counts: Arc<Mutex<RefCounts>>,
3794    #[cfg(any(test, feature = "test-support"))]
3795    handle_id: usize,
3796}
3797
3798impl<T: View> ViewHandle<T> {
3799    fn new(window_id: usize, view_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
3800        ref_counts.lock().inc_view(window_id, view_id);
3801        #[cfg(any(test, feature = "test-support"))]
3802        let handle_id = ref_counts
3803            .lock()
3804            .leak_detector
3805            .lock()
3806            .handle_created(Some(type_name::<T>()), view_id);
3807
3808        Self {
3809            window_id,
3810            view_id,
3811            view_type: PhantomData,
3812            ref_counts: ref_counts.clone(),
3813
3814            #[cfg(any(test, feature = "test-support"))]
3815            handle_id,
3816        }
3817    }
3818
3819    pub fn downgrade(&self) -> WeakViewHandle<T> {
3820        WeakViewHandle::new(self.window_id, self.view_id)
3821    }
3822
3823    pub fn window_id(&self) -> usize {
3824        self.window_id
3825    }
3826
3827    pub fn id(&self) -> usize {
3828        self.view_id
3829    }
3830
3831    pub fn read<'a, C: ReadView>(&self, cx: &'a C) -> &'a T {
3832        cx.read_view(self)
3833    }
3834
3835    pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
3836    where
3837        C: ReadViewWith,
3838        F: FnOnce(&T, &AppContext) -> S,
3839    {
3840        let mut read = Some(read);
3841        cx.read_view_with(self, &mut |view, cx| {
3842            let read = read.take().unwrap();
3843            read(view, cx)
3844        })
3845    }
3846
3847    pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
3848    where
3849        C: UpdateView,
3850        F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
3851    {
3852        let mut update = Some(update);
3853        cx.update_view(self, &mut |view, cx| {
3854            let update = update.take().unwrap();
3855            update(view, cx)
3856        })
3857    }
3858
3859    pub fn defer<C, F>(&self, cx: &mut C, update: F)
3860    where
3861        C: AsMut<MutableAppContext>,
3862        F: 'static + FnOnce(&mut T, &mut ViewContext<T>),
3863    {
3864        let this = self.clone();
3865        cx.as_mut().defer(move |cx| {
3866            this.update(cx, |view, cx| update(view, cx));
3867        });
3868    }
3869
3870    pub fn is_focused(&self, cx: &AppContext) -> bool {
3871        cx.focused_view_id(self.window_id)
3872            .map_or(false, |focused_id| focused_id == self.view_id)
3873    }
3874
3875    #[cfg(any(test, feature = "test-support"))]
3876    pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
3877        use postage::prelude::{Sink as _, Stream as _};
3878
3879        let (mut tx, mut rx) = postage::mpsc::channel(1);
3880        let mut cx = cx.cx.borrow_mut();
3881        let subscription = cx.observe(self, move |_, _| {
3882            tx.try_send(()).ok();
3883        });
3884
3885        let duration = if std::env::var("CI").is_ok() {
3886            Duration::from_secs(5)
3887        } else {
3888            Duration::from_secs(1)
3889        };
3890
3891        async move {
3892            let notification = crate::util::timeout(duration, rx.recv())
3893                .await
3894                .expect("next notification timed out");
3895            drop(subscription);
3896            notification.expect("model dropped while test was waiting for its next notification")
3897        }
3898    }
3899
3900    #[cfg(any(test, feature = "test-support"))]
3901    pub fn condition(
3902        &self,
3903        cx: &TestAppContext,
3904        mut predicate: impl FnMut(&T, &AppContext) -> bool,
3905    ) -> impl Future<Output = ()> {
3906        use postage::prelude::{Sink as _, Stream as _};
3907
3908        let (tx, mut rx) = postage::mpsc::channel(1024);
3909
3910        let mut cx = cx.cx.borrow_mut();
3911        let subscriptions = self.update(&mut *cx, |_, cx| {
3912            (
3913                cx.observe(self, {
3914                    let mut tx = tx.clone();
3915                    move |_, _, _| {
3916                        tx.blocking_send(()).ok();
3917                    }
3918                }),
3919                cx.subscribe(self, {
3920                    let mut tx = tx.clone();
3921                    move |_, _, _, _| {
3922                        tx.blocking_send(()).ok();
3923                    }
3924                }),
3925            )
3926        });
3927
3928        let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
3929        let handle = self.downgrade();
3930        let duration = if std::env::var("CI").is_ok() {
3931            Duration::from_secs(2)
3932        } else {
3933            Duration::from_millis(500)
3934        };
3935
3936        async move {
3937            crate::util::timeout(duration, async move {
3938                loop {
3939                    {
3940                        let cx = cx.borrow();
3941                        let cx = cx.as_ref();
3942                        if predicate(
3943                            handle
3944                                .upgrade(cx)
3945                                .expect("view dropped with pending condition")
3946                                .read(cx),
3947                            cx,
3948                        ) {
3949                            break;
3950                        }
3951                    }
3952
3953                    cx.borrow().foreground().start_waiting();
3954                    rx.recv()
3955                        .await
3956                        .expect("view dropped with pending condition");
3957                    cx.borrow().foreground().finish_waiting();
3958                }
3959            })
3960            .await
3961            .expect("condition timed out");
3962            drop(subscriptions);
3963        }
3964    }
3965}
3966
3967impl<T: View> Clone for ViewHandle<T> {
3968    fn clone(&self) -> Self {
3969        ViewHandle::new(self.window_id, self.view_id, &self.ref_counts)
3970    }
3971}
3972
3973impl<T> PartialEq for ViewHandle<T> {
3974    fn eq(&self, other: &Self) -> bool {
3975        self.window_id == other.window_id && self.view_id == other.view_id
3976    }
3977}
3978
3979impl<T> PartialEq<WeakViewHandle<T>> for ViewHandle<T> {
3980    fn eq(&self, other: &WeakViewHandle<T>) -> bool {
3981        self.window_id == other.window_id && self.view_id == other.view_id
3982    }
3983}
3984
3985impl<T> PartialEq<ViewHandle<T>> for WeakViewHandle<T> {
3986    fn eq(&self, other: &ViewHandle<T>) -> bool {
3987        self.window_id == other.window_id && self.view_id == other.view_id
3988    }
3989}
3990
3991impl<T> Eq for ViewHandle<T> {}
3992
3993impl<T> Hash for ViewHandle<T> {
3994    fn hash<H: Hasher>(&self, state: &mut H) {
3995        self.window_id.hash(state);
3996        self.view_id.hash(state);
3997    }
3998}
3999
4000impl<T> Debug for ViewHandle<T> {
4001    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4002        f.debug_struct(&format!("ViewHandle<{}>", type_name::<T>()))
4003            .field("window_id", &self.window_id)
4004            .field("view_id", &self.view_id)
4005            .finish()
4006    }
4007}
4008
4009impl<T> Drop for ViewHandle<T> {
4010    fn drop(&mut self) {
4011        self.ref_counts
4012            .lock()
4013            .dec_view(self.window_id, self.view_id);
4014        #[cfg(any(test, feature = "test-support"))]
4015        self.ref_counts
4016            .lock()
4017            .leak_detector
4018            .lock()
4019            .handle_dropped(self.view_id, self.handle_id);
4020    }
4021}
4022
4023impl<T: View> Handle<T> for ViewHandle<T> {
4024    type Weak = WeakViewHandle<T>;
4025
4026    fn id(&self) -> usize {
4027        self.view_id
4028    }
4029
4030    fn location(&self) -> EntityLocation {
4031        EntityLocation::View(self.window_id, self.view_id)
4032    }
4033
4034    fn downgrade(&self) -> Self::Weak {
4035        self.downgrade()
4036    }
4037
4038    fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
4039    where
4040        Self: Sized,
4041    {
4042        weak.upgrade(cx)
4043    }
4044}
4045
4046pub struct AnyViewHandle {
4047    window_id: usize,
4048    view_id: usize,
4049    view_type: TypeId,
4050    ref_counts: Arc<Mutex<RefCounts>>,
4051
4052    #[cfg(any(test, feature = "test-support"))]
4053    handle_id: usize,
4054}
4055
4056impl AnyViewHandle {
4057    fn new(
4058        window_id: usize,
4059        view_id: usize,
4060        view_type: TypeId,
4061        ref_counts: Arc<Mutex<RefCounts>>,
4062    ) -> Self {
4063        ref_counts.lock().inc_view(window_id, view_id);
4064
4065        #[cfg(any(test, feature = "test-support"))]
4066        let handle_id = ref_counts
4067            .lock()
4068            .leak_detector
4069            .lock()
4070            .handle_created(None, view_id);
4071
4072        Self {
4073            window_id,
4074            view_id,
4075            view_type,
4076            ref_counts,
4077            #[cfg(any(test, feature = "test-support"))]
4078            handle_id,
4079        }
4080    }
4081
4082    pub fn id(&self) -> usize {
4083        self.view_id
4084    }
4085
4086    pub fn is<T: 'static>(&self) -> bool {
4087        TypeId::of::<T>() == self.view_type
4088    }
4089
4090    pub fn is_focused(&self, cx: &AppContext) -> bool {
4091        cx.focused_view_id(self.window_id)
4092            .map_or(false, |focused_id| focused_id == self.view_id)
4093    }
4094
4095    pub fn downcast<T: View>(self) -> Option<ViewHandle<T>> {
4096        if self.is::<T>() {
4097            let result = Some(ViewHandle {
4098                window_id: self.window_id,
4099                view_id: self.view_id,
4100                ref_counts: self.ref_counts.clone(),
4101                view_type: PhantomData,
4102                #[cfg(any(test, feature = "test-support"))]
4103                handle_id: self.handle_id,
4104            });
4105            unsafe {
4106                Arc::decrement_strong_count(&self.ref_counts);
4107            }
4108            std::mem::forget(self);
4109            result
4110        } else {
4111            None
4112        }
4113    }
4114
4115    pub fn downgrade(&self) -> AnyWeakViewHandle {
4116        AnyWeakViewHandle {
4117            window_id: self.window_id,
4118            view_id: self.view_id,
4119            view_type: self.view_type,
4120        }
4121    }
4122
4123    pub fn view_type(&self) -> TypeId {
4124        self.view_type
4125    }
4126
4127    pub fn debug_json(&self, cx: &AppContext) -> serde_json::Value {
4128        cx.views
4129            .get(&(self.window_id, self.view_id))
4130            .map_or_else(|| serde_json::Value::Null, |view| view.debug_json(cx))
4131    }
4132}
4133
4134impl Clone for AnyViewHandle {
4135    fn clone(&self) -> Self {
4136        Self::new(
4137            self.window_id,
4138            self.view_id,
4139            self.view_type,
4140            self.ref_counts.clone(),
4141        )
4142    }
4143}
4144
4145impl From<&AnyViewHandle> for AnyViewHandle {
4146    fn from(handle: &AnyViewHandle) -> Self {
4147        handle.clone()
4148    }
4149}
4150
4151impl<T: View> From<&ViewHandle<T>> for AnyViewHandle {
4152    fn from(handle: &ViewHandle<T>) -> Self {
4153        Self::new(
4154            handle.window_id,
4155            handle.view_id,
4156            TypeId::of::<T>(),
4157            handle.ref_counts.clone(),
4158        )
4159    }
4160}
4161
4162impl<T: View> From<ViewHandle<T>> for AnyViewHandle {
4163    fn from(handle: ViewHandle<T>) -> Self {
4164        let any_handle = AnyViewHandle {
4165            window_id: handle.window_id,
4166            view_id: handle.view_id,
4167            view_type: TypeId::of::<T>(),
4168            ref_counts: handle.ref_counts.clone(),
4169            #[cfg(any(test, feature = "test-support"))]
4170            handle_id: handle.handle_id,
4171        };
4172        unsafe {
4173            Arc::decrement_strong_count(&handle.ref_counts);
4174        }
4175        std::mem::forget(handle);
4176        any_handle
4177    }
4178}
4179
4180impl Drop for AnyViewHandle {
4181    fn drop(&mut self) {
4182        self.ref_counts
4183            .lock()
4184            .dec_view(self.window_id, self.view_id);
4185        #[cfg(any(test, feature = "test-support"))]
4186        self.ref_counts
4187            .lock()
4188            .leak_detector
4189            .lock()
4190            .handle_dropped(self.view_id, self.handle_id);
4191    }
4192}
4193
4194pub struct AnyModelHandle {
4195    model_id: usize,
4196    model_type: TypeId,
4197    ref_counts: Arc<Mutex<RefCounts>>,
4198
4199    #[cfg(any(test, feature = "test-support"))]
4200    handle_id: usize,
4201}
4202
4203impl AnyModelHandle {
4204    fn new(model_id: usize, model_type: TypeId, ref_counts: Arc<Mutex<RefCounts>>) -> Self {
4205        ref_counts.lock().inc_model(model_id);
4206
4207        #[cfg(any(test, feature = "test-support"))]
4208        let handle_id = ref_counts
4209            .lock()
4210            .leak_detector
4211            .lock()
4212            .handle_created(None, model_id);
4213
4214        Self {
4215            model_id,
4216            model_type,
4217            ref_counts,
4218
4219            #[cfg(any(test, feature = "test-support"))]
4220            handle_id,
4221        }
4222    }
4223
4224    pub fn downcast<T: Entity>(self) -> Option<ModelHandle<T>> {
4225        if self.is::<T>() {
4226            let result = Some(ModelHandle {
4227                model_id: self.model_id,
4228                model_type: PhantomData,
4229                ref_counts: self.ref_counts.clone(),
4230
4231                #[cfg(any(test, feature = "test-support"))]
4232                handle_id: self.handle_id,
4233            });
4234            unsafe {
4235                Arc::decrement_strong_count(&self.ref_counts);
4236            }
4237            std::mem::forget(self);
4238            result
4239        } else {
4240            None
4241        }
4242    }
4243
4244    pub fn downgrade(&self) -> AnyWeakModelHandle {
4245        AnyWeakModelHandle {
4246            model_id: self.model_id,
4247            model_type: self.model_type,
4248        }
4249    }
4250
4251    pub fn is<T: Entity>(&self) -> bool {
4252        self.model_type == TypeId::of::<T>()
4253    }
4254
4255    pub fn model_type(&self) -> TypeId {
4256        self.model_type
4257    }
4258}
4259
4260impl<T: Entity> From<ModelHandle<T>> for AnyModelHandle {
4261    fn from(handle: ModelHandle<T>) -> Self {
4262        Self::new(
4263            handle.model_id,
4264            TypeId::of::<T>(),
4265            handle.ref_counts.clone(),
4266        )
4267    }
4268}
4269
4270impl Clone for AnyModelHandle {
4271    fn clone(&self) -> Self {
4272        Self::new(self.model_id, self.model_type, self.ref_counts.clone())
4273    }
4274}
4275
4276impl Drop for AnyModelHandle {
4277    fn drop(&mut self) {
4278        let mut ref_counts = self.ref_counts.lock();
4279        ref_counts.dec_model(self.model_id);
4280
4281        #[cfg(any(test, feature = "test-support"))]
4282        ref_counts
4283            .leak_detector
4284            .lock()
4285            .handle_dropped(self.model_id, self.handle_id);
4286    }
4287}
4288
4289pub struct AnyWeakModelHandle {
4290    model_id: usize,
4291    model_type: TypeId,
4292}
4293
4294impl AnyWeakModelHandle {
4295    pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<AnyModelHandle> {
4296        cx.upgrade_any_model_handle(self)
4297    }
4298}
4299
4300impl<T: Entity> From<WeakModelHandle<T>> for AnyWeakModelHandle {
4301    fn from(handle: WeakModelHandle<T>) -> Self {
4302        AnyWeakModelHandle {
4303            model_id: handle.model_id,
4304            model_type: TypeId::of::<T>(),
4305        }
4306    }
4307}
4308
4309pub struct WeakViewHandle<T> {
4310    window_id: usize,
4311    view_id: usize,
4312    view_type: PhantomData<T>,
4313}
4314
4315impl<T> WeakHandle for WeakViewHandle<T> {
4316    fn id(&self) -> usize {
4317        self.view_id
4318    }
4319}
4320
4321impl<T: View> WeakViewHandle<T> {
4322    fn new(window_id: usize, view_id: usize) -> Self {
4323        Self {
4324            window_id,
4325            view_id,
4326            view_type: PhantomData,
4327        }
4328    }
4329
4330    pub fn id(&self) -> usize {
4331        self.view_id
4332    }
4333
4334    pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<ViewHandle<T>> {
4335        cx.upgrade_view_handle(self)
4336    }
4337}
4338
4339impl<T> Clone for WeakViewHandle<T> {
4340    fn clone(&self) -> Self {
4341        Self {
4342            window_id: self.window_id,
4343            view_id: self.view_id,
4344            view_type: PhantomData,
4345        }
4346    }
4347}
4348
4349impl<T> PartialEq for WeakViewHandle<T> {
4350    fn eq(&self, other: &Self) -> bool {
4351        self.window_id == other.window_id && self.view_id == other.view_id
4352    }
4353}
4354
4355impl<T> Eq for WeakViewHandle<T> {}
4356
4357impl<T> Hash for WeakViewHandle<T> {
4358    fn hash<H: Hasher>(&self, state: &mut H) {
4359        self.window_id.hash(state);
4360        self.view_id.hash(state);
4361    }
4362}
4363
4364pub struct AnyWeakViewHandle {
4365    window_id: usize,
4366    view_id: usize,
4367    view_type: TypeId,
4368}
4369
4370impl AnyWeakViewHandle {
4371    pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<AnyViewHandle> {
4372        cx.upgrade_any_view_handle(self)
4373    }
4374}
4375
4376impl<T: View> From<WeakViewHandle<T>> for AnyWeakViewHandle {
4377    fn from(handle: WeakViewHandle<T>) -> Self {
4378        AnyWeakViewHandle {
4379            window_id: handle.window_id,
4380            view_id: handle.view_id,
4381            view_type: TypeId::of::<T>(),
4382        }
4383    }
4384}
4385
4386#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4387pub struct ElementStateId {
4388    view_id: usize,
4389    element_id: usize,
4390    tag: TypeId,
4391}
4392
4393pub struct ElementStateHandle<T> {
4394    value_type: PhantomData<T>,
4395    id: ElementStateId,
4396    ref_counts: Weak<Mutex<RefCounts>>,
4397}
4398
4399impl<T: 'static> ElementStateHandle<T> {
4400    fn new(id: ElementStateId, frame_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
4401        ref_counts.lock().inc_element_state(id, frame_id);
4402        Self {
4403            value_type: PhantomData,
4404            id,
4405            ref_counts: Arc::downgrade(ref_counts),
4406        }
4407    }
4408
4409    pub fn read<'a>(&self, cx: &'a AppContext) -> &'a T {
4410        cx.element_states
4411            .get(&self.id)
4412            .unwrap()
4413            .downcast_ref()
4414            .unwrap()
4415    }
4416
4417    pub fn update<C, R>(&self, cx: &mut C, f: impl FnOnce(&mut T, &mut C) -> R) -> R
4418    where
4419        C: DerefMut<Target = MutableAppContext>,
4420    {
4421        let mut element_state = cx.deref_mut().cx.element_states.remove(&self.id).unwrap();
4422        let result = f(element_state.downcast_mut().unwrap(), cx);
4423        cx.deref_mut()
4424            .cx
4425            .element_states
4426            .insert(self.id, element_state);
4427        result
4428    }
4429}
4430
4431impl<T> Drop for ElementStateHandle<T> {
4432    fn drop(&mut self) {
4433        if let Some(ref_counts) = self.ref_counts.upgrade() {
4434            ref_counts.lock().dec_element_state(self.id);
4435        }
4436    }
4437}
4438
4439pub struct CursorStyleHandle {
4440    id: usize,
4441    next_cursor_style_handle_id: Arc<AtomicUsize>,
4442    platform: Arc<dyn Platform>,
4443}
4444
4445impl Drop for CursorStyleHandle {
4446    fn drop(&mut self) {
4447        if self.id + 1 == self.next_cursor_style_handle_id.load(SeqCst) {
4448            self.platform.set_cursor_style(CursorStyle::Arrow);
4449        }
4450    }
4451}
4452
4453#[must_use]
4454pub enum Subscription {
4455    Subscription {
4456        id: usize,
4457        entity_id: usize,
4458        subscriptions:
4459            Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, Option<SubscriptionCallback>>>>>>,
4460    },
4461    GlobalSubscription {
4462        id: usize,
4463        type_id: TypeId,
4464        subscriptions: Option<
4465            Weak<Mutex<HashMap<TypeId, BTreeMap<usize, Option<GlobalSubscriptionCallback>>>>>,
4466        >,
4467    },
4468    Observation {
4469        id: usize,
4470        entity_id: usize,
4471        observations:
4472            Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, Option<ObservationCallback>>>>>>,
4473    },
4474    GlobalObservation {
4475        id: usize,
4476        type_id: TypeId,
4477        observations: Option<
4478            Weak<Mutex<HashMap<TypeId, BTreeMap<usize, Option<GlobalObservationCallback>>>>>,
4479        >,
4480    },
4481    FocusObservation {
4482        id: usize,
4483        view_id: usize,
4484        observations:
4485            Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, Option<FocusObservationCallback>>>>>>,
4486    },
4487    ReleaseObservation {
4488        id: usize,
4489        entity_id: usize,
4490        observations:
4491            Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>>,
4492    },
4493}
4494
4495impl Subscription {
4496    pub fn detach(&mut self) {
4497        match self {
4498            Subscription::Subscription { subscriptions, .. } => {
4499                subscriptions.take();
4500            }
4501            Subscription::GlobalSubscription { subscriptions, .. } => {
4502                subscriptions.take();
4503            }
4504            Subscription::Observation { observations, .. } => {
4505                observations.take();
4506            }
4507            Subscription::GlobalObservation { observations, .. } => {
4508                observations.take();
4509            }
4510            Subscription::ReleaseObservation { observations, .. } => {
4511                observations.take();
4512            }
4513            Subscription::FocusObservation { observations, .. } => {
4514                observations.take();
4515            }
4516        }
4517    }
4518}
4519
4520impl Drop for Subscription {
4521    fn drop(&mut self) {
4522        match self {
4523            Subscription::Subscription {
4524                id,
4525                entity_id,
4526                subscriptions,
4527            } => {
4528                if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
4529                    match subscriptions
4530                        .lock()
4531                        .entry(*entity_id)
4532                        .or_default()
4533                        .entry(*id)
4534                    {
4535                        btree_map::Entry::Vacant(entry) => {
4536                            entry.insert(None);
4537                        }
4538                        btree_map::Entry::Occupied(entry) => {
4539                            entry.remove();
4540                        }
4541                    }
4542                }
4543            }
4544            Subscription::GlobalSubscription {
4545                id,
4546                type_id,
4547                subscriptions,
4548            } => {
4549                if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
4550                    match subscriptions.lock().entry(*type_id).or_default().entry(*id) {
4551                        btree_map::Entry::Vacant(entry) => {
4552                            entry.insert(None);
4553                        }
4554                        btree_map::Entry::Occupied(entry) => {
4555                            entry.remove();
4556                        }
4557                    }
4558                }
4559            }
4560            Subscription::Observation {
4561                id,
4562                entity_id,
4563                observations,
4564            } => {
4565                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
4566                    match observations
4567                        .lock()
4568                        .entry(*entity_id)
4569                        .or_default()
4570                        .entry(*id)
4571                    {
4572                        btree_map::Entry::Vacant(entry) => {
4573                            entry.insert(None);
4574                        }
4575                        btree_map::Entry::Occupied(entry) => {
4576                            entry.remove();
4577                        }
4578                    }
4579                }
4580            }
4581            Subscription::GlobalObservation {
4582                id,
4583                type_id,
4584                observations,
4585            } => {
4586                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
4587                    match observations.lock().entry(*type_id).or_default().entry(*id) {
4588                        collections::btree_map::Entry::Vacant(entry) => {
4589                            entry.insert(None);
4590                        }
4591                        collections::btree_map::Entry::Occupied(entry) => {
4592                            entry.remove();
4593                        }
4594                    }
4595                }
4596            }
4597            Subscription::ReleaseObservation {
4598                id,
4599                entity_id,
4600                observations,
4601            } => {
4602                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
4603                    if let Some(observations) = observations.lock().get_mut(entity_id) {
4604                        observations.remove(id);
4605                    }
4606                }
4607            }
4608            Subscription::FocusObservation {
4609                id,
4610                view_id,
4611                observations,
4612            } => {
4613                if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
4614                    match observations.lock().entry(*view_id).or_default().entry(*id) {
4615                        btree_map::Entry::Vacant(entry) => {
4616                            entry.insert(None);
4617                        }
4618                        btree_map::Entry::Occupied(entry) => {
4619                            entry.remove();
4620                        }
4621                    }
4622                }
4623            }
4624        }
4625    }
4626}
4627
4628lazy_static! {
4629    static ref LEAK_BACKTRACE: bool =
4630        std::env::var("LEAK_BACKTRACE").map_or(false, |b| !b.is_empty());
4631}
4632
4633#[cfg(any(test, feature = "test-support"))]
4634#[derive(Default)]
4635pub struct LeakDetector {
4636    next_handle_id: usize,
4637    handle_backtraces: HashMap<
4638        usize,
4639        (
4640            Option<&'static str>,
4641            HashMap<usize, Option<backtrace::Backtrace>>,
4642        ),
4643    >,
4644}
4645
4646#[cfg(any(test, feature = "test-support"))]
4647impl LeakDetector {
4648    fn handle_created(&mut self, type_name: Option<&'static str>, entity_id: usize) -> usize {
4649        let handle_id = post_inc(&mut self.next_handle_id);
4650        let entry = self.handle_backtraces.entry(entity_id).or_default();
4651        let backtrace = if *LEAK_BACKTRACE {
4652            Some(backtrace::Backtrace::new_unresolved())
4653        } else {
4654            None
4655        };
4656        if let Some(type_name) = type_name {
4657            entry.0.get_or_insert(type_name);
4658        }
4659        entry.1.insert(handle_id, backtrace);
4660        handle_id
4661    }
4662
4663    fn handle_dropped(&mut self, entity_id: usize, handle_id: usize) {
4664        if let Some((_, backtraces)) = self.handle_backtraces.get_mut(&entity_id) {
4665            assert!(backtraces.remove(&handle_id).is_some());
4666            if backtraces.is_empty() {
4667                self.handle_backtraces.remove(&entity_id);
4668            }
4669        }
4670    }
4671
4672    pub fn assert_dropped(&mut self, entity_id: usize) {
4673        if let Some((type_name, backtraces)) = self.handle_backtraces.get_mut(&entity_id) {
4674            for trace in backtraces.values_mut() {
4675                if let Some(trace) = trace {
4676                    trace.resolve();
4677                    eprintln!("{:?}", crate::util::CwdBacktrace(trace));
4678                }
4679            }
4680
4681            let hint = if *LEAK_BACKTRACE {
4682                ""
4683            } else {
4684                " – set LEAK_BACKTRACE=1 for more information"
4685            };
4686
4687            panic!(
4688                "{} handles to {} {} still exist{}",
4689                backtraces.len(),
4690                type_name.unwrap_or("entity"),
4691                entity_id,
4692                hint
4693            );
4694        }
4695    }
4696
4697    pub fn detect(&mut self) {
4698        let mut found_leaks = false;
4699        for (id, (type_name, backtraces)) in self.handle_backtraces.iter_mut() {
4700            eprintln!(
4701                "leaked {} handles to {} {}",
4702                backtraces.len(),
4703                type_name.unwrap_or("entity"),
4704                id
4705            );
4706            for trace in backtraces.values_mut() {
4707                if let Some(trace) = trace {
4708                    trace.resolve();
4709                    eprintln!("{:?}", crate::util::CwdBacktrace(trace));
4710                }
4711            }
4712            found_leaks = true;
4713        }
4714
4715        let hint = if *LEAK_BACKTRACE {
4716            ""
4717        } else {
4718            " – set LEAK_BACKTRACE=1 for more information"
4719        };
4720        assert!(!found_leaks, "detected leaked handles{}", hint);
4721    }
4722}
4723
4724#[derive(Default)]
4725struct RefCounts {
4726    entity_counts: HashMap<usize, usize>,
4727    element_state_counts: HashMap<ElementStateId, ElementStateRefCount>,
4728    dropped_models: HashSet<usize>,
4729    dropped_views: HashSet<(usize, usize)>,
4730    dropped_element_states: HashSet<ElementStateId>,
4731
4732    #[cfg(any(test, feature = "test-support"))]
4733    leak_detector: Arc<Mutex<LeakDetector>>,
4734}
4735
4736struct ElementStateRefCount {
4737    ref_count: usize,
4738    frame_id: usize,
4739}
4740
4741impl RefCounts {
4742    fn inc_model(&mut self, model_id: usize) {
4743        match self.entity_counts.entry(model_id) {
4744            Entry::Occupied(mut entry) => {
4745                *entry.get_mut() += 1;
4746            }
4747            Entry::Vacant(entry) => {
4748                entry.insert(1);
4749                self.dropped_models.remove(&model_id);
4750            }
4751        }
4752    }
4753
4754    fn inc_view(&mut self, window_id: usize, view_id: usize) {
4755        match self.entity_counts.entry(view_id) {
4756            Entry::Occupied(mut entry) => *entry.get_mut() += 1,
4757            Entry::Vacant(entry) => {
4758                entry.insert(1);
4759                self.dropped_views.remove(&(window_id, view_id));
4760            }
4761        }
4762    }
4763
4764    fn inc_element_state(&mut self, id: ElementStateId, frame_id: usize) {
4765        match self.element_state_counts.entry(id) {
4766            Entry::Occupied(mut entry) => {
4767                let entry = entry.get_mut();
4768                if entry.frame_id == frame_id || entry.ref_count >= 2 {
4769                    panic!("used the same element state more than once in the same frame");
4770                }
4771                entry.ref_count += 1;
4772                entry.frame_id = frame_id;
4773            }
4774            Entry::Vacant(entry) => {
4775                entry.insert(ElementStateRefCount {
4776                    ref_count: 1,
4777                    frame_id,
4778                });
4779                self.dropped_element_states.remove(&id);
4780            }
4781        }
4782    }
4783
4784    fn dec_model(&mut self, model_id: usize) {
4785        let count = self.entity_counts.get_mut(&model_id).unwrap();
4786        *count -= 1;
4787        if *count == 0 {
4788            self.entity_counts.remove(&model_id);
4789            self.dropped_models.insert(model_id);
4790        }
4791    }
4792
4793    fn dec_view(&mut self, window_id: usize, view_id: usize) {
4794        let count = self.entity_counts.get_mut(&view_id).unwrap();
4795        *count -= 1;
4796        if *count == 0 {
4797            self.entity_counts.remove(&view_id);
4798            self.dropped_views.insert((window_id, view_id));
4799        }
4800    }
4801
4802    fn dec_element_state(&mut self, id: ElementStateId) {
4803        let entry = self.element_state_counts.get_mut(&id).unwrap();
4804        entry.ref_count -= 1;
4805        if entry.ref_count == 0 {
4806            self.element_state_counts.remove(&id);
4807            self.dropped_element_states.insert(id);
4808        }
4809    }
4810
4811    fn is_entity_alive(&self, entity_id: usize) -> bool {
4812        self.entity_counts.contains_key(&entity_id)
4813    }
4814
4815    fn take_dropped(
4816        &mut self,
4817    ) -> (
4818        HashSet<usize>,
4819        HashSet<(usize, usize)>,
4820        HashSet<ElementStateId>,
4821    ) {
4822        (
4823            std::mem::take(&mut self.dropped_models),
4824            std::mem::take(&mut self.dropped_views),
4825            std::mem::take(&mut self.dropped_element_states),
4826        )
4827    }
4828}
4829
4830#[cfg(test)]
4831mod tests {
4832    use super::*;
4833    use crate::{actions, elements::*, impl_actions};
4834    use serde::Deserialize;
4835    use smol::future::poll_once;
4836    use std::{
4837        cell::Cell,
4838        sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
4839    };
4840
4841    #[crate::test(self)]
4842    fn test_model_handles(cx: &mut MutableAppContext) {
4843        struct Model {
4844            other: Option<ModelHandle<Model>>,
4845            events: Vec<String>,
4846        }
4847
4848        impl Entity for Model {
4849            type Event = usize;
4850        }
4851
4852        impl Model {
4853            fn new(other: Option<ModelHandle<Self>>, cx: &mut ModelContext<Self>) -> Self {
4854                if let Some(other) = other.as_ref() {
4855                    cx.observe(other, |me, _, _| {
4856                        me.events.push("notified".into());
4857                    })
4858                    .detach();
4859                    cx.subscribe(other, |me, _, event, _| {
4860                        me.events.push(format!("observed event {}", event));
4861                    })
4862                    .detach();
4863                }
4864
4865                Self {
4866                    other,
4867                    events: Vec::new(),
4868                }
4869            }
4870        }
4871
4872        let handle_1 = cx.add_model(|cx| Model::new(None, cx));
4873        let handle_2 = cx.add_model(|cx| Model::new(Some(handle_1.clone()), cx));
4874        assert_eq!(cx.cx.models.len(), 2);
4875
4876        handle_1.update(cx, |model, cx| {
4877            model.events.push("updated".into());
4878            cx.emit(1);
4879            cx.notify();
4880            cx.emit(2);
4881        });
4882        assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
4883        assert_eq!(
4884            handle_2.read(cx).events,
4885            vec![
4886                "observed event 1".to_string(),
4887                "notified".to_string(),
4888                "observed event 2".to_string(),
4889            ]
4890        );
4891
4892        handle_2.update(cx, |model, _| {
4893            drop(handle_1);
4894            model.other.take();
4895        });
4896
4897        assert_eq!(cx.cx.models.len(), 1);
4898        assert!(cx.subscriptions.lock().is_empty());
4899        assert!(cx.observations.lock().is_empty());
4900    }
4901
4902    #[crate::test(self)]
4903    fn test_model_events(cx: &mut MutableAppContext) {
4904        #[derive(Default)]
4905        struct Model {
4906            events: Vec<usize>,
4907        }
4908
4909        impl Entity for Model {
4910            type Event = usize;
4911        }
4912
4913        let handle_1 = cx.add_model(|_| Model::default());
4914        let handle_2 = cx.add_model(|_| Model::default());
4915
4916        handle_1.update(cx, |_, cx| {
4917            cx.subscribe(&handle_2, move |model: &mut Model, emitter, event, cx| {
4918                model.events.push(*event);
4919
4920                cx.subscribe(&emitter, |model, _, event, _| {
4921                    model.events.push(*event * 2);
4922                })
4923                .detach();
4924            })
4925            .detach();
4926        });
4927
4928        handle_2.update(cx, |_, c| c.emit(7));
4929        assert_eq!(handle_1.read(cx).events, vec![7]);
4930
4931        handle_2.update(cx, |_, c| c.emit(5));
4932        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
4933    }
4934
4935    #[crate::test(self)]
4936    fn test_model_emit_before_subscribe_in_same_update_cycle(cx: &mut MutableAppContext) {
4937        #[derive(Default)]
4938        struct Model;
4939
4940        impl Entity for Model {
4941            type Event = ();
4942        }
4943
4944        let events = Rc::new(RefCell::new(Vec::new()));
4945        cx.add_model(|cx| {
4946            drop(cx.subscribe(&cx.handle(), {
4947                let events = events.clone();
4948                move |_, _, _, _| events.borrow_mut().push("dropped before flush")
4949            }));
4950            cx.subscribe(&cx.handle(), {
4951                let events = events.clone();
4952                move |_, _, _, _| events.borrow_mut().push("before emit")
4953            })
4954            .detach();
4955            cx.emit(());
4956            cx.subscribe(&cx.handle(), {
4957                let events = events.clone();
4958                move |_, _, _, _| events.borrow_mut().push("after emit")
4959            })
4960            .detach();
4961            Model
4962        });
4963        assert_eq!(*events.borrow(), ["before emit"]);
4964    }
4965
4966    #[crate::test(self)]
4967    fn test_observe_and_notify_from_model(cx: &mut MutableAppContext) {
4968        #[derive(Default)]
4969        struct Model {
4970            count: usize,
4971            events: Vec<usize>,
4972        }
4973
4974        impl Entity for Model {
4975            type Event = ();
4976        }
4977
4978        let handle_1 = cx.add_model(|_| Model::default());
4979        let handle_2 = cx.add_model(|_| Model::default());
4980
4981        handle_1.update(cx, |_, c| {
4982            c.observe(&handle_2, move |model, observed, c| {
4983                model.events.push(observed.read(c).count);
4984                c.observe(&observed, |model, observed, c| {
4985                    model.events.push(observed.read(c).count * 2);
4986                })
4987                .detach();
4988            })
4989            .detach();
4990        });
4991
4992        handle_2.update(cx, |model, c| {
4993            model.count = 7;
4994            c.notify()
4995        });
4996        assert_eq!(handle_1.read(cx).events, vec![7]);
4997
4998        handle_2.update(cx, |model, c| {
4999            model.count = 5;
5000            c.notify()
5001        });
5002        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10])
5003    }
5004
5005    #[crate::test(self)]
5006    fn test_model_notify_before_observe_in_same_update_cycle(cx: &mut MutableAppContext) {
5007        #[derive(Default)]
5008        struct Model;
5009
5010        impl Entity for Model {
5011            type Event = ();
5012        }
5013
5014        let events = Rc::new(RefCell::new(Vec::new()));
5015        cx.add_model(|cx| {
5016            drop(cx.observe(&cx.handle(), {
5017                let events = events.clone();
5018                move |_, _, _| events.borrow_mut().push("dropped before flush")
5019            }));
5020            cx.observe(&cx.handle(), {
5021                let events = events.clone();
5022                move |_, _, _| events.borrow_mut().push("before notify")
5023            })
5024            .detach();
5025            cx.notify();
5026            cx.observe(&cx.handle(), {
5027                let events = events.clone();
5028                move |_, _, _| events.borrow_mut().push("after notify")
5029            })
5030            .detach();
5031            Model
5032        });
5033        assert_eq!(*events.borrow(), ["before notify"]);
5034    }
5035
5036    #[crate::test(self)]
5037    fn test_defer_and_after_window_update(cx: &mut MutableAppContext) {
5038        struct View {
5039            render_count: usize,
5040        }
5041
5042        impl Entity for View {
5043            type Event = usize;
5044        }
5045
5046        impl super::View for View {
5047            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5048                post_inc(&mut self.render_count);
5049                Empty::new().boxed()
5050            }
5051
5052            fn ui_name() -> &'static str {
5053                "View"
5054            }
5055        }
5056
5057        let (_, view) = cx.add_window(Default::default(), |_| View { render_count: 0 });
5058        let called_defer = Rc::new(AtomicBool::new(false));
5059        let called_after_window_update = Rc::new(AtomicBool::new(false));
5060
5061        view.update(cx, |this, cx| {
5062            assert_eq!(this.render_count, 1);
5063            cx.defer({
5064                let called_defer = called_defer.clone();
5065                move |this, _| {
5066                    assert_eq!(this.render_count, 1);
5067                    called_defer.store(true, SeqCst);
5068                }
5069            });
5070            cx.after_window_update({
5071                let called_after_window_update = called_after_window_update.clone();
5072                move |this, cx| {
5073                    assert_eq!(this.render_count, 2);
5074                    called_after_window_update.store(true, SeqCst);
5075                    cx.notify();
5076                }
5077            });
5078            assert!(!called_defer.load(SeqCst));
5079            assert!(!called_after_window_update.load(SeqCst));
5080            cx.notify();
5081        });
5082
5083        assert!(called_defer.load(SeqCst));
5084        assert!(called_after_window_update.load(SeqCst));
5085        assert_eq!(view.read(cx).render_count, 3);
5086    }
5087
5088    #[crate::test(self)]
5089    fn test_view_handles(cx: &mut MutableAppContext) {
5090        struct View {
5091            other: Option<ViewHandle<View>>,
5092            events: Vec<String>,
5093        }
5094
5095        impl Entity for View {
5096            type Event = usize;
5097        }
5098
5099        impl super::View for View {
5100            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5101                Empty::new().boxed()
5102            }
5103
5104            fn ui_name() -> &'static str {
5105                "View"
5106            }
5107        }
5108
5109        impl View {
5110            fn new(other: Option<ViewHandle<View>>, cx: &mut ViewContext<Self>) -> Self {
5111                if let Some(other) = other.as_ref() {
5112                    cx.subscribe(other, |me, _, event, _| {
5113                        me.events.push(format!("observed event {}", event));
5114                    })
5115                    .detach();
5116                }
5117                Self {
5118                    other,
5119                    events: Vec::new(),
5120                }
5121            }
5122        }
5123
5124        let (window_id, _) = cx.add_window(Default::default(), |cx| View::new(None, cx));
5125        let handle_1 = cx.add_view(window_id, |cx| View::new(None, cx));
5126        let handle_2 = cx.add_view(window_id, |cx| View::new(Some(handle_1.clone()), cx));
5127        assert_eq!(cx.cx.views.len(), 3);
5128
5129        handle_1.update(cx, |view, cx| {
5130            view.events.push("updated".into());
5131            cx.emit(1);
5132            cx.emit(2);
5133        });
5134        assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
5135        assert_eq!(
5136            handle_2.read(cx).events,
5137            vec![
5138                "observed event 1".to_string(),
5139                "observed event 2".to_string(),
5140            ]
5141        );
5142
5143        handle_2.update(cx, |view, _| {
5144            drop(handle_1);
5145            view.other.take();
5146        });
5147
5148        assert_eq!(cx.cx.views.len(), 2);
5149        assert!(cx.subscriptions.lock().is_empty());
5150        assert!(cx.observations.lock().is_empty());
5151    }
5152
5153    #[crate::test(self)]
5154    fn test_add_window(cx: &mut MutableAppContext) {
5155        struct View {
5156            mouse_down_count: Arc<AtomicUsize>,
5157        }
5158
5159        impl Entity for View {
5160            type Event = ();
5161        }
5162
5163        impl super::View for View {
5164            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5165                let mouse_down_count = self.mouse_down_count.clone();
5166                EventHandler::new(Empty::new().boxed())
5167                    .on_mouse_down(move |_| {
5168                        mouse_down_count.fetch_add(1, SeqCst);
5169                        true
5170                    })
5171                    .boxed()
5172            }
5173
5174            fn ui_name() -> &'static str {
5175                "View"
5176            }
5177        }
5178
5179        let mouse_down_count = Arc::new(AtomicUsize::new(0));
5180        let (window_id, _) = cx.add_window(Default::default(), |_| View {
5181            mouse_down_count: mouse_down_count.clone(),
5182        });
5183        let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
5184        // Ensure window's root element is in a valid lifecycle state.
5185        presenter.borrow_mut().dispatch_event(
5186            Event::LeftMouseDown {
5187                position: Default::default(),
5188                ctrl: false,
5189                alt: false,
5190                shift: false,
5191                cmd: false,
5192                click_count: 1,
5193            },
5194            cx,
5195        );
5196        assert_eq!(mouse_down_count.load(SeqCst), 1);
5197    }
5198
5199    #[crate::test(self)]
5200    fn test_entity_release_hooks(cx: &mut MutableAppContext) {
5201        struct Model {
5202            released: Rc<Cell<bool>>,
5203        }
5204
5205        struct View {
5206            released: Rc<Cell<bool>>,
5207        }
5208
5209        impl Entity for Model {
5210            type Event = ();
5211
5212            fn release(&mut self, _: &mut MutableAppContext) {
5213                self.released.set(true);
5214            }
5215        }
5216
5217        impl Entity for View {
5218            type Event = ();
5219
5220            fn release(&mut self, _: &mut MutableAppContext) {
5221                self.released.set(true);
5222            }
5223        }
5224
5225        impl super::View for View {
5226            fn ui_name() -> &'static str {
5227                "View"
5228            }
5229
5230            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5231                Empty::new().boxed()
5232            }
5233        }
5234
5235        let model_released = Rc::new(Cell::new(false));
5236        let model_release_observed = Rc::new(Cell::new(false));
5237        let view_released = Rc::new(Cell::new(false));
5238        let view_release_observed = Rc::new(Cell::new(false));
5239
5240        let model = cx.add_model(|_| Model {
5241            released: model_released.clone(),
5242        });
5243        let (window_id, view) = cx.add_window(Default::default(), |_| View {
5244            released: view_released.clone(),
5245        });
5246        assert!(!model_released.get());
5247        assert!(!view_released.get());
5248
5249        cx.observe_release(&model, {
5250            let model_release_observed = model_release_observed.clone();
5251            move |_, _| model_release_observed.set(true)
5252        })
5253        .detach();
5254        cx.observe_release(&view, {
5255            let view_release_observed = view_release_observed.clone();
5256            move |_, _| view_release_observed.set(true)
5257        })
5258        .detach();
5259
5260        cx.update(move |_| {
5261            drop(model);
5262        });
5263        assert!(model_released.get());
5264        assert!(model_release_observed.get());
5265
5266        drop(view);
5267        cx.remove_window(window_id);
5268        assert!(view_released.get());
5269        assert!(view_release_observed.get());
5270    }
5271
5272    #[crate::test(self)]
5273    fn test_view_events(cx: &mut MutableAppContext) {
5274        #[derive(Default)]
5275        struct View {
5276            events: Vec<usize>,
5277        }
5278
5279        impl Entity for View {
5280            type Event = usize;
5281        }
5282
5283        impl super::View for View {
5284            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5285                Empty::new().boxed()
5286            }
5287
5288            fn ui_name() -> &'static str {
5289                "View"
5290            }
5291        }
5292
5293        struct Model;
5294
5295        impl Entity for Model {
5296            type Event = usize;
5297        }
5298
5299        let (window_id, handle_1) = cx.add_window(Default::default(), |_| View::default());
5300        let handle_2 = cx.add_view(window_id, |_| View::default());
5301        let handle_3 = cx.add_model(|_| Model);
5302
5303        handle_1.update(cx, |_, cx| {
5304            cx.subscribe(&handle_2, move |me, emitter, event, cx| {
5305                me.events.push(*event);
5306
5307                cx.subscribe(&emitter, |me, _, event, _| {
5308                    me.events.push(*event * 2);
5309                })
5310                .detach();
5311            })
5312            .detach();
5313
5314            cx.subscribe(&handle_3, |me, _, event, _| {
5315                me.events.push(*event);
5316            })
5317            .detach();
5318        });
5319
5320        handle_2.update(cx, |_, c| c.emit(7));
5321        assert_eq!(handle_1.read(cx).events, vec![7]);
5322
5323        handle_2.update(cx, |_, c| c.emit(5));
5324        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
5325
5326        handle_3.update(cx, |_, c| c.emit(9));
5327        assert_eq!(handle_1.read(cx).events, vec![7, 5, 10, 9]);
5328    }
5329
5330    #[crate::test(self)]
5331    fn test_global_events(cx: &mut MutableAppContext) {
5332        #[derive(Clone, Debug, Eq, PartialEq)]
5333        struct GlobalEvent(u64);
5334
5335        let events = Rc::new(RefCell::new(Vec::new()));
5336        let first_subscription;
5337        let second_subscription;
5338
5339        {
5340            let events = events.clone();
5341            first_subscription = cx.subscribe_global(move |e: &GlobalEvent, _| {
5342                events.borrow_mut().push(("First", e.clone()));
5343            });
5344        }
5345
5346        {
5347            let events = events.clone();
5348            second_subscription = cx.subscribe_global(move |e: &GlobalEvent, _| {
5349                events.borrow_mut().push(("Second", e.clone()));
5350            });
5351        }
5352
5353        cx.update(|cx| {
5354            cx.emit_global(GlobalEvent(1));
5355            cx.emit_global(GlobalEvent(2));
5356        });
5357
5358        drop(first_subscription);
5359
5360        cx.update(|cx| {
5361            cx.emit_global(GlobalEvent(3));
5362        });
5363
5364        drop(second_subscription);
5365
5366        cx.update(|cx| {
5367            cx.emit_global(GlobalEvent(4));
5368        });
5369
5370        assert_eq!(
5371            &*events.borrow(),
5372            &[
5373                ("First", GlobalEvent(1)),
5374                ("Second", GlobalEvent(1)),
5375                ("First", GlobalEvent(2)),
5376                ("Second", GlobalEvent(2)),
5377                ("Second", GlobalEvent(3)),
5378            ]
5379        );
5380    }
5381
5382    #[crate::test(self)]
5383    fn test_global_events_emitted_before_subscription_in_same_update_cycle(
5384        cx: &mut MutableAppContext,
5385    ) {
5386        let events = Rc::new(RefCell::new(Vec::new()));
5387        cx.update(|cx| {
5388            {
5389                let events = events.clone();
5390                drop(cx.subscribe_global(move |_: &(), _| {
5391                    events.borrow_mut().push("dropped before emit");
5392                }));
5393            }
5394
5395            {
5396                let events = events.clone();
5397                cx.subscribe_global(move |_: &(), _| {
5398                    events.borrow_mut().push("before emit");
5399                })
5400                .detach();
5401            }
5402
5403            cx.emit_global(());
5404
5405            {
5406                let events = events.clone();
5407                cx.subscribe_global(move |_: &(), _| {
5408                    events.borrow_mut().push("after emit");
5409                })
5410                .detach();
5411            }
5412        });
5413
5414        assert_eq!(*events.borrow(), ["before emit"]);
5415    }
5416
5417    #[crate::test(self)]
5418    fn test_global_nested_events(cx: &mut MutableAppContext) {
5419        #[derive(Clone, Debug, Eq, PartialEq)]
5420        struct GlobalEvent(u64);
5421
5422        let events = Rc::new(RefCell::new(Vec::new()));
5423
5424        {
5425            let events = events.clone();
5426            cx.subscribe_global(move |e: &GlobalEvent, cx| {
5427                events.borrow_mut().push(("Outer", e.clone()));
5428
5429                if e.0 == 1 {
5430                    let events = events.clone();
5431                    cx.subscribe_global(move |e: &GlobalEvent, _| {
5432                        events.borrow_mut().push(("Inner", e.clone()));
5433                    })
5434                    .detach();
5435                }
5436            })
5437            .detach();
5438        }
5439
5440        cx.update(|cx| {
5441            cx.emit_global(GlobalEvent(1));
5442            cx.emit_global(GlobalEvent(2));
5443            cx.emit_global(GlobalEvent(3));
5444        });
5445        cx.update(|cx| {
5446            cx.emit_global(GlobalEvent(4));
5447        });
5448
5449        assert_eq!(
5450            &*events.borrow(),
5451            &[
5452                ("Outer", GlobalEvent(1)),
5453                ("Outer", GlobalEvent(2)),
5454                ("Outer", GlobalEvent(3)),
5455                ("Outer", GlobalEvent(4)),
5456                ("Inner", GlobalEvent(4)),
5457            ]
5458        );
5459    }
5460
5461    #[crate::test(self)]
5462    fn test_global(cx: &mut MutableAppContext) {
5463        type Global = usize;
5464
5465        let observation_count = Rc::new(RefCell::new(0));
5466        let subscription = cx.observe_global::<Global, _>({
5467            let observation_count = observation_count.clone();
5468            move |_, _| {
5469                *observation_count.borrow_mut() += 1;
5470            }
5471        });
5472
5473        assert!(!cx.has_global::<Global>());
5474        assert_eq!(cx.default_global::<Global>(), &0);
5475        assert_eq!(*observation_count.borrow(), 1);
5476        assert!(cx.has_global::<Global>());
5477        assert_eq!(
5478            cx.update_global::<Global, _, _>(|global, _| {
5479                *global = 1;
5480                "Update Result"
5481            }),
5482            "Update Result"
5483        );
5484        assert_eq!(*observation_count.borrow(), 2);
5485        assert_eq!(cx.global::<Global>(), &1);
5486
5487        drop(subscription);
5488        cx.update_global::<Global, _, _>(|global, _| {
5489            *global = 2;
5490        });
5491        assert_eq!(*observation_count.borrow(), 2);
5492
5493        type OtherGlobal = f32;
5494
5495        let observation_count = Rc::new(RefCell::new(0));
5496        cx.observe_global::<OtherGlobal, _>({
5497            let observation_count = observation_count.clone();
5498            move |_, _| {
5499                *observation_count.borrow_mut() += 1;
5500            }
5501        })
5502        .detach();
5503
5504        assert_eq!(
5505            cx.update_default_global::<OtherGlobal, _, _>(|global, _| {
5506                assert_eq!(global, &0.0);
5507                *global = 2.0;
5508                "Default update result"
5509            }),
5510            "Default update result"
5511        );
5512        assert_eq!(cx.global::<OtherGlobal>(), &2.0);
5513        assert_eq!(*observation_count.borrow(), 1);
5514    }
5515
5516    #[crate::test(self)]
5517    fn test_dropping_subscribers(cx: &mut MutableAppContext) {
5518        struct View;
5519
5520        impl Entity for View {
5521            type Event = ();
5522        }
5523
5524        impl super::View for View {
5525            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5526                Empty::new().boxed()
5527            }
5528
5529            fn ui_name() -> &'static str {
5530                "View"
5531            }
5532        }
5533
5534        struct Model;
5535
5536        impl Entity for Model {
5537            type Event = ();
5538        }
5539
5540        let (window_id, _) = cx.add_window(Default::default(), |_| View);
5541        let observing_view = cx.add_view(window_id, |_| View);
5542        let emitting_view = cx.add_view(window_id, |_| View);
5543        let observing_model = cx.add_model(|_| Model);
5544        let observed_model = cx.add_model(|_| Model);
5545
5546        observing_view.update(cx, |_, cx| {
5547            cx.subscribe(&emitting_view, |_, _, _, _| {}).detach();
5548            cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
5549        });
5550        observing_model.update(cx, |_, cx| {
5551            cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
5552        });
5553
5554        cx.update(|_| {
5555            drop(observing_view);
5556            drop(observing_model);
5557        });
5558
5559        emitting_view.update(cx, |_, cx| cx.emit(()));
5560        observed_model.update(cx, |_, cx| cx.emit(()));
5561    }
5562
5563    #[crate::test(self)]
5564    fn test_view_emit_before_subscribe_in_same_update_cycle(cx: &mut MutableAppContext) {
5565        #[derive(Default)]
5566        struct TestView;
5567
5568        impl Entity for TestView {
5569            type Event = ();
5570        }
5571
5572        impl View for TestView {
5573            fn ui_name() -> &'static str {
5574                "TestView"
5575            }
5576
5577            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5578                Empty::new().boxed()
5579            }
5580        }
5581
5582        let events = Rc::new(RefCell::new(Vec::new()));
5583        cx.add_window(Default::default(), |cx| {
5584            drop(cx.subscribe(&cx.handle(), {
5585                let events = events.clone();
5586                move |_, _, _, _| events.borrow_mut().push("dropped before flush")
5587            }));
5588            cx.subscribe(&cx.handle(), {
5589                let events = events.clone();
5590                move |_, _, _, _| events.borrow_mut().push("before emit")
5591            })
5592            .detach();
5593            cx.emit(());
5594            cx.subscribe(&cx.handle(), {
5595                let events = events.clone();
5596                move |_, _, _, _| events.borrow_mut().push("after emit")
5597            })
5598            .detach();
5599            TestView
5600        });
5601        assert_eq!(*events.borrow(), ["before emit"]);
5602    }
5603
5604    #[crate::test(self)]
5605    fn test_observe_and_notify_from_view(cx: &mut MutableAppContext) {
5606        #[derive(Default)]
5607        struct View {
5608            events: Vec<usize>,
5609        }
5610
5611        impl Entity for View {
5612            type Event = usize;
5613        }
5614
5615        impl super::View for View {
5616            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5617                Empty::new().boxed()
5618            }
5619
5620            fn ui_name() -> &'static str {
5621                "View"
5622            }
5623        }
5624
5625        #[derive(Default)]
5626        struct Model {
5627            count: usize,
5628        }
5629
5630        impl Entity for Model {
5631            type Event = ();
5632        }
5633
5634        let (_, view) = cx.add_window(Default::default(), |_| View::default());
5635        let model = cx.add_model(|_| Model::default());
5636
5637        view.update(cx, |_, c| {
5638            c.observe(&model, |me, observed, c| {
5639                me.events.push(observed.read(c).count)
5640            })
5641            .detach();
5642        });
5643
5644        model.update(cx, |model, c| {
5645            model.count = 11;
5646            c.notify();
5647        });
5648        assert_eq!(view.read(cx).events, vec![11]);
5649    }
5650
5651    #[crate::test(self)]
5652    fn test_view_notify_before_observe_in_same_update_cycle(cx: &mut MutableAppContext) {
5653        #[derive(Default)]
5654        struct TestView;
5655
5656        impl Entity for TestView {
5657            type Event = ();
5658        }
5659
5660        impl View for TestView {
5661            fn ui_name() -> &'static str {
5662                "TestView"
5663            }
5664
5665            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5666                Empty::new().boxed()
5667            }
5668        }
5669
5670        let events = Rc::new(RefCell::new(Vec::new()));
5671        cx.add_window(Default::default(), |cx| {
5672            drop(cx.observe(&cx.handle(), {
5673                let events = events.clone();
5674                move |_, _, _| events.borrow_mut().push("dropped before flush")
5675            }));
5676            cx.observe(&cx.handle(), {
5677                let events = events.clone();
5678                move |_, _, _| events.borrow_mut().push("before notify")
5679            })
5680            .detach();
5681            cx.notify();
5682            cx.observe(&cx.handle(), {
5683                let events = events.clone();
5684                move |_, _, _| events.borrow_mut().push("after notify")
5685            })
5686            .detach();
5687            TestView
5688        });
5689        assert_eq!(*events.borrow(), ["before notify"]);
5690    }
5691
5692    #[crate::test(self)]
5693    fn test_dropping_observers(cx: &mut MutableAppContext) {
5694        struct View;
5695
5696        impl Entity for View {
5697            type Event = ();
5698        }
5699
5700        impl super::View for View {
5701            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5702                Empty::new().boxed()
5703            }
5704
5705            fn ui_name() -> &'static str {
5706                "View"
5707            }
5708        }
5709
5710        struct Model;
5711
5712        impl Entity for Model {
5713            type Event = ();
5714        }
5715
5716        let (window_id, _) = cx.add_window(Default::default(), |_| View);
5717        let observing_view = cx.add_view(window_id, |_| View);
5718        let observing_model = cx.add_model(|_| Model);
5719        let observed_model = cx.add_model(|_| Model);
5720
5721        observing_view.update(cx, |_, cx| {
5722            cx.observe(&observed_model, |_, _, _| {}).detach();
5723        });
5724        observing_model.update(cx, |_, cx| {
5725            cx.observe(&observed_model, |_, _, _| {}).detach();
5726        });
5727
5728        cx.update(|_| {
5729            drop(observing_view);
5730            drop(observing_model);
5731        });
5732
5733        observed_model.update(cx, |_, cx| cx.notify());
5734    }
5735
5736    #[crate::test(self)]
5737    fn test_dropping_subscriptions_during_callback(cx: &mut MutableAppContext) {
5738        struct Model;
5739
5740        impl Entity for Model {
5741            type Event = u64;
5742        }
5743
5744        // Events
5745        let observing_model = cx.add_model(|_| Model);
5746        let observed_model = cx.add_model(|_| Model);
5747
5748        let events = Rc::new(RefCell::new(Vec::new()));
5749
5750        observing_model.update(cx, |_, cx| {
5751            let events = events.clone();
5752            let subscription = Rc::new(RefCell::new(None));
5753            *subscription.borrow_mut() = Some(cx.subscribe(&observed_model, {
5754                let subscription = subscription.clone();
5755                move |_, _, e, _| {
5756                    subscription.borrow_mut().take();
5757                    events.borrow_mut().push(e.clone());
5758                }
5759            }));
5760        });
5761
5762        observed_model.update(cx, |_, cx| {
5763            cx.emit(1);
5764            cx.emit(2);
5765        });
5766
5767        assert_eq!(*events.borrow(), [1]);
5768
5769        // Global Events
5770        #[derive(Clone, Debug, Eq, PartialEq)]
5771        struct GlobalEvent(u64);
5772
5773        let events = Rc::new(RefCell::new(Vec::new()));
5774
5775        {
5776            let events = events.clone();
5777            let subscription = Rc::new(RefCell::new(None));
5778            *subscription.borrow_mut() = Some(cx.subscribe_global({
5779                let subscription = subscription.clone();
5780                move |e: &GlobalEvent, _| {
5781                    subscription.borrow_mut().take();
5782                    events.borrow_mut().push(e.clone());
5783                }
5784            }));
5785        }
5786
5787        cx.update(|cx| {
5788            cx.emit_global(GlobalEvent(1));
5789            cx.emit_global(GlobalEvent(2));
5790        });
5791
5792        assert_eq!(*events.borrow(), [GlobalEvent(1)]);
5793
5794        // Model Observation
5795        let observing_model = cx.add_model(|_| Model);
5796        let observed_model = cx.add_model(|_| Model);
5797
5798        let observation_count = Rc::new(RefCell::new(0));
5799
5800        observing_model.update(cx, |_, cx| {
5801            let observation_count = observation_count.clone();
5802            let subscription = Rc::new(RefCell::new(None));
5803            *subscription.borrow_mut() = Some(cx.observe(&observed_model, {
5804                let subscription = subscription.clone();
5805                move |_, _, _| {
5806                    subscription.borrow_mut().take();
5807                    *observation_count.borrow_mut() += 1;
5808                }
5809            }));
5810        });
5811
5812        observed_model.update(cx, |_, cx| {
5813            cx.notify();
5814        });
5815
5816        observed_model.update(cx, |_, cx| {
5817            cx.notify();
5818        });
5819
5820        assert_eq!(*observation_count.borrow(), 1);
5821
5822        // View Observation
5823        struct View;
5824
5825        impl Entity for View {
5826            type Event = ();
5827        }
5828
5829        impl super::View for View {
5830            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5831                Empty::new().boxed()
5832            }
5833
5834            fn ui_name() -> &'static str {
5835                "View"
5836            }
5837        }
5838
5839        let (window_id, _) = cx.add_window(Default::default(), |_| View);
5840        let observing_view = cx.add_view(window_id, |_| View);
5841        let observed_view = cx.add_view(window_id, |_| View);
5842
5843        let observation_count = Rc::new(RefCell::new(0));
5844        observing_view.update(cx, |_, cx| {
5845            let observation_count = observation_count.clone();
5846            let subscription = Rc::new(RefCell::new(None));
5847            *subscription.borrow_mut() = Some(cx.observe(&observed_view, {
5848                let subscription = subscription.clone();
5849                move |_, _, _| {
5850                    subscription.borrow_mut().take();
5851                    *observation_count.borrow_mut() += 1;
5852                }
5853            }));
5854        });
5855
5856        observed_view.update(cx, |_, cx| {
5857            cx.notify();
5858        });
5859
5860        observed_view.update(cx, |_, cx| {
5861            cx.notify();
5862        });
5863
5864        assert_eq!(*observation_count.borrow(), 1);
5865
5866        // Global Observation
5867        let observation_count = Rc::new(RefCell::new(0));
5868        let subscription = Rc::new(RefCell::new(None));
5869        *subscription.borrow_mut() = Some(cx.observe_global::<(), _>({
5870            let observation_count = observation_count.clone();
5871            let subscription = subscription.clone();
5872            move |_, _| {
5873                subscription.borrow_mut().take();
5874                *observation_count.borrow_mut() += 1;
5875            }
5876        }));
5877
5878        cx.default_global::<()>();
5879        cx.set_global(());
5880        assert_eq!(*observation_count.borrow(), 1);
5881    }
5882
5883    #[crate::test(self)]
5884    fn test_focus(cx: &mut MutableAppContext) {
5885        struct View {
5886            name: String,
5887            events: Arc<Mutex<Vec<String>>>,
5888        }
5889
5890        impl Entity for View {
5891            type Event = ();
5892        }
5893
5894        impl super::View for View {
5895            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
5896                Empty::new().boxed()
5897            }
5898
5899            fn ui_name() -> &'static str {
5900                "View"
5901            }
5902
5903            fn on_focus(&mut self, _: &mut ViewContext<Self>) {
5904                self.events.lock().push(format!("{} focused", &self.name));
5905            }
5906
5907            fn on_blur(&mut self, _: &mut ViewContext<Self>) {
5908                self.events.lock().push(format!("{} blurred", &self.name));
5909            }
5910        }
5911
5912        let view_events: Arc<Mutex<Vec<String>>> = Default::default();
5913        let (window_id, view_1) = cx.add_window(Default::default(), |_| View {
5914            events: view_events.clone(),
5915            name: "view 1".to_string(),
5916        });
5917        let view_2 = cx.add_view(window_id, |_| View {
5918            events: view_events.clone(),
5919            name: "view 2".to_string(),
5920        });
5921
5922        let observed_events: Arc<Mutex<Vec<String>>> = Default::default();
5923        view_1.update(cx, |_, cx| {
5924            cx.observe_focus(&view_2, {
5925                let observed_events = observed_events.clone();
5926                move |this, view, cx| {
5927                    observed_events.lock().push(format!(
5928                        "{} observed {}'s focus",
5929                        this.name,
5930                        view.read(cx).name
5931                    ))
5932                }
5933            })
5934            .detach();
5935        });
5936        view_2.update(cx, |_, cx| {
5937            cx.observe_focus(&view_1, {
5938                let observed_events = observed_events.clone();
5939                move |this, view, cx| {
5940                    observed_events.lock().push(format!(
5941                        "{} observed {}'s focus",
5942                        this.name,
5943                        view.read(cx).name
5944                    ))
5945                }
5946            })
5947            .detach();
5948        });
5949
5950        view_1.update(cx, |_, cx| {
5951            // Ensure only the latest focus is honored.
5952            cx.focus(&view_2);
5953            cx.focus(&view_1);
5954            cx.focus(&view_2);
5955        });
5956        view_1.update(cx, |_, cx| cx.focus(&view_1));
5957        view_1.update(cx, |_, cx| cx.focus(&view_2));
5958        view_1.update(cx, |_, _| drop(view_2));
5959
5960        assert_eq!(
5961            *view_events.lock(),
5962            [
5963                "view 1 focused".to_string(),
5964                "view 1 blurred".to_string(),
5965                "view 2 focused".to_string(),
5966                "view 2 blurred".to_string(),
5967                "view 1 focused".to_string(),
5968                "view 1 blurred".to_string(),
5969                "view 2 focused".to_string(),
5970                "view 1 focused".to_string(),
5971            ],
5972        );
5973        assert_eq!(
5974            *observed_events.lock(),
5975            [
5976                "view 1 observed view 2's focus".to_string(),
5977                "view 2 observed view 1's focus".to_string(),
5978                "view 1 observed view 2's focus".to_string(),
5979            ]
5980        );
5981    }
5982
5983    #[crate::test(self)]
5984    fn test_deserialize_actions(cx: &mut MutableAppContext) {
5985        #[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
5986        pub struct ComplexAction {
5987            arg: String,
5988            count: usize,
5989        }
5990
5991        actions!(test::something, [SimpleAction]);
5992        impl_actions!(test::something, [ComplexAction]);
5993
5994        cx.add_global_action(move |_: &SimpleAction, _: &mut MutableAppContext| {});
5995        cx.add_global_action(move |_: &ComplexAction, _: &mut MutableAppContext| {});
5996
5997        let action1 = cx
5998            .deserialize_action(
5999                "test::something::ComplexAction",
6000                Some(r#"{"arg": "a", "count": 5}"#),
6001            )
6002            .unwrap();
6003        let action2 = cx
6004            .deserialize_action("test::something::SimpleAction", None)
6005            .unwrap();
6006        assert_eq!(
6007            action1.as_any().downcast_ref::<ComplexAction>().unwrap(),
6008            &ComplexAction {
6009                arg: "a".to_string(),
6010                count: 5,
6011            }
6012        );
6013        assert_eq!(
6014            action2.as_any().downcast_ref::<SimpleAction>().unwrap(),
6015            &SimpleAction
6016        );
6017    }
6018
6019    #[crate::test(self)]
6020    fn test_dispatch_action(cx: &mut MutableAppContext) {
6021        struct ViewA {
6022            id: usize,
6023        }
6024
6025        impl Entity for ViewA {
6026            type Event = ();
6027        }
6028
6029        impl View for ViewA {
6030            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6031                Empty::new().boxed()
6032            }
6033
6034            fn ui_name() -> &'static str {
6035                "View"
6036            }
6037        }
6038
6039        struct ViewB {
6040            id: usize,
6041        }
6042
6043        impl Entity for ViewB {
6044            type Event = ();
6045        }
6046
6047        impl View for ViewB {
6048            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6049                Empty::new().boxed()
6050            }
6051
6052            fn ui_name() -> &'static str {
6053                "View"
6054            }
6055        }
6056
6057        #[derive(Clone, Deserialize)]
6058        pub struct Action(pub String);
6059
6060        impl_actions!(test, [Action]);
6061
6062        let actions = Rc::new(RefCell::new(Vec::new()));
6063
6064        cx.add_global_action({
6065            let actions = actions.clone();
6066            move |_: &Action, _: &mut MutableAppContext| {
6067                actions.borrow_mut().push("global".to_string());
6068            }
6069        });
6070
6071        cx.add_action({
6072            let actions = actions.clone();
6073            move |view: &mut ViewA, action: &Action, cx| {
6074                assert_eq!(action.0, "bar");
6075                cx.propagate_action();
6076                actions.borrow_mut().push(format!("{} a", view.id));
6077            }
6078        });
6079
6080        cx.add_action({
6081            let actions = actions.clone();
6082            move |view: &mut ViewA, _: &Action, cx| {
6083                if view.id != 1 {
6084                    cx.add_view(|cx| {
6085                        cx.propagate_action(); // Still works on a nested ViewContext
6086                        ViewB { id: 5 }
6087                    });
6088                }
6089                actions.borrow_mut().push(format!("{} b", view.id));
6090            }
6091        });
6092
6093        cx.add_action({
6094            let actions = actions.clone();
6095            move |view: &mut ViewB, _: &Action, cx| {
6096                cx.propagate_action();
6097                actions.borrow_mut().push(format!("{} c", view.id));
6098            }
6099        });
6100
6101        cx.add_action({
6102            let actions = actions.clone();
6103            move |view: &mut ViewB, _: &Action, cx| {
6104                cx.propagate_action();
6105                actions.borrow_mut().push(format!("{} d", view.id));
6106            }
6107        });
6108
6109        cx.capture_action({
6110            let actions = actions.clone();
6111            move |view: &mut ViewA, _: &Action, cx| {
6112                cx.propagate_action();
6113                actions.borrow_mut().push(format!("{} capture", view.id));
6114            }
6115        });
6116
6117        let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
6118        let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 });
6119        let view_3 = cx.add_view(window_id, |_| ViewA { id: 3 });
6120        let view_4 = cx.add_view(window_id, |_| ViewB { id: 4 });
6121
6122        cx.dispatch_action(
6123            window_id,
6124            vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()],
6125            &Action("bar".to_string()),
6126        );
6127
6128        assert_eq!(
6129            *actions.borrow(),
6130            vec![
6131                "1 capture",
6132                "3 capture",
6133                "4 d",
6134                "4 c",
6135                "3 b",
6136                "3 a",
6137                "2 d",
6138                "2 c",
6139                "1 b"
6140            ]
6141        );
6142
6143        // Remove view_1, which doesn't propagate the action
6144        actions.borrow_mut().clear();
6145        cx.dispatch_action(
6146            window_id,
6147            vec![view_2.id(), view_3.id(), view_4.id()],
6148            &Action("bar".to_string()),
6149        );
6150
6151        assert_eq!(
6152            *actions.borrow(),
6153            vec![
6154                "3 capture",
6155                "4 d",
6156                "4 c",
6157                "3 b",
6158                "3 a",
6159                "2 d",
6160                "2 c",
6161                "global"
6162            ]
6163        );
6164    }
6165
6166    #[crate::test(self)]
6167    fn test_dispatch_keystroke(cx: &mut MutableAppContext) {
6168        #[derive(Clone, Deserialize)]
6169        pub struct Action(String);
6170
6171        impl_actions!(test, [Action]);
6172
6173        struct View {
6174            id: usize,
6175            keymap_context: keymap::Context,
6176        }
6177
6178        impl Entity for View {
6179            type Event = ();
6180        }
6181
6182        impl super::View for View {
6183            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6184                Empty::new().boxed()
6185            }
6186
6187            fn ui_name() -> &'static str {
6188                "View"
6189            }
6190
6191            fn keymap_context(&self, _: &AppContext) -> keymap::Context {
6192                self.keymap_context.clone()
6193            }
6194        }
6195
6196        impl View {
6197            fn new(id: usize) -> Self {
6198                View {
6199                    id,
6200                    keymap_context: keymap::Context::default(),
6201                }
6202            }
6203        }
6204
6205        let mut view_1 = View::new(1);
6206        let mut view_2 = View::new(2);
6207        let mut view_3 = View::new(3);
6208        view_1.keymap_context.set.insert("a".into());
6209        view_2.keymap_context.set.insert("a".into());
6210        view_2.keymap_context.set.insert("b".into());
6211        view_3.keymap_context.set.insert("a".into());
6212        view_3.keymap_context.set.insert("b".into());
6213        view_3.keymap_context.set.insert("c".into());
6214
6215        let (window_id, view_1) = cx.add_window(Default::default(), |_| view_1);
6216        let view_2 = cx.add_view(window_id, |_| view_2);
6217        let view_3 = cx.add_view(window_id, |_| view_3);
6218
6219        // This keymap's only binding dispatches an action on view 2 because that view will have
6220        // "a" and "b" in its context, but not "c".
6221        cx.add_bindings(vec![keymap::Binding::new(
6222            "a",
6223            Action("a".to_string()),
6224            Some("a && b && !c"),
6225        )]);
6226
6227        cx.add_bindings(vec![keymap::Binding::new(
6228            "b",
6229            Action("b".to_string()),
6230            None,
6231        )]);
6232
6233        let actions = Rc::new(RefCell::new(Vec::new()));
6234        cx.add_action({
6235            let actions = actions.clone();
6236            move |view: &mut View, action: &Action, cx| {
6237                if action.0 == "a" {
6238                    actions.borrow_mut().push(format!("{} a", view.id));
6239                } else {
6240                    actions
6241                        .borrow_mut()
6242                        .push(format!("{} {}", view.id, action.0));
6243                    cx.propagate_action();
6244                }
6245            }
6246        });
6247
6248        cx.add_global_action({
6249            let actions = actions.clone();
6250            move |action: &Action, _| {
6251                actions.borrow_mut().push(format!("global {}", action.0));
6252            }
6253        });
6254
6255        cx.dispatch_keystroke(
6256            window_id,
6257            vec![view_1.id(), view_2.id(), view_3.id()],
6258            &Keystroke::parse("a").unwrap(),
6259        );
6260
6261        assert_eq!(&*actions.borrow(), &["2 a"]);
6262
6263        actions.borrow_mut().clear();
6264        cx.dispatch_keystroke(
6265            window_id,
6266            vec![view_1.id(), view_2.id(), view_3.id()],
6267            &Keystroke::parse("b").unwrap(),
6268        );
6269
6270        assert_eq!(&*actions.borrow(), &["3 b", "2 b", "1 b", "global b"]);
6271    }
6272
6273    #[crate::test(self)]
6274    async fn test_model_condition(cx: &mut TestAppContext) {
6275        struct Counter(usize);
6276
6277        impl super::Entity for Counter {
6278            type Event = ();
6279        }
6280
6281        impl Counter {
6282            fn inc(&mut self, cx: &mut ModelContext<Self>) {
6283                self.0 += 1;
6284                cx.notify();
6285            }
6286        }
6287
6288        let model = cx.add_model(|_| Counter(0));
6289
6290        let condition1 = model.condition(&cx, |model, _| model.0 == 2);
6291        let condition2 = model.condition(&cx, |model, _| model.0 == 3);
6292        smol::pin!(condition1, condition2);
6293
6294        model.update(cx, |model, cx| model.inc(cx));
6295        assert_eq!(poll_once(&mut condition1).await, None);
6296        assert_eq!(poll_once(&mut condition2).await, None);
6297
6298        model.update(cx, |model, cx| model.inc(cx));
6299        assert_eq!(poll_once(&mut condition1).await, Some(()));
6300        assert_eq!(poll_once(&mut condition2).await, None);
6301
6302        model.update(cx, |model, cx| model.inc(cx));
6303        assert_eq!(poll_once(&mut condition2).await, Some(()));
6304
6305        model.update(cx, |_, cx| cx.notify());
6306    }
6307
6308    #[crate::test(self)]
6309    #[should_panic]
6310    async fn test_model_condition_timeout(cx: &mut TestAppContext) {
6311        struct Model;
6312
6313        impl super::Entity for Model {
6314            type Event = ();
6315        }
6316
6317        let model = cx.add_model(|_| Model);
6318        model.condition(&cx, |_, _| false).await;
6319    }
6320
6321    #[crate::test(self)]
6322    #[should_panic(expected = "model dropped with pending condition")]
6323    async fn test_model_condition_panic_on_drop(cx: &mut TestAppContext) {
6324        struct Model;
6325
6326        impl super::Entity for Model {
6327            type Event = ();
6328        }
6329
6330        let model = cx.add_model(|_| Model);
6331        let condition = model.condition(&cx, |_, _| false);
6332        cx.update(|_| drop(model));
6333        condition.await;
6334    }
6335
6336    #[crate::test(self)]
6337    async fn test_view_condition(cx: &mut TestAppContext) {
6338        struct Counter(usize);
6339
6340        impl super::Entity for Counter {
6341            type Event = ();
6342        }
6343
6344        impl super::View for Counter {
6345            fn ui_name() -> &'static str {
6346                "test view"
6347            }
6348
6349            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6350                Empty::new().boxed()
6351            }
6352        }
6353
6354        impl Counter {
6355            fn inc(&mut self, cx: &mut ViewContext<Self>) {
6356                self.0 += 1;
6357                cx.notify();
6358            }
6359        }
6360
6361        let (_, view) = cx.add_window(|_| Counter(0));
6362
6363        let condition1 = view.condition(&cx, |view, _| view.0 == 2);
6364        let condition2 = view.condition(&cx, |view, _| view.0 == 3);
6365        smol::pin!(condition1, condition2);
6366
6367        view.update(cx, |view, cx| view.inc(cx));
6368        assert_eq!(poll_once(&mut condition1).await, None);
6369        assert_eq!(poll_once(&mut condition2).await, None);
6370
6371        view.update(cx, |view, cx| view.inc(cx));
6372        assert_eq!(poll_once(&mut condition1).await, Some(()));
6373        assert_eq!(poll_once(&mut condition2).await, None);
6374
6375        view.update(cx, |view, cx| view.inc(cx));
6376        assert_eq!(poll_once(&mut condition2).await, Some(()));
6377        view.update(cx, |_, cx| cx.notify());
6378    }
6379
6380    #[crate::test(self)]
6381    #[should_panic]
6382    async fn test_view_condition_timeout(cx: &mut TestAppContext) {
6383        struct View;
6384
6385        impl super::Entity for View {
6386            type Event = ();
6387        }
6388
6389        impl super::View for View {
6390            fn ui_name() -> &'static str {
6391                "test view"
6392            }
6393
6394            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6395                Empty::new().boxed()
6396            }
6397        }
6398
6399        let (_, view) = cx.add_window(|_| View);
6400        view.condition(&cx, |_, _| false).await;
6401    }
6402
6403    #[crate::test(self)]
6404    #[should_panic(expected = "view dropped with pending condition")]
6405    async fn test_view_condition_panic_on_drop(cx: &mut TestAppContext) {
6406        struct View;
6407
6408        impl super::Entity for View {
6409            type Event = ();
6410        }
6411
6412        impl super::View for View {
6413            fn ui_name() -> &'static str {
6414                "test view"
6415            }
6416
6417            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6418                Empty::new().boxed()
6419            }
6420        }
6421
6422        let window_id = cx.add_window(|_| View).0;
6423        let view = cx.add_view(window_id, |_| View);
6424
6425        let condition = view.condition(&cx, |_, _| false);
6426        cx.update(|_| drop(view));
6427        condition.await;
6428    }
6429
6430    #[crate::test(self)]
6431    fn test_refresh_windows(cx: &mut MutableAppContext) {
6432        struct View(usize);
6433
6434        impl super::Entity for View {
6435            type Event = ();
6436        }
6437
6438        impl super::View for View {
6439            fn ui_name() -> &'static str {
6440                "test view"
6441            }
6442
6443            fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
6444                Empty::new().named(format!("render count: {}", post_inc(&mut self.0)))
6445            }
6446        }
6447
6448        let (window_id, root_view) = cx.add_window(Default::default(), |_| View(0));
6449        let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
6450
6451        assert_eq!(
6452            presenter.borrow().rendered_views[&root_view.id()].name(),
6453            Some("render count: 0")
6454        );
6455
6456        let view = cx.add_view(window_id, |cx| {
6457            cx.refresh_windows();
6458            View(0)
6459        });
6460
6461        assert_eq!(
6462            presenter.borrow().rendered_views[&root_view.id()].name(),
6463            Some("render count: 1")
6464        );
6465        assert_eq!(
6466            presenter.borrow().rendered_views[&view.id()].name(),
6467            Some("render count: 0")
6468        );
6469
6470        cx.update(|cx| cx.refresh_windows());
6471        assert_eq!(
6472            presenter.borrow().rendered_views[&root_view.id()].name(),
6473            Some("render count: 2")
6474        );
6475        assert_eq!(
6476            presenter.borrow().rendered_views[&view.id()].name(),
6477            Some("render count: 1")
6478        );
6479
6480        cx.update(|cx| {
6481            cx.refresh_windows();
6482            drop(view);
6483        });
6484        assert_eq!(
6485            presenter.borrow().rendered_views[&root_view.id()].name(),
6486            Some("render count: 3")
6487        );
6488        assert_eq!(presenter.borrow().rendered_views.len(), 1);
6489    }
6490}