use std::{
any::{TypeId, type_name},
cell::{BorrowMutError, Cell, Ref, RefCell, RefMut},
marker::PhantomData,
mem,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
rc::{Rc, Weak},
sync::{Arc, atomic::Ordering::SeqCst},
time::{Duration, Instant},
};
use anyhow::{Context as _, Result, anyhow};
use derive_more::{Deref, DerefMut};
use futures::{
Future, FutureExt,
channel::oneshot,
future::{LocalBoxFuture, Shared},
};
use itertools::Itertools;
use parking_lot::RwLock;
use slotmap::SlotMap;
pub use async_context::*;
use collections::{FxHashMap, FxHashSet, HashMap, VecDeque};
pub use context::*;
pub use entity_map::*;
use http_client::{HttpClient, Url};
use smallvec::SmallVec;
#[cfg(any(test, feature = "test-support"))]
pub use test_context::*;
use util::{ResultExt, debug_panic};
#[cfg(all(target_os = "macos", any(test, feature = "test-support")))]
pub use visual_test_context::*;
#[cfg(any(feature = "inspector", debug_assertions))]
use crate::InspectorElementRegistry;
use crate::{
Action, ActionBuildError, ActionRegistry, Any, AnyView, AnyWindowHandle, AppContext, Asset,
AssetSource, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DispatchPhase, DisplayId,
EventEmitter, FocusHandle, FocusMap, ForegroundExecutor, Global, KeyBinding, KeyContext,
Keymap, Keystroke, LayoutId, Menu, MenuItem, OwnedMenu, PathPromptOptions, Pixels, Platform,
PlatformDisplay, PlatformKeyboardLayout, PlatformKeyboardMapper, Point, Priority,
PromptBuilder, PromptButton, PromptHandle, PromptLevel, Render, RenderImage,
RenderablePromptHandle, Reservation, ScreenCaptureSource, SharedString, SubscriberSet,
Subscription, SvgRenderer, Task, TextRenderingMode, TextSystem, Window, WindowAppearance,
WindowHandle, WindowId, WindowInvalidator,
colors::{Colors, GlobalColors},
current_platform, hash, init_app_menus,
};
mod async_context;
mod context;
mod entity_map;
#[cfg(any(test, feature = "test-support"))]
mod test_context;
#[cfg(all(target_os = "macos", any(test, feature = "test-support")))]
mod visual_test_context;
pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(100);
#[doc(hidden)]
pub struct AppCell {
app: RefCell<App>,
}
impl AppCell {
#[doc(hidden)]
#[track_caller]
pub fn borrow(&self) -> AppRef<'_> {
if option_env!("TRACK_THREAD_BORROWS").is_some() {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
}
AppRef(self.app.borrow())
}
#[doc(hidden)]
#[track_caller]
pub fn borrow_mut(&self) -> AppRefMut<'_> {
if option_env!("TRACK_THREAD_BORROWS").is_some() {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
}
AppRefMut(self.app.borrow_mut())
}
#[doc(hidden)]
#[track_caller]
pub fn try_borrow_mut(&self) -> Result<AppRefMut<'_>, BorrowMutError> {
if option_env!("TRACK_THREAD_BORROWS").is_some() {
let thread_id = std::thread::current().id();
eprintln!("borrowed {thread_id:?}");
}
Ok(AppRefMut(self.app.try_borrow_mut()?))
}
}
#[doc(hidden)]
#[derive(Deref, DerefMut)]
pub struct AppRef<'a>(Ref<'a, App>);
impl Drop for AppRef<'_> {
fn drop(&mut self) {
if option_env!("TRACK_THREAD_BORROWS").is_some() {
let thread_id = std::thread::current().id();
eprintln!("dropped borrow from {thread_id:?}");
}
}
}
#[doc(hidden)]
#[derive(Deref, DerefMut)]
pub struct AppRefMut<'a>(RefMut<'a, App>);
impl Drop for AppRefMut<'_> {
fn drop(&mut self) {
if option_env!("TRACK_THREAD_BORROWS").is_some() {
let thread_id = std::thread::current().id();
eprintln!("dropped {thread_id:?}");
}
}
}
pub struct Application(Rc<AppCell>);
impl Application {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
#[cfg(any(test, feature = "test-support"))]
log::info!("GPUI was compiled in test mode");
let liveness = Arc::new(());
Self(App::new_app(
current_platform(false, Arc::downgrade(&liveness)),
liveness,
Arc::new(()),
Arc::new(NullHttpClient),
))
}
pub fn headless() -> Self {
let liveness = Arc::new(());
Self(App::new_app(
current_platform(true, Arc::downgrade(&liveness)),
liveness,
Arc::new(()),
Arc::new(NullHttpClient),
))
}
pub fn with_assets(self, asset_source: impl AssetSource) -> Self {
let mut context_lock = self.0.borrow_mut();
let asset_source = Arc::new(asset_source);
context_lock.asset_source = asset_source.clone();
context_lock.svg_renderer = SvgRenderer::new(asset_source);
drop(context_lock);
self
}
pub fn with_http_client(self, http_client: Arc<dyn HttpClient>) -> Self {
let mut context_lock = self.0.borrow_mut();
context_lock.http_client = http_client;
drop(context_lock);
self
}
pub fn with_quit_mode(self, mode: QuitMode) -> Self {
self.0.borrow_mut().quit_mode = mode;
self
}
pub fn run<F>(self, on_finish_launching: F)
where
F: 'static + FnOnce(&mut App),
{
let this = self.0.clone();
let platform = self.0.borrow().platform.clone();
platform.run(Box::new(move || {
let cx = &mut *this.borrow_mut();
on_finish_launching(cx);
}));
}
pub fn on_open_urls<F>(&self, mut callback: F) -> &Self
where
F: 'static + FnMut(Vec<String>),
{
self.0.borrow().platform.on_open_urls(Box::new(callback));
self
}
pub fn on_reopen<F>(&self, mut callback: F) -> &Self
where
F: 'static + FnMut(&mut App),
{
let this = Rc::downgrade(&self.0);
self.0.borrow_mut().platform.on_reopen(Box::new(move || {
if let Some(app) = this.upgrade() {
callback(&mut app.borrow_mut());
}
}));
self
}
pub fn background_executor(&self) -> BackgroundExecutor {
self.0.borrow().background_executor.clone()
}
pub fn foreground_executor(&self) -> ForegroundExecutor {
self.0.borrow().foreground_executor.clone()
}
pub fn text_system(&self) -> Arc<TextSystem> {
self.0.borrow().text_system.clone()
}
pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
self.0.borrow().path_for_auxiliary_executable(name)
}
}
type Handler = Box<dyn FnMut(&mut App) -> bool + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut App) -> bool + 'static>;
pub(crate) type KeystrokeObserver =
Box<dyn FnMut(&KeystrokeEvent, &mut Window, &mut App) -> bool + 'static>;
type QuitHandler = Box<dyn FnOnce(&mut App) -> LocalBoxFuture<'static, ()> + 'static>;
type WindowClosedHandler = Box<dyn FnMut(&mut App)>;
type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut App) + 'static>;
type NewEntityListener = Box<dyn FnMut(AnyEntity, &mut Option<&mut Window>, &mut App) + 'static>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum QuitMode {
#[default]
Default,
LastWindowClosed,
Explicit,
}
#[doc(hidden)]
#[derive(Clone, PartialEq, Eq)]
pub struct SystemWindowTab {
pub id: WindowId,
pub title: SharedString,
pub handle: AnyWindowHandle,
pub last_active_at: Instant,
}
impl SystemWindowTab {
pub fn new(title: SharedString, handle: AnyWindowHandle) -> Self {
Self {
id: handle.id,
title,
handle,
last_active_at: Instant::now(),
}
}
}
#[derive(Default)]
pub struct SystemWindowTabController {
visible: Option<bool>,
tab_groups: FxHashMap<usize, Vec<SystemWindowTab>>,
}
impl Global for SystemWindowTabController {}
impl SystemWindowTabController {
pub fn new() -> Self {
Self {
visible: None,
tab_groups: FxHashMap::default(),
}
}
pub fn init(cx: &mut App) {
cx.set_global(SystemWindowTabController::new());
}
pub fn tab_groups(&self) -> &FxHashMap<usize, Vec<SystemWindowTab>> {
&self.tab_groups
}
pub fn get_next_tab_group_window(cx: &mut App, id: WindowId) -> Option<&AnyWindowHandle> {
let controller = cx.global::<SystemWindowTabController>();
let current_group = controller
.tab_groups
.iter()
.find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));
let current_group = current_group?;
let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
let idx = group_ids.iter().position(|g| *g == current_group)?;
let next_idx = (idx + 1) % group_ids.len();
controller
.tab_groups
.get(group_ids[next_idx])
.and_then(|tabs| {
tabs.iter()
.max_by_key(|tab| tab.last_active_at)
.or_else(|| tabs.first())
.map(|tab| &tab.handle)
})
}
pub fn get_prev_tab_group_window(cx: &mut App, id: WindowId) -> Option<&AnyWindowHandle> {
let controller = cx.global::<SystemWindowTabController>();
let current_group = controller
.tab_groups
.iter()
.find_map(|(group, tabs)| tabs.iter().find(|tab| tab.id == id).map(|_| group));
let current_group = current_group?;
let mut group_ids: Vec<_> = controller.tab_groups.keys().collect();
let idx = group_ids.iter().position(|g| *g == current_group)?;
let prev_idx = if idx == 0 {
group_ids.len() - 1
} else {
idx - 1
};
controller
.tab_groups
.get(group_ids[prev_idx])
.and_then(|tabs| {
tabs.iter()
.max_by_key(|tab| tab.last_active_at)
.or_else(|| tabs.first())
.map(|tab| &tab.handle)
})
}
pub fn tabs(&self, id: WindowId) -> Option<&Vec<SystemWindowTab>> {
self.tab_groups
.values()
.find(|tabs| tabs.iter().any(|tab| tab.id == id))
}
pub fn init_visible(cx: &mut App, visible: bool) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
if controller.visible.is_none() {
controller.visible = Some(visible);
}
}
pub fn is_visible(&self) -> bool {
self.visible.unwrap_or(false)
}
pub fn set_visible(cx: &mut App, visible: bool) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
controller.visible = Some(visible);
}
pub fn update_last_active(cx: &mut App, id: WindowId) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
for windows in controller.tab_groups.values_mut() {
for tab in windows.iter_mut() {
if tab.id == id {
tab.last_active_at = Instant::now();
}
}
}
}
pub fn update_tab_position(cx: &mut App, id: WindowId, ix: usize) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
for (_, windows) in controller.tab_groups.iter_mut() {
if let Some(current_pos) = windows.iter().position(|tab| tab.id == id) {
if ix < windows.len() && current_pos != ix {
let window_tab = windows.remove(current_pos);
windows.insert(ix, window_tab);
}
break;
}
}
}
pub fn update_tab_title(cx: &mut App, id: WindowId, title: SharedString) {
let controller = cx.global::<SystemWindowTabController>();
let tab = controller
.tab_groups
.values()
.flat_map(|windows| windows.iter())
.find(|tab| tab.id == id);
if tab.map_or(true, |t| t.title == title) {
return;
}
let mut controller = cx.global_mut::<SystemWindowTabController>();
for windows in controller.tab_groups.values_mut() {
for tab in windows.iter_mut() {
if tab.id == id {
tab.title = title;
return;
}
}
}
}
pub fn add_tab(cx: &mut App, id: WindowId, tabs: Vec<SystemWindowTab>) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let Some(tab) = tabs.iter().find(|tab| tab.id == id).cloned() else {
return;
};
let mut expected_tab_ids: Vec<_> = tabs
.iter()
.filter(|tab| tab.id != id)
.map(|tab| tab.id)
.sorted()
.collect();
let mut tab_group_id = None;
for (group_id, group_tabs) in &controller.tab_groups {
let tab_ids: Vec<_> = group_tabs.iter().map(|tab| tab.id).sorted().collect();
if tab_ids == expected_tab_ids {
tab_group_id = Some(*group_id);
break;
}
}
if let Some(tab_group_id) = tab_group_id {
if let Some(tabs) = controller.tab_groups.get_mut(&tab_group_id) {
tabs.push(tab);
}
} else {
let new_group_id = controller.tab_groups.len();
controller.tab_groups.insert(new_group_id, tabs);
}
}
pub fn remove_tab(cx: &mut App, id: WindowId) -> Option<SystemWindowTab> {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let mut removed_tab = None;
controller.tab_groups.retain(|_, tabs| {
if let Some(pos) = tabs.iter().position(|tab| tab.id == id) {
removed_tab = Some(tabs.remove(pos));
}
!tabs.is_empty()
});
removed_tab
}
pub fn move_tab_to_new_window(cx: &mut App, id: WindowId) {
let mut removed_tab = Self::remove_tab(cx, id);
let mut controller = cx.global_mut::<SystemWindowTabController>();
if let Some(tab) = removed_tab {
let new_group_id = controller.tab_groups.keys().max().map_or(0, |k| k + 1);
controller.tab_groups.insert(new_group_id, vec![tab]);
}
}
pub fn merge_all_windows(cx: &mut App, id: WindowId) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let Some(initial_tabs) = controller.tabs(id) else {
return;
};
let initial_tabs_len = initial_tabs.len();
let mut all_tabs = initial_tabs.clone();
for (_, mut tabs) in controller.tab_groups.drain() {
tabs.retain(|tab| !all_tabs[..initial_tabs_len].contains(tab));
all_tabs.extend(tabs);
}
controller.tab_groups.insert(0, all_tabs);
}
pub fn select_next_tab(cx: &mut App, id: WindowId) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let Some(tabs) = controller.tabs(id) else {
return;
};
let current_index = tabs.iter().position(|tab| tab.id == id).unwrap();
let next_index = (current_index + 1) % tabs.len();
let _ = &tabs[next_index].handle.update(cx, |_, window, _| {
window.activate_window();
});
}
pub fn select_previous_tab(cx: &mut App, id: WindowId) {
let mut controller = cx.global_mut::<SystemWindowTabController>();
let Some(tabs) = controller.tabs(id) else {
return;
};
let current_index = tabs.iter().position(|tab| tab.id == id).unwrap();
let previous_index = if current_index == 0 {
tabs.len() - 1
} else {
current_index - 1
};
let _ = &tabs[previous_index].handle.update(cx, |_, window, _| {
window.activate_window();
});
}
}
pub(crate) enum GpuiMode {
#[cfg(any(test, feature = "test-support"))]
Test {
skip_drawing: bool,
},
Production,
}
impl GpuiMode {
#[cfg(any(test, feature = "test-support"))]
pub fn test() -> Self {
GpuiMode::Test {
skip_drawing: false,
}
}
#[inline]
pub(crate) fn skip_drawing(&self) -> bool {
match self {
#[cfg(any(test, feature = "test-support"))]
GpuiMode::Test { skip_drawing } => *skip_drawing,
GpuiMode::Production => false,
}
}
}
pub struct App {
pub(crate) this: Weak<AppCell>,
pub(crate) _liveness: Arc<()>,
pub(crate) platform: Rc<dyn Platform>,
pub(crate) mode: GpuiMode,
text_system: Arc<TextSystem>,
flushing_effects: bool,
pending_updates: usize,
pub(crate) actions: Rc<ActionRegistry>,
pub(crate) active_drag: Option<AnyDrag>,
pub(crate) background_executor: BackgroundExecutor,
pub(crate) foreground_executor: ForegroundExecutor,
pub(crate) loading_assets: FxHashMap<(TypeId, u64), Box<dyn Any>>,
asset_source: Arc<dyn AssetSource>,
pub(crate) svg_renderer: SvgRenderer,
http_client: Arc<dyn HttpClient>,
pub(crate) globals_by_type: FxHashMap<TypeId, Box<dyn Any>>,
pub(crate) entities: EntityMap,
pub(crate) window_update_stack: Vec<WindowId>,
pub(crate) new_entity_observers: SubscriberSet<TypeId, NewEntityListener>,
pub(crate) windows: SlotMap<WindowId, Option<Box<Window>>>,
pub(crate) window_handles: FxHashMap<WindowId, AnyWindowHandle>,
pub(crate) focus_handles: Arc<FocusMap>,
pub(crate) keymap: Rc<RefCell<Keymap>>,
pub(crate) keyboard_layout: Box<dyn PlatformKeyboardLayout>,
pub(crate) keyboard_mapper: Rc<dyn PlatformKeyboardMapper>,
pub(crate) global_action_listeners:
FxHashMap<TypeId, Vec<Rc<dyn Fn(&dyn Any, DispatchPhase, &mut Self)>>>,
pending_effects: VecDeque<Effect>,
pub(crate) pending_notifications: FxHashSet<EntityId>,
pub(crate) pending_global_notifications: FxHashSet<TypeId>,
pub(crate) observers: SubscriberSet<EntityId, Handler>,
pub(crate) event_listeners: SubscriberSet<EntityId, (TypeId, Listener)>,
pub(crate) keystroke_observers: SubscriberSet<(), KeystrokeObserver>,
pub(crate) keystroke_interceptors: SubscriberSet<(), KeystrokeObserver>,
pub(crate) keyboard_layout_observers: SubscriberSet<(), Handler>,
pub(crate) release_listeners: SubscriberSet<EntityId, ReleaseListener>,
pub(crate) global_observers: SubscriberSet<TypeId, Handler>,
pub(crate) quit_observers: SubscriberSet<(), QuitHandler>,
pub(crate) restart_observers: SubscriberSet<(), Handler>,
pub(crate) restart_path: Option<PathBuf>,
pub(crate) window_closed_observers: SubscriberSet<(), WindowClosedHandler>,
pub(crate) layout_id_buffer: Vec<LayoutId>, pub(crate) propagate_event: bool,
pub(crate) prompt_builder: Option<PromptBuilder>,
pub(crate) window_invalidators_by_entity:
FxHashMap<EntityId, FxHashMap<WindowId, WindowInvalidator>>,
pub(crate) tracked_entities: FxHashMap<WindowId, FxHashSet<EntityId>>,
#[cfg(any(feature = "inspector", debug_assertions))]
pub(crate) inspector_renderer: Option<crate::InspectorRenderer>,
#[cfg(any(feature = "inspector", debug_assertions))]
pub(crate) inspector_element_registry: InspectorElementRegistry,
#[cfg(any(test, feature = "test-support", debug_assertions))]
pub(crate) name: Option<&'static str>,
pub(crate) text_rendering_mode: Rc<Cell<TextRenderingMode>>,
quit_mode: QuitMode,
quitting: bool,
}
impl App {
#[allow(clippy::new_ret_no_self)]
pub(crate) fn new_app(
platform: Rc<dyn Platform>,
liveness: Arc<()>,
asset_source: Arc<dyn AssetSource>,
http_client: Arc<dyn HttpClient>,
) -> Rc<AppCell> {
let background_executor = platform.background_executor();
let foreground_executor = platform.foreground_executor();
assert!(
background_executor.is_main_thread(),
"must construct App on main thread"
);
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let entities = EntityMap::new();
let keyboard_layout = platform.keyboard_layout();
let keyboard_mapper = platform.keyboard_mapper();
let app = Rc::new_cyclic(|this| AppCell {
app: RefCell::new(App {
this: this.clone(),
_liveness: liveness,
platform: platform.clone(),
text_system,
text_rendering_mode: Rc::new(Cell::new(TextRenderingMode::default())),
mode: GpuiMode::Production,
actions: Rc::new(ActionRegistry::default()),
flushing_effects: false,
pending_updates: 0,
active_drag: None,
background_executor,
foreground_executor,
svg_renderer: SvgRenderer::new(asset_source.clone()),
loading_assets: Default::default(),
asset_source,
http_client,
globals_by_type: FxHashMap::default(),
entities,
new_entity_observers: SubscriberSet::new(),
windows: SlotMap::with_key(),
window_update_stack: Vec::new(),
window_handles: FxHashMap::default(),
focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
keymap: Rc::new(RefCell::new(Keymap::default())),
keyboard_layout,
keyboard_mapper,
global_action_listeners: FxHashMap::default(),
pending_effects: VecDeque::new(),
pending_notifications: FxHashSet::default(),
pending_global_notifications: FxHashSet::default(),
observers: SubscriberSet::new(),
tracked_entities: FxHashMap::default(),
window_invalidators_by_entity: FxHashMap::default(),
event_listeners: SubscriberSet::new(),
release_listeners: SubscriberSet::new(),
keystroke_observers: SubscriberSet::new(),
keystroke_interceptors: SubscriberSet::new(),
keyboard_layout_observers: SubscriberSet::new(),
global_observers: SubscriberSet::new(),
quit_observers: SubscriberSet::new(),
restart_observers: SubscriberSet::new(),
restart_path: None,
window_closed_observers: SubscriberSet::new(),
layout_id_buffer: Default::default(),
propagate_event: true,
prompt_builder: Some(PromptBuilder::Default),
#[cfg(any(feature = "inspector", debug_assertions))]
inspector_renderer: None,
#[cfg(any(feature = "inspector", debug_assertions))]
inspector_element_registry: InspectorElementRegistry::default(),
quit_mode: QuitMode::default(),
quitting: false,
#[cfg(any(test, feature = "test-support", debug_assertions))]
name: None,
}),
});
init_app_menus(platform.as_ref(), &app.borrow());
SystemWindowTabController::init(&mut app.borrow_mut());
platform.on_keyboard_layout_change(Box::new({
let app = Rc::downgrade(&app);
move || {
if let Some(app) = app.upgrade() {
let cx = &mut app.borrow_mut();
cx.keyboard_layout = cx.platform.keyboard_layout();
cx.keyboard_mapper = cx.platform.keyboard_mapper();
cx.keyboard_layout_observers
.clone()
.retain(&(), move |callback| (callback)(cx));
}
}
}));
platform.on_quit(Box::new({
let cx = app.clone();
move || {
cx.borrow_mut().shutdown();
}
}));
app
}
pub fn shutdown(&mut self) {
let mut futures = Vec::new();
for observer in self.quit_observers.remove(&()) {
futures.push(observer(self));
}
self.windows.clear();
self.window_handles.clear();
self.flush_effects();
self.quitting = true;
let futures = futures::future::join_all(futures);
if self
.background_executor
.block_with_timeout(SHUTDOWN_TIMEOUT, futures)
.is_err()
{
log::error!("timed out waiting on app_will_quit");
}
self.quitting = false;
}
pub fn keyboard_layout(&self) -> &dyn PlatformKeyboardLayout {
self.keyboard_layout.as_ref()
}
pub fn keyboard_mapper(&self) -> &Rc<dyn PlatformKeyboardMapper> {
&self.keyboard_mapper
}
pub fn on_keyboard_layout_change<F>(&self, mut callback: F) -> Subscription
where
F: 'static + FnMut(&mut App),
{
let (subscription, activate) = self.keyboard_layout_observers.insert(
(),
Box::new(move |cx| {
callback(cx);
true
}),
);
activate();
subscription
}
pub fn quit(&self) {
self.platform.quit();
}
pub fn refresh_windows(&mut self) {
self.pending_effects.push_back(Effect::RefreshWindows);
}
pub(crate) fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
self.start_update();
let result = update(self);
self.finish_update();
result
}
pub(crate) fn start_update(&mut self) {
self.pending_updates += 1;
}
pub(crate) fn finish_update(&mut self) {
if !self.flushing_effects && self.pending_updates == 1 {
self.flushing_effects = true;
self.flush_effects();
self.flushing_effects = false;
}
self.pending_updates -= 1;
}
pub fn observe<W>(
&mut self,
entity: &Entity<W>,
mut on_notify: impl FnMut(Entity<W>, &mut App) + 'static,
) -> Subscription
where
W: 'static,
{
self.observe_internal(entity, move |e, cx| {
on_notify(e, cx);
true
})
}
pub(crate) fn detect_accessed_entities<R>(
&mut self,
callback: impl FnOnce(&mut App) -> R,
) -> (R, FxHashSet<EntityId>) {
let accessed_entities_start = self.entities.accessed_entities.borrow().clone();
let result = callback(self);
let accessed_entities_end = self.entities.accessed_entities.borrow().clone();
let entities_accessed_in_callback = accessed_entities_end
.difference(&accessed_entities_start)
.copied()
.collect::<FxHashSet<EntityId>>();
(result, entities_accessed_in_callback)
}
pub(crate) fn record_entities_accessed(
&mut self,
window_handle: AnyWindowHandle,
invalidator: WindowInvalidator,
entities: &FxHashSet<EntityId>,
) {
let mut tracked_entities =
std::mem::take(self.tracked_entities.entry(window_handle.id).or_default());
for entity in tracked_entities.iter() {
self.window_invalidators_by_entity
.entry(*entity)
.and_modify(|windows| {
windows.remove(&window_handle.id);
});
}
for entity in entities.iter() {
self.window_invalidators_by_entity
.entry(*entity)
.or_default()
.insert(window_handle.id, invalidator.clone());
}
tracked_entities.clear();
tracked_entities.extend(entities.iter().copied());
self.tracked_entities
.insert(window_handle.id, tracked_entities);
}
pub(crate) fn new_observer(&mut self, key: EntityId, value: Handler) -> Subscription {
let (subscription, activate) = self.observers.insert(key, value);
self.defer(move |_| activate());
subscription
}
pub(crate) fn observe_internal<W>(
&mut self,
entity: &Entity<W>,
mut on_notify: impl FnMut(Entity<W>, &mut App) -> bool + 'static,
) -> Subscription
where
W: 'static,
{
let entity_id = entity.entity_id();
let handle = entity.downgrade();
self.new_observer(
entity_id,
Box::new(move |cx| {
if let Some(entity) = handle.upgrade() {
on_notify(entity, cx)
} else {
false
}
}),
)
}
pub fn subscribe<T, Event>(
&mut self,
entity: &Entity<T>,
mut on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
) -> Subscription
where
T: 'static + EventEmitter<Event>,
Event: 'static,
{
self.subscribe_internal(entity, move |entity, event, cx| {
on_event(entity, event, cx);
true
})
}
pub(crate) fn new_subscription(
&mut self,
key: EntityId,
value: (TypeId, Listener),
) -> Subscription {
let (subscription, activate) = self.event_listeners.insert(key, value);
self.defer(move |_| activate());
subscription
}
pub(crate) fn subscribe_internal<T, Evt>(
&mut self,
entity: &Entity<T>,
mut on_event: impl FnMut(Entity<T>, &Evt, &mut App) -> bool + 'static,
) -> Subscription
where
T: 'static + EventEmitter<Evt>,
Evt: 'static,
{
let entity_id = entity.entity_id();
let handle = entity.downgrade();
self.new_subscription(
entity_id,
(
TypeId::of::<Evt>(),
Box::new(move |event, cx| {
let event: &Evt = event.downcast_ref().expect("invalid event type");
if let Some(entity) = handle.upgrade() {
on_event(entity, event, cx)
} else {
false
}
}),
),
)
}
pub fn windows(&self) -> Vec<AnyWindowHandle> {
self.windows
.keys()
.flat_map(|window_id| self.window_handles.get(&window_id).copied())
.collect()
}
pub fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> {
self.platform.window_stack()
}
pub fn active_window(&self) -> Option<AnyWindowHandle> {
self.platform.active_window()
}
pub fn open_window<V: 'static + Render>(
&mut self,
options: crate::WindowOptions,
build_root_view: impl FnOnce(&mut Window, &mut App) -> Entity<V>,
) -> anyhow::Result<WindowHandle<V>> {
self.update(|cx| {
let id = cx.windows.insert(None);
let handle = WindowHandle::new(id);
match Window::new(handle.into(), options, cx) {
Ok(mut window) => {
cx.window_update_stack.push(id);
let root_view = build_root_view(&mut window, cx);
cx.window_update_stack.pop();
window.root.replace(root_view.into());
window.defer(cx, |window: &mut Window, cx| window.appearance_changed(cx));
let clear = window.draw(cx);
clear.clear();
cx.window_handles.insert(id, window.handle);
cx.windows.get_mut(id).unwrap().replace(Box::new(window));
Ok(handle)
}
Err(e) => {
cx.windows.remove(id);
Err(e)
}
}
})
}
pub fn activate(&self, ignoring_other_apps: bool) {
self.platform.activate(ignoring_other_apps);
}
pub fn hide(&self) {
self.platform.hide();
}
pub fn hide_other_apps(&self) {
self.platform.hide_other_apps();
}
pub fn unhide_other_apps(&self) {
self.platform.unhide_other_apps();
}
pub fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
self.platform.displays()
}
pub fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>> {
self.platform.primary_display()
}
pub fn is_screen_capture_supported(&self) -> bool {
self.platform.is_screen_capture_supported()
}
pub fn screen_capture_sources(
&self,
) -> oneshot::Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>> {
self.platform.screen_capture_sources()
}
pub fn find_display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
self.displays()
.iter()
.find(|display| display.id() == id)
.cloned()
}
pub fn window_appearance(&self) -> WindowAppearance {
self.platform.window_appearance()
}
pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
self.platform.read_from_clipboard()
}
pub fn set_text_rendering_mode(&mut self, mode: TextRenderingMode) {
self.text_rendering_mode.set(mode);
}
pub fn text_rendering_mode(&self) -> TextRenderingMode {
self.text_rendering_mode.get()
}
pub fn write_to_clipboard(&self, item: ClipboardItem) {
self.platform.write_to_clipboard(item)
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
pub fn read_from_primary(&self) -> Option<ClipboardItem> {
self.platform.read_from_primary()
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
pub fn write_to_primary(&self, item: ClipboardItem) {
self.platform.write_to_primary(item)
}
#[cfg(target_os = "macos")]
pub fn read_from_find_pasteboard(&self) -> Option<ClipboardItem> {
self.platform.read_from_find_pasteboard()
}
#[cfg(target_os = "macos")]
pub fn write_to_find_pasteboard(&self, item: ClipboardItem) {
self.platform.write_to_find_pasteboard(item)
}
pub fn write_credentials(
&self,
url: &str,
username: &str,
password: &[u8],
) -> Task<Result<()>> {
self.platform.write_credentials(url, username, password)
}
pub fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> {
self.platform.read_credentials(url)
}
pub fn delete_credentials(&self, url: &str) -> Task<Result<()>> {
self.platform.delete_credentials(url)
}
pub fn open_url(&self, url: &str) {
self.platform.open_url(url);
}
pub fn register_url_scheme(&self, scheme: &str) -> Task<Result<()>> {
self.platform.register_url_scheme(scheme)
}
pub fn app_path(&self) -> Result<PathBuf> {
self.platform.app_path()
}
pub fn compositor_name(&self) -> &'static str {
self.platform.compositor_name()
}
pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
self.platform.path_for_auxiliary_executable(name)
}
pub fn prompt_for_paths(
&self,
options: PathPromptOptions,
) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>> {
self.platform.prompt_for_paths(options)
}
pub fn prompt_for_new_path(
&self,
directory: &Path,
suggested_name: Option<&str>,
) -> oneshot::Receiver<Result<Option<PathBuf>>> {
self.platform.prompt_for_new_path(directory, suggested_name)
}
pub fn reveal_path(&self, path: &Path) {
self.platform.reveal_path(path)
}
pub fn open_with_system(&self, path: &Path) {
self.platform.open_with_system(path)
}
pub fn should_auto_hide_scrollbars(&self) -> bool {
self.platform.should_auto_hide_scrollbars()
}
pub fn restart(&mut self) {
self.restart_observers
.clone()
.retain(&(), |observer| observer(self));
self.platform.restart(self.restart_path.take())
}
pub fn set_restart_path(&mut self, path: PathBuf) {
self.restart_path = Some(path);
}
pub fn http_client(&self) -> Arc<dyn HttpClient> {
self.http_client.clone()
}
pub fn set_http_client(&mut self, new_client: Arc<dyn HttpClient>) {
self.http_client = new_client;
}
pub fn set_quit_mode(&mut self, mode: QuitMode) {
self.quit_mode = mode;
}
pub fn svg_renderer(&self) -> SvgRenderer {
self.svg_renderer.clone()
}
pub(crate) fn push_effect(&mut self, effect: Effect) {
match &effect {
Effect::Notify { emitter } => {
if !self.pending_notifications.insert(*emitter) {
return;
}
}
Effect::NotifyGlobalObservers { global_type } => {
if !self.pending_global_notifications.insert(*global_type) {
return;
}
}
_ => {}
};
self.pending_effects.push_back(effect);
}
fn flush_effects(&mut self) {
loop {
self.release_dropped_entities();
self.release_dropped_focus_handles();
if let Some(effect) = self.pending_effects.pop_front() {
match effect {
Effect::Notify { emitter } => {
self.apply_notify_effect(emitter);
}
Effect::Emit {
emitter,
event_type,
event,
} => self.apply_emit_effect(emitter, event_type, event),
Effect::RefreshWindows => {
self.apply_refresh_effect();
}
Effect::NotifyGlobalObservers { global_type } => {
self.apply_notify_global_observers_effect(global_type);
}
Effect::Defer { callback } => {
self.apply_defer_effect(callback);
}
Effect::EntityCreated {
entity,
tid,
window,
} => {
self.apply_entity_created_effect(entity, tid, window);
}
}
} else {
#[cfg(any(test, feature = "test-support"))]
for window in self
.windows
.values()
.filter_map(|window| {
let window = window.as_deref()?;
window.invalidator.is_dirty().then_some(window.handle)
})
.collect::<Vec<_>>()
{
self.update_window(window, |_, window, cx| window.draw(cx).clear())
.unwrap();
}
if self.pending_effects.is_empty() {
break;
}
}
}
}
fn release_dropped_entities(&mut self) {
loop {
let dropped = self.entities.take_dropped();
if dropped.is_empty() {
break;
}
for (entity_id, mut entity) in dropped {
self.observers.remove(&entity_id);
self.event_listeners.remove(&entity_id);
for release_callback in self.release_listeners.remove(&entity_id) {
release_callback(entity.as_mut(), self);
}
}
}
}
fn release_dropped_focus_handles(&mut self) {
self.focus_handles
.clone()
.write()
.retain(|handle_id, focus| {
if focus.ref_count.load(SeqCst) == 0 {
for window_handle in self.windows() {
window_handle
.update(self, |_, window, _| {
if window.focus == Some(handle_id) {
window.blur();
}
})
.unwrap();
}
false
} else {
true
}
});
}
fn apply_notify_effect(&mut self, emitter: EntityId) {
self.pending_notifications.remove(&emitter);
self.observers
.clone()
.retain(&emitter, |handler| handler(self));
}
fn apply_emit_effect(&mut self, emitter: EntityId, event_type: TypeId, event: Box<dyn Any>) {
self.event_listeners
.clone()
.retain(&emitter, |(stored_type, handler)| {
if *stored_type == event_type {
handler(event.as_ref(), self)
} else {
true
}
});
}
fn apply_refresh_effect(&mut self) {
for window in self.windows.values_mut() {
if let Some(window) = window.as_deref_mut() {
window.refreshing = true;
window.invalidator.set_dirty(true);
}
}
}
fn apply_notify_global_observers_effect(&mut self, type_id: TypeId) {
self.pending_global_notifications.remove(&type_id);
self.global_observers
.clone()
.retain(&type_id, |observer| observer(self));
}
fn apply_defer_effect(&mut self, callback: Box<dyn FnOnce(&mut Self) + 'static>) {
callback(self);
}
fn apply_entity_created_effect(
&mut self,
entity: AnyEntity,
tid: TypeId,
window: Option<WindowId>,
) {
self.new_entity_observers.clone().retain(&tid, |observer| {
if let Some(id) = window {
self.update_window_id(id, {
let entity = entity.clone();
|_, window, cx| (observer)(entity, &mut Some(window), cx)
})
.expect("All windows should be off the stack when flushing effects");
} else {
(observer)(entity.clone(), &mut None, self)
}
true
});
}
fn update_window_id<T, F>(&mut self, id: WindowId, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Window, &mut App) -> T,
{
self.update(|cx| {
let mut window = cx.windows.get_mut(id)?.take()?;
let root_view = window.root.clone().unwrap();
cx.window_update_stack.push(window.handle.id);
let result = update(root_view, &mut window, cx);
cx.window_update_stack.pop();
if window.removed {
cx.window_handles.remove(&id);
cx.windows.remove(id);
cx.window_closed_observers.clone().retain(&(), |callback| {
callback(cx);
true
});
let quit_on_empty = match cx.quit_mode {
QuitMode::Explicit => false,
QuitMode::LastWindowClosed => true,
QuitMode::Default => cfg!(not(target_os = "macos")),
};
if quit_on_empty && cx.windows.is_empty() {
cx.quit();
}
} else {
cx.windows.get_mut(id)?.replace(window);
}
Some(result)
})
.context("window not found")
}
pub fn to_async(&self) -> AsyncApp {
AsyncApp {
app: self.this.clone(),
background_executor: self.background_executor.clone(),
foreground_executor: self.foreground_executor.clone(),
}
}
pub fn background_executor(&self) -> &BackgroundExecutor {
&self.background_executor
}
pub fn foreground_executor(&self) -> &ForegroundExecutor {
if self.quitting {
panic!("Can't spawn on main thread after on_app_quit")
};
&self.foreground_executor
}
#[track_caller]
pub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R>
where
AsyncFn: AsyncFnOnce(&mut AsyncApp) -> R + 'static,
R: 'static,
{
if self.quitting {
debug_panic!("Can't spawn on main thread after on_app_quit")
};
let mut cx = self.to_async();
self.foreground_executor
.spawn(async move { f(&mut cx).await })
}
pub fn spawn_with_priority<AsyncFn, R>(&self, priority: Priority, f: AsyncFn) -> Task<R>
where
AsyncFn: AsyncFnOnce(&mut AsyncApp) -> R + 'static,
R: 'static,
{
if self.quitting {
debug_panic!("Can't spawn on main thread after on_app_quit")
};
let mut cx = self.to_async();
self.foreground_executor
.spawn_with_priority(priority, async move { f(&mut cx).await })
}
pub fn defer(&mut self, f: impl FnOnce(&mut App) + 'static) {
self.push_effect(Effect::Defer {
callback: Box::new(f),
});
}
pub fn asset_source(&self) -> &Arc<dyn AssetSource> {
&self.asset_source
}
pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system
}
pub fn has_global<G: Global>(&self) -> bool {
self.globals_by_type.contains_key(&TypeId::of::<G>())
}
#[track_caller]
pub fn global<G: Global>(&self) -> &G {
self.globals_by_type
.get(&TypeId::of::<G>())
.map(|any_state| any_state.downcast_ref::<G>().unwrap())
.with_context(|| format!("no state of type {} exists", type_name::<G>()))
.unwrap()
}
pub fn try_global<G: Global>(&self) -> Option<&G> {
self.globals_by_type
.get(&TypeId::of::<G>())
.map(|any_state| any_state.downcast_ref::<G>().unwrap())
}
#[track_caller]
pub fn global_mut<G: Global>(&mut self) -> &mut G {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type
.get_mut(&global_type)
.and_then(|any_state| any_state.downcast_mut::<G>())
.with_context(|| format!("no state of type {} exists", type_name::<G>()))
.unwrap()
}
pub fn default_global<G: Global + Default>(&mut self) -> &mut G {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type
.entry(global_type)
.or_insert_with(|| Box::<G>::default())
.downcast_mut::<G>()
.unwrap()
}
pub fn set_global<G: Global>(&mut self, global: G) {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type.insert(global_type, Box::new(global));
}
#[cfg(any(test, feature = "test-support"))]
pub fn clear_globals(&mut self) {
self.globals_by_type.drain();
}
pub fn remove_global<G: Global>(&mut self) -> G {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
*self
.globals_by_type
.remove(&global_type)
.unwrap_or_else(|| panic!("no global added for {}", std::any::type_name::<G>()))
.downcast()
.unwrap()
}
pub fn observe_global<G: Global>(
&mut self,
mut f: impl FnMut(&mut Self) + 'static,
) -> Subscription {
let (subscription, activate) = self.global_observers.insert(
TypeId::of::<G>(),
Box::new(move |cx| {
f(cx);
true
}),
);
self.defer(move |_| activate());
subscription
}
#[track_caller]
pub(crate) fn lease_global<G: Global>(&mut self) -> GlobalLease<G> {
GlobalLease::new(
self.globals_by_type
.remove(&TypeId::of::<G>())
.with_context(|| format!("no global registered of type {}", type_name::<G>()))
.unwrap(),
)
}
pub(crate) fn end_global_lease<G: Global>(&mut self, lease: GlobalLease<G>) {
let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type.insert(global_type, lease.global);
}
pub(crate) fn new_entity_observer(
&self,
key: TypeId,
value: NewEntityListener,
) -> Subscription {
let (subscription, activate) = self.new_entity_observers.insert(key, value);
activate();
subscription
}
pub fn observe_new<T: 'static>(
&self,
on_new: impl 'static + Fn(&mut T, Option<&mut Window>, &mut Context<T>),
) -> Subscription {
self.new_entity_observer(
TypeId::of::<T>(),
Box::new(
move |any_entity: AnyEntity, window: &mut Option<&mut Window>, cx: &mut App| {
any_entity
.downcast::<T>()
.unwrap()
.update(cx, |entity_state, cx| {
on_new(entity_state, window.as_deref_mut(), cx)
})
},
),
)
}
pub fn observe_release<T>(
&self,
handle: &Entity<T>,
on_release: impl FnOnce(&mut T, &mut App) + 'static,
) -> Subscription
where
T: 'static,
{
let (subscription, activate) = self.release_listeners.insert(
handle.entity_id(),
Box::new(move |entity, cx| {
let entity = entity.downcast_mut().expect("invalid entity type");
on_release(entity, cx)
}),
);
activate();
subscription
}
pub fn observe_release_in<T>(
&self,
handle: &Entity<T>,
window: &Window,
on_release: impl FnOnce(&mut T, &mut Window, &mut App) + 'static,
) -> Subscription
where
T: 'static,
{
let window_handle = window.handle;
self.observe_release(handle, move |entity, cx| {
let _ = window_handle.update(cx, |_, window, cx| on_release(entity, window, cx));
})
}
pub fn observe_keystrokes(
&mut self,
mut f: impl FnMut(&KeystrokeEvent, &mut Window, &mut App) + 'static,
) -> Subscription {
fn inner(
keystroke_observers: &SubscriberSet<(), KeystrokeObserver>,
handler: KeystrokeObserver,
) -> Subscription {
let (subscription, activate) = keystroke_observers.insert((), handler);
activate();
subscription
}
inner(
&self.keystroke_observers,
Box::new(move |event, window, cx| {
f(event, window, cx);
true
}),
)
}
pub fn intercept_keystrokes(
&mut self,
mut f: impl FnMut(&KeystrokeEvent, &mut Window, &mut App) + 'static,
) -> Subscription {
fn inner(
keystroke_interceptors: &SubscriberSet<(), KeystrokeObserver>,
handler: KeystrokeObserver,
) -> Subscription {
let (subscription, activate) = keystroke_interceptors.insert((), handler);
activate();
subscription
}
inner(
&self.keystroke_interceptors,
Box::new(move |event, window, cx| {
f(event, window, cx);
true
}),
)
}
pub fn bind_keys(&mut self, bindings: impl IntoIterator<Item = KeyBinding>) {
self.keymap.borrow_mut().add_bindings(bindings);
self.pending_effects.push_back(Effect::RefreshWindows);
}
pub fn clear_key_bindings(&mut self) {
self.keymap.borrow_mut().clear();
self.pending_effects.push_back(Effect::RefreshWindows);
}
pub fn key_bindings(&self) -> Rc<RefCell<Keymap>> {
self.keymap.clone()
}
pub fn on_action<A: Action>(
&mut self,
listener: impl Fn(&A, &mut Self) + 'static,
) -> &mut Self {
self.global_action_listeners
.entry(TypeId::of::<A>())
.or_default()
.push(Rc::new(move |action, phase, cx| {
if phase == DispatchPhase::Bubble {
let action = action.downcast_ref().unwrap();
listener(action, cx)
}
}));
self
}
pub fn stop_propagation(&mut self) {
self.propagate_event = false;
}
pub fn propagate(&mut self) {
self.propagate_event = true;
}
pub fn build_action(
&self,
name: &str,
data: Option<serde_json::Value>,
) -> std::result::Result<Box<dyn Action>, ActionBuildError> {
self.actions.build_action(name, data)
}
pub fn all_action_names(&self) -> &[&'static str] {
self.actions.all_action_names()
}
pub fn all_bindings_for_input(&self, input: &[Keystroke]) -> Vec<KeyBinding> {
RefCell::borrow(&self.keymap).all_bindings_for_input(input)
}
pub fn action_schemas(
&self,
generator: &mut schemars::SchemaGenerator,
) -> Vec<(&'static str, Option<schemars::Schema>)> {
self.actions.action_schemas(generator)
}
pub fn deprecated_actions_to_preferred_actions(&self) -> &HashMap<&'static str, &'static str> {
self.actions.deprecated_aliases()
}
pub fn action_deprecation_messages(&self) -> &HashMap<&'static str, &'static str> {
self.actions.deprecation_messages()
}
pub fn action_documentation(&self) -> &HashMap<&'static str, &'static str> {
self.actions.documentation()
}
pub fn on_app_quit<Fut>(
&self,
mut on_quit: impl FnMut(&mut App) -> Fut + 'static,
) -> Subscription
where
Fut: 'static + Future<Output = ()>,
{
let (subscription, activate) = self.quit_observers.insert(
(),
Box::new(move |cx| {
let future = on_quit(cx);
future.boxed_local()
}),
);
activate();
subscription
}
pub fn on_app_restart(&self, mut on_restart: impl 'static + FnMut(&mut App)) -> Subscription {
let (subscription, activate) = self.restart_observers.insert(
(),
Box::new(move |cx| {
on_restart(cx);
true
}),
);
activate();
subscription
}
pub fn on_window_closed(&self, mut on_closed: impl FnMut(&mut App) + 'static) -> Subscription {
let (subscription, activate) = self.window_closed_observers.insert((), Box::new(on_closed));
activate();
subscription
}
pub(crate) fn clear_pending_keystrokes(&mut self) {
for window in self.windows() {
window
.update(self, |_, window, cx| {
if window.pending_input_keystrokes().is_some() {
window.clear_pending_keystrokes();
window.pending_input_changed(cx);
}
})
.ok();
}
}
pub fn is_action_available(&mut self, action: &dyn Action) -> bool {
let mut action_available = false;
if let Some(window) = self.active_window()
&& let Ok(window_action_available) =
window.update(self, |_, window, cx| window.is_action_available(action, cx))
{
action_available = window_action_available;
}
action_available
|| self
.global_action_listeners
.contains_key(&action.as_any().type_id())
}
pub fn set_menus(&self, menus: Vec<Menu>) {
self.platform.set_menus(menus, &self.keymap.borrow());
}
pub fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
self.platform.get_menus()
}
pub fn set_dock_menu(&self, menus: Vec<MenuItem>) {
self.platform.set_dock_menu(menus, &self.keymap.borrow())
}
pub fn perform_dock_menu_action(&self, action: usize) {
self.platform.perform_dock_menu_action(action);
}
pub fn add_recent_document(&self, path: &Path) {
self.platform.add_recent_document(path);
}
pub fn update_jump_list(
&self,
menus: Vec<MenuItem>,
entries: Vec<SmallVec<[PathBuf; 2]>>,
) -> Vec<SmallVec<[PathBuf; 2]>> {
self.platform.update_jump_list(menus, entries)
}
pub fn dispatch_action(&mut self, action: &dyn Action) {
if let Some(active_window) = self.active_window() {
active_window
.update(self, |_, window, cx| {
window.dispatch_action(action.boxed_clone(), cx)
})
.log_err();
} else {
self.dispatch_global_action(action);
}
}
fn dispatch_global_action(&mut self, action: &dyn Action) {
self.propagate_event = true;
if let Some(mut global_listeners) = self
.global_action_listeners
.remove(&action.as_any().type_id())
{
for listener in &global_listeners {
listener(action.as_any(), DispatchPhase::Capture, self);
if !self.propagate_event {
break;
}
}
global_listeners.extend(
self.global_action_listeners
.remove(&action.as_any().type_id())
.unwrap_or_default(),
);
self.global_action_listeners
.insert(action.as_any().type_id(), global_listeners);
}
if self.propagate_event
&& let Some(mut global_listeners) = self
.global_action_listeners
.remove(&action.as_any().type_id())
{
for listener in global_listeners.iter().rev() {
listener(action.as_any(), DispatchPhase::Bubble, self);
if !self.propagate_event {
break;
}
}
global_listeners.extend(
self.global_action_listeners
.remove(&action.as_any().type_id())
.unwrap_or_default(),
);
self.global_action_listeners
.insert(action.as_any().type_id(), global_listeners);
}
}
pub fn has_active_drag(&self) -> bool {
self.active_drag.is_some()
}
pub fn active_drag_cursor_style(&self) -> Option<CursorStyle> {
self.active_drag.as_ref().and_then(|drag| drag.cursor_style)
}
pub fn stop_active_drag(&mut self, window: &mut Window) -> bool {
if self.active_drag.is_some() {
self.active_drag = None;
window.refresh();
true
} else {
false
}
}
pub fn set_active_drag_cursor_style(
&mut self,
cursor_style: CursorStyle,
window: &mut Window,
) -> bool {
if let Some(ref mut drag) = self.active_drag {
drag.cursor_style = Some(cursor_style);
window.refresh();
true
} else {
false
}
}
pub fn set_prompt_builder(
&mut self,
renderer: impl Fn(
PromptLevel,
&str,
Option<&str>,
&[PromptButton],
PromptHandle,
&mut Window,
&mut App,
) -> RenderablePromptHandle
+ 'static,
) {
self.prompt_builder = Some(PromptBuilder::Custom(Box::new(renderer)));
}
pub fn reset_prompt_builder(&mut self) {
self.prompt_builder = Some(PromptBuilder::Default);
}
pub fn remove_asset<A: Asset>(&mut self, source: &A::Source) {
let asset_id = (TypeId::of::<A>(), hash(source));
self.loading_assets.remove(&asset_id);
}
pub fn fetch_asset<A: Asset>(&mut self, source: &A::Source) -> (Shared<Task<A::Output>>, bool) {
let asset_id = (TypeId::of::<A>(), hash(source));
let mut is_first = false;
let task = self
.loading_assets
.remove(&asset_id)
.map(|boxed_task| *boxed_task.downcast::<Shared<Task<A::Output>>>().unwrap())
.unwrap_or_else(|| {
is_first = true;
let future = A::load(source.clone(), self);
self.background_executor().spawn(future).shared()
});
self.loading_assets.insert(asset_id, Box::new(task.clone()));
(task, is_first)
}
#[track_caller]
pub fn focus_handle(&self) -> FocusHandle {
FocusHandle::new(&self.focus_handles)
}
pub fn notify(&mut self, entity_id: EntityId) {
let window_invalidators = mem::take(
self.window_invalidators_by_entity
.entry(entity_id)
.or_default(),
);
if window_invalidators.is_empty() {
if self.pending_notifications.insert(entity_id) {
self.pending_effects
.push_back(Effect::Notify { emitter: entity_id });
}
} else {
for invalidator in window_invalidators.values() {
invalidator.invalidate_view(entity_id, self);
}
}
self.window_invalidators_by_entity
.insert(entity_id, window_invalidators);
}
#[cfg(any(test, feature = "test-support", debug_assertions))]
pub fn get_name(&self) -> Option<&'static str> {
self.name
}
pub fn can_select_mixed_files_and_dirs(&self) -> bool {
self.platform.can_select_mixed_files_and_dirs()
}
pub fn drop_image(&mut self, image: Arc<RenderImage>, current_window: Option<&mut Window>) {
for window in self.windows.values_mut().flatten() {
_ = window.drop_image(image.clone());
}
if let Some(window) = current_window {
_ = window.drop_image(image);
}
}
#[cfg(any(feature = "inspector", debug_assertions))]
pub fn set_inspector_renderer(&mut self, f: crate::InspectorRenderer) {
self.inspector_renderer = Some(f);
}
#[cfg(any(feature = "inspector", debug_assertions))]
pub fn register_inspector_element<T: 'static, R: crate::IntoElement>(
&mut self,
f: impl 'static + Fn(crate::InspectorElementId, &T, &mut Window, &mut App) -> R,
) {
self.inspector_element_registry.register(f);
}
pub fn init_colors(&mut self) {
self.set_global(GlobalColors(Arc::new(Colors::default())));
}
}
impl AppContext for App {
type Result<T> = T;
fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T> {
self.update(|cx| {
let slot = cx.entities.reserve();
let handle = slot.clone();
let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
cx.push_effect(Effect::EntityCreated {
entity: handle.clone().into_any(),
tid: TypeId::of::<T>(),
window: cx.window_update_stack.last().cloned(),
});
cx.entities.insert(slot, entity);
handle
})
}
fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>> {
Reservation(self.entities.reserve())
}
fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_entity: impl FnOnce(&mut Context<T>) -> T,
) -> Self::Result<Entity<T>> {
self.update(|cx| {
let slot = reservation.0;
let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
cx.entities.insert(slot, entity)
})
}
fn update_entity<T: 'static, R>(
&mut self,
handle: &Entity<T>,
update: impl FnOnce(&mut T, &mut Context<T>) -> R,
) -> R {
self.update(|cx| {
let mut entity = cx.entities.lease(handle);
let result = update(
&mut entity,
&mut Context::new_context(cx, handle.downgrade()),
);
cx.entities.end_lease(entity);
result
})
}
fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
where
T: 'static,
{
GpuiBorrow::new(handle.clone(), self)
}
fn read_entity<T, R>(
&self,
handle: &Entity<T>,
read: impl FnOnce(&T, &App) -> R,
) -> Self::Result<R>
where
T: 'static,
{
let entity = self.entities.read(handle);
read(entity, self)
}
fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Window, &mut App) -> T,
{
self.update_window_id(handle.id, update)
}
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(Entity<T>, &App) -> R,
) -> Result<R>
where
T: 'static,
{
let window = self
.windows
.get(window.id)
.context("window not found")?
.as_deref()
.expect("attempted to read a window that is already on the stack");
let root_view = window.root.clone().unwrap();
let view = root_view
.downcast::<T>()
.map_err(|_| anyhow!("root view's type has changed"))?;
Ok(read(view, self))
}
fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
where
R: Send + 'static,
{
self.background_executor.spawn(future)
}
fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
where
G: Global,
{
let mut g = self.global::<G>();
callback(g, self)
}
}
pub(crate) enum Effect {
Notify {
emitter: EntityId,
},
Emit {
emitter: EntityId,
event_type: TypeId,
event: Box<dyn Any>,
},
RefreshWindows,
NotifyGlobalObservers {
global_type: TypeId,
},
Defer {
callback: Box<dyn FnOnce(&mut App) + 'static>,
},
EntityCreated {
entity: AnyEntity,
tid: TypeId,
window: Option<WindowId>,
},
}
impl std::fmt::Debug for Effect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Effect::Notify { emitter } => write!(f, "Notify({})", emitter),
Effect::Emit { emitter, .. } => write!(f, "Emit({:?})", emitter),
Effect::RefreshWindows => write!(f, "RefreshWindows"),
Effect::NotifyGlobalObservers { global_type } => {
write!(f, "NotifyGlobalObservers({:?})", global_type)
}
Effect::Defer { .. } => write!(f, "Defer(..)"),
Effect::EntityCreated { entity, .. } => write!(f, "EntityCreated({:?})", entity),
}
}
}
pub(crate) struct GlobalLease<G: Global> {
global: Box<dyn Any>,
global_type: PhantomData<G>,
}
impl<G: Global> GlobalLease<G> {
fn new(global: Box<dyn Any>) -> Self {
GlobalLease {
global,
global_type: PhantomData,
}
}
}
impl<G: Global> Deref for GlobalLease<G> {
type Target = G;
fn deref(&self) -> &Self::Target {
self.global.downcast_ref().unwrap()
}
}
impl<G: Global> DerefMut for GlobalLease<G> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.global.downcast_mut().unwrap()
}
}
pub struct AnyDrag {
pub view: AnyView,
pub value: Arc<dyn Any>,
pub cursor_offset: Point<Pixels>,
pub cursor_style: Option<CursorStyle>,
}
#[derive(Clone)]
pub struct AnyTooltip {
pub view: AnyView,
pub mouse_position: Point<Pixels>,
pub check_visible_and_update: Rc<dyn Fn(Bounds<Pixels>, &mut Window, &mut App) -> bool>,
}
#[derive(Debug)]
pub struct KeystrokeEvent {
pub keystroke: Keystroke,
pub action: Option<Box<dyn Action>>,
pub context_stack: Vec<KeyContext>,
}
struct NullHttpClient;
impl HttpClient for NullHttpClient {
fn send(
&self,
_req: http_client::Request<http_client::AsyncBody>,
) -> futures::future::BoxFuture<
'static,
anyhow::Result<http_client::Response<http_client::AsyncBody>>,
> {
async move {
anyhow::bail!("No HttpClient available");
}
.boxed()
}
fn user_agent(&self) -> Option<&http_client::http::HeaderValue> {
None
}
fn proxy(&self) -> Option<&Url> {
None
}
}
pub struct GpuiBorrow<'a, T> {
inner: Option<Lease<T>>,
app: &'a mut App,
}
impl<'a, T: 'static> GpuiBorrow<'a, T> {
fn new(inner: Entity<T>, app: &'a mut App) -> Self {
app.start_update();
let lease = app.entities.lease(&inner);
Self {
inner: Some(lease),
app,
}
}
}
impl<'a, T: 'static> std::borrow::Borrow<T> for GpuiBorrow<'a, T> {
fn borrow(&self) -> &T {
self.inner.as_ref().unwrap().borrow()
}
}
impl<'a, T: 'static> std::borrow::BorrowMut<T> for GpuiBorrow<'a, T> {
fn borrow_mut(&mut self) -> &mut T {
self.inner.as_mut().unwrap().borrow_mut()
}
}
impl<'a, T: 'static> std::ops::Deref for GpuiBorrow<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.inner.as_ref().unwrap()
}
}
impl<'a, T: 'static> std::ops::DerefMut for GpuiBorrow<'a, T> {
fn deref_mut(&mut self) -> &mut T {
self.inner.as_mut().unwrap()
}
}
impl<'a, T> Drop for GpuiBorrow<'a, T> {
fn drop(&mut self) {
let lease = self.inner.take().unwrap();
self.app.notify(lease.id);
self.app.entities.end_lease(lease);
self.app.finish_update();
}
}
#[cfg(test)]
mod test {
use std::{cell::RefCell, rc::Rc};
use crate::{AppContext, TestAppContext};
#[test]
fn test_gpui_borrow() {
let cx = TestAppContext::single();
let observation_count = Rc::new(RefCell::new(0));
let state = cx.update(|cx| {
let state = cx.new(|_| false);
cx.observe(&state, {
let observation_count = observation_count.clone();
move |_, _| {
let mut count = observation_count.borrow_mut();
*count += 1;
}
})
.detach();
state
});
cx.update(|cx| {
*std::borrow::BorrowMut::borrow_mut(&mut state.as_mut(cx)) = true;
});
cx.update(|cx| {
state.write(cx, false);
});
assert_eq!(*observation_count.borrow(), 2);
}
}