app.rs

   1use std::{
   2    any::{TypeId, type_name},
   3    cell::{BorrowMutError, Ref, RefCell, RefMut},
   4    marker::PhantomData,
   5    mem,
   6    ops::{Deref, DerefMut},
   7    path::{Path, PathBuf},
   8    rc::{Rc, Weak},
   9    sync::{Arc, atomic::Ordering::SeqCst},
  10    time::{Duration, Instant},
  11};
  12
  13use anyhow::{Context as _, Result, anyhow};
  14use derive_more::{Deref, DerefMut};
  15use futures::{
  16    Future, FutureExt,
  17    channel::oneshot,
  18    future::{LocalBoxFuture, Shared},
  19};
  20use itertools::Itertools;
  21use parking_lot::RwLock;
  22use slotmap::SlotMap;
  23
  24pub use async_context::*;
  25use collections::{FxHashMap, FxHashSet, HashMap, VecDeque};
  26pub use context::*;
  27pub use entity_map::*;
  28use http_client::{HttpClient, Url};
  29use smallvec::SmallVec;
  30#[cfg(any(test, feature = "test-support"))]
  31pub use test_context::*;
  32use util::{ResultExt, debug_panic};
  33
  34#[cfg(any(feature = "inspector", debug_assertions))]
  35use crate::InspectorElementRegistry;
  36use crate::{
  37    Action, ActionBuildError, ActionRegistry, Any, AnyView, AnyWindowHandle, AppContext, Asset,
  38    AssetSource, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DispatchPhase, DisplayId,
  39    EventEmitter, FocusHandle, FocusMap, ForegroundExecutor, Global, KeyBinding, KeyContext,
  40    Keymap, Keystroke, LayoutId, Menu, MenuItem, OwnedMenu, PathPromptOptions, Pixels, Platform,
  41    PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, Point, PromptBuilder,
  42    PromptButton, PromptHandle, PromptLevel, Render, RenderImage, RenderablePromptHandle,
  43    Reservation, ScreenCaptureSource, SharedString, SubscriberSet, Subscription, SvgRenderer, Task,
  44    TextSystem, Window, WindowAppearance, WindowHandle, WindowId, WindowInvalidator,
  45    colors::{Colors, GlobalColors},
  46    current_platform, hash, init_app_menus,
  47};
  48
  49mod async_context;
  50mod context;
  51mod entity_map;
  52#[cfg(any(test, feature = "test-support"))]
  53mod test_context;
  54
  55/// The duration for which futures returned from [Context::on_app_quit] can run before the application fully quits.
  56pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(100);
  57
  58/// Temporary(?) wrapper around [`RefCell<App>`] to help us debug any double borrows.
  59/// Strongly consider removing after stabilization.
  60#[doc(hidden)]
  61pub struct AppCell {
  62    app: RefCell<App>,
  63}
  64
  65impl AppCell {
  66    #[doc(hidden)]
  67    #[track_caller]
  68    pub fn borrow(&self) -> AppRef<'_> {
  69        if option_env!("TRACK_THREAD_BORROWS").is_some() {
  70            let thread_id = std::thread::current().id();
  71            eprintln!("borrowed {thread_id:?}");
  72        }
  73        AppRef(self.app.borrow())
  74    }
  75
  76    #[doc(hidden)]
  77    #[track_caller]
  78    pub fn borrow_mut(&self) -> AppRefMut<'_> {
  79        if option_env!("TRACK_THREAD_BORROWS").is_some() {
  80            let thread_id = std::thread::current().id();
  81            eprintln!("borrowed {thread_id:?}");
  82        }
  83        AppRefMut(self.app.borrow_mut())
  84    }
  85
  86    #[doc(hidden)]
  87    #[track_caller]
  88    pub fn try_borrow_mut(&self) -> Result<AppRefMut<'_>, BorrowMutError> {
  89        if option_env!("TRACK_THREAD_BORROWS").is_some() {
  90            let thread_id = std::thread::current().id();
  91            eprintln!("borrowed {thread_id:?}");
  92        }
  93        Ok(AppRefMut(self.app.try_borrow_mut()?))
  94    }
  95}
  96
  97#[doc(hidden)]
  98#[derive(Deref, DerefMut)]
  99pub struct AppRef<'a>(Ref<'a, App>);
 100
 101impl Drop for AppRef<'_> {
 102    fn drop(&mut self) {
 103        if option_env!("TRACK_THREAD_BORROWS").is_some() {
 104            let thread_id = std::thread::current().id();
 105            eprintln!("dropped borrow from {thread_id:?}");
 106        }
 107    }
 108}
 109
 110#[doc(hidden)]
 111#[derive(Deref, DerefMut)]
 112pub struct AppRefMut<'a>(RefMut<'a, App>);
 113
 114impl Drop for AppRefMut<'_> {
 115    fn drop(&mut self) {
 116        if option_env!("TRACK_THREAD_BORROWS").is_some() {
 117            let thread_id = std::thread::current().id();
 118            eprintln!("dropped {thread_id:?}");
 119        }
 120    }
 121}
 122
 123/// A reference to a GPUI application, typically constructed in the `main` function of your app.
 124/// You won't interact with this type much outside of initial configuration and startup.
 125pub struct Application(Rc<AppCell>);
 126
 127/// Represents an application before it is fully launched. Once your app is
 128/// configured, you'll start the app with `App::run`.
 129impl Application {
 130    /// Builds an app with the given asset source.
 131    #[allow(clippy::new_without_default)]
 132    #[profiling::function]
 133    pub fn new() -> Self {
 134        #[cfg(any(test, feature = "test-support"))]
 135        log::info!("GPUI was compiled in test mode");
 136
 137        Self(App::new_app(
 138            current_platform(false),
 139            Arc::new(()),
 140            Arc::new(NullHttpClient),
 141        ))
 142    }
 143
 144    /// Build an app in headless mode. This prevents opening windows,
 145    /// but makes it possible to run an application in an context like
 146    /// SSH, where GUI applications are not allowed.
 147    pub fn headless() -> Self {
 148        Self(App::new_app(
 149            current_platform(true),
 150            Arc::new(()),
 151            Arc::new(NullHttpClient),
 152        ))
 153    }
 154
 155    /// Assign
 156    pub fn with_assets(self, asset_source: impl AssetSource) -> Self {
 157        let mut context_lock = self.0.borrow_mut();
 158        let asset_source = Arc::new(asset_source);
 159        context_lock.asset_source = asset_source.clone();
 160        context_lock.svg_renderer = SvgRenderer::new(asset_source);
 161        drop(context_lock);
 162        self
 163    }
 164
 165    /// Sets the HTTP client for the application.
 166    pub fn with_http_client(self, http_client: Arc<dyn HttpClient>) -> Self {
 167        let mut context_lock = self.0.borrow_mut();
 168        context_lock.http_client = http_client;
 169        drop(context_lock);
 170        self
 171    }
 172
 173    /// Start the application. The provided callback will be called once the
 174    /// app is fully launched.
 175    #[profiling::function]
 176    pub fn run<F>(self, on_finish_launching: F)
 177    where
 178        F: 'static + FnOnce(&mut App),
 179    {
 180        let this = self.0.clone();
 181        let platform = self.0.borrow().platform.clone();
 182        platform.run(Box::new(move || {
 183            let cx = &mut *this.borrow_mut();
 184            on_finish_launching(cx);
 185        }));
 186    }
 187
 188    /// Register a handler to be invoked when the platform instructs the application
 189    /// to open one or more URLs.
 190    pub fn on_open_urls<F>(&self, mut callback: F) -> &Self
 191    where
 192        F: 'static + FnMut(Vec<String>),
 193    {
 194        self.0.borrow().platform.on_open_urls(Box::new(callback));
 195        self
 196    }
 197
 198    /// Invokes a handler when an already-running application is launched.
 199    /// On macOS, this can occur when the application icon is double-clicked or the app is launched via the dock.
 200    pub fn on_reopen<F>(&self, mut callback: F) -> &Self
 201    where
 202        F: 'static + FnMut(&mut App),
 203    {
 204        let this = Rc::downgrade(&self.0);
 205        self.0.borrow_mut().platform.on_reopen(Box::new(move || {
 206            if let Some(app) = this.upgrade() {
 207                callback(&mut app.borrow_mut());
 208            }
 209        }));
 210        self
 211    }
 212
 213    /// Returns a handle to the [`BackgroundExecutor`] associated with this app, which can be used to spawn futures in the background.
 214    pub fn background_executor(&self) -> BackgroundExecutor {
 215        self.0.borrow().background_executor.clone()
 216    }
 217
 218    /// Returns a handle to the [`ForegroundExecutor`] associated with this app, which can be used to spawn futures in the foreground.
 219    pub fn foreground_executor(&self) -> ForegroundExecutor {
 220        self.0.borrow().foreground_executor.clone()
 221    }
 222
 223    /// Returns a reference to the [`TextSystem`] associated with this app.
 224    pub fn text_system(&self) -> Arc<TextSystem> {
 225        self.0.borrow().text_system.clone()
 226    }
 227
 228    /// Returns the file URL of the executable with the specified name in the application bundle
 229    pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
 230        self.0.borrow().path_for_auxiliary_executable(name)
 231    }
 232}
 233
 234type Handler = Box<dyn FnMut(&mut App) -> bool + 'static>;
 235type Listener = Box<dyn FnMut(&dyn Any, &mut App) -> bool + 'static>;
 236pub(crate) type KeystrokeObserver =
 237    Box<dyn FnMut(&KeystrokeEvent, &mut Window, &mut App) -> bool + 'static>;
 238type QuitHandler = Box<dyn FnOnce(&mut App) -> LocalBoxFuture<'static, ()> + 'static>;
 239type WindowClosedHandler = Box<dyn FnMut(&mut App)>;
 240type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut App) + 'static>;
 241type NewEntityListener = Box<dyn FnMut(AnyEntity, &mut Option<&mut Window>, &mut App) + 'static>;
 242
 243#[doc(hidden)]
 244#[derive(Clone, PartialEq, Eq)]
 245pub struct SystemWindowTab {
 246    pub id: WindowId,
 247    pub title: SharedString,
 248    pub handle: AnyWindowHandle,
 249    pub last_active_at: Instant,
 250}
 251
 252impl SystemWindowTab {
 253    /// Create a new instance of the window tab.
 254    pub fn new(title: SharedString, handle: AnyWindowHandle) -> Self {
 255        Self {
 256            id: handle.id,
 257            title,
 258            handle,
 259            last_active_at: Instant::now(),
 260        }
 261    }
 262}
 263
 264/// A controller for managing window tabs.
 265#[derive(Default)]
 266pub struct SystemWindowTabController {
 267    visible: Option<bool>,
 268    tab_groups: FxHashMap<usize, Vec<SystemWindowTab>>,
 269}
 270
 271impl Global for SystemWindowTabController {}
 272
 273impl SystemWindowTabController {
 274    /// Create a new instance of the window tab controller.
 275    pub fn new() -> Self {
 276        Self {
 277            visible: None,
 278            tab_groups: FxHashMap::default(),
 279        }
 280    }
 281
 282    /// Initialize the global window tab controller.
 283    pub fn init(cx: &mut App) {
 284        cx.set_global(SystemWindowTabController::new());
 285    }
 286
 287    /// Get all tab groups.
 288    pub fn tab_groups(&self) -> &FxHashMap<usize, Vec<SystemWindowTab>> {
 289        &self.tab_groups
 290    }
 291
 292    /// Get the next tab group window handle.
 293    pub fn get_next_tab_group_window(cx: &mut App, id: WindowId) -> Option<&AnyWindowHandle> {
 294        let controller = cx.global::<SystemWindowTabController>();
 295        let current_group = controller
 296            .tab_groups
 297            .iter()
 298            .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));
 299
 300        let current_group = current_group?;
 301        let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
 302        let idx = group_ids.iter().position(|g| *g == current_group)?;
 303        let next_idx = (idx + 1) % group_ids.len();
 304
 305        controller
 306            .tab_groups
 307            .get(group_ids[next_idx])
 308            .and_then(|tabs| {
 309                tabs.iter()
 310                    .max_by_key(|tab| tab.last_active_at)
 311                    .or_else(|| tabs.first())
 312                    .map(|tab| &tab.handle)
 313            })
 314    }
 315
 316    /// Get the previous tab group window handle.
 317    pub fn get_prev_tab_group_window(cx: &mut App, id: WindowId) -> Option<&AnyWindowHandle> {
 318        let controller = cx.global::<SystemWindowTabController>();
 319        let current_group = controller
 320            .tab_groups
 321            .iter()
 322            .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));
 323
 324        let current_group = current_group?;
 325        let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
 326        let idx = group_ids.iter().position(|g| *g == current_group)?;
 327        let prev_idx = if idx == 0 {
 328            group_ids.len() - 1
 329        } else {
 330            idx - 1
 331        };
 332
 333        controller
 334            .tab_groups
 335            .get(group_ids[prev_idx])
 336            .and_then(|tabs| {
 337                tabs.iter()
 338                    .max_by_key(|tab| tab.last_active_at)
 339                    .or_else(|| tabs.first())
 340                    .map(|tab| &tab.handle)
 341            })
 342    }
 343
 344    /// Get all tabs in the same window.
 345    pub fn tabs(&self, id: WindowId) -> Option<&Vec<SystemWindowTab>> {
 346        let tab_group = self
 347            .tab_groups
 348            .iter()
 349            .find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| *group));
 350
 351        if let Some(tab_group) = tab_group {
 352            self.tab_groups.get(&tab_group)
 353        } else {
 354            None
 355        }
 356    }
 357
 358    /// Initialize the visibility of the system window tab controller.
 359    pub fn init_visible(cx: &mut App, visible: bool) {
 360        let mut controller = cx.global_mut::<SystemWindowTabController>();
 361        if controller.visible.is_none() {
 362            controller.visible = Some(visible);
 363        }
 364    }
 365
 366    /// Get the visibility of the system window tab controller.
 367    pub fn is_visible(&self) -> bool {
 368        self.visible.unwrap_or(false)
 369    }
 370
 371    /// Set the visibility of the system window tab controller.
 372    pub fn set_visible(cx: &mut App, visible: bool) {
 373        let mut controller = cx.global_mut::<SystemWindowTabController>();
 374        controller.visible = Some(visible);
 375    }
 376
 377    /// Update the last active of a window.
 378    pub fn update_last_active(cx: &mut App, id: WindowId) {
 379        let mut controller = cx.global_mut::<SystemWindowTabController>();
 380        for windows in controller.tab_groups.values_mut() {
 381            for tab in windows.iter_mut() {
 382                if tab.id == id {
 383                    tab.last_active_at = Instant::now();
 384                }
 385            }
 386        }
 387    }
 388
 389    /// Update the position of a tab within its group.
 390    pub fn update_tab_position(cx: &mut App, id: WindowId, ix: usize) {
 391        let mut controller = cx.global_mut::<SystemWindowTabController>();
 392        for (_, windows) in controller.tab_groups.iter_mut() {
 393            if let Some(current_pos) = windows.iter().position(|tab| tab.id == id) {
 394                if ix < windows.len() && current_pos != ix {
 395                    let window_tab = windows.remove(current_pos);
 396                    windows.insert(ix, window_tab);
 397                }
 398                break;
 399            }
 400        }
 401    }
 402
 403    /// Update the title of a tab.
 404    pub fn update_tab_title(cx: &mut App, id: WindowId, title: SharedString) {
 405        let controller = cx.global::<SystemWindowTabController>();
 406        let tab = controller
 407            .tab_groups
 408            .values()
 409            .flat_map(|windows| windows.iter())
 410            .find(|tab| tab.id == id);
 411
 412        if tab.map_or(true, |t| t.title == title) {
 413            return;
 414        }
 415
 416        let mut controller = cx.global_mut::<SystemWindowTabController>();
 417        for windows in controller.tab_groups.values_mut() {
 418            for tab in windows.iter_mut() {
 419                if tab.id == id {
 420                    tab.title = title.clone();
 421                }
 422            }
 423        }
 424    }
 425
 426    /// Insert a tab into a tab group.
 427    pub fn add_tab(cx: &mut App, id: WindowId, tabs: Vec<SystemWindowTab>) {
 428        let mut controller = cx.global_mut::<SystemWindowTabController>();
 429        let Some(tab) = tabs.clone().into_iter().find(|tab| tab.id == id) else {
 430            return;
 431        };
 432
 433        let mut expected_tab_ids: Vec<_> = tabs
 434            .iter()
 435            .filter(|tab| tab.id != id)
 436            .map(|tab| tab.id)
 437            .sorted()
 438            .collect();
 439
 440        let mut tab_group_id = None;
 441        for (group_id, group_tabs) in &controller.tab_groups {
 442            let tab_ids: Vec<_> = group_tabs.iter().map(|tab| tab.id).sorted().collect();
 443            if tab_ids == expected_tab_ids {
 444                tab_group_id = Some(*group_id);
 445                break;
 446            }
 447        }
 448
 449        if let Some(tab_group_id) = tab_group_id {
 450            if let Some(tabs) = controller.tab_groups.get_mut(&tab_group_id) {
 451                tabs.push(tab);
 452            }
 453        } else {
 454            let new_group_id = controller.tab_groups.len();
 455            controller.tab_groups.insert(new_group_id, tabs);
 456        }
 457    }
 458
 459    /// Remove a tab from a tab group.
 460    pub fn remove_tab(cx: &mut App, id: WindowId) -> Option<SystemWindowTab> {
 461        let mut controller = cx.global_mut::<SystemWindowTabController>();
 462        let mut removed_tab = None;
 463
 464        controller.tab_groups.retain(|_, tabs| {
 465            if let Some(pos) = tabs.iter().position(|tab| tab.id == id) {
 466                removed_tab = Some(tabs.remove(pos));
 467            }
 468            !tabs.is_empty()
 469        });
 470
 471        removed_tab
 472    }
 473
 474    /// Move a tab to a new tab group.
 475    pub fn move_tab_to_new_window(cx: &mut App, id: WindowId) {
 476        let mut removed_tab = Self::remove_tab(cx, id);
 477        let mut controller = cx.global_mut::<SystemWindowTabController>();
 478
 479        if let Some(tab) = removed_tab {
 480            let new_group_id = controller.tab_groups.keys().max().map_or(0, |k| k + 1);
 481            controller.tab_groups.insert(new_group_id, vec![tab]);
 482        }
 483    }
 484
 485    /// Merge all tab groups into a single group.
 486    pub fn merge_all_windows(cx: &mut App, id: WindowId) {
 487        let mut controller = cx.global_mut::<SystemWindowTabController>();
 488        let Some(initial_tabs) = controller.tabs(id) else {
 489            return;
 490        };
 491
 492        let mut all_tabs = initial_tabs.clone();
 493        for tabs in controller.tab_groups.values() {
 494            all_tabs.extend(
 495                tabs.iter()
 496                    .filter(|tab| !initial_tabs.contains(tab))
 497                    .cloned(),
 498            );
 499        }
 500
 501        controller.tab_groups.clear();
 502        controller.tab_groups.insert(0, all_tabs);
 503    }
 504
 505    /// Selects the next tab in the tab group in the trailing direction.
 506    pub fn select_next_tab(cx: &mut App, id: WindowId) {
 507        let mut controller = cx.global_mut::<SystemWindowTabController>();
 508        let Some(tabs) = controller.tabs(id) else {
 509            return;
 510        };
 511
 512        let current_index = tabs.iter().position(|tab| tab.id == id).unwrap();
 513        let next_index = (current_index + 1) % tabs.len();
 514
 515        let _ = &tabs[next_index].handle.update(cx, |_, window, _| {
 516            window.activate_window();
 517        });
 518    }
 519
 520    /// Selects the previous tab in the tab group in the leading direction.
 521    pub fn select_previous_tab(cx: &mut App, id: WindowId) {
 522        let mut controller = cx.global_mut::<SystemWindowTabController>();
 523        let Some(tabs) = controller.tabs(id) else {
 524            return;
 525        };
 526
 527        let current_index = tabs.iter().position(|tab| tab.id == id).unwrap();
 528        let previous_index = if current_index == 0 {
 529            tabs.len() - 1
 530        } else {
 531            current_index - 1
 532        };
 533
 534        let _ = &tabs[previous_index].handle.update(cx, |_, window, _| {
 535            window.activate_window();
 536        });
 537    }
 538}
 539
 540/// Contains the state of the full application, and passed as a reference to a variety of callbacks.
 541/// Other [Context] derefs to this type.
 542/// You need a reference to an `App` to access the state of a [Entity].
 543pub struct App {
 544    pub(crate) this: Weak<AppCell>,
 545    pub(crate) platform: Rc<dyn Platform>,
 546    text_system: Arc<TextSystem>,
 547    flushing_effects: bool,
 548    pending_updates: usize,
 549    pub(crate) actions: Rc<ActionRegistry>,
 550    pub(crate) active_drag: Option<AnyDrag>,
 551    pub(crate) background_executor: BackgroundExecutor,
 552    pub(crate) foreground_executor: ForegroundExecutor,
 553    pub(crate) loading_assets: FxHashMap<(TypeId, u64), Box<dyn Any>>,
 554    asset_source: Arc<dyn AssetSource>,
 555    pub(crate) svg_renderer: SvgRenderer,
 556    http_client: Arc<dyn HttpClient>,
 557    pub(crate) globals_by_type: FxHashMap<TypeId, Box<dyn Any>>,
 558    pub(crate) entities: EntityMap,
 559    pub(crate) window_update_stack: Vec<WindowId>,
 560    pub(crate) new_entity_observers: SubscriberSet<TypeId, NewEntityListener>,
 561    pub(crate) windows: SlotMap<WindowId, Option<Window>>,
 562    pub(crate) window_handles: FxHashMap<WindowId, AnyWindowHandle>,
 563    pub(crate) focus_handles: Arc<FocusMap>,
 564    pub(crate) keymap: Rc<RefCell<Keymap>>,
 565    pub(crate) keyboard_layout: Box<dyn PlatformKeyboardLayout>,
 566    pub(crate) keyboard_mapper: Rc<dyn PlatformKeyboardMapper>,
 567    pub(crate) global_action_listeners:
 568        FxHashMap<TypeId, Vec<Rc<dyn Fn(&dyn Any, DispatchPhase, &mut Self)>>>,
 569    pending_effects: VecDeque<Effect>,
 570    pub(crate) pending_notifications: FxHashSet<EntityId>,
 571    pub(crate) pending_global_notifications: FxHashSet<TypeId>,
 572    pub(crate) observers: SubscriberSet<EntityId, Handler>,
 573    // TypeId is the type of the event that the listener callback expects
 574    pub(crate) event_listeners: SubscriberSet<EntityId, (TypeId, Listener)>,
 575    pub(crate) keystroke_observers: SubscriberSet<(), KeystrokeObserver>,
 576    pub(crate) keystroke_interceptors: SubscriberSet<(), KeystrokeObserver>,
 577    pub(crate) keyboard_layout_observers: SubscriberSet<(), Handler>,
 578    pub(crate) release_listeners: SubscriberSet<EntityId, ReleaseListener>,
 579    pub(crate) global_observers: SubscriberSet<TypeId, Handler>,
 580    pub(crate) quit_observers: SubscriberSet<(), QuitHandler>,
 581    pub(crate) restart_observers: SubscriberSet<(), Handler>,
 582    pub(crate) restart_path: Option<PathBuf>,
 583    pub(crate) window_closed_observers: SubscriberSet<(), WindowClosedHandler>,
 584    pub(crate) layout_id_buffer: Vec<LayoutId>, // We recycle this memory across layout requests.
 585    pub(crate) propagate_event: bool,
 586    pub(crate) prompt_builder: Option<PromptBuilder>,
 587    pub(crate) window_invalidators_by_entity:
 588        FxHashMap<EntityId, FxHashMap<WindowId, WindowInvalidator>>,
 589    pub(crate) tracked_entities: FxHashMap<WindowId, FxHashSet<EntityId>>,
 590    #[cfg(any(feature = "inspector", debug_assertions))]
 591    pub(crate) inspector_renderer: Option<crate::InspectorRenderer>,
 592    #[cfg(any(feature = "inspector", debug_assertions))]
 593    pub(crate) inspector_element_registry: InspectorElementRegistry,
 594    #[cfg(any(test, feature = "test-support", debug_assertions))]
 595    pub(crate) name: Option<&'static str>,
 596    quitting: bool,
 597}
 598
 599impl App {
 600    #[allow(clippy::new_ret_no_self)]
 601    pub(crate) fn new_app(
 602        platform: Rc<dyn Platform>,
 603        asset_source: Arc<dyn AssetSource>,
 604        http_client: Arc<dyn HttpClient>,
 605    ) -> Rc<AppCell> {
 606        let executor = platform.background_executor();
 607        let foreground_executor = platform.foreground_executor();
 608        assert!(
 609            executor.is_main_thread(),
 610            "must construct App on main thread"
 611        );
 612
 613        let text_system = Arc::new(TextSystem::new(platform.text_system()));
 614        let entities = EntityMap::new();
 615        let keyboard_layout = platform.keyboard_layout();
 616        let keyboard_mapper = platform.keyboard_mapper();
 617
 618        let app = Rc::new_cyclic(|this| AppCell {
 619            app: RefCell::new(App {
 620                this: this.clone(),
 621                platform: platform.clone(),
 622                text_system,
 623                actions: Rc::new(ActionRegistry::default()),
 624                flushing_effects: false,
 625                pending_updates: 0,
 626                active_drag: None,
 627                background_executor: executor,
 628                foreground_executor,
 629                svg_renderer: SvgRenderer::new(asset_source.clone()),
 630                loading_assets: Default::default(),
 631                asset_source,
 632                http_client,
 633                globals_by_type: FxHashMap::default(),
 634                entities,
 635                new_entity_observers: SubscriberSet::new(),
 636                windows: SlotMap::with_key(),
 637                window_update_stack: Vec::new(),
 638                window_handles: FxHashMap::default(),
 639                focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
 640                keymap: Rc::new(RefCell::new(Keymap::default())),
 641                keyboard_layout,
 642                keyboard_mapper,
 643                global_action_listeners: FxHashMap::default(),
 644                pending_effects: VecDeque::new(),
 645                pending_notifications: FxHashSet::default(),
 646                pending_global_notifications: FxHashSet::default(),
 647                observers: SubscriberSet::new(),
 648                tracked_entities: FxHashMap::default(),
 649                window_invalidators_by_entity: FxHashMap::default(),
 650                event_listeners: SubscriberSet::new(),
 651                release_listeners: SubscriberSet::new(),
 652                keystroke_observers: SubscriberSet::new(),
 653                keystroke_interceptors: SubscriberSet::new(),
 654                keyboard_layout_observers: SubscriberSet::new(),
 655                global_observers: SubscriberSet::new(),
 656                quit_observers: SubscriberSet::new(),
 657                restart_observers: SubscriberSet::new(),
 658                restart_path: None,
 659                window_closed_observers: SubscriberSet::new(),
 660                layout_id_buffer: Default::default(),
 661                propagate_event: true,
 662                prompt_builder: Some(PromptBuilder::Default),
 663                #[cfg(any(feature = "inspector", debug_assertions))]
 664                inspector_renderer: None,
 665                #[cfg(any(feature = "inspector", debug_assertions))]
 666                inspector_element_registry: InspectorElementRegistry::default(),
 667                quitting: false,
 668
 669                #[cfg(any(test, feature = "test-support", debug_assertions))]
 670                name: None,
 671            }),
 672        });
 673
 674        init_app_menus(platform.as_ref(), &app.borrow());
 675        SystemWindowTabController::init(&mut app.borrow_mut());
 676
 677        platform.on_keyboard_layout_change(Box::new({
 678            let app = Rc::downgrade(&app);
 679            move || {
 680                if let Some(app) = app.upgrade() {
 681                    let cx = &mut app.borrow_mut();
 682                    cx.keyboard_layout = cx.platform.keyboard_layout();
 683                    cx.keyboard_mapper = cx.platform.keyboard_mapper();
 684                    cx.keyboard_layout_observers
 685                        .clone()
 686                        .retain(&(), move |callback| (callback)(cx));
 687                }
 688            }
 689        }));
 690
 691        platform.on_quit(Box::new({
 692            let cx = app.clone();
 693            move || {
 694                cx.borrow_mut().shutdown();
 695            }
 696        }));
 697
 698        app
 699    }
 700
 701    /// Quit the application gracefully. Handlers registered with [`Context::on_app_quit`]
 702    /// will be given 100ms to complete before exiting.
 703    pub fn shutdown(&mut self) {
 704        let mut futures = Vec::new();
 705
 706        for observer in self.quit_observers.remove(&()) {
 707            futures.push(observer(self));
 708        }
 709
 710        self.windows.clear();
 711        self.window_handles.clear();
 712        self.flush_effects();
 713        self.quitting = true;
 714
 715        let futures = futures::future::join_all(futures);
 716        if self
 717            .background_executor
 718            .block_with_timeout(SHUTDOWN_TIMEOUT, futures)
 719            .is_err()
 720        {
 721            log::error!("timed out waiting on app_will_quit");
 722        }
 723
 724        self.quitting = false;
 725    }
 726
 727    /// Get the id of the current keyboard layout
 728    pub fn keyboard_layout(&self) -> &dyn PlatformKeyboardLayout {
 729        self.keyboard_layout.as_ref()
 730    }
 731
 732    /// Get the current keyboard mapper.
 733    pub fn keyboard_mapper(&self) -> &Rc<dyn PlatformKeyboardMapper> {
 734        &self.keyboard_mapper
 735    }
 736
 737    /// Invokes a handler when the current keyboard layout changes
 738    pub fn on_keyboard_layout_change<F>(&self, mut callback: F) -> Subscription
 739    where
 740        F: 'static + FnMut(&mut App),
 741    {
 742        let (subscription, activate) = self.keyboard_layout_observers.insert(
 743            (),
 744            Box::new(move |cx| {
 745                callback(cx);
 746                true
 747            }),
 748        );
 749        activate();
 750        subscription
 751    }
 752
 753    /// Gracefully quit the application via the platform's standard routine.
 754    pub fn quit(&self) {
 755        self.platform.quit();
 756    }
 757
 758    /// Schedules all windows in the application to be redrawn. This can be called
 759    /// multiple times in an update cycle and still result in a single redraw.
 760    pub fn refresh_windows(&mut self) {
 761        self.pending_effects.push_back(Effect::RefreshWindows);
 762    }
 763
 764    pub(crate) fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
 765        self.start_update();
 766        let result = update(self);
 767        self.finish_update();
 768        result
 769    }
 770
 771    pub(crate) fn start_update(&mut self) {
 772        self.pending_updates += 1;
 773    }
 774
 775    pub(crate) fn finish_update(&mut self) {
 776        if !self.flushing_effects && self.pending_updates == 1 {
 777            self.flushing_effects = true;
 778            self.flush_effects();
 779            self.flushing_effects = false;
 780        }
 781        self.pending_updates -= 1;
 782    }
 783
 784    /// Arrange a callback to be invoked when the given entity calls `notify` on its respective context.
 785    pub fn observe<W>(
 786        &mut self,
 787        entity: &Entity<W>,
 788        mut on_notify: impl FnMut(Entity<W>, &mut App) + 'static,
 789    ) -> Subscription
 790    where
 791        W: 'static,
 792    {
 793        self.observe_internal(entity, move |e, cx| {
 794            on_notify(e, cx);
 795            true
 796        })
 797    }
 798
 799    pub(crate) fn detect_accessed_entities<R>(
 800        &mut self,
 801        callback: impl FnOnce(&mut App) -> R,
 802    ) -> (R, FxHashSet<EntityId>) {
 803        let accessed_entities_start = self.entities.accessed_entities.borrow().clone();
 804        let result = callback(self);
 805        let accessed_entities_end = self.entities.accessed_entities.borrow().clone();
 806        let entities_accessed_in_callback = accessed_entities_end
 807            .difference(&accessed_entities_start)
 808            .copied()
 809            .collect::<FxHashSet<EntityId>>();
 810        (result, entities_accessed_in_callback)
 811    }
 812
 813    pub(crate) fn record_entities_accessed(
 814        &mut self,
 815        window_handle: AnyWindowHandle,
 816        invalidator: WindowInvalidator,
 817        entities: &FxHashSet<EntityId>,
 818    ) {
 819        let mut tracked_entities =
 820            std::mem::take(self.tracked_entities.entry(window_handle.id).or_default());
 821        for entity in tracked_entities.iter() {
 822            self.window_invalidators_by_entity
 823                .entry(*entity)
 824                .and_modify(|windows| {
 825                    windows.remove(&window_handle.id);
 826                });
 827        }
 828        for entity in entities.iter() {
 829            self.window_invalidators_by_entity
 830                .entry(*entity)
 831                .or_default()
 832                .insert(window_handle.id, invalidator.clone());
 833        }
 834        tracked_entities.clear();
 835        tracked_entities.extend(entities.iter().copied());
 836        self.tracked_entities
 837            .insert(window_handle.id, tracked_entities);
 838    }
 839
 840    pub(crate) fn new_observer(&mut self, key: EntityId, value: Handler) -> Subscription {
 841        let (subscription, activate) = self.observers.insert(key, value);
 842        self.defer(move |_| activate());
 843        subscription
 844    }
 845
 846    pub(crate) fn observe_internal<W>(
 847        &mut self,
 848        entity: &Entity<W>,
 849        mut on_notify: impl FnMut(Entity<W>, &mut App) -> bool + 'static,
 850    ) -> Subscription
 851    where
 852        W: 'static,
 853    {
 854        let entity_id = entity.entity_id();
 855        let handle = entity.downgrade();
 856        self.new_observer(
 857            entity_id,
 858            Box::new(move |cx| {
 859                if let Some(entity) = handle.upgrade() {
 860                    on_notify(entity, cx)
 861                } else {
 862                    false
 863                }
 864            }),
 865        )
 866    }
 867
 868    /// Arrange for the given callback to be invoked whenever the given entity emits an event of a given type.
 869    /// The callback is provided a handle to the emitting entity and a reference to the emitted event.
 870    pub fn subscribe<T, Event>(
 871        &mut self,
 872        entity: &Entity<T>,
 873        mut on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
 874    ) -> Subscription
 875    where
 876        T: 'static + EventEmitter<Event>,
 877        Event: 'static,
 878    {
 879        self.subscribe_internal(entity, move |entity, event, cx| {
 880            on_event(entity, event, cx);
 881            true
 882        })
 883    }
 884
 885    pub(crate) fn new_subscription(
 886        &mut self,
 887        key: EntityId,
 888        value: (TypeId, Listener),
 889    ) -> Subscription {
 890        let (subscription, activate) = self.event_listeners.insert(key, value);
 891        self.defer(move |_| activate());
 892        subscription
 893    }
 894    pub(crate) fn subscribe_internal<T, Evt>(
 895        &mut self,
 896        entity: &Entity<T>,
 897        mut on_event: impl FnMut(Entity<T>, &Evt, &mut App) -> bool + 'static,
 898    ) -> Subscription
 899    where
 900        T: 'static + EventEmitter<Evt>,
 901        Evt: 'static,
 902    {
 903        let entity_id = entity.entity_id();
 904        let handle = entity.downgrade();
 905        self.new_subscription(
 906            entity_id,
 907            (
 908                TypeId::of::<Evt>(),
 909                Box::new(move |event, cx| {
 910                    let event: &Evt = event.downcast_ref().expect("invalid event type");
 911                    if let Some(entity) = handle.upgrade() {
 912                        on_event(entity, event, cx)
 913                    } else {
 914                        false
 915                    }
 916                }),
 917            ),
 918        )
 919    }
 920
 921    /// Returns handles to all open windows in the application.
 922    /// Each handle could be downcast to a handle typed for the root view of that window.
 923    /// To find all windows of a given type, you could filter on
 924    pub fn windows(&self) -> Vec<AnyWindowHandle> {
 925        self.windows
 926            .keys()
 927            .flat_map(|window_id| self.window_handles.get(&window_id).copied())
 928            .collect()
 929    }
 930
 931    /// Returns the window handles ordered by their appearance on screen, front to back.
 932    ///
 933    /// The first window in the returned list is the active/topmost window of the application.
 934    ///
 935    /// This method returns None if the platform doesn't implement the method yet.
 936    pub fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> {
 937        self.platform.window_stack()
 938    }
 939
 940    /// Returns a handle to the window that is currently focused at the platform level, if one exists.
 941    pub fn active_window(&self) -> Option<AnyWindowHandle> {
 942        self.platform.active_window()
 943    }
 944
 945    /// Opens a new window with the given option and the root view returned by the given function.
 946    /// The function is invoked with a `Window`, which can be used to interact with window-specific
 947    /// functionality.
 948    pub fn open_window<V: 'static + Render>(
 949        &mut self,
 950        options: crate::WindowOptions,
 951        build_root_view: impl FnOnce(&mut Window, &mut App) -> Entity<V>,
 952    ) -> anyhow::Result<WindowHandle<V>> {
 953        self.update(|cx| {
 954            let id = cx.windows.insert(None);
 955            let handle = WindowHandle::new(id);
 956            match Window::new(handle.into(), options, cx) {
 957                Ok(mut window) => {
 958                    cx.window_update_stack.push(id);
 959                    let root_view = build_root_view(&mut window, cx);
 960                    cx.window_update_stack.pop();
 961                    window.root.replace(root_view.into());
 962                    window.defer(cx, |window: &mut Window, cx| window.appearance_changed(cx));
 963
 964                    // allow a window to draw at least once before returning
 965                    // this didn't cause any issues on non windows platforms as it seems we always won the race to on_request_frame
 966                    // on windows we quite frequently lose the race and return a window that has never rendered, which leads to a crash
 967                    // where DispatchTree::root_node_id asserts on empty nodes
 968                    let clear = window.draw(cx);
 969                    clear.clear();
 970
 971                    cx.window_handles.insert(id, window.handle);
 972                    cx.windows.get_mut(id).unwrap().replace(window);
 973                    Ok(handle)
 974                }
 975                Err(e) => {
 976                    cx.windows.remove(id);
 977                    Err(e)
 978                }
 979            }
 980        })
 981    }
 982
 983    /// Instructs the platform to activate the application by bringing it to the foreground.
 984    pub fn activate(&self, ignoring_other_apps: bool) {
 985        self.platform.activate(ignoring_other_apps);
 986    }
 987
 988    /// Hide the application at the platform level.
 989    pub fn hide(&self) {
 990        self.platform.hide();
 991    }
 992
 993    /// Hide other applications at the platform level.
 994    pub fn hide_other_apps(&self) {
 995        self.platform.hide_other_apps();
 996    }
 997
 998    /// Unhide other applications at the platform level.
 999    pub fn unhide_other_apps(&self) {
1000        self.platform.unhide_other_apps();
1001    }
1002
1003    /// Returns the list of currently active displays.
1004    pub fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
1005        self.platform.displays()
1006    }
1007
1008    /// Returns the primary display that will be used for new windows.
1009    pub fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>> {
1010        self.platform.primary_display()
1011    }
1012
1013    /// Returns whether `screen_capture_sources` may work.
1014    pub fn is_screen_capture_supported(&self) -> bool {
1015        self.platform.is_screen_capture_supported()
1016    }
1017
1018    /// Returns a list of available screen capture sources.
1019    pub fn screen_capture_sources(
1020        &self,
1021    ) -> oneshot::Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> {
1022        self.platform.screen_capture_sources()
1023    }
1024
1025    /// Returns the display with the given ID, if one exists.
1026    pub fn find_display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
1027        self.displays()
1028            .iter()
1029            .find(|display| display.id() == id)
1030            .cloned()
1031    }
1032
1033    /// Returns the appearance of the application's windows.
1034    pub fn window_appearance(&self) -> WindowAppearance {
1035        self.platform.window_appearance()
1036    }
1037
1038    /// Writes data to the primary selection buffer.
1039    /// Only available on Linux.
1040    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
1041    pub fn write_to_primary(&self, item: ClipboardItem) {
1042        self.platform.write_to_primary(item)
1043    }
1044
1045    /// Writes data to the platform clipboard.
1046    pub fn write_to_clipboard(&self, item: ClipboardItem) {
1047        self.platform.write_to_clipboard(item)
1048    }
1049
1050    /// Reads data from the primary selection buffer.
1051    /// Only available on Linux.
1052    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
1053    pub fn read_from_primary(&self) -> Option<ClipboardItem> {
1054        self.platform.read_from_primary()
1055    }
1056
1057    /// Reads data from the platform clipboard.
1058    pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
1059        self.platform.read_from_clipboard()
1060    }
1061
1062    /// Writes credentials to the platform keychain.
1063    pub fn write_credentials(
1064        &self,
1065        url: &str,
1066        username: &str,
1067        password: &[u8],
1068    ) -> Task<Result<()>> {
1069        self.platform.write_credentials(url, username, password)
1070    }
1071
1072    /// Reads credentials from the platform keychain.
1073    pub fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> {
1074        self.platform.read_credentials(url)
1075    }
1076
1077    /// Deletes credentials from the platform keychain.
1078    pub fn delete_credentials(&self, url: &str) -> Task<Result<()>> {
1079        self.platform.delete_credentials(url)
1080    }
1081
1082    /// Directs the platform's default browser to open the given URL.
1083    pub fn open_url(&self, url: &str) {
1084        self.platform.open_url(url);
1085    }
1086
1087    /// Registers the given URL scheme (e.g. `zed` for `zed://` urls) to be
1088    /// opened by the current app.
1089    ///
1090    /// On some platforms (e.g. macOS) you may be able to register URL schemes
1091    /// as part of app distribution, but this method exists to let you register
1092    /// schemes at runtime.
1093    pub fn register_url_scheme(&self, scheme: &str) -> Task<Result<()>> {
1094        self.platform.register_url_scheme(scheme)
1095    }
1096
1097    /// Returns the full pathname of the current app bundle.
1098    ///
1099    /// Returns an error if the app is not being run from a bundle.
1100    pub fn app_path(&self) -> Result<PathBuf> {
1101        self.platform.app_path()
1102    }
1103
1104    /// On Linux, returns the name of the compositor in use.
1105    ///
1106    /// Returns an empty string on other platforms.
1107    pub fn compositor_name(&self) -> &'static str {
1108        self.platform.compositor_name()
1109    }
1110
1111    /// Returns the file URL of the executable with the specified name in the application bundle
1112    pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
1113        self.platform.path_for_auxiliary_executable(name)
1114    }
1115
1116    /// Displays a platform modal for selecting paths.
1117    ///
1118    /// When one or more paths are selected, they'll be relayed asynchronously via the returned oneshot channel.
1119    /// If cancelled, a `None` will be relayed instead.
1120    /// May return an error on Linux if the file picker couldn't be opened.
1121    pub fn prompt_for_paths(
1122        &self,
1123        options: PathPromptOptions,
1124    ) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>> {
1125        self.platform.prompt_for_paths(options)
1126    }
1127
1128    /// Displays a platform modal for selecting a new path where a file can be saved.
1129    ///
1130    /// The provided directory will be used to set the initial location.
1131    /// When a path is selected, it is relayed asynchronously via the returned oneshot channel.
1132    /// If cancelled, a `None` will be relayed instead.
1133    /// May return an error on Linux if the file picker couldn't be opened.
1134    pub fn prompt_for_new_path(
1135        &self,
1136        directory: &Path,
1137        suggested_name: Option<&str>,
1138    ) -> oneshot::Receiver<Result<Option<PathBuf>>> {
1139        self.platform.prompt_for_new_path(directory, suggested_name)
1140    }
1141
1142    /// Reveals the specified path at the platform level, such as in Finder on macOS.
1143    pub fn reveal_path(&self, path: &Path) {
1144        self.platform.reveal_path(path)
1145    }
1146
1147    /// Opens the specified path with the system's default application.
1148    pub fn open_with_system(&self, path: &Path) {
1149        self.platform.open_with_system(path)
1150    }
1151
1152    /// Returns whether the user has configured scrollbars to auto-hide at the platform level.
1153    pub fn should_auto_hide_scrollbars(&self) -> bool {
1154        self.platform.should_auto_hide_scrollbars()
1155    }
1156
1157    /// Restarts the application.
1158    pub fn restart(&mut self) {
1159        self.restart_observers
1160            .clone()
1161            .retain(&(), |observer| observer(self));
1162        self.platform.restart(self.restart_path.take())
1163    }
1164
1165    /// Sets the path to use when restarting the application.
1166    pub fn set_restart_path(&mut self, path: PathBuf) {
1167        self.restart_path = Some(path);
1168    }
1169
1170    /// Returns the HTTP client for the application.
1171    pub fn http_client(&self) -> Arc<dyn HttpClient> {
1172        self.http_client.clone()
1173    }
1174
1175    /// Sets the HTTP client for the application.
1176    pub fn set_http_client(&mut self, new_client: Arc<dyn HttpClient>) {
1177        self.http_client = new_client;
1178    }
1179
1180    /// Returns the SVG renderer used by the application.
1181    pub fn svg_renderer(&self) -> SvgRenderer {
1182        self.svg_renderer.clone()
1183    }
1184
1185    pub(crate) fn push_effect(&mut self, effect: Effect) {
1186        match &effect {
1187            Effect::Notify { emitter } => {
1188                if !self.pending_notifications.insert(*emitter) {
1189                    return;
1190                }
1191            }
1192            Effect::NotifyGlobalObservers { global_type } => {
1193                if !self.pending_global_notifications.insert(*global_type) {
1194                    return;
1195                }
1196            }
1197            _ => {}
1198        };
1199
1200        self.pending_effects.push_back(effect);
1201    }
1202
1203    /// Called at the end of [`App::update`] to complete any side effects
1204    /// such as notifying observers, emitting events, etc. Effects can themselves
1205    /// cause effects, so we continue looping until all effects are processed.
1206    fn flush_effects(&mut self) {
1207        loop {
1208            self.release_dropped_entities();
1209            self.release_dropped_focus_handles();
1210            if let Some(effect) = self.pending_effects.pop_front() {
1211                match effect {
1212                    Effect::Notify { emitter } => {
1213                        self.apply_notify_effect(emitter);
1214                    }
1215
1216                    Effect::Emit {
1217                        emitter,
1218                        event_type,
1219                        event,
1220                    } => self.apply_emit_effect(emitter, event_type, event),
1221
1222                    Effect::RefreshWindows => {
1223                        self.apply_refresh_effect();
1224                    }
1225
1226                    Effect::NotifyGlobalObservers { global_type } => {
1227                        self.apply_notify_global_observers_effect(global_type);
1228                    }
1229
1230                    Effect::Defer { callback } => {
1231                        self.apply_defer_effect(callback);
1232                    }
1233                    Effect::EntityCreated {
1234                        entity,
1235                        tid,
1236                        window,
1237                    } => {
1238                        self.apply_entity_created_effect(entity, tid, window);
1239                    }
1240                }
1241            } else {
1242                #[cfg(any(test, feature = "test-support"))]
1243                for window in self
1244                    .windows
1245                    .values()
1246                    .filter_map(|window| {
1247                        let window = window.as_ref()?;
1248                        window.invalidator.is_dirty().then_some(window.handle)
1249                    })
1250                    .collect::<Vec<_>>()
1251                {
1252                    self.update_window(window, |_, window, cx| window.draw(cx).clear())
1253                        .unwrap();
1254                }
1255
1256                if self.pending_effects.is_empty() {
1257                    break;
1258                }
1259            }
1260        }
1261    }
1262
1263    /// Repeatedly called during `flush_effects` to release any entities whose
1264    /// reference count has become zero. We invoke any release observers before dropping
1265    /// each entity.
1266    fn release_dropped_entities(&mut self) {
1267        loop {
1268            let dropped = self.entities.take_dropped();
1269            if dropped.is_empty() {
1270                break;
1271            }
1272
1273            for (entity_id, mut entity) in dropped {
1274                self.observers.remove(&entity_id);
1275                self.event_listeners.remove(&entity_id);
1276                for release_callback in self.release_listeners.remove(&entity_id) {
1277                    release_callback(entity.as_mut(), self);
1278                }
1279            }
1280        }
1281    }
1282
1283    /// Repeatedly called during `flush_effects` to handle a focused handle being dropped.
1284    fn release_dropped_focus_handles(&mut self) {
1285        self.focus_handles
1286            .clone()
1287            .write()
1288            .retain(|handle_id, focus| {
1289                if focus.ref_count.load(SeqCst) == 0 {
1290                    for window_handle in self.windows() {
1291                        window_handle
1292                            .update(self, |_, window, _| {
1293                                if window.focus == Some(handle_id) {
1294                                    window.blur();
1295                                }
1296                            })
1297                            .unwrap();
1298                    }
1299                    false
1300                } else {
1301                    true
1302                }
1303            });
1304    }
1305
1306    fn apply_notify_effect(&mut self, emitter: EntityId) {
1307        self.pending_notifications.remove(&emitter);
1308
1309        self.observers
1310            .clone()
1311            .retain(&emitter, |handler| handler(self));
1312    }
1313
1314    fn apply_emit_effect(&mut self, emitter: EntityId, event_type: TypeId, event: Box<dyn Any>) {
1315        self.event_listeners
1316            .clone()
1317            .retain(&emitter, |(stored_type, handler)| {
1318                if *stored_type == event_type {
1319                    handler(event.as_ref(), self)
1320                } else {
1321                    true
1322                }
1323            });
1324    }
1325
1326    fn apply_refresh_effect(&mut self) {
1327        for window in self.windows.values_mut() {
1328            if let Some(window) = window.as_mut() {
1329                window.refreshing = true;
1330                window.invalidator.set_dirty(true);
1331            }
1332        }
1333    }
1334
1335    fn apply_notify_global_observers_effect(&mut self, type_id: TypeId) {
1336        self.pending_global_notifications.remove(&type_id);
1337        self.global_observers
1338            .clone()
1339            .retain(&type_id, |observer| observer(self));
1340    }
1341
1342    fn apply_defer_effect(&mut self, callback: Box<dyn FnOnce(&mut Self) + 'static>) {
1343        callback(self);
1344    }
1345
1346    fn apply_entity_created_effect(
1347        &mut self,
1348        entity: AnyEntity,
1349        tid: TypeId,
1350        window: Option<WindowId>,
1351    ) {
1352        self.new_entity_observers.clone().retain(&tid, |observer| {
1353            if let Some(id) = window {
1354                self.update_window_id(id, {
1355                    let entity = entity.clone();
1356                    |_, window, cx| (observer)(entity, &mut Some(window), cx)
1357                })
1358                .expect("All windows should be off the stack when flushing effects");
1359            } else {
1360                (observer)(entity.clone(), &mut None, self)
1361            }
1362            true
1363        });
1364    }
1365
1366    fn update_window_id<T, F>(&mut self, id: WindowId, update: F) -> Result<T>
1367    where
1368        F: FnOnce(AnyView, &mut Window, &mut App) -> T,
1369    {
1370        self.update(|cx| {
1371            let mut window = cx.windows.get_mut(id)?.take()?;
1372
1373            let root_view = window.root.clone().unwrap();
1374
1375            cx.window_update_stack.push(window.handle.id);
1376            let result = update(root_view, &mut window, cx);
1377            cx.window_update_stack.pop();
1378
1379            if window.removed {
1380                cx.window_handles.remove(&id);
1381                cx.windows.remove(id);
1382
1383                cx.window_closed_observers.clone().retain(&(), |callback| {
1384                    callback(cx);
1385                    true
1386                });
1387            } else {
1388                cx.windows.get_mut(id)?.replace(window);
1389            }
1390
1391            Some(result)
1392        })
1393        .context("window not found")
1394    }
1395
1396    /// Creates an `AsyncApp`, which can be cloned and has a static lifetime
1397    /// so it can be held across `await` points.
1398    pub fn to_async(&self) -> AsyncApp {
1399        AsyncApp {
1400            app: self.this.clone(),
1401            background_executor: self.background_executor.clone(),
1402            foreground_executor: self.foreground_executor.clone(),
1403        }
1404    }
1405
1406    /// Obtains a reference to the executor, which can be used to spawn futures.
1407    pub fn background_executor(&self) -> &BackgroundExecutor {
1408        &self.background_executor
1409    }
1410
1411    /// Obtains a reference to the executor, which can be used to spawn futures.
1412    pub fn foreground_executor(&self) -> &ForegroundExecutor {
1413        if self.quitting {
1414            panic!("Can't spawn on main thread after on_app_quit")
1415        };
1416        &self.foreground_executor
1417    }
1418
1419    /// Spawns the future returned by the given function on the main thread. The closure will be invoked
1420    /// with [AsyncApp], which allows the application state to be accessed across await points.
1421    #[track_caller]
1422    pub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R>
1423    where
1424        AsyncFn: AsyncFnOnce(&mut AsyncApp) -> R + 'static,
1425        R: 'static,
1426    {
1427        if self.quitting {
1428            debug_panic!("Can't spawn on main thread after on_app_quit")
1429        };
1430
1431        let mut cx = self.to_async();
1432
1433        self.foreground_executor
1434            .spawn(async move { f(&mut cx).await })
1435    }
1436
1437    /// Schedules the given function to be run at the end of the current effect cycle, allowing entities
1438    /// that are currently on the stack to be returned to the app.
1439    pub fn defer(&mut self, f: impl FnOnce(&mut App) + 'static) {
1440        self.push_effect(Effect::Defer {
1441            callback: Box::new(f),
1442        });
1443    }
1444
1445    /// Accessor for the application's asset source, which is provided when constructing the `App`.
1446    pub fn asset_source(&self) -> &Arc<dyn AssetSource> {
1447        &self.asset_source
1448    }
1449
1450    /// Accessor for the text system.
1451    pub fn text_system(&self) -> &Arc<TextSystem> {
1452        &self.text_system
1453    }
1454
1455    /// Check whether a global of the given type has been assigned.
1456    pub fn has_global<G: Global>(&self) -> bool {
1457        self.globals_by_type.contains_key(&TypeId::of::<G>())
1458    }
1459
1460    /// Access the global of the given type. Panics if a global for that type has not been assigned.
1461    #[track_caller]
1462    pub fn global<G: Global>(&self) -> &G {
1463        self.globals_by_type
1464            .get(&TypeId::of::<G>())
1465            .map(|any_state| any_state.downcast_ref::<G>().unwrap())
1466            .with_context(|| format!("no state of type {} exists", type_name::<G>()))
1467            .unwrap()
1468    }
1469
1470    /// Access the global of the given type if a value has been assigned.
1471    pub fn try_global<G: Global>(&self) -> Option<&G> {
1472        self.globals_by_type
1473            .get(&TypeId::of::<G>())
1474            .map(|any_state| any_state.downcast_ref::<G>().unwrap())
1475    }
1476
1477    /// Access the global of the given type mutably. Panics if a global for that type has not been assigned.
1478    #[track_caller]
1479    pub fn global_mut<G: Global>(&mut self) -> &mut G {
1480        let global_type = TypeId::of::<G>();
1481        self.push_effect(Effect::NotifyGlobalObservers { global_type });
1482        self.globals_by_type
1483            .get_mut(&global_type)
1484            .and_then(|any_state| any_state.downcast_mut::<G>())
1485            .with_context(|| format!("no state of type {} exists", type_name::<G>()))
1486            .unwrap()
1487    }
1488
1489    /// Access the global of the given type mutably. A default value is assigned if a global of this type has not
1490    /// yet been assigned.
1491    pub fn default_global<G: Global + Default>(&mut self) -> &mut G {
1492        let global_type = TypeId::of::<G>();
1493        self.push_effect(Effect::NotifyGlobalObservers { global_type });
1494        self.globals_by_type
1495            .entry(global_type)
1496            .or_insert_with(|| Box::<G>::default())
1497            .downcast_mut::<G>()
1498            .unwrap()
1499    }
1500
1501    /// Sets the value of the global of the given type.
1502    pub fn set_global<G: Global>(&mut self, global: G) {
1503        let global_type = TypeId::of::<G>();
1504        self.push_effect(Effect::NotifyGlobalObservers { global_type });
1505        self.globals_by_type.insert(global_type, Box::new(global));
1506    }
1507
1508    /// Clear all stored globals. Does not notify global observers.
1509    #[cfg(any(test, feature = "test-support"))]
1510    pub fn clear_globals(&mut self) {
1511        self.globals_by_type.drain();
1512    }
1513
1514    /// Remove the global of the given type from the app context. Does not notify global observers.
1515    pub fn remove_global<G: Global>(&mut self) -> G {
1516        let global_type = TypeId::of::<G>();
1517        self.push_effect(Effect::NotifyGlobalObservers { global_type });
1518        *self
1519            .globals_by_type
1520            .remove(&global_type)
1521            .unwrap_or_else(|| panic!("no global added for {}", std::any::type_name::<G>()))
1522            .downcast()
1523            .unwrap()
1524    }
1525
1526    /// Register a callback to be invoked when a global of the given type is updated.
1527    pub fn observe_global<G: Global>(
1528        &mut self,
1529        mut f: impl FnMut(&mut Self) + 'static,
1530    ) -> Subscription {
1531        let (subscription, activate) = self.global_observers.insert(
1532            TypeId::of::<G>(),
1533            Box::new(move |cx| {
1534                f(cx);
1535                true
1536            }),
1537        );
1538        self.defer(move |_| activate());
1539        subscription
1540    }
1541
1542    /// Move the global of the given type to the stack.
1543    #[track_caller]
1544    pub(crate) fn lease_global<G: Global>(&mut self) -> GlobalLease<G> {
1545        GlobalLease::new(
1546            self.globals_by_type
1547                .remove(&TypeId::of::<G>())
1548                .with_context(|| format!("no global registered of type {}", type_name::<G>()))
1549                .unwrap(),
1550        )
1551    }
1552
1553    /// Restore the global of the given type after it is moved to the stack.
1554    pub(crate) fn end_global_lease<G: Global>(&mut self, lease: GlobalLease<G>) {
1555        let global_type = TypeId::of::<G>();
1556
1557        self.push_effect(Effect::NotifyGlobalObservers { global_type });
1558        self.globals_by_type.insert(global_type, lease.global);
1559    }
1560
1561    pub(crate) fn new_entity_observer(
1562        &self,
1563        key: TypeId,
1564        value: NewEntityListener,
1565    ) -> Subscription {
1566        let (subscription, activate) = self.new_entity_observers.insert(key, value);
1567        activate();
1568        subscription
1569    }
1570
1571    /// Arrange for the given function to be invoked whenever a view of the specified type is created.
1572    /// The function will be passed a mutable reference to the view along with an appropriate context.
1573    pub fn observe_new<T: 'static>(
1574        &self,
1575        on_new: impl 'static + Fn(&mut T, Option<&mut Window>, &mut Context<T>),
1576    ) -> Subscription {
1577        self.new_entity_observer(
1578            TypeId::of::<T>(),
1579            Box::new(
1580                move |any_entity: AnyEntity, window: &mut Option<&mut Window>, cx: &mut App| {
1581                    any_entity
1582                        .downcast::<T>()
1583                        .unwrap()
1584                        .update(cx, |entity_state, cx| {
1585                            on_new(entity_state, window.as_deref_mut(), cx)
1586                        })
1587                },
1588            ),
1589        )
1590    }
1591
1592    /// Observe the release of a entity. The callback is invoked after the entity
1593    /// has no more strong references but before it has been dropped.
1594    pub fn observe_release<T>(
1595        &self,
1596        handle: &Entity<T>,
1597        on_release: impl FnOnce(&mut T, &mut App) + 'static,
1598    ) -> Subscription
1599    where
1600        T: 'static,
1601    {
1602        let (subscription, activate) = self.release_listeners.insert(
1603            handle.entity_id(),
1604            Box::new(move |entity, cx| {
1605                let entity = entity.downcast_mut().expect("invalid entity type");
1606                on_release(entity, cx)
1607            }),
1608        );
1609        activate();
1610        subscription
1611    }
1612
1613    /// Observe the release of a entity. The callback is invoked after the entity
1614    /// has no more strong references but before it has been dropped.
1615    pub fn observe_release_in<T>(
1616        &self,
1617        handle: &Entity<T>,
1618        window: &Window,
1619        on_release: impl FnOnce(&mut T, &mut Window, &mut App) + 'static,
1620    ) -> Subscription
1621    where
1622        T: 'static,
1623    {
1624        let window_handle = window.handle;
1625        self.observe_release(handle, move |entity, cx| {
1626            let _ = window_handle.update(cx, |_, window, cx| on_release(entity, window, cx));
1627        })
1628    }
1629
1630    /// Register a callback to be invoked when a keystroke is received by the application
1631    /// in any window. Note that this fires after all other action and event mechanisms have resolved
1632    /// and that this API will not be invoked if the event's propagation is stopped.
1633    pub fn observe_keystrokes(
1634        &mut self,
1635        mut f: impl FnMut(&KeystrokeEvent, &mut Window, &mut App) + 'static,
1636    ) -> Subscription {
1637        fn inner(
1638            keystroke_observers: &SubscriberSet<(), KeystrokeObserver>,
1639            handler: KeystrokeObserver,
1640        ) -> Subscription {
1641            let (subscription, activate) = keystroke_observers.insert((), handler);
1642            activate();
1643            subscription
1644        }
1645
1646        inner(
1647            &self.keystroke_observers,
1648            Box::new(move |event, window, cx| {
1649                f(event, window, cx);
1650                true
1651            }),
1652        )
1653    }
1654
1655    /// Register a callback to be invoked when a keystroke is received by the application
1656    /// in any window. Note that this fires _before_ all other action and event mechanisms have resolved
1657    /// unlike [`App::observe_keystrokes`] which fires after. This means that `cx.stop_propagation` calls
1658    /// within interceptors will prevent action dispatch
1659    pub fn intercept_keystrokes(
1660        &mut self,
1661        mut f: impl FnMut(&KeystrokeEvent, &mut Window, &mut App) + 'static,
1662    ) -> Subscription {
1663        fn inner(
1664            keystroke_interceptors: &SubscriberSet<(), KeystrokeObserver>,
1665            handler: KeystrokeObserver,
1666        ) -> Subscription {
1667            let (subscription, activate) = keystroke_interceptors.insert((), handler);
1668            activate();
1669            subscription
1670        }
1671
1672        inner(
1673            &self.keystroke_interceptors,
1674            Box::new(move |event, window, cx| {
1675                f(event, window, cx);
1676                true
1677            }),
1678        )
1679    }
1680
1681    /// Register key bindings.
1682    pub fn bind_keys(&mut self, bindings: impl IntoIterator<Item = KeyBinding>) {
1683        self.keymap.borrow_mut().add_bindings(bindings);
1684        self.pending_effects.push_back(Effect::RefreshWindows);
1685    }
1686
1687    /// Clear all key bindings in the app.
1688    pub fn clear_key_bindings(&mut self) {
1689        self.keymap.borrow_mut().clear();
1690        self.pending_effects.push_back(Effect::RefreshWindows);
1691    }
1692
1693    /// Get all key bindings in the app.
1694    pub fn key_bindings(&self) -> Rc<RefCell<Keymap>> {
1695        self.keymap.clone()
1696    }
1697
1698    /// Register a global handler for actions invoked via the keyboard. These handlers are run at
1699    /// the end of the bubble phase for actions, and so will only be invoked if there are no other
1700    /// handlers or if they called `cx.propagate()`.
1701    pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut Self) + 'static) {
1702        self.global_action_listeners
1703            .entry(TypeId::of::<A>())
1704            .or_default()
1705            .push(Rc::new(move |action, phase, cx| {
1706                if phase == DispatchPhase::Bubble {
1707                    let action = action.downcast_ref().unwrap();
1708                    listener(action, cx)
1709                }
1710            }));
1711    }
1712
1713    /// Event handlers propagate events by default. Call this method to stop dispatching to
1714    /// event handlers with a lower z-index (mouse) or higher in the tree (keyboard). This is
1715    /// the opposite of [`Self::propagate`]. It's also possible to cancel a call to [`Self::propagate`] by
1716    /// calling this method before effects are flushed.
1717    pub fn stop_propagation(&mut self) {
1718        self.propagate_event = false;
1719    }
1720
1721    /// Action handlers stop propagation by default during the bubble phase of action dispatch
1722    /// dispatching to action handlers higher in the element tree. This is the opposite of
1723    /// [`Self::stop_propagation`]. It's also possible to cancel a call to [`Self::stop_propagation`] by calling
1724    /// this method before effects are flushed.
1725    pub fn propagate(&mut self) {
1726        self.propagate_event = true;
1727    }
1728
1729    /// Build an action from some arbitrary data, typically a keymap entry.
1730    pub fn build_action(
1731        &self,
1732        name: &str,
1733        data: Option<serde_json::Value>,
1734    ) -> std::result::Result<Box<dyn Action>, ActionBuildError> {
1735        self.actions.build_action(name, data)
1736    }
1737
1738    /// Get all action names that have been registered. Note that registration only allows for
1739    /// actions to be built dynamically, and is unrelated to binding actions in the element tree.
1740    pub fn all_action_names(&self) -> &[&'static str] {
1741        self.actions.all_action_names()
1742    }
1743
1744    /// Returns key bindings that invoke the given action on the currently focused element, without
1745    /// checking context. Bindings are returned in the order they were added. For display, the last
1746    /// binding should take precedence.
1747    pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
1748        RefCell::borrow(&self.keymap).all_bindings_for_input(input)
1749    }
1750
1751    /// Get all non-internal actions that have been registered, along with their schemas.
1752    pub fn action_schemas(
1753        &self,
1754        generator: &mut schemars::SchemaGenerator,
1755    ) -> Vec<(&'static str, Option<schemars::Schema>)> {
1756        self.actions.action_schemas(generator)
1757    }
1758
1759    /// Get a map from a deprecated action name to the canonical name.
1760    pub fn deprecated_actions_to_preferred_actions(&self) -> &HashMap<&'static str, &'static str> {
1761        self.actions.deprecated_aliases()
1762    }
1763
1764    /// Get a map from an action name to the deprecation messages.
1765    pub fn action_deprecation_messages(&self) -> &HashMap<&'static str, &'static str> {
1766        self.actions.deprecation_messages()
1767    }
1768
1769    /// Get a map from an action name to the documentation.
1770    pub fn action_documentation(&self) -> &HashMap<&'static str, &'static str> {
1771        self.actions.documentation()
1772    }
1773
1774    /// Register a callback to be invoked when the application is about to quit.
1775    /// It is not possible to cancel the quit event at this point.
1776    pub fn on_app_quit<Fut>(
1777        &self,
1778        mut on_quit: impl FnMut(&mut App) -> Fut + 'static,
1779    ) -> Subscription
1780    where
1781        Fut: 'static + Future<Output = ()>,
1782    {
1783        let (subscription, activate) = self.quit_observers.insert(
1784            (),
1785            Box::new(move |cx| {
1786                let future = on_quit(cx);
1787                future.boxed_local()
1788            }),
1789        );
1790        activate();
1791        subscription
1792    }
1793
1794    /// Register a callback to be invoked when the application is about to restart.
1795    ///
1796    /// These callbacks are called before any `on_app_quit` callbacks.
1797    pub fn on_app_restart(&self, mut on_restart: impl 'static + FnMut(&mut App)) -> Subscription {
1798        let (subscription, activate) = self.restart_observers.insert(
1799            (),
1800            Box::new(move |cx| {
1801                on_restart(cx);
1802                true
1803            }),
1804        );
1805        activate();
1806        subscription
1807    }
1808
1809    /// Register a callback to be invoked when a window is closed
1810    /// The window is no longer accessible at the point this callback is invoked.
1811    pub fn on_window_closed(&self, mut on_closed: impl FnMut(&mut App) + 'static) -> Subscription {
1812        let (subscription, activate) = self.window_closed_observers.insert((), Box::new(on_closed));
1813        activate();
1814        subscription
1815    }
1816
1817    pub(crate) fn clear_pending_keystrokes(&mut self) {
1818        for window in self.windows() {
1819            window
1820                .update(self, |_, window, _| {
1821                    window.clear_pending_keystrokes();
1822                })
1823                .ok();
1824        }
1825    }
1826
1827    /// Checks if the given action is bound in the current context, as defined by the app's current focus,
1828    /// the bindings in the element tree, and any global action listeners.
1829    pub fn is_action_available(&mut self, action: &dyn Action) -> bool {
1830        let mut action_available = false;
1831        if let Some(window) = self.active_window()
1832            && let Ok(window_action_available) =
1833                window.update(self, |_, window, cx| window.is_action_available(action, cx))
1834        {
1835            action_available = window_action_available;
1836        }
1837
1838        action_available
1839            || self
1840                .global_action_listeners
1841                .contains_key(&action.as_any().type_id())
1842    }
1843
1844    /// Sets the menu bar for this application. This will replace any existing menu bar.
1845    pub fn set_menus(&self, menus: Vec<Menu>) {
1846        self.platform.set_menus(menus, &self.keymap.borrow());
1847    }
1848
1849    /// Gets the menu bar for this application.
1850    pub fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
1851        self.platform.get_menus()
1852    }
1853
1854    /// Sets the right click menu for the app icon in the dock
1855    pub fn set_dock_menu(&self, menus: Vec<MenuItem>) {
1856        self.platform.set_dock_menu(menus, &self.keymap.borrow())
1857    }
1858
1859    /// Performs the action associated with the given dock menu item, only used on Windows for now.
1860    pub fn perform_dock_menu_action(&self, action: usize) {
1861        self.platform.perform_dock_menu_action(action);
1862    }
1863
1864    /// Adds given path to the bottom of the list of recent paths for the application.
1865    /// The list is usually shown on the application icon's context menu in the dock,
1866    /// and allows to open the recent files via that context menu.
1867    /// If the path is already in the list, it will be moved to the bottom of the list.
1868    pub fn add_recent_document(&self, path: &Path) {
1869        self.platform.add_recent_document(path);
1870    }
1871
1872    /// Updates the jump list with the updated list of recent paths for the application, only used on Windows for now.
1873    /// Note that this also sets the dock menu on Windows.
1874    pub fn update_jump_list(
1875        &self,
1876        menus: Vec<MenuItem>,
1877        entries: Vec<SmallVec<[PathBuf; 2]>>,
1878    ) -> Vec<SmallVec<[PathBuf; 2]>> {
1879        self.platform.update_jump_list(menus, entries)
1880    }
1881
1882    /// Dispatch an action to the currently active window or global action handler
1883    /// See [`crate::Action`] for more information on how actions work
1884    pub fn dispatch_action(&mut self, action: &dyn Action) {
1885        if let Some(active_window) = self.active_window() {
1886            active_window
1887                .update(self, |_, window, cx| {
1888                    window.dispatch_action(action.boxed_clone(), cx)
1889                })
1890                .log_err();
1891        } else {
1892            self.dispatch_global_action(action);
1893        }
1894    }
1895
1896    fn dispatch_global_action(&mut self, action: &dyn Action) {
1897        self.propagate_event = true;
1898
1899        if let Some(mut global_listeners) = self
1900            .global_action_listeners
1901            .remove(&action.as_any().type_id())
1902        {
1903            for listener in &global_listeners {
1904                listener(action.as_any(), DispatchPhase::Capture, self);
1905                if !self.propagate_event {
1906                    break;
1907                }
1908            }
1909
1910            global_listeners.extend(
1911                self.global_action_listeners
1912                    .remove(&action.as_any().type_id())
1913                    .unwrap_or_default(),
1914            );
1915
1916            self.global_action_listeners
1917                .insert(action.as_any().type_id(), global_listeners);
1918        }
1919
1920        if self.propagate_event
1921            && let Some(mut global_listeners) = self
1922                .global_action_listeners
1923                .remove(&action.as_any().type_id())
1924        {
1925            for listener in global_listeners.iter().rev() {
1926                listener(action.as_any(), DispatchPhase::Bubble, self);
1927                if !self.propagate_event {
1928                    break;
1929                }
1930            }
1931
1932            global_listeners.extend(
1933                self.global_action_listeners
1934                    .remove(&action.as_any().type_id())
1935                    .unwrap_or_default(),
1936            );
1937
1938            self.global_action_listeners
1939                .insert(action.as_any().type_id(), global_listeners);
1940        }
1941    }
1942
1943    /// Is there currently something being dragged?
1944    pub fn has_active_drag(&self) -> bool {
1945        self.active_drag.is_some()
1946    }
1947
1948    /// Gets the cursor style of the currently active drag operation.
1949    pub fn active_drag_cursor_style(&self) -> Option<CursorStyle> {
1950        self.active_drag.as_ref().and_then(|drag| drag.cursor_style)
1951    }
1952
1953    /// Stops active drag and clears any related effects.
1954    pub fn stop_active_drag(&mut self, window: &mut Window) -> bool {
1955        if self.active_drag.is_some() {
1956            self.active_drag = None;
1957            window.refresh();
1958            true
1959        } else {
1960            false
1961        }
1962    }
1963
1964    /// Sets the cursor style for the currently active drag operation.
1965    pub fn set_active_drag_cursor_style(
1966        &mut self,
1967        cursor_style: CursorStyle,
1968        window: &mut Window,
1969    ) -> bool {
1970        if let Some(ref mut drag) = self.active_drag {
1971            drag.cursor_style = Some(cursor_style);
1972            window.refresh();
1973            true
1974        } else {
1975            false
1976        }
1977    }
1978
1979    /// Set the prompt renderer for GPUI. This will replace the default or platform specific
1980    /// prompts with this custom implementation.
1981    pub fn set_prompt_builder(
1982        &mut self,
1983        renderer: impl Fn(
1984            PromptLevel,
1985            &str,
1986            Option<&str>,
1987            &[PromptButton],
1988            PromptHandle,
1989            &mut Window,
1990            &mut App,
1991        ) -> RenderablePromptHandle
1992        + 'static,
1993    ) {
1994        self.prompt_builder = Some(PromptBuilder::Custom(Box::new(renderer)));
1995    }
1996
1997    /// Reset the prompt builder to the default implementation.
1998    pub fn reset_prompt_builder(&mut self) {
1999        self.prompt_builder = Some(PromptBuilder::Default);
2000    }
2001
2002    /// Remove an asset from GPUI's cache
2003    pub fn remove_asset<A: Asset>(&mut self, source: &A::Source) {
2004        let asset_id = (TypeId::of::<A>(), hash(source));
2005        self.loading_assets.remove(&asset_id);
2006    }
2007
2008    /// Asynchronously load an asset, if the asset hasn't finished loading this will return None.
2009    ///
2010    /// Note that the multiple calls to this method will only result in one `Asset::load` call at a
2011    /// time, and the results of this call will be cached
2012    pub fn fetch_asset<A: Asset>(&mut self, source: &A::Source) -> (Shared<Task<A::Output>>, bool) {
2013        let asset_id = (TypeId::of::<A>(), hash(source));
2014        let mut is_first = false;
2015        let task = self
2016            .loading_assets
2017            .remove(&asset_id)
2018            .map(|boxed_task| *boxed_task.downcast::<Shared<Task<A::Output>>>().unwrap())
2019            .unwrap_or_else(|| {
2020                is_first = true;
2021                let future = A::load(source.clone(), self);
2022
2023                self.background_executor().spawn(future).shared()
2024            });
2025
2026        self.loading_assets.insert(asset_id, Box::new(task.clone()));
2027
2028        (task, is_first)
2029    }
2030
2031    /// Obtain a new [`FocusHandle`], which allows you to track and manipulate the keyboard focus
2032    /// for elements rendered within this window.
2033    #[track_caller]
2034    pub fn focus_handle(&self) -> FocusHandle {
2035        FocusHandle::new(&self.focus_handles)
2036    }
2037
2038    /// Tell GPUI that an entity has changed and observers of it should be notified.
2039    pub fn notify(&mut self, entity_id: EntityId) {
2040        let window_invalidators = mem::take(
2041            self.window_invalidators_by_entity
2042                .entry(entity_id)
2043                .or_default(),
2044        );
2045
2046        if window_invalidators.is_empty() {
2047            if self.pending_notifications.insert(entity_id) {
2048                self.pending_effects
2049                    .push_back(Effect::Notify { emitter: entity_id });
2050            }
2051        } else {
2052            for invalidator in window_invalidators.values() {
2053                invalidator.invalidate_view(entity_id, self);
2054            }
2055        }
2056
2057        self.window_invalidators_by_entity
2058            .insert(entity_id, window_invalidators);
2059    }
2060
2061    /// Returns the name for this [`App`].
2062    #[cfg(any(test, feature = "test-support", debug_assertions))]
2063    pub fn get_name(&self) -> Option<&'static str> {
2064        self.name
2065    }
2066
2067    /// Returns `true` if the platform file picker supports selecting a mix of files and directories.
2068    pub fn can_select_mixed_files_and_dirs(&self) -> bool {
2069        self.platform.can_select_mixed_files_and_dirs()
2070    }
2071
2072    /// Removes an image from the sprite atlas on all windows.
2073    ///
2074    /// If the current window is being updated, it will be removed from `App.windows`, you can use `current_window` to specify the current window.
2075    /// This is a no-op if the image is not in the sprite atlas.
2076    pub fn drop_image(&mut self, image: Arc<RenderImage>, current_window: Option<&mut Window>) {
2077        // remove the texture from all other windows
2078        for window in self.windows.values_mut().flatten() {
2079            _ = window.drop_image(image.clone());
2080        }
2081
2082        // remove the texture from the current window
2083        if let Some(window) = current_window {
2084            _ = window.drop_image(image);
2085        }
2086    }
2087
2088    /// Sets the renderer for the inspector.
2089    #[cfg(any(feature = "inspector", debug_assertions))]
2090    pub fn set_inspector_renderer(&mut self, f: crate::InspectorRenderer) {
2091        self.inspector_renderer = Some(f);
2092    }
2093
2094    /// Registers a renderer specific to an inspector state.
2095    #[cfg(any(feature = "inspector", debug_assertions))]
2096    pub fn register_inspector_element<T: 'static, R: crate::IntoElement>(
2097        &mut self,
2098        f: impl 'static + Fn(crate::InspectorElementId, &T, &mut Window, &mut App) -> R,
2099    ) {
2100        self.inspector_element_registry.register(f);
2101    }
2102
2103    /// Initializes gpui's default colors for the application.
2104    ///
2105    /// These colors can be accessed through `cx.default_colors()`.
2106    pub fn init_colors(&mut self) {
2107        self.set_global(GlobalColors(Arc::new(Colors::default())));
2108    }
2109}
2110
2111impl AppContext for App {
2112    type Result<T> = T;
2113
2114    /// Builds an entity that is owned by the application.
2115    ///
2116    /// The given function will be invoked with a [`Context`] and must return an object representing the entity. An
2117    /// [`Entity`] handle will be returned, which can be used to access the entity in a context.
2118    fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T> {
2119        self.update(|cx| {
2120            let slot = cx.entities.reserve();
2121            let handle = slot.clone();
2122            let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
2123
2124            cx.push_effect(Effect::EntityCreated {
2125                entity: handle.clone().into_any(),
2126                tid: TypeId::of::<T>(),
2127                window: cx.window_update_stack.last().cloned(),
2128            });
2129
2130            cx.entities.insert(slot, entity);
2131            handle
2132        })
2133    }
2134
2135    fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>> {
2136        Reservation(self.entities.reserve())
2137    }
2138
2139    fn insert_entity<T: 'static>(
2140        &mut self,
2141        reservation: Reservation<T>,
2142        build_entity: impl FnOnce(&mut Context<T>) -> T,
2143    ) -> Self::Result<Entity<T>> {
2144        self.update(|cx| {
2145            let slot = reservation.0;
2146            let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
2147            cx.entities.insert(slot, entity)
2148        })
2149    }
2150
2151    /// Updates the entity referenced by the given handle. The function is passed a mutable reference to the
2152    /// entity along with a `Context` for the entity.
2153    fn update_entity<T: 'static, R>(
2154        &mut self,
2155        handle: &Entity<T>,
2156        update: impl FnOnce(&mut T, &mut Context<T>) -> R,
2157    ) -> R {
2158        self.update(|cx| {
2159            let mut entity = cx.entities.lease(handle);
2160            let result = update(
2161                &mut entity,
2162                &mut Context::new_context(cx, handle.downgrade()),
2163            );
2164            cx.entities.end_lease(entity);
2165            result
2166        })
2167    }
2168
2169    fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
2170    where
2171        T: 'static,
2172    {
2173        GpuiBorrow::new(handle.clone(), self)
2174    }
2175
2176    fn read_entity<T, R>(
2177        &self,
2178        handle: &Entity<T>,
2179        read: impl FnOnce(&T, &App) -> R,
2180    ) -> Self::Result<R>
2181    where
2182        T: 'static,
2183    {
2184        let entity = self.entities.read(handle);
2185        read(entity, self)
2186    }
2187
2188    fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
2189    where
2190        F: FnOnce(AnyView, &mut Window, &mut App) -> T,
2191    {
2192        self.update_window_id(handle.id, update)
2193    }
2194
2195    fn read_window<T, R>(
2196        &self,
2197        window: &WindowHandle<T>,
2198        read: impl FnOnce(Entity<T>, &App) -> R,
2199    ) -> Result<R>
2200    where
2201        T: 'static,
2202    {
2203        let window = self
2204            .windows
2205            .get(window.id)
2206            .context("window not found")?
2207            .as_ref()
2208            .expect("attempted to read a window that is already on the stack");
2209
2210        let root_view = window.root.clone().unwrap();
2211        let view = root_view
2212            .downcast::<T>()
2213            .map_err(|_| anyhow!("root view's type has changed"))?;
2214
2215        Ok(read(view, self))
2216    }
2217
2218    fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
2219    where
2220        R: Send + 'static,
2221    {
2222        self.background_executor.spawn(future)
2223    }
2224
2225    fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
2226    where
2227        G: Global,
2228    {
2229        let mut g = self.global::<G>();
2230        callback(g, self)
2231    }
2232}
2233
2234/// These effects are processed at the end of each application update cycle.
2235pub(crate) enum Effect {
2236    Notify {
2237        emitter: EntityId,
2238    },
2239    Emit {
2240        emitter: EntityId,
2241        event_type: TypeId,
2242        event: Box<dyn Any>,
2243    },
2244    RefreshWindows,
2245    NotifyGlobalObservers {
2246        global_type: TypeId,
2247    },
2248    Defer {
2249        callback: Box<dyn FnOnce(&mut App) + 'static>,
2250    },
2251    EntityCreated {
2252        entity: AnyEntity,
2253        tid: TypeId,
2254        window: Option<WindowId>,
2255    },
2256}
2257
2258impl std::fmt::Debug for Effect {
2259    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2260        match self {
2261            Effect::Notify { emitter } => write!(f, "Notify({})", emitter),
2262            Effect::Emit { emitter, .. } => write!(f, "Emit({:?})", emitter),
2263            Effect::RefreshWindows => write!(f, "RefreshWindows"),
2264            Effect::NotifyGlobalObservers { global_type } => {
2265                write!(f, "NotifyGlobalObservers({:?})", global_type)
2266            }
2267            Effect::Defer { .. } => write!(f, "Defer(..)"),
2268            Effect::EntityCreated { entity, .. } => write!(f, "EntityCreated({:?})", entity),
2269        }
2270    }
2271}
2272
2273/// Wraps a global variable value during `update_global` while the value has been moved to the stack.
2274pub(crate) struct GlobalLease<G: Global> {
2275    global: Box<dyn Any>,
2276    global_type: PhantomData<G>,
2277}
2278
2279impl<G: Global> GlobalLease<G> {
2280    fn new(global: Box<dyn Any>) -> Self {
2281        GlobalLease {
2282            global,
2283            global_type: PhantomData,
2284        }
2285    }
2286}
2287
2288impl<G: Global> Deref for GlobalLease<G> {
2289    type Target = G;
2290
2291    fn deref(&self) -> &Self::Target {
2292        self.global.downcast_ref().unwrap()
2293    }
2294}
2295
2296impl<G: Global> DerefMut for GlobalLease<G> {
2297    fn deref_mut(&mut self) -> &mut Self::Target {
2298        self.global.downcast_mut().unwrap()
2299    }
2300}
2301
2302/// Contains state associated with an active drag operation, started by dragging an element
2303/// within the window or by dragging into the app from the underlying platform.
2304pub struct AnyDrag {
2305    /// The view used to render this drag
2306    pub view: AnyView,
2307
2308    /// The value of the dragged item, to be dropped
2309    pub value: Arc<dyn Any>,
2310
2311    /// This is used to render the dragged item in the same place
2312    /// on the original element that the drag was initiated
2313    pub cursor_offset: Point<Pixels>,
2314
2315    /// The cursor style to use while dragging
2316    pub cursor_style: Option<CursorStyle>,
2317}
2318
2319/// Contains state associated with a tooltip. You'll only need this struct if you're implementing
2320/// tooltip behavior on a custom element. Otherwise, use [Div::tooltip](crate::Interactivity::tooltip).
2321#[derive(Clone)]
2322pub struct AnyTooltip {
2323    /// The view used to display the tooltip
2324    pub view: AnyView,
2325
2326    /// The absolute position of the mouse when the tooltip was deployed.
2327    pub mouse_position: Point<Pixels>,
2328
2329    /// Given the bounds of the tooltip, checks whether the tooltip should still be visible and
2330    /// updates its state accordingly. This is needed atop the hovered element's mouse move handler
2331    /// to handle the case where the element is not painted (e.g. via use of `visible_on_hover`).
2332    pub check_visible_and_update: Rc<dyn Fn(Bounds<Pixels>, &mut Window, &mut App) -> bool>,
2333}
2334
2335/// A keystroke event, and potentially the associated action
2336#[derive(Debug)]
2337pub struct KeystrokeEvent {
2338    /// The keystroke that occurred
2339    pub keystroke: Keystroke,
2340
2341    /// The action that was resolved for the keystroke, if any
2342    pub action: Option<Box<dyn Action>>,
2343
2344    /// The context stack at the time
2345    pub context_stack: Vec<KeyContext>,
2346}
2347
2348struct NullHttpClient;
2349
2350impl HttpClient for NullHttpClient {
2351    fn send(
2352        &self,
2353        _req: http_client::Request<http_client::AsyncBody>,
2354    ) -> futures::future::BoxFuture<
2355        'static,
2356        anyhow::Result<http_client::Response<http_client::AsyncBody>>,
2357    > {
2358        async move {
2359            anyhow::bail!("No HttpClient available");
2360        }
2361        .boxed()
2362    }
2363
2364    fn user_agent(&self) -> Option<&http_client::http::HeaderValue> {
2365        None
2366    }
2367
2368    fn proxy(&self) -> Option<&Url> {
2369        None
2370    }
2371
2372    fn type_name(&self) -> &'static str {
2373        type_name::<Self>()
2374    }
2375}
2376
2377/// A mutable reference to an entity owned by GPUI
2378pub struct GpuiBorrow<'a, T> {
2379    inner: Option<Lease<T>>,
2380    app: &'a mut App,
2381}
2382
2383impl<'a, T: 'static> GpuiBorrow<'a, T> {
2384    fn new(inner: Entity<T>, app: &'a mut App) -> Self {
2385        app.start_update();
2386        let lease = app.entities.lease(&inner);
2387        Self {
2388            inner: Some(lease),
2389            app,
2390        }
2391    }
2392}
2393
2394impl<'a, T: 'static> std::borrow::Borrow<T> for GpuiBorrow<'a, T> {
2395    fn borrow(&self) -> &T {
2396        self.inner.as_ref().unwrap().borrow()
2397    }
2398}
2399
2400impl<'a, T: 'static> std::borrow::BorrowMut<T> for GpuiBorrow<'a, T> {
2401    fn borrow_mut(&mut self) -> &mut T {
2402        self.inner.as_mut().unwrap().borrow_mut()
2403    }
2404}
2405
2406impl<'a, T: 'static> std::ops::Deref for GpuiBorrow<'a, T> {
2407    type Target = T;
2408
2409    fn deref(&self) -> &Self::Target {
2410        self.inner.as_ref().unwrap()
2411    }
2412}
2413
2414impl<'a, T: 'static> std::ops::DerefMut for GpuiBorrow<'a, T> {
2415    fn deref_mut(&mut self) -> &mut T {
2416        self.inner.as_mut().unwrap()
2417    }
2418}
2419
2420impl<'a, T> Drop for GpuiBorrow<'a, T> {
2421    fn drop(&mut self) {
2422        let lease = self.inner.take().unwrap();
2423        self.app.notify(lease.id);
2424        self.app.entities.end_lease(lease);
2425        self.app.finish_update();
2426    }
2427}
2428
2429#[cfg(test)]
2430mod test {
2431    use std::{cell::RefCell, rc::Rc};
2432
2433    use crate::{AppContext, TestAppContext};
2434
2435    #[test]
2436    fn test_gpui_borrow() {
2437        let cx = TestAppContext::single();
2438        let observation_count = Rc::new(RefCell::new(0));
2439
2440        let state = cx.update(|cx| {
2441            let state = cx.new(|_| false);
2442            cx.observe(&state, {
2443                let observation_count = observation_count.clone();
2444                move |_, _| {
2445                    let mut count = observation_count.borrow_mut();
2446                    *count += 1;
2447                }
2448            })
2449            .detach();
2450
2451            state
2452        });
2453
2454        cx.update(|cx| {
2455            // Calling this like this so that we don't clobber the borrow_mut above
2456            *std::borrow::BorrowMut::borrow_mut(&mut state.as_mut(cx)) = true;
2457        });
2458
2459        cx.update(|cx| {
2460            state.write(cx, false);
2461        });
2462
2463        assert_eq!(*observation_count.borrow(), 2);
2464    }
2465}