2022-04-07 23:20:49 +00:00
|
|
|
|
pub mod action;
|
2022-08-09 23:59:13 +00:00
|
|
|
|
mod callback_collection;
|
2022-10-10 21:46:07 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
|
pub mod test_app_context;
|
2022-04-07 23:20:49 +00:00
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
use std::{
|
2022-03-02 02:21:53 +00:00
|
|
|
|
any::{type_name, Any, TypeId},
|
2022-06-23 12:28:10 +00:00
|
|
|
|
cell::RefCell,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fmt::{self, Debug},
|
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
|
marker::PhantomData,
|
2021-08-26 13:00:00 +00:00
|
|
|
|
mem,
|
2022-07-20 23:44:26 +00:00
|
|
|
|
ops::{Deref, DerefMut, Range},
|
2021-05-05 02:04:11 +00:00
|
|
|
|
path::{Path, PathBuf},
|
2021-11-02 19:16:25 +00:00
|
|
|
|
pin::Pin,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
rc::{self, Rc},
|
2022-04-22 16:54:09 +00:00
|
|
|
|
sync::{Arc, Weak},
|
2021-04-20 15:21:29 +00:00
|
|
|
|
time::Duration,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-10-10 21:46:07 +00:00
|
|
|
|
use anyhow::{anyhow, Context, Result};
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
use postage::oneshot;
|
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
use smol::prelude::*;
|
|
|
|
|
|
|
|
|
|
pub use action::*;
|
|
|
|
|
use callback_collection::{CallbackCollection, Mapping};
|
|
|
|
|
use collections::{btree_map, hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque};
|
|
|
|
|
use keymap::MatchResult;
|
|
|
|
|
use platform::Event;
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
|
pub use test_app_context::{ContextHandle, TestAppContext};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
elements::ElementBox,
|
|
|
|
|
executor::{self, Task},
|
|
|
|
|
geometry::rect::RectF,
|
|
|
|
|
keymap::{self, Binding, Keystroke},
|
|
|
|
|
platform::{self, KeyDownEvent, Platform, PromptLevel, WindowOptions},
|
|
|
|
|
presenter::Presenter,
|
|
|
|
|
util::post_inc,
|
2022-10-16 17:46:31 +00:00
|
|
|
|
Appearance, AssetCache, AssetSource, ClipboardItem, FontCache, InputHandler, KeyUpEvent,
|
|
|
|
|
ModifiersChangedEvent, MouseButton, MouseRegionId, PathPromptOptions, TextLayoutCache,
|
2022-10-10 21:46:07 +00:00
|
|
|
|
};
|
2022-08-09 23:59:13 +00:00
|
|
|
|
|
2021-08-23 15:04:32 +00:00
|
|
|
|
pub trait Entity: 'static {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
type Event;
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
|
|
|
|
fn release(&mut self, _: &mut MutableAppContext) {}
|
2021-11-02 19:16:25 +00:00
|
|
|
|
fn app_will_quit(
|
|
|
|
|
&mut self,
|
|
|
|
|
_: &mut MutableAppContext,
|
|
|
|
|
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
|
|
|
|
|
None
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 21:55:27 +00:00
|
|
|
|
pub trait View: Entity + Sized {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn ui_name() -> &'static str;
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox;
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_in(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {}
|
|
|
|
|
fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {}
|
|
|
|
|
fn key_down(&mut self, _: &KeyDownEvent, _: &mut ViewContext<Self>) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
fn key_up(&mut self, _: &KeyUpEvent, _: &mut ViewContext<Self>) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
fn modifiers_changed(&mut self, _: &ModifiersChangedEvent, _: &mut ViewContext<Self>) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
|
|
|
|
|
Self::default_keymap_context()
|
|
|
|
|
}
|
|
|
|
|
fn default_keymap_context() -> keymap::Context {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let mut cx = keymap::Context::default();
|
|
|
|
|
cx.set.insert(Self::ui_name().into());
|
|
|
|
|
cx
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-04-12 02:51:46 +00:00
|
|
|
|
fn debug_json(&self, _: &AppContext) -> serde_json::Value {
|
|
|
|
|
serde_json::Value::Null
|
|
|
|
|
}
|
2022-07-20 23:44:26 +00:00
|
|
|
|
|
|
|
|
|
fn text_for_range(&self, _: Range<usize>, _: &AppContext) -> Option<String> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
fn selected_text_range(&self, _: &AppContext) -> Option<Range<usize>> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
fn marked_text_range(&self, _: &AppContext) -> Option<Range<usize>> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
fn unmark_text(&mut self, _: &mut ViewContext<Self>) {}
|
|
|
|
|
fn replace_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
_: Option<Range<usize>>,
|
|
|
|
|
_: &str,
|
|
|
|
|
_: &mut ViewContext<Self>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
fn replace_and_mark_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
_: Option<Range<usize>>,
|
|
|
|
|
_: &str,
|
|
|
|
|
_: Option<Range<usize>>,
|
|
|
|
|
_: &mut ViewContext<Self>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
pub trait ReadModel {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub trait ReadModelWith {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn read_model_with<E: Entity, T>(
|
2021-05-12 21:16:49 +00:00
|
|
|
|
&self,
|
|
|
|
|
handle: &ModelHandle<E>,
|
2021-10-01 21:23:35 +00:00
|
|
|
|
read: &mut dyn FnMut(&E, &AppContext) -> T,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
) -> T;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub trait UpdateModel {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<T: Entity, O>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
|
|
|
|
|
) -> O;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
pub trait UpgradeModelHandle {
|
2022-02-11 13:41:19 +00:00
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
|
|
|
|
handle: &WeakModelHandle<T>,
|
|
|
|
|
) -> Option<ModelHandle<T>>;
|
2022-02-16 01:56:50 +00:00
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool;
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle>;
|
2022-02-11 13:41:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait UpgradeViewHandle {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>>;
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle>;
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
pub trait ReadView {
|
|
|
|
|
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub trait ReadViewWith {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn read_view_with<V, T>(
|
|
|
|
|
&self,
|
|
|
|
|
handle: &ViewHandle<V>,
|
|
|
|
|
read: &mut dyn FnMut(&V, &AppContext) -> T,
|
|
|
|
|
) -> T
|
2021-05-12 21:16:49 +00:00
|
|
|
|
where
|
2021-10-01 21:23:35 +00:00
|
|
|
|
V: View;
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub trait UpdateView {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_view<T, S>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ViewHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
|
) -> S
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-10-01 21:23:35 +00:00
|
|
|
|
T: View;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 22:56:21 +00:00
|
|
|
|
pub struct Menu<'a> {
|
|
|
|
|
pub name: &'a str,
|
2021-04-12 21:09:49 +00:00
|
|
|
|
pub items: Vec<MenuItem<'a>>,
|
2021-04-08 22:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum MenuItem<'a> {
|
2022-06-17 00:47:39 +00:00
|
|
|
|
Separator,
|
|
|
|
|
Submenu(Menu<'a>),
|
2021-04-08 22:56:21 +00:00
|
|
|
|
Action {
|
|
|
|
|
name: &'a str,
|
2022-04-07 23:20:49 +00:00
|
|
|
|
action: Box<dyn Action>,
|
2021-04-08 22:56:21 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct App(Rc<RefCell<MutableAppContext>>);
|
|
|
|
|
|
2021-07-09 23:27:33 +00:00
|
|
|
|
#[derive(Clone)]
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub struct AsyncAppContext(Rc<RefCell<MutableAppContext>>);
|
|
|
|
|
|
2022-07-20 23:44:26 +00:00
|
|
|
|
pub struct WindowInputHandler {
|
|
|
|
|
app: Rc<RefCell<MutableAppContext>>,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl App {
|
2021-03-19 03:33:16 +00:00
|
|
|
|
pub fn new(asset_source: impl AssetSource) -> Result<Self> {
|
2021-04-09 22:58:19 +00:00
|
|
|
|
let platform = platform::current::platform();
|
2021-06-07 23:42:49 +00:00
|
|
|
|
let foreground_platform = platform::current::foreground_platform();
|
2021-03-18 23:54:35 +00:00
|
|
|
|
let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
|
|
|
|
|
let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
|
2021-03-19 03:33:16 +00:00
|
|
|
|
foreground,
|
2021-07-08 19:03:00 +00:00
|
|
|
|
Arc::new(executor::Background::new()),
|
2021-04-12 21:25:00 +00:00
|
|
|
|
platform.clone(),
|
2021-06-07 23:42:49 +00:00
|
|
|
|
foreground_platform.clone(),
|
2021-07-29 14:53:10 +00:00
|
|
|
|
Arc::new(FontCache::new(platform.fonts())),
|
2022-03-01 02:19:30 +00:00
|
|
|
|
Default::default(),
|
2021-03-19 03:33:16 +00:00
|
|
|
|
asset_source,
|
2021-03-18 23:54:35 +00:00
|
|
|
|
))));
|
2021-04-12 21:25:00 +00:00
|
|
|
|
|
2021-11-02 19:16:25 +00:00
|
|
|
|
foreground_platform.on_quit(Box::new({
|
|
|
|
|
let cx = app.0.clone();
|
|
|
|
|
move || {
|
|
|
|
|
cx.borrow_mut().quit();
|
|
|
|
|
}
|
|
|
|
|
}));
|
2022-05-20 17:04:43 +00:00
|
|
|
|
foreground_platform.on_will_open_menu(Box::new({
|
|
|
|
|
let cx = app.0.clone();
|
|
|
|
|
move || {
|
|
|
|
|
let mut cx = cx.borrow_mut();
|
|
|
|
|
cx.keystroke_matcher.clear_pending();
|
|
|
|
|
}
|
|
|
|
|
}));
|
2022-05-19 23:24:02 +00:00
|
|
|
|
foreground_platform.on_validate_menu_command(Box::new({
|
|
|
|
|
let cx = app.0.clone();
|
|
|
|
|
move |action| {
|
|
|
|
|
let cx = cx.borrow_mut();
|
2022-05-20 17:04:43 +00:00
|
|
|
|
!cx.keystroke_matcher.has_pending_keystrokes() && cx.is_action_available(action)
|
2022-05-19 23:24:02 +00:00
|
|
|
|
}
|
|
|
|
|
}));
|
2021-11-02 19:16:25 +00:00
|
|
|
|
foreground_platform.on_menu_command(Box::new({
|
|
|
|
|
let cx = app.0.clone();
|
|
|
|
|
move |action| {
|
|
|
|
|
let mut cx = cx.borrow_mut();
|
|
|
|
|
if let Some(key_window_id) = cx.cx.platform.key_window_id() {
|
2022-08-05 03:55:10 +00:00
|
|
|
|
if let Some(view_id) = cx.focused_view_id(key_window_id) {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
cx.handle_dispatch_action_from_effect(key_window_id, Some(view_id), action);
|
2022-08-05 03:55:10 +00:00
|
|
|
|
return;
|
2021-11-02 19:16:25 +00:00
|
|
|
|
}
|
2021-04-12 22:42:33 +00:00
|
|
|
|
}
|
2022-08-05 03:55:10 +00:00
|
|
|
|
cx.dispatch_global_action_any(action);
|
2021-04-12 22:42:33 +00:00
|
|
|
|
}
|
2021-04-12 21:25:00 +00:00
|
|
|
|
}));
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
app.0.borrow_mut().weak_self = Some(Rc::downgrade(&app.0));
|
|
|
|
|
Ok(app)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 13:55:11 +00:00
|
|
|
|
pub fn background(&self) -> Arc<executor::Background> {
|
|
|
|
|
self.0.borrow().background().clone()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 19:38:09 +00:00
|
|
|
|
pub fn on_become_active<F>(self, mut callback: F) -> Self
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut MutableAppContext),
|
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let cx = self.0.clone();
|
2021-04-09 19:38:09 +00:00
|
|
|
|
self.0
|
2021-06-07 23:02:24 +00:00
|
|
|
|
.borrow_mut()
|
2021-06-07 23:42:49 +00:00
|
|
|
|
.foreground_platform
|
2021-05-28 22:25:15 +00:00
|
|
|
|
.on_become_active(Box::new(move || callback(&mut *cx.borrow_mut())));
|
2021-04-09 19:38:09 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn on_resign_active<F>(self, mut callback: F) -> Self
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut MutableAppContext),
|
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let cx = self.0.clone();
|
2021-04-09 19:38:09 +00:00
|
|
|
|
self.0
|
2021-06-07 23:02:24 +00:00
|
|
|
|
.borrow_mut()
|
2021-06-07 23:42:49 +00:00
|
|
|
|
.foreground_platform
|
2021-05-28 22:25:15 +00:00
|
|
|
|
.on_resign_active(Box::new(move || callback(&mut *cx.borrow_mut())));
|
2021-04-09 19:38:09 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
pub fn on_quit<F>(&mut self, mut callback: F) -> &mut Self
|
2021-11-01 18:56:49 +00:00
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut MutableAppContext),
|
|
|
|
|
{
|
|
|
|
|
let cx = self.0.clone();
|
|
|
|
|
self.0
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.foreground_platform
|
|
|
|
|
.on_quit(Box::new(move || callback(&mut *cx.borrow_mut())));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
pub fn on_event<F>(&mut self, mut callback: F) -> &mut Self
|
2021-04-09 19:38:09 +00:00
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
|
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let cx = self.0.clone();
|
2021-06-07 23:02:24 +00:00
|
|
|
|
self.0
|
|
|
|
|
.borrow_mut()
|
2021-06-07 23:42:49 +00:00
|
|
|
|
.foreground_platform
|
2021-06-07 23:02:24 +00:00
|
|
|
|
.on_event(Box::new(move |event| {
|
|
|
|
|
callback(event, &mut *cx.borrow_mut())
|
2021-04-09 19:38:09 +00:00
|
|
|
|
}));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 23:33:56 +00:00
|
|
|
|
pub fn on_open_urls<F>(&mut self, mut callback: F) -> &mut Self
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(Vec<String>, &mut MutableAppContext),
|
|
|
|
|
{
|
|
|
|
|
let cx = self.0.clone();
|
|
|
|
|
self.0
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.foreground_platform
|
|
|
|
|
.on_open_urls(Box::new(move |paths| {
|
|
|
|
|
callback(paths, &mut *cx.borrow_mut())
|
|
|
|
|
}));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 03:33:17 +00:00
|
|
|
|
pub fn run<F>(self, on_finish_launching: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnOnce(&mut MutableAppContext),
|
|
|
|
|
{
|
2021-06-07 23:42:49 +00:00
|
|
|
|
let platform = self.0.borrow().foreground_platform.clone();
|
2021-06-07 23:21:34 +00:00
|
|
|
|
platform.run(Box::new(move || {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let mut cx = self.0.borrow_mut();
|
2021-08-31 00:59:13 +00:00
|
|
|
|
let cx = &mut *cx;
|
|
|
|
|
crate::views::init(cx);
|
|
|
|
|
on_finish_launching(cx);
|
2021-04-10 03:33:17 +00:00
|
|
|
|
}))
|
2021-04-09 19:38:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-04 15:02:20 +00:00
|
|
|
|
pub fn platform(&self) -> Arc<dyn Platform> {
|
|
|
|
|
self.0.borrow().platform()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
pub fn font_cache(&self) -> Arc<FontCache> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.0.borrow().cx.font_cache.clone()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
|
|
|
|
|
let mut state = self.0.borrow_mut();
|
2021-11-29 22:16:19 +00:00
|
|
|
|
let result = state.update(callback);
|
2021-09-30 21:06:09 +00:00
|
|
|
|
state.pending_notifications.clear();
|
2021-04-10 06:05:09 +00:00
|
|
|
|
result
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-04-10 06:05:09 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-07-20 23:44:26 +00:00
|
|
|
|
impl WindowInputHandler {
|
|
|
|
|
fn read_focused_view<T, F>(&self, f: F) -> Option<T>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(&dyn AnyView, &AppContext) -> T,
|
|
|
|
|
{
|
2022-08-17 00:44:58 +00:00
|
|
|
|
// Input-related application hooks are sometimes called by the OS during
|
|
|
|
|
// a call to a window-manipulation API, like prompting the user for file
|
|
|
|
|
// paths. In that case, the AppContext will already be borrowed, so any
|
|
|
|
|
// InputHandler methods need to fail gracefully.
|
|
|
|
|
//
|
|
|
|
|
// See https://github.com/zed-industries/feedback/issues/444
|
|
|
|
|
let app = self.app.try_borrow().ok()?;
|
|
|
|
|
|
2022-07-20 23:44:26 +00:00
|
|
|
|
let view_id = app.focused_view_id(self.window_id)?;
|
|
|
|
|
let view = app.cx.views.get(&(self.window_id, view_id))?;
|
|
|
|
|
let result = f(view.as_ref(), &app);
|
|
|
|
|
Some(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_focused_view<T, F>(&mut self, f: F) -> Option<T>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(usize, usize, &mut dyn AnyView, &mut MutableAppContext) -> T,
|
|
|
|
|
{
|
2022-08-17 00:44:58 +00:00
|
|
|
|
let mut app = self.app.try_borrow_mut().ok()?;
|
2022-07-20 23:44:26 +00:00
|
|
|
|
app.update(|app| {
|
|
|
|
|
let view_id = app.focused_view_id(self.window_id)?;
|
|
|
|
|
let mut view = app.cx.views.remove(&(self.window_id, view_id))?;
|
|
|
|
|
let result = f(self.window_id, view_id, view.as_mut(), &mut *app);
|
|
|
|
|
app.cx.views.insert((self.window_id, view_id), view);
|
|
|
|
|
Some(result)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InputHandler for WindowInputHandler {
|
|
|
|
|
fn text_for_range(&self, range: Range<usize>) -> Option<String> {
|
2022-07-21 20:44:58 +00:00
|
|
|
|
self.read_focused_view(|view, cx| view.text_for_range(range.clone(), cx))
|
|
|
|
|
.flatten()
|
2022-07-20 23:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn selected_text_range(&self) -> Option<Range<usize>> {
|
2022-07-21 20:44:58 +00:00
|
|
|
|
self.read_focused_view(|view, cx| view.selected_text_range(cx))
|
|
|
|
|
.flatten()
|
2022-07-20 23:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_text_in_range(&mut self, range: Option<Range<usize>>, text: &str) {
|
|
|
|
|
self.update_focused_view(|window_id, view_id, view, cx| {
|
|
|
|
|
view.replace_text_in_range(range, text, cx, window_id, view_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn marked_text_range(&self) -> Option<Range<usize>> {
|
2022-07-21 20:44:58 +00:00
|
|
|
|
self.read_focused_view(|view, cx| view.marked_text_range(cx))
|
|
|
|
|
.flatten()
|
2022-07-20 23:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unmark_text(&mut self) {
|
|
|
|
|
self.update_focused_view(|window_id, view_id, view, cx| {
|
|
|
|
|
view.unmark_text(cx, window_id, view_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_and_mark_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
range: Option<Range<usize>>,
|
|
|
|
|
new_text: &str,
|
|
|
|
|
new_selected_range: Option<Range<usize>>,
|
|
|
|
|
) {
|
|
|
|
|
self.update_focused_view(|window_id, view_id, view, cx| {
|
|
|
|
|
view.replace_and_mark_text_in_range(
|
|
|
|
|
range,
|
|
|
|
|
new_text,
|
|
|
|
|
new_selected_range,
|
|
|
|
|
cx,
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-07-21 15:35:40 +00:00
|
|
|
|
|
|
|
|
|
fn rect_for_range(&self, range_utf16: Range<usize>) -> Option<RectF> {
|
|
|
|
|
let app = self.app.borrow();
|
|
|
|
|
let (presenter, _) = app.presenters_and_platform_windows.get(&self.window_id)?;
|
|
|
|
|
let presenter = presenter.borrow();
|
|
|
|
|
presenter.rect_for_text_range(range_utf16, &app)
|
|
|
|
|
}
|
2022-07-20 23:44:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
impl AsyncAppContext {
|
2021-07-09 23:27:33 +00:00
|
|
|
|
pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(AsyncAppContext) -> Fut,
|
|
|
|
|
Fut: 'static + Future<Output = T>,
|
|
|
|
|
T: 'static,
|
|
|
|
|
{
|
|
|
|
|
self.0.borrow().foreground.spawn(f(self.clone()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 15:58:11 +00:00
|
|
|
|
pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
|
2021-05-12 21:16:49 +00:00
|
|
|
|
callback(self.0.borrow().as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.0.borrow_mut().update(callback)
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
|
|
|
|
where
|
|
|
|
|
T: Entity,
|
|
|
|
|
F: FnOnce(&mut ModelContext<T>) -> T,
|
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.update(|cx| cx.add_model(build_model))
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
2021-05-12 23:20:03 +00:00
|
|
|
|
|
2022-04-22 09:45:18 +00:00
|
|
|
|
pub fn add_window<T, F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_options: WindowOptions,
|
|
|
|
|
build_root_view: F,
|
|
|
|
|
) -> (usize, ViewHandle<T>)
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
|
{
|
|
|
|
|
self.update(|cx| cx.add_window(window_options, build_root_view))
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 17:35:24 +00:00
|
|
|
|
pub fn remove_window(&mut self, window_id: usize) {
|
|
|
|
|
self.update(|cx| cx.remove_window(window_id))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 13:02:57 +00:00
|
|
|
|
pub fn activate_window(&mut self, window_id: usize) {
|
|
|
|
|
self.update(|cx| cx.activate_window(window_id))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 12:52:47 +00:00
|
|
|
|
pub fn prompt(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
level: PromptLevel,
|
|
|
|
|
msg: &str,
|
|
|
|
|
answers: &[&str],
|
|
|
|
|
) -> oneshot::Receiver<usize> {
|
|
|
|
|
self.update(|cx| cx.prompt(window_id, level, msg, answers))
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 05:12:04 +00:00
|
|
|
|
pub fn platform(&self) -> Arc<dyn Platform> {
|
|
|
|
|
self.0.borrow().platform()
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 23:27:33 +00:00
|
|
|
|
pub fn foreground(&self) -> Rc<executor::Foreground> {
|
|
|
|
|
self.0.borrow().foreground.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
pub fn background(&self) -> Arc<executor::Background> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.0.borrow().cx.background.clone()
|
2021-05-12 23:20:03 +00:00
|
|
|
|
}
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpdateModel for AsyncAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<E: Entity, O>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<E>,
|
|
|
|
|
update: &mut dyn FnMut(&mut E, &mut ModelContext<E>) -> O,
|
|
|
|
|
) -> O {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.0.borrow_mut().update_model(handle, update)
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl UpgradeModelHandle for AsyncAppContext {
|
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
2022-02-11 13:41:19 +00:00
|
|
|
|
handle: &WeakModelHandle<T>,
|
2021-08-18 17:45:29 +00:00
|
|
|
|
) -> Option<ModelHandle<T>> {
|
2022-02-16 01:56:50 +00:00
|
|
|
|
self.0.borrow().upgrade_model_handle(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.0.borrow().model_handle_is_upgradable(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
self.0.borrow().upgrade_any_model_handle(handle)
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
impl UpgradeViewHandle for AsyncAppContext {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
|
|
|
|
|
self.0.borrow_mut().upgrade_view_handle(handle)
|
|
|
|
|
}
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
self.0.borrow_mut().upgrade_any_view_handle(handle)
|
|
|
|
|
}
|
2022-02-11 13:41:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
impl ReadModelWith for AsyncAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn read_model_with<E: Entity, T>(
|
2021-05-12 21:16:49 +00:00
|
|
|
|
&self,
|
|
|
|
|
handle: &ModelHandle<E>,
|
2021-10-01 21:23:35 +00:00
|
|
|
|
read: &mut dyn FnMut(&E, &AppContext) -> T,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
) -> T {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let cx = self.0.borrow();
|
|
|
|
|
let cx = cx.as_ref();
|
|
|
|
|
read(handle.read(cx), cx)
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpdateView for AsyncAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_view<T, S>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ViewHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
|
) -> S
|
2021-05-12 21:16:49 +00:00
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
{
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.0.borrow_mut().update_view(handle, update)
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ReadViewWith for AsyncAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn read_view_with<V, T>(
|
|
|
|
|
&self,
|
|
|
|
|
handle: &ViewHandle<V>,
|
|
|
|
|
read: &mut dyn FnMut(&V, &AppContext) -> T,
|
|
|
|
|
) -> T
|
2021-05-12 21:16:49 +00:00
|
|
|
|
where
|
|
|
|
|
V: View,
|
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let cx = self.0.borrow();
|
|
|
|
|
let cx = cx.as_ref();
|
|
|
|
|
read(handle.read(cx), cx)
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
type ActionCallback =
|
2022-04-07 23:20:49 +00:00
|
|
|
|
dyn FnMut(&mut dyn AnyView, &dyn Action, &mut MutableAppContext, usize, usize);
|
|
|
|
|
type GlobalActionCallback = dyn FnMut(&dyn Action, &mut MutableAppContext);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
type SubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext) -> bool>;
|
2022-03-11 04:03:01 +00:00
|
|
|
|
type GlobalSubscriptionCallback = Box<dyn FnMut(&dyn Any, &mut MutableAppContext)>;
|
2021-08-23 22:54:24 +00:00
|
|
|
|
type ObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
|
2022-07-06 13:53:40 +00:00
|
|
|
|
type FocusObservationCallback = Box<dyn FnMut(bool, &mut MutableAppContext) -> bool>;
|
2022-05-23 16:23:25 +00:00
|
|
|
|
type GlobalObservationCallback = Box<dyn FnMut(&mut MutableAppContext)>;
|
2022-04-20 10:54:34 +00:00
|
|
|
|
type ReleaseObservationCallback = Box<dyn FnOnce(&dyn Any, &mut MutableAppContext)>;
|
2022-05-30 08:01:23 +00:00
|
|
|
|
type ActionObservationCallback = Box<dyn FnMut(TypeId, &mut MutableAppContext)>;
|
2022-07-05 10:15:40 +00:00
|
|
|
|
type WindowActivationCallback = Box<dyn FnMut(bool, &mut MutableAppContext) -> bool>;
|
2022-08-09 22:52:25 +00:00
|
|
|
|
type WindowFullscreenCallback = Box<dyn FnMut(bool, &mut MutableAppContext) -> bool>;
|
2022-04-08 22:32:56 +00:00
|
|
|
|
type DeserializeActionCallback = fn(json: &str) -> anyhow::Result<Box<dyn Action>>;
|
2022-06-23 09:43:19 +00:00
|
|
|
|
type WindowShouldCloseSubscriptionCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
|
2021-08-23 22:54:24 +00:00
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct MutableAppContext {
|
2021-03-21 04:15:04 +00:00
|
|
|
|
weak_self: Option<rc::Weak<RefCell<Self>>>,
|
2021-06-07 23:42:49 +00:00
|
|
|
|
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
|
2021-03-19 03:33:16 +00:00
|
|
|
|
assets: Arc<AssetCache>,
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx: AppContext,
|
2022-04-13 18:32:39 +00:00
|
|
|
|
action_deserializers: HashMap<&'static str, (TypeId, DeserializeActionCallback)>,
|
2022-02-27 23:14:40 +00:00
|
|
|
|
capture_actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
2021-08-22 17:58:19 +00:00
|
|
|
|
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
2021-12-18 15:12:08 +00:00
|
|
|
|
global_actions: HashMap<TypeId, Box<GlobalActionCallback>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
keystroke_matcher: keymap::Matcher,
|
|
|
|
|
next_entity_id: usize,
|
|
|
|
|
next_window_id: usize,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
next_subscription_id: usize,
|
2022-02-14 22:04:27 +00:00
|
|
|
|
frame_count: usize,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
|
2022-08-09 23:59:13 +00:00
|
|
|
|
focus_observations: CallbackCollection<usize, FocusObservationCallback>,
|
|
|
|
|
global_subscriptions: CallbackCollection<TypeId, GlobalSubscriptionCallback>,
|
|
|
|
|
global_observations: CallbackCollection<TypeId, GlobalObservationCallback>,
|
|
|
|
|
subscriptions: CallbackCollection<usize, SubscriptionCallback>,
|
|
|
|
|
observations: CallbackCollection<usize, ObservationCallback>,
|
|
|
|
|
window_activation_observations: CallbackCollection<usize, WindowActivationCallback>,
|
|
|
|
|
window_fullscreen_observations: CallbackCollection<usize, WindowFullscreenCallback>,
|
2022-08-09 22:52:25 +00:00
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
release_observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>,
|
2022-05-30 08:01:23 +00:00
|
|
|
|
action_dispatch_observations: Arc<Mutex<BTreeMap<usize, ActionObservationCallback>>>,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
#[allow(clippy::type_complexity)]
|
2021-04-12 22:42:33 +00:00
|
|
|
|
presenters_and_platform_windows:
|
|
|
|
|
HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
foreground: Rc<executor::Foreground>,
|
|
|
|
|
pending_effects: VecDeque<Effect>,
|
2021-09-30 21:06:09 +00:00
|
|
|
|
pending_notifications: HashSet<usize>,
|
2022-03-22 23:38:17 +00:00
|
|
|
|
pending_global_notifications: HashSet<TypeId>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pending_flushes: usize,
|
|
|
|
|
flushing_effects: bool,
|
2022-02-25 23:14:16 +00:00
|
|
|
|
halt_action_dispatch: bool,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MutableAppContext {
|
2021-06-07 23:02:24 +00:00
|
|
|
|
fn new(
|
2021-03-19 03:33:16 +00:00
|
|
|
|
foreground: Rc<executor::Foreground>,
|
2021-07-08 19:03:00 +00:00
|
|
|
|
background: Arc<executor::Background>,
|
2021-06-07 23:35:27 +00:00
|
|
|
|
platform: Arc<dyn platform::Platform>,
|
2021-06-07 23:42:49 +00:00
|
|
|
|
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
|
2021-07-29 14:53:10 +00:00
|
|
|
|
font_cache: Arc<FontCache>,
|
2022-03-01 02:19:30 +00:00
|
|
|
|
ref_counts: RefCounts,
|
2021-03-19 03:33:16 +00:00
|
|
|
|
asset_source: impl AssetSource,
|
|
|
|
|
) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
Self {
|
2021-03-21 04:15:04 +00:00
|
|
|
|
weak_self: None,
|
2021-06-07 23:42:49 +00:00
|
|
|
|
foreground_platform,
|
2021-03-19 03:33:16 +00:00
|
|
|
|
assets: Arc::new(AssetCache::new(asset_source)),
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx: AppContext {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
models: Default::default(),
|
2021-05-06 04:48:16 +00:00
|
|
|
|
views: Default::default(),
|
2022-08-05 03:55:10 +00:00
|
|
|
|
parents: Default::default(),
|
2021-04-27 03:24:23 +00:00
|
|
|
|
windows: Default::default(),
|
2022-03-17 13:33:01 +00:00
|
|
|
|
globals: Default::default(),
|
2021-08-27 16:04:21 +00:00
|
|
|
|
element_states: Default::default(),
|
2022-03-01 02:19:30 +00:00
|
|
|
|
ref_counts: Arc::new(Mutex::new(ref_counts)),
|
2021-07-08 19:03:00 +00:00
|
|
|
|
background,
|
2021-07-29 14:53:10 +00:00
|
|
|
|
font_cache,
|
2021-07-16 15:53:23 +00:00
|
|
|
|
platform,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
},
|
2022-08-09 23:59:13 +00:00
|
|
|
|
action_deserializers: Default::default(),
|
|
|
|
|
capture_actions: Default::default(),
|
|
|
|
|
actions: Default::default(),
|
|
|
|
|
global_actions: Default::default(),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
keystroke_matcher: keymap::Matcher::default(),
|
|
|
|
|
next_entity_id: 0,
|
|
|
|
|
next_window_id: 0,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
next_subscription_id: 0,
|
2022-02-14 22:04:27 +00:00
|
|
|
|
frame_count: 0,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
subscriptions: Default::default(),
|
2022-03-11 04:03:01 +00:00
|
|
|
|
global_subscriptions: Default::default(),
|
2021-08-23 22:54:24 +00:00
|
|
|
|
observations: Default::default(),
|
2022-04-14 16:22:32 +00:00
|
|
|
|
focus_observations: Default::default(),
|
2022-01-21 13:22:06 +00:00
|
|
|
|
release_observations: Default::default(),
|
2022-03-22 23:38:17 +00:00
|
|
|
|
global_observations: Default::default(),
|
2022-07-05 10:15:40 +00:00
|
|
|
|
window_activation_observations: Default::default(),
|
2022-08-09 22:52:25 +00:00
|
|
|
|
window_fullscreen_observations: Default::default(),
|
2022-05-30 08:01:23 +00:00
|
|
|
|
action_dispatch_observations: Default::default(),
|
2022-08-09 23:59:13 +00:00
|
|
|
|
presenters_and_platform_windows: Default::default(),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
foreground,
|
|
|
|
|
pending_effects: VecDeque::new(),
|
2022-08-09 23:59:13 +00:00
|
|
|
|
pending_notifications: Default::default(),
|
|
|
|
|
pending_global_notifications: Default::default(),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pending_flushes: 0,
|
|
|
|
|
flushing_effects: false,
|
2022-02-25 23:14:16 +00:00
|
|
|
|
halt_action_dispatch: false,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
|
pub fn upgrade(&self) -> App {
|
|
|
|
|
App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 19:16:25 +00:00
|
|
|
|
pub fn quit(&mut self) {
|
|
|
|
|
let mut futures = Vec::new();
|
|
|
|
|
for model_id in self.cx.models.keys().copied().collect::<Vec<_>>() {
|
|
|
|
|
let mut model = self.cx.models.remove(&model_id).unwrap();
|
|
|
|
|
futures.extend(model.app_will_quit(self));
|
|
|
|
|
self.cx.models.insert(model_id, model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for view_id in self.cx.views.keys().copied().collect::<Vec<_>>() {
|
|
|
|
|
let mut view = self.cx.views.remove(&view_id).unwrap();
|
|
|
|
|
futures.extend(view.app_will_quit(self));
|
|
|
|
|
self.cx.views.insert(view_id, view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.remove_all_windows();
|
|
|
|
|
|
|
|
|
|
let futures = futures::future::join_all(futures);
|
|
|
|
|
if self
|
|
|
|
|
.background
|
|
|
|
|
.block_with_timeout(Duration::from_millis(100), futures)
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
|
|
|
|
log::error!("timed out waiting on app_will_quit");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
pub fn remove_all_windows(&mut self) {
|
2021-11-02 19:16:25 +00:00
|
|
|
|
for (window_id, _) in self.cx.windows.drain() {
|
|
|
|
|
self.presenters_and_platform_windows.remove(&window_id);
|
|
|
|
|
}
|
2022-03-01 09:55:12 +00:00
|
|
|
|
self.flush_effects();
|
2021-11-02 19:16:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 23:35:27 +00:00
|
|
|
|
pub fn platform(&self) -> Arc<dyn platform::Platform> {
|
2021-07-16 15:53:23 +00:00
|
|
|
|
self.cx.platform.clone()
|
2021-04-08 22:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 22:58:19 +00:00
|
|
|
|
pub fn font_cache(&self) -> &Arc<FontCache> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.cx.font_cache
|
2021-04-09 22:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 23:40:43 +00:00
|
|
|
|
pub fn foreground(&self) -> &Rc<executor::Foreground> {
|
2021-04-05 23:45:55 +00:00
|
|
|
|
&self.foreground
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
pub fn background(&self) -> &Arc<executor::Background> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.cx.background
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 02:51:14 +00:00
|
|
|
|
pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
|
2022-03-01 18:16:58 +00:00
|
|
|
|
self.presenters_and_platform_windows
|
2021-04-08 02:51:14 +00:00
|
|
|
|
.get(&window_id)
|
2022-03-01 18:16:58 +00:00
|
|
|
|
.and_then(|(presenter, _)| presenter.borrow().debug_elements(self))
|
2021-04-08 02:51:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
pub fn deserialize_action(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
argument: Option<&str>,
|
|
|
|
|
) -> Result<Box<dyn Action>> {
|
|
|
|
|
let callback = self
|
|
|
|
|
.action_deserializers
|
|
|
|
|
.get(name)
|
2022-04-13 18:32:39 +00:00
|
|
|
|
.ok_or_else(|| anyhow!("unknown action {}", name))?
|
|
|
|
|
.1;
|
2022-04-08 22:32:56 +00:00
|
|
|
|
callback(argument.unwrap_or("{}"))
|
2022-04-09 00:32:58 +00:00
|
|
|
|
.with_context(|| format!("invalid data for action {}", name))
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 23:14:40 +00:00
|
|
|
|
pub fn add_action<A, V, F>(&mut self, handler: F)
|
|
|
|
|
where
|
|
|
|
|
A: Action,
|
|
|
|
|
V: View,
|
|
|
|
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
|
|
|
|
{
|
|
|
|
|
self.add_action_internal(handler, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn capture_action<A, V, F>(&mut self, handler: F)
|
|
|
|
|
where
|
|
|
|
|
A: Action,
|
|
|
|
|
V: View,
|
|
|
|
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
|
|
|
|
{
|
|
|
|
|
self.add_action_internal(handler, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_action_internal<A, V, F>(&mut self, mut handler: F, capture: bool)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-08-22 17:58:19 +00:00
|
|
|
|
A: Action,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
V: View,
|
2021-08-22 17:58:19 +00:00
|
|
|
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
|
|
|
|
let handler = Box::new(
|
|
|
|
|
move |view: &mut dyn AnyView,
|
2022-04-07 23:20:49 +00:00
|
|
|
|
action: &dyn Action,
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx: &mut MutableAppContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize| {
|
2021-08-23 10:00:31 +00:00
|
|
|
|
let action = action.as_any().downcast_ref().unwrap();
|
2021-08-22 17:58:19 +00:00
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
handler(
|
|
|
|
|
view.as_any_mut()
|
|
|
|
|
.downcast_mut()
|
|
|
|
|
.expect("downcast is type safe"),
|
2021-08-23 10:00:31 +00:00
|
|
|
|
action,
|
2021-08-22 17:58:19 +00:00
|
|
|
|
&mut cx,
|
|
|
|
|
);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
self.action_deserializers
|
|
|
|
|
.entry(A::qualified_name())
|
2022-04-13 18:32:39 +00:00
|
|
|
|
.or_insert((TypeId::of::<A>(), A::from_json_str));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = if capture {
|
|
|
|
|
&mut self.capture_actions
|
|
|
|
|
} else {
|
|
|
|
|
&mut self.actions
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
actions
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.entry(TypeId::of::<V>())
|
|
|
|
|
.or_default()
|
2021-08-22 17:58:19 +00:00
|
|
|
|
.entry(TypeId::of::<A>())
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.or_default()
|
|
|
|
|
.push(handler);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 19:37:34 +00:00
|
|
|
|
pub fn add_async_action<A, V, F>(&mut self, mut handler: F)
|
|
|
|
|
where
|
|
|
|
|
A: Action,
|
|
|
|
|
V: View,
|
|
|
|
|
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>) -> Option<Task<Result<()>>>,
|
|
|
|
|
{
|
|
|
|
|
self.add_action(move |view, action, cx| {
|
2022-08-10 21:39:24 +00:00
|
|
|
|
if let Some(task) = handler(view, action, cx) {
|
|
|
|
|
task.detach_and_log_err(cx);
|
|
|
|
|
}
|
2022-02-05 19:37:34 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
|
pub fn add_global_action<A, F>(&mut self, mut handler: F)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-08-22 17:58:19 +00:00
|
|
|
|
A: Action,
|
|
|
|
|
F: 'static + FnMut(&A, &mut MutableAppContext),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
2022-04-07 23:20:49 +00:00
|
|
|
|
let handler = Box::new(move |action: &dyn Action, cx: &mut MutableAppContext| {
|
2021-08-23 10:00:31 +00:00
|
|
|
|
let action = action.as_any().downcast_ref().unwrap();
|
|
|
|
|
handler(action, cx);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
self.action_deserializers
|
|
|
|
|
.entry(A::qualified_name())
|
2022-04-13 18:32:39 +00:00
|
|
|
|
.or_insert((TypeId::of::<A>(), A::from_json_str));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
|
2021-12-18 15:12:08 +00:00
|
|
|
|
if self
|
|
|
|
|
.global_actions
|
|
|
|
|
.insert(TypeId::of::<A>(), handler)
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
2022-04-06 17:20:57 +00:00
|
|
|
|
panic!(
|
|
|
|
|
"registered multiple global handlers for {}",
|
|
|
|
|
type_name::<A>()
|
|
|
|
|
);
|
2021-12-18 15:12:08 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.cx.windows.keys().cloned()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 03:45:30 +00:00
|
|
|
|
pub fn activate_window(&self, window_id: usize) {
|
|
|
|
|
if let Some((_, window)) = self.presenters_and_platform_windows.get(&window_id) {
|
|
|
|
|
window.activate()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.cx
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
2021-05-06 05:21:19 +00:00
|
|
|
|
.and_then(|window| window.root_view.clone().downcast::<T>())
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 19:57:39 +00:00
|
|
|
|
pub fn window_is_active(&self, window_id: usize) -> bool {
|
|
|
|
|
self.cx
|
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map_or(false, |window| window.is_active)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
pub fn window_is_fullscreen(&self, window_id: usize) -> bool {
|
|
|
|
|
self.cx
|
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map_or(false, |window| window.is_fullscreen)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 13:43:51 +00:00
|
|
|
|
pub fn window_bounds(&self, window_id: usize) -> RectF {
|
|
|
|
|
self.presenters_and_platform_windows[&window_id].1.bounds()
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 19:22:23 +00:00
|
|
|
|
pub fn render_view(&mut self, params: RenderParams) -> Result<ElementBox> {
|
|
|
|
|
let window_id = params.window_id;
|
|
|
|
|
let view_id = params.view_id;
|
2021-08-30 15:51:26 +00:00
|
|
|
|
let mut view = self
|
2021-08-27 15:25:13 +00:00
|
|
|
|
.cx
|
|
|
|
|
.views
|
2022-08-09 22:09:38 +00:00
|
|
|
|
.remove(&(window_id, view_id))
|
2022-08-10 21:39:24 +00:00
|
|
|
|
.ok_or_else(|| anyhow!("view not found"))?;
|
2022-05-26 19:22:23 +00:00
|
|
|
|
let element = view.render(params, self);
|
2021-08-27 15:25:13 +00:00
|
|
|
|
self.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
Ok(element)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 17:45:42 +00:00
|
|
|
|
pub fn render_views(
|
2021-08-27 15:25:13 +00:00
|
|
|
|
&mut self,
|
2021-08-20 17:45:42 +00:00
|
|
|
|
window_id: usize,
|
|
|
|
|
titlebar_height: f32,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
appearance: Appearance,
|
2021-08-20 17:45:42 +00:00
|
|
|
|
) -> HashMap<usize, ElementBox> {
|
2022-02-14 22:04:27 +00:00
|
|
|
|
self.start_frame();
|
2022-08-10 21:39:24 +00:00
|
|
|
|
#[allow(clippy::needless_collect)]
|
2021-08-27 15:25:13 +00:00
|
|
|
|
let view_ids = self
|
|
|
|
|
.views
|
|
|
|
|
.keys()
|
|
|
|
|
.filter_map(|(win_id, view_id)| {
|
|
|
|
|
if *win_id == window_id {
|
|
|
|
|
Some(*view_id)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
2022-08-10 21:39:24 +00:00
|
|
|
|
|
2021-08-27 15:25:13 +00:00
|
|
|
|
view_ids
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|view_id| {
|
|
|
|
|
(
|
|
|
|
|
view_id,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
self.render_view(RenderParams {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
titlebar_height,
|
2022-05-27 02:00:01 +00:00
|
|
|
|
hovered_region_ids: Default::default(),
|
2022-08-13 02:00:48 +00:00
|
|
|
|
clicked_region_ids: None,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
refreshing: false,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
appearance,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
})
|
|
|
|
|
.unwrap(),
|
2021-08-27 15:25:13 +00:00
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 22:04:27 +00:00
|
|
|
|
pub(crate) fn start_frame(&mut self) {
|
|
|
|
|
self.frame_count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
pub fn update<T, F: FnOnce(&mut Self) -> T>(&mut self, callback: F) -> T {
|
2021-04-09 22:58:19 +00:00
|
|
|
|
self.pending_flushes += 1;
|
2021-11-29 22:16:19 +00:00
|
|
|
|
let result = callback(self);
|
2021-04-09 22:58:19 +00:00
|
|
|
|
self.flush_effects();
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 23:02:24 +00:00
|
|
|
|
pub fn set_menus(&mut self, menus: Vec<Menu>) {
|
2022-05-19 17:04:01 +00:00
|
|
|
|
self.foreground_platform
|
|
|
|
|
.set_menus(menus, &self.keystroke_matcher);
|
2021-04-12 21:38:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 12:51:22 +00:00
|
|
|
|
fn show_character_palette(&self, window_id: usize) {
|
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
|
|
|
|
window.show_character_palette();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 13:13:30 +00:00
|
|
|
|
pub fn minimize_window(&self, window_id: usize) {
|
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
|
|
|
|
window.minimize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn zoom_window(&self, window_id: usize) {
|
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
|
|
|
|
window.zoom();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 09:53:37 +00:00
|
|
|
|
pub fn toggle_window_full_screen(&self, window_id: usize) {
|
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
|
|
|
|
window.toggle_full_screen();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
fn prompt(
|
2021-05-12 09:54:48 +00:00
|
|
|
|
&self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
level: PromptLevel,
|
|
|
|
|
msg: &str,
|
|
|
|
|
answers: &[&str],
|
2022-01-20 08:51:29 +00:00
|
|
|
|
) -> oneshot::Receiver<usize> {
|
2021-05-12 09:54:48 +00:00
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
2022-01-20 08:51:29 +00:00
|
|
|
|
window.prompt(level, msg, answers)
|
2021-05-12 09:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
pub fn prompt_for_paths(
|
|
|
|
|
&self,
|
|
|
|
|
options: PathPromptOptions,
|
|
|
|
|
) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
|
|
|
|
|
self.foreground_platform.prompt_for_paths(options)
|
2021-04-14 14:30:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
|
|
|
|
|
self.foreground_platform.prompt_for_new_path(directory)
|
2021-05-05 02:04:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 04:03:01 +00:00
|
|
|
|
pub fn emit_global<E: Any>(&mut self, payload: E) {
|
|
|
|
|
self.pending_effects.push_back(Effect::GlobalEvent {
|
|
|
|
|
payload: Box::new(payload),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-08-23 22:02:30 +00:00
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
2021-08-23 21:58:37 +00:00
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(H, &E::Event, &mut Self),
|
2021-08-23 22:02:30 +00:00
|
|
|
|
{
|
2021-08-23 21:58:37 +00:00
|
|
|
|
self.subscribe_internal(handle, move |handle, event, cx| {
|
|
|
|
|
callback(handle, event, cx);
|
|
|
|
|
true
|
|
|
|
|
})
|
2021-08-23 22:02:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 19:25:36 +00:00
|
|
|
|
pub fn subscribe_global<E, F>(&mut self, mut callback: F) -> Subscription
|
2022-03-11 04:03:01 +00:00
|
|
|
|
where
|
2022-03-11 19:25:36 +00:00
|
|
|
|
E: Any,
|
2022-03-11 04:03:01 +00:00
|
|
|
|
F: 'static + FnMut(&E, &mut Self),
|
|
|
|
|
{
|
2022-03-22 15:11:55 +00:00
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
2022-03-11 04:03:01 +00:00
|
|
|
|
let type_id = TypeId::of::<E>();
|
2022-03-22 15:24:48 +00:00
|
|
|
|
self.pending_effects.push_back(Effect::GlobalSubscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
type_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: Box::new(move |payload, cx| {
|
|
|
|
|
let payload = payload.downcast_ref().expect("downcast is type safe");
|
|
|
|
|
callback(payload, cx)
|
|
|
|
|
}),
|
|
|
|
|
});
|
2022-08-09 23:59:13 +00:00
|
|
|
|
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Subscription::GlobalSubscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
id: subscription_id,
|
2022-03-11 04:03:01 +00:00
|
|
|
|
type_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
subscriptions: Some(self.global_subscriptions.downgrade()),
|
2022-03-11 04:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 16:32:08 +00:00
|
|
|
|
pub fn observe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-08-23 22:02:30 +00:00
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
2021-08-23 21:58:37 +00:00
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(H, &mut Self),
|
2021-08-23 22:02:30 +00:00
|
|
|
|
{
|
2021-08-23 21:58:37 +00:00
|
|
|
|
self.observe_internal(handle, move |handle, cx| {
|
|
|
|
|
callback(handle, cx);
|
|
|
|
|
true
|
|
|
|
|
})
|
2021-08-23 22:02:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn subscribe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-08-23 22:02:30 +00:00
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
2021-08-23 21:58:37 +00:00
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(H, &E::Event, &mut Self) -> bool,
|
2021-08-23 22:02:30 +00:00
|
|
|
|
{
|
2022-03-22 14:57:30 +00:00
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
2021-08-23 21:58:37 +00:00
|
|
|
|
let emitter = handle.downgrade();
|
2022-03-22 15:24:48 +00:00
|
|
|
|
self.pending_effects.push_back(Effect::Subscription {
|
2022-03-22 14:57:30 +00:00
|
|
|
|
entity_id: handle.id(),
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: Box::new(move |payload, cx| {
|
|
|
|
|
if let Some(emitter) = H::upgrade_from(&emitter, cx.as_ref()) {
|
|
|
|
|
let payload = payload.downcast_ref().expect("downcast is type safe");
|
|
|
|
|
callback(emitter, payload, cx)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
2021-08-23 22:54:24 +00:00
|
|
|
|
Subscription::Subscription {
|
2022-03-22 14:57:30 +00:00
|
|
|
|
id: subscription_id,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
entity_id: handle.id(),
|
2022-08-09 23:59:13 +00:00
|
|
|
|
subscriptions: Some(self.subscriptions.downgrade()),
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
2021-08-23 22:02:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
fn observe_internal<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-08-23 22:02:30 +00:00
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
2021-08-23 21:58:37 +00:00
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(H, &mut Self) -> bool,
|
2021-08-23 22:02:30 +00:00
|
|
|
|
{
|
2022-03-22 15:24:48 +00:00
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
2021-08-23 21:58:37 +00:00
|
|
|
|
let observed = handle.downgrade();
|
2022-03-22 15:24:48 +00:00
|
|
|
|
let entity_id = handle.id();
|
|
|
|
|
self.pending_effects.push_back(Effect::Observation {
|
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: Box::new(move |cx| {
|
|
|
|
|
if let Some(observed) = H::upgrade_from(&observed, cx) {
|
|
|
|
|
callback(observed, cx)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
2021-08-23 22:54:24 +00:00
|
|
|
|
Subscription::Observation {
|
2022-03-22 15:24:48 +00:00
|
|
|
|
id: subscription_id,
|
|
|
|
|
entity_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Some(self.observations.downgrade()),
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
2021-08-23 22:02:30 +00:00
|
|
|
|
}
|
2022-01-21 13:22:06 +00:00
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> Subscription
|
|
|
|
|
where
|
2022-07-06 13:53:40 +00:00
|
|
|
|
F: 'static + FnMut(ViewHandle<V>, bool, &mut MutableAppContext) -> bool,
|
2022-04-14 16:22:32 +00:00
|
|
|
|
V: View,
|
|
|
|
|
{
|
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
let observed = handle.downgrade();
|
|
|
|
|
let view_id = handle.id();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
self.pending_effects.push_back(Effect::FocusObservation {
|
|
|
|
|
view_id,
|
|
|
|
|
subscription_id,
|
2022-07-06 13:53:40 +00:00
|
|
|
|
callback: Box::new(move |focused, cx| {
|
2022-04-14 16:22:32 +00:00
|
|
|
|
if let Some(observed) = observed.upgrade(cx) {
|
2022-07-06 13:53:40 +00:00
|
|
|
|
callback(observed, focused, cx)
|
2022-04-14 16:22:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
2022-08-09 22:10:30 +00:00
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
Subscription::FocusObservation {
|
|
|
|
|
id: subscription_id,
|
|
|
|
|
view_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Some(self.focus_observations.downgrade()),
|
2022-04-14 16:22:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
|
pub fn observe_global<G, F>(&mut self, mut observe: F) -> Subscription
|
2022-03-22 23:38:17 +00:00
|
|
|
|
where
|
|
|
|
|
G: Any,
|
2022-05-23 16:23:25 +00:00
|
|
|
|
F: 'static + FnMut(&mut MutableAppContext),
|
2022-03-22 23:38:17 +00:00
|
|
|
|
{
|
|
|
|
|
let type_id = TypeId::of::<G>();
|
|
|
|
|
let id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
|
2022-08-09 22:10:30 +00:00
|
|
|
|
self.global_observations.add_callback(
|
|
|
|
|
type_id,
|
|
|
|
|
id,
|
|
|
|
|
Box::new(move |cx: &mut MutableAppContext| observe(cx)),
|
|
|
|
|
);
|
2022-03-22 23:38:17 +00:00
|
|
|
|
|
|
|
|
|
Subscription::GlobalObservation {
|
|
|
|
|
id,
|
|
|
|
|
type_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Some(self.global_observations.downgrade()),
|
2022-03-22 23:38:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 17:02:26 +00:00
|
|
|
|
pub fn observe_default_global<G, F>(&mut self, observe: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
G: Any + Default,
|
|
|
|
|
F: 'static + FnMut(&mut MutableAppContext),
|
|
|
|
|
{
|
|
|
|
|
if !self.has_global::<G>() {
|
|
|
|
|
self.set_global(G::default());
|
|
|
|
|
}
|
|
|
|
|
self.observe_global::<G, F>(observe)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 10:54:34 +00:00
|
|
|
|
pub fn observe_release<E, H, F>(&mut self, handle: &H, callback: F) -> Subscription
|
2022-01-21 13:22:06 +00:00
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
|
|
|
|
H: Handle<E>,
|
2022-04-20 10:54:34 +00:00
|
|
|
|
F: 'static + FnOnce(&E, &mut Self),
|
2022-01-21 13:22:06 +00:00
|
|
|
|
{
|
|
|
|
|
let id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
self.release_observations
|
|
|
|
|
.lock()
|
|
|
|
|
.entry(handle.id())
|
|
|
|
|
.or_default()
|
2022-03-08 18:00:54 +00:00
|
|
|
|
.insert(
|
|
|
|
|
id,
|
|
|
|
|
Box::new(move |released, cx| {
|
|
|
|
|
let released = released.downcast_ref().unwrap();
|
|
|
|
|
callback(released, cx)
|
|
|
|
|
}),
|
|
|
|
|
);
|
2022-01-21 13:22:06 +00:00
|
|
|
|
Subscription::ReleaseObservation {
|
|
|
|
|
id,
|
|
|
|
|
entity_id: handle.id(),
|
|
|
|
|
observations: Some(Arc::downgrade(&self.release_observations)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 08:01:23 +00:00
|
|
|
|
pub fn observe_actions<F>(&mut self, callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(TypeId, &mut MutableAppContext),
|
|
|
|
|
{
|
|
|
|
|
let id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
self.action_dispatch_observations
|
|
|
|
|
.lock()
|
|
|
|
|
.insert(id, Box::new(callback));
|
|
|
|
|
Subscription::ActionObservation {
|
|
|
|
|
id,
|
|
|
|
|
observations: Some(Arc::downgrade(&self.action_dispatch_observations)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 10:15:40 +00:00
|
|
|
|
fn observe_window_activation<F>(&mut self, window_id: usize, callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(bool, &mut MutableAppContext) -> bool,
|
|
|
|
|
{
|
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::WindowActivationObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: Box::new(callback),
|
|
|
|
|
});
|
|
|
|
|
Subscription::WindowActivationObservation {
|
|
|
|
|
id: subscription_id,
|
|
|
|
|
window_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Some(self.window_activation_observations.downgrade()),
|
2022-07-05 10:15:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 22:52:25 +00:00
|
|
|
|
fn observe_fullscreen<F>(&mut self, window_id: usize, callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(bool, &mut MutableAppContext) -> bool,
|
|
|
|
|
{
|
|
|
|
|
let subscription_id = post_inc(&mut self.next_subscription_id);
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::WindowFullscreenObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: Box::new(callback),
|
|
|
|
|
});
|
|
|
|
|
Subscription::WindowFullscreenObservation {
|
|
|
|
|
id: subscription_id,
|
|
|
|
|
window_id,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Some(self.window_activation_observations.downgrade()),
|
2022-08-09 22:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 02:24:36 +00:00
|
|
|
|
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut MutableAppContext)) {
|
2022-03-23 00:03:24 +00:00
|
|
|
|
self.pending_effects.push_back(Effect::Deferred {
|
2022-03-25 02:24:36 +00:00
|
|
|
|
callback: Box::new(callback),
|
2022-03-23 00:03:24 +00:00
|
|
|
|
after_window_update: false,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn after_window_update(&mut self, callback: impl 'static + FnOnce(&mut MutableAppContext)) {
|
|
|
|
|
self.pending_effects.push_back(Effect::Deferred {
|
|
|
|
|
callback: Box::new(callback),
|
|
|
|
|
after_window_update: true,
|
|
|
|
|
})
|
2022-01-22 20:14:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 21:06:09 +00:00
|
|
|
|
pub(crate) fn notify_model(&mut self, model_id: usize) {
|
|
|
|
|
if self.pending_notifications.insert(model_id) {
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::ModelNotification { model_id });
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-23 22:02:30 +00:00
|
|
|
|
|
2021-04-28 19:02:39 +00:00
|
|
|
|
pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
|
2021-09-30 21:06:09 +00:00
|
|
|
|
if self.pending_notifications.insert(view_id) {
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::ViewNotification { window_id, view_id });
|
|
|
|
|
}
|
2021-04-28 19:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 23:38:17 +00:00
|
|
|
|
pub(crate) fn notify_global(&mut self, type_id: TypeId) {
|
|
|
|
|
if self.pending_global_notifications.insert(type_id) {
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::GlobalNotification { type_id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
pub(crate) fn name_for_view(&self, window_id: usize, view_id: usize) -> Option<&str> {
|
|
|
|
|
self.views
|
|
|
|
|
.get(&(window_id, view_id))
|
|
|
|
|
.map(|view| view.ui_name())
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 20:33:39 +00:00
|
|
|
|
pub fn all_action_names<'a>(&'a self) -> impl Iterator<Item = &'static str> + 'a {
|
|
|
|
|
self.action_deserializers.keys().copied()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 18:32:39 +00:00
|
|
|
|
pub fn available_actions(
|
|
|
|
|
&self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
2022-04-14 20:50:00 +00:00
|
|
|
|
) -> impl Iterator<Item = (&'static str, Box<dyn Action>, SmallVec<[&Binding; 1]>)> {
|
2022-04-13 18:32:39 +00:00
|
|
|
|
let mut action_types: HashSet<_> = self.global_actions.keys().copied().collect();
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
for view_id in self.ancestors(window_id, view_id) {
|
2022-04-13 18:32:39 +00:00
|
|
|
|
if let Some(view) = self.views.get(&(window_id, view_id)) {
|
|
|
|
|
let view_type = view.as_any().type_id();
|
|
|
|
|
if let Some(actions) = self.actions.get(&view_type) {
|
|
|
|
|
action_types.extend(actions.keys().copied());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.action_deserializers
|
|
|
|
|
.iter()
|
2022-04-14 20:50:00 +00:00
|
|
|
|
.filter_map(move |(name, (type_id, deserialize))| {
|
2022-04-13 18:32:39 +00:00
|
|
|
|
if action_types.contains(type_id) {
|
2022-04-14 20:50:00 +00:00
|
|
|
|
Some((
|
|
|
|
|
*name,
|
|
|
|
|
deserialize("{}").ok()?,
|
|
|
|
|
self.keystroke_matcher
|
|
|
|
|
.bindings_for_action_type(*type_id)
|
|
|
|
|
.collect(),
|
|
|
|
|
))
|
2022-04-13 18:32:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 23:24:02 +00:00
|
|
|
|
pub fn is_action_available(&self, action: &dyn Action) -> bool {
|
|
|
|
|
let action_type = action.as_any().type_id();
|
|
|
|
|
if let Some(window_id) = self.cx.platform.key_window_id() {
|
2022-08-05 03:55:10 +00:00
|
|
|
|
if let Some(focused_view_id) = self.focused_view_id(window_id) {
|
2022-10-16 17:46:31 +00:00
|
|
|
|
for view_id in self.ancestors(window_id, focused_view_id) {
|
2022-05-19 23:24:02 +00:00
|
|
|
|
if let Some(view) = self.views.get(&(window_id, view_id)) {
|
|
|
|
|
let view_type = view.as_any().type_id();
|
|
|
|
|
if let Some(actions) = self.actions.get(&view_type) {
|
|
|
|
|
if actions.contains_key(&action_type) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.global_actions.contains_key(&action_type)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 01:46:50 +00:00
|
|
|
|
/// Return keystrokes that would dispatch the given action closest to the focused view, if there are any.
|
2022-05-25 13:24:44 +00:00
|
|
|
|
pub(crate) fn keystrokes_for_action(
|
|
|
|
|
&self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
dispatch_path: &[usize],
|
|
|
|
|
action: &dyn Action,
|
|
|
|
|
) -> Option<SmallVec<[Keystroke; 2]>> {
|
2022-05-25 01:46:50 +00:00
|
|
|
|
for view_id in dispatch_path.iter().rev() {
|
|
|
|
|
let view = self
|
|
|
|
|
.cx
|
|
|
|
|
.views
|
|
|
|
|
.get(&(window_id, *view_id))
|
|
|
|
|
.expect("view in responder chain does not exist");
|
|
|
|
|
let cx = view.keymap_context(self.as_ref());
|
|
|
|
|
let keystrokes = self.keystroke_matcher.keystrokes_for_action(action, &cx);
|
|
|
|
|
if keystrokes.is_some() {
|
|
|
|
|
return keystrokes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
// Traverses the parent tree. Walks down the tree toward the passed
|
|
|
|
|
// view calling visit with true. Then walks back up the tree calling visit with false.
|
|
|
|
|
// If `visit` returns false this function will immediately return.
|
|
|
|
|
// Returns a bool indicating if the traversal was completed early.
|
|
|
|
|
fn visit_dispatch_path(
|
2021-03-10 04:00:51 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
2022-08-05 03:55:10 +00:00
|
|
|
|
view_id: usize,
|
|
|
|
|
mut visit: impl FnMut(usize, bool, &mut MutableAppContext) -> bool,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
) -> bool {
|
2022-08-05 03:55:10 +00:00
|
|
|
|
// List of view ids from the leaf to the root of the window
|
2022-10-16 17:46:31 +00:00
|
|
|
|
let path = self.ancestors(window_id, view_id).collect::<Vec<_>>();
|
2022-08-05 03:55:10 +00:00
|
|
|
|
|
|
|
|
|
// Walk down from the root to the leaf calling visit with capture_phase = true
|
|
|
|
|
for view_id in path.iter().rev() {
|
|
|
|
|
if !visit(*view_id, true, self) {
|
|
|
|
|
return false;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-08-05 03:55:10 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
// Walk up from the leaf to the root calling visit with capture_phase = false
|
|
|
|
|
for view_id in path.iter() {
|
|
|
|
|
if !visit(*view_id, false, self) {
|
|
|
|
|
return false;
|
2021-11-29 22:16:19 +00:00
|
|
|
|
}
|
2022-08-05 03:55:10 +00:00
|
|
|
|
}
|
2022-05-30 08:01:23 +00:00
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
true
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
// Returns an iterator over all of the view ids from the passed view up to the root of the window
|
|
|
|
|
// Includes the passed view itself
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn ancestors(&self, window_id: usize, mut view_id: usize) -> impl Iterator<Item = usize> + '_ {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
std::iter::once(view_id)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.chain(std::iter::from_fn(move || {
|
|
|
|
|
if let Some(ParentId::View(parent_id)) = self.parents.get(&(window_id, view_id)) {
|
|
|
|
|
view_id = *parent_id;
|
|
|
|
|
Some(view_id)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}))
|
2022-08-05 03:55:10 +00:00
|
|
|
|
}
|
2022-08-05 19:38:05 +00:00
|
|
|
|
|
2022-02-27 23:14:40 +00:00
|
|
|
|
fn actions_mut(
|
|
|
|
|
&mut self,
|
|
|
|
|
capture_phase: bool,
|
|
|
|
|
) -> &mut HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>> {
|
|
|
|
|
if capture_phase {
|
|
|
|
|
&mut self.capture_actions
|
|
|
|
|
} else {
|
|
|
|
|
&mut self.actions
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
|
pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
|
|
|
|
|
self.dispatch_global_action_any(&action);
|
2021-04-09 19:38:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 23:20:49 +00:00
|
|
|
|
fn dispatch_global_action_any(&mut self, action: &dyn Action) -> bool {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
2021-12-18 15:12:08 +00:00
|
|
|
|
if let Some((name, mut handler)) = this.global_actions.remove_entry(&action.id()) {
|
|
|
|
|
handler(action, this);
|
|
|
|
|
this.global_actions.insert(name, handler);
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-11-29 22:16:19 +00:00
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 19:38:09 +00:00
|
|
|
|
pub fn add_bindings<T: IntoIterator<Item = keymap::Binding>>(&mut self, bindings: T) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
self.keystroke_matcher.add_bindings(bindings);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 23:50:44 +00:00
|
|
|
|
pub fn clear_bindings(&mut self) {
|
|
|
|
|
self.keystroke_matcher.clear_bindings();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
pub fn dispatch_key_down(&mut self, window_id: usize, event: &KeyDownEvent) -> bool {
|
|
|
|
|
if let Some(focused_view_id) = self.focused_view_id(window_id) {
|
|
|
|
|
for view_id in self
|
|
|
|
|
.ancestors(window_id, focused_view_id)
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
{
|
|
|
|
|
if let Some(mut view) = self.cx.views.remove(&(window_id, view_id)) {
|
|
|
|
|
let handled = view.key_down(event, self, window_id, view_id);
|
|
|
|
|
self.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
if handled {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("view {} does not exist", view_id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn dispatch_key_up(&mut self, window_id: usize, event: &KeyUpEvent) -> bool {
|
|
|
|
|
if let Some(focused_view_id) = self.focused_view_id(window_id) {
|
|
|
|
|
for view_id in self
|
|
|
|
|
.ancestors(window_id, focused_view_id)
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
{
|
|
|
|
|
if let Some(mut view) = self.cx.views.remove(&(window_id, view_id)) {
|
|
|
|
|
let handled = view.key_up(event, self, window_id, view_id);
|
|
|
|
|
self.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
if handled {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("view {} does not exist", view_id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn dispatch_modifiers_changed(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
event: &ModifiersChangedEvent,
|
|
|
|
|
) -> bool {
|
|
|
|
|
if let Some(focused_view_id) = self.focused_view_id(window_id) {
|
|
|
|
|
for view_id in self
|
|
|
|
|
.ancestors(window_id, focused_view_id)
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
{
|
|
|
|
|
if let Some(mut view) = self.cx.views.remove(&(window_id, view_id)) {
|
|
|
|
|
let handled = view.modifiers_changed(event, self, window_id, view_id);
|
|
|
|
|
self.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
if handled {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("view {} does not exist", view_id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
pub fn dispatch_keystroke(&mut self, window_id: usize, keystroke: &Keystroke) -> bool {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
if let Some(focused_view_id) = self.focused_view_id(window_id) {
|
2022-10-26 23:57:23 +00:00
|
|
|
|
let dispatch_path = self
|
2022-10-16 17:46:31 +00:00
|
|
|
|
.ancestors(window_id, focused_view_id)
|
2022-10-26 23:57:23 +00:00
|
|
|
|
.map(|view_id| {
|
|
|
|
|
(
|
|
|
|
|
view_id,
|
|
|
|
|
self.cx
|
|
|
|
|
.views
|
|
|
|
|
.get(&(window_id, view_id))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.keymap_context(self.as_ref()),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
match self
|
|
|
|
|
.keystroke_matcher
|
|
|
|
|
.push_keystroke(keystroke.clone(), dispatch_path)
|
2022-10-16 17:46:31 +00:00
|
|
|
|
{
|
2022-10-26 23:57:23 +00:00
|
|
|
|
MatchResult::None => false,
|
|
|
|
|
MatchResult::Pending => true,
|
2022-10-27 19:33:51 +00:00
|
|
|
|
MatchResult::Matches(matches) => {
|
|
|
|
|
for (view_id, action) in matches {
|
|
|
|
|
if self.handle_dispatch_action_from_effect(
|
|
|
|
|
window_id,
|
|
|
|
|
Some(view_id),
|
|
|
|
|
action.as_ref(),
|
|
|
|
|
) {
|
|
|
|
|
self.keystroke_matcher.clear_pending();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-10-27 19:33:51 +00:00
|
|
|
|
false
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-26 23:57:23 +00:00
|
|
|
|
} else {
|
|
|
|
|
false
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 14:54:34 +00:00
|
|
|
|
pub fn default_global<T: 'static + Default>(&mut self) -> &T {
|
2022-03-22 23:38:17 +00:00
|
|
|
|
let type_id = TypeId::of::<T>();
|
2022-03-23 17:27:27 +00:00
|
|
|
|
self.update(|this| {
|
2022-06-29 18:58:12 +00:00
|
|
|
|
if let Entry::Vacant(entry) = this.cx.globals.entry(type_id) {
|
|
|
|
|
entry.insert(Box::new(T::default()));
|
2022-03-23 17:27:27 +00:00
|
|
|
|
this.notify_global(type_id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
self.globals.get(&type_id).unwrap().downcast_ref().unwrap()
|
2022-03-17 14:54:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 13:33:01 +00:00
|
|
|
|
pub fn set_global<T: 'static>(&mut self, state: T) {
|
2022-03-23 17:27:27 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let type_id = TypeId::of::<T>();
|
|
|
|
|
this.cx.globals.insert(type_id, Box::new(state));
|
|
|
|
|
this.notify_global(type_id);
|
|
|
|
|
});
|
2022-02-28 01:07:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 14:54:34 +00:00
|
|
|
|
pub fn update_default_global<T, F, U>(&mut self, update: F) -> U
|
|
|
|
|
where
|
|
|
|
|
T: 'static + Default,
|
|
|
|
|
F: FnOnce(&mut T, &mut MutableAppContext) -> U,
|
|
|
|
|
{
|
2022-03-23 17:27:27 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let type_id = TypeId::of::<T>();
|
|
|
|
|
let mut state = this
|
|
|
|
|
.cx
|
|
|
|
|
.globals
|
|
|
|
|
.remove(&type_id)
|
|
|
|
|
.unwrap_or_else(|| Box::new(T::default()));
|
|
|
|
|
let result = update(state.downcast_mut().unwrap(), this);
|
|
|
|
|
this.cx.globals.insert(type_id, state);
|
|
|
|
|
this.notify_global(type_id);
|
|
|
|
|
result
|
|
|
|
|
})
|
2022-03-17 14:54:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_global<T, F, U>(&mut self, update: F) -> U
|
2022-02-28 01:07:46 +00:00
|
|
|
|
where
|
2022-03-17 14:54:34 +00:00
|
|
|
|
T: 'static,
|
2022-02-28 01:07:46 +00:00
|
|
|
|
F: FnOnce(&mut T, &mut MutableAppContext) -> U,
|
|
|
|
|
{
|
2022-03-23 17:27:27 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let type_id = TypeId::of::<T>();
|
2022-08-05 03:55:10 +00:00
|
|
|
|
if let Some(mut state) = this.cx.globals.remove(&type_id) {
|
|
|
|
|
let result = update(state.downcast_mut().unwrap(), this);
|
|
|
|
|
this.cx.globals.insert(type_id, state);
|
|
|
|
|
this.notify_global(type_id);
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
panic!("No global added for {}", std::any::type_name::<T>());
|
|
|
|
|
}
|
2022-03-23 17:27:27 +00:00
|
|
|
|
})
|
2022-02-28 01:07:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 07:50:26 +00:00
|
|
|
|
pub fn clear_globals(&mut self) {
|
|
|
|
|
self.cx.globals.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
|
|
|
|
where
|
|
|
|
|
T: Entity,
|
|
|
|
|
F: FnOnce(&mut ModelContext<T>) -> T,
|
|
|
|
|
{
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let model_id = post_inc(&mut this.next_entity_id);
|
|
|
|
|
let handle = ModelHandle::new(model_id, &this.cx.ref_counts);
|
|
|
|
|
let mut cx = ModelContext::new(this, model_id);
|
|
|
|
|
let model = build_model(&mut cx);
|
|
|
|
|
this.cx.models.insert(model_id, Box::new(model));
|
|
|
|
|
handle
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
|
pub fn add_window<T, F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_options: WindowOptions,
|
|
|
|
|
build_root_view: F,
|
|
|
|
|
) -> (usize, ViewHandle<T>)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
|
{
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let window_id = post_inc(&mut this.next_window_id);
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let root_view = this
|
|
|
|
|
.build_and_insert_view(window_id, ParentId::Root, |cx| Some(build_root_view(cx)))
|
|
|
|
|
.unwrap();
|
2021-11-29 22:16:19 +00:00
|
|
|
|
this.cx.windows.insert(
|
|
|
|
|
window_id,
|
|
|
|
|
Window {
|
|
|
|
|
root_view: root_view.clone().into(),
|
2022-03-04 16:28:53 +00:00
|
|
|
|
focused_view_id: Some(root_view.id()),
|
2022-07-05 10:15:40 +00:00
|
|
|
|
is_active: false,
|
2021-11-29 22:16:19 +00:00
|
|
|
|
invalidation: None,
|
2022-08-09 19:34:57 +00:00
|
|
|
|
is_fullscreen: false,
|
2021-11-29 22:16:19 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
2022-10-16 17:46:31 +00:00
|
|
|
|
root_view.update(this, |view, cx| view.focus_in(cx.handle().into(), cx));
|
2022-09-12 10:03:03 +00:00
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
let window =
|
2022-09-12 10:03:03 +00:00
|
|
|
|
this.cx
|
|
|
|
|
.platform
|
|
|
|
|
.open_window(window_id, window_options, this.foreground.clone());
|
2022-09-14 09:58:05 +00:00
|
|
|
|
this.register_platform_window(window_id, window);
|
2021-04-06 12:29:42 +00:00
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
(window_id, root_view)
|
|
|
|
|
})
|
2021-04-06 12:29:42 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-09-12 10:03:03 +00:00
|
|
|
|
pub fn add_status_bar_item<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
|
{
|
|
|
|
|
self.update(|this| {
|
|
|
|
|
let window_id = post_inc(&mut this.next_window_id);
|
|
|
|
|
let root_view = this
|
|
|
|
|
.build_and_insert_view(window_id, ParentId::Root, |cx| Some(build_root_view(cx)))
|
|
|
|
|
.unwrap();
|
|
|
|
|
this.cx.windows.insert(
|
|
|
|
|
window_id,
|
|
|
|
|
Window {
|
|
|
|
|
root_view: root_view.clone().into(),
|
|
|
|
|
focused_view_id: Some(root_view.id()),
|
|
|
|
|
is_active: false,
|
|
|
|
|
invalidation: None,
|
|
|
|
|
is_fullscreen: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-10-16 17:46:31 +00:00
|
|
|
|
root_view.update(this, |view, cx| view.focus_in(cx.handle().into(), cx));
|
2022-09-12 10:03:03 +00:00
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
let status_item = this.cx.platform.add_status_item();
|
|
|
|
|
this.register_platform_window(window_id, status_item);
|
|
|
|
|
|
|
|
|
|
(window_id, root_view)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 14:10:45 +00:00
|
|
|
|
pub fn remove_status_bar_item(&mut self, id: usize) {
|
|
|
|
|
self.remove_window(id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
fn register_platform_window(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
mut window: Box<dyn platform::Window>,
|
|
|
|
|
) {
|
|
|
|
|
let presenter = Rc::new(RefCell::new(self.build_presenter(
|
|
|
|
|
window_id,
|
|
|
|
|
window.titlebar_height(),
|
|
|
|
|
window.appearance(),
|
|
|
|
|
)));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
let presenter = Rc::downgrade(&presenter);
|
2022-09-12 21:24:36 +00:00
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
window.on_event(Box::new(move |event| {
|
|
|
|
|
app.update(|cx| {
|
|
|
|
|
if let Some(presenter) = presenter.upgrade() {
|
|
|
|
|
if let Event::KeyDown(KeyDownEvent { keystroke, .. }) = &event {
|
|
|
|
|
if cx.dispatch_keystroke(window_id, keystroke) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-09-12 10:03:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
presenter.borrow_mut().dispatch_event(event, false, cx)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}));
|
|
|
|
|
}
|
2022-09-14 09:47:43 +00:00
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
window.on_active_status_change(Box::new(move |is_active| {
|
|
|
|
|
app.update(|cx| cx.window_changed_active_status(window_id, is_active))
|
|
|
|
|
}));
|
|
|
|
|
}
|
2022-09-12 10:03:03 +00:00
|
|
|
|
|
2022-09-14 09:58:05 +00:00
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
window.on_resize(Box::new(move || {
|
|
|
|
|
app.update(|cx| cx.window_was_resized(window_id))
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
window.on_fullscreen(Box::new(move |is_fullscreen| {
|
|
|
|
|
app.update(|cx| cx.window_was_fullscreen_changed(window_id, is_fullscreen))
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
window.on_close(Box::new(move || {
|
|
|
|
|
app.update(|cx| cx.remove_window(window_id));
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
window.on_appearance_changed(Box::new(move || app.update(|cx| cx.refresh_windows())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.set_input_handler(Box::new(WindowInputHandler {
|
|
|
|
|
app: self.upgrade().0,
|
|
|
|
|
window_id,
|
|
|
|
|
}));
|
|
|
|
|
|
2022-09-14 13:43:51 +00:00
|
|
|
|
let scene = presenter.borrow_mut().build_scene(
|
|
|
|
|
window.content_size(),
|
|
|
|
|
window.scale_factor(),
|
|
|
|
|
false,
|
|
|
|
|
self,
|
|
|
|
|
);
|
2022-09-14 09:58:05 +00:00
|
|
|
|
window.present_scene(scene);
|
|
|
|
|
self.presenters_and_platform_windows
|
|
|
|
|
.insert(window_id, (presenter.clone(), window));
|
2022-09-12 10:03:03 +00:00
|
|
|
|
}
|
2022-08-22 16:17:48 +00:00
|
|
|
|
|
2022-05-16 15:45:50 +00:00
|
|
|
|
pub fn replace_root_view<T, F>(&mut self, window_id: usize, build_root_view: F) -> ViewHandle<T>
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
|
{
|
|
|
|
|
self.update(|this| {
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let root_view = this
|
|
|
|
|
.build_and_insert_view(window_id, ParentId::Root, |cx| Some(build_root_view(cx)))
|
|
|
|
|
.unwrap();
|
2022-05-16 15:45:50 +00:00
|
|
|
|
let window = this.cx.windows.get_mut(&window_id).unwrap();
|
|
|
|
|
window.root_view = root_view.clone().into();
|
|
|
|
|
window.focused_view_id = Some(root_view.id());
|
|
|
|
|
root_view
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 17:34:49 +00:00
|
|
|
|
pub fn remove_window(&mut self, window_id: usize) {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.cx.windows.remove(&window_id);
|
2021-05-05 17:34:49 +00:00
|
|
|
|
self.presenters_and_platform_windows.remove(&window_id);
|
2022-01-21 13:22:06 +00:00
|
|
|
|
self.flush_effects();
|
2021-05-05 17:34:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 09:47:43 +00:00
|
|
|
|
pub fn build_presenter(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
titlebar_height: f32,
|
|
|
|
|
appearance: Appearance,
|
|
|
|
|
) -> Presenter {
|
2021-08-23 13:20:23 +00:00
|
|
|
|
Presenter::new(
|
|
|
|
|
window_id,
|
|
|
|
|
titlebar_height,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
appearance,
|
2021-08-23 13:20:23 +00:00
|
|
|
|
self.cx.font_cache.clone(),
|
|
|
|
|
TextLayoutCache::new(self.cx.platform.fonts()),
|
|
|
|
|
self.assets.clone(),
|
|
|
|
|
self,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
pub fn add_view<T, F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
parent_handle: impl Into<AnyViewHandle>,
|
|
|
|
|
build_view: F,
|
|
|
|
|
) -> ViewHandle<T>
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
|
{
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let parent_handle = parent_handle.into();
|
|
|
|
|
self.build_and_insert_view(
|
|
|
|
|
parent_handle.window_id,
|
|
|
|
|
ParentId::View(parent_handle.view_id),
|
|
|
|
|
|cx| Some(build_view(cx)),
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_option_view<T, F>(
|
2022-08-05 03:55:10 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
parent_handle: impl Into<AnyViewHandle>,
|
|
|
|
|
build_view: F,
|
|
|
|
|
) -> Option<ViewHandle<T>>
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
|
|
|
|
|
{
|
|
|
|
|
let parent_handle = parent_handle.into();
|
|
|
|
|
self.build_and_insert_view(
|
|
|
|
|
parent_handle.window_id,
|
|
|
|
|
ParentId::View(parent_handle.view_id),
|
|
|
|
|
build_view,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn build_and_insert_view<T, F>(
|
2021-03-10 04:00:51 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
2022-08-05 03:55:10 +00:00
|
|
|
|
parent_id: ParentId,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
build_view: F,
|
|
|
|
|
) -> Option<ViewHandle<T>>
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
|
|
|
|
|
{
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let view_id = post_inc(&mut this.next_entity_id);
|
|
|
|
|
let mut cx = ViewContext::new(this, window_id, view_id);
|
|
|
|
|
let handle = if let Some(view) = build_view(&mut cx) {
|
|
|
|
|
this.cx.views.insert((window_id, view_id), Box::new(view));
|
2022-08-05 03:55:10 +00:00
|
|
|
|
this.cx.parents.insert((window_id, view_id), parent_id);
|
2021-11-29 22:16:19 +00:00
|
|
|
|
if let Some(window) = this.cx.windows.get_mut(&window_id) {
|
|
|
|
|
window
|
|
|
|
|
.invalidation
|
|
|
|
|
.get_or_insert_with(Default::default)
|
|
|
|
|
.updated
|
|
|
|
|
.insert(view_id);
|
|
|
|
|
}
|
2021-12-03 16:52:39 +00:00
|
|
|
|
Some(ViewHandle::new(window_id, view_id, &this.cx.ref_counts))
|
2021-11-29 22:16:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
handle
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn remove_dropped_entities(&mut self) {
|
|
|
|
|
loop {
|
2021-08-27 16:04:21 +00:00
|
|
|
|
let (dropped_models, dropped_views, dropped_element_states) =
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.cx.ref_counts.lock().take_dropped();
|
2021-08-27 16:04:21 +00:00
|
|
|
|
if dropped_models.is_empty()
|
|
|
|
|
&& dropped_views.is_empty()
|
|
|
|
|
&& dropped_element_states.is_empty()
|
|
|
|
|
{
|
2021-03-10 04:00:51 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for model_id in dropped_models {
|
2022-08-09 22:10:30 +00:00
|
|
|
|
self.subscriptions.remove(model_id);
|
|
|
|
|
self.observations.remove(model_id);
|
2021-06-25 16:35:06 +00:00
|
|
|
|
let mut model = self.cx.models.remove(&model_id).unwrap();
|
|
|
|
|
model.release(self);
|
2022-03-08 18:00:54 +00:00
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::ModelRelease { model_id, model });
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (window_id, view_id) in dropped_views {
|
2022-08-09 22:10:30 +00:00
|
|
|
|
self.subscriptions.remove(view_id);
|
|
|
|
|
self.observations.remove(view_id);
|
2021-06-25 16:35:06 +00:00
|
|
|
|
let mut view = self.cx.views.remove(&(window_id, view_id)).unwrap();
|
|
|
|
|
view.release(self);
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let change_focus_to = self.cx.windows.get_mut(&window_id).and_then(|window| {
|
2021-05-06 04:48:16 +00:00
|
|
|
|
window
|
|
|
|
|
.invalidation
|
|
|
|
|
.get_or_insert_with(Default::default)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.removed
|
|
|
|
|
.push(view_id);
|
2022-03-04 16:28:53 +00:00
|
|
|
|
if window.focused_view_id == Some(view_id) {
|
2021-05-06 22:30:10 +00:00
|
|
|
|
Some(window.root_view.id())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-08-05 03:55:10 +00:00
|
|
|
|
self.cx.parents.remove(&(window_id, view_id));
|
2021-05-06 22:30:10 +00:00
|
|
|
|
|
|
|
|
|
if let Some(view_id) = change_focus_to {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.handle_focus_effect(window_id, Some(view_id));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-01-21 13:22:06 +00:00
|
|
|
|
|
|
|
|
|
self.pending_effects
|
2022-03-08 18:00:54 +00:00
|
|
|
|
.push_back(Effect::ViewRelease { view_id, view });
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-04-27 03:24:23 +00:00
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
for key in dropped_element_states {
|
|
|
|
|
self.cx.element_states.remove(&key);
|
2021-04-27 03:24:23 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flush_effects(&mut self) {
|
2021-04-10 06:11:13 +00:00
|
|
|
|
self.pending_flushes = self.pending_flushes.saturating_sub(1);
|
2022-03-23 00:03:24 +00:00
|
|
|
|
let mut after_window_update_callbacks = Vec::new();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
|
|
if !self.flushing_effects && self.pending_flushes == 0 {
|
|
|
|
|
self.flushing_effects = true;
|
|
|
|
|
|
2021-08-26 14:36:56 +00:00
|
|
|
|
let mut refreshing = false;
|
2021-04-28 20:16:03 +00:00
|
|
|
|
loop {
|
|
|
|
|
if let Some(effect) = self.pending_effects.pop_front() {
|
|
|
|
|
match effect {
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::Subscription {
|
2022-03-22 14:57:30 +00:00
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
} => self.subscriptions.add_or_remove_callback(
|
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-08-09 22:10:30 +00:00
|
|
|
|
Effect::Event { entity_id, payload } => {
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut subscriptions = self.subscriptions.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
subscriptions.emit_and_cleanup(entity_id, self, |callback, this| {
|
|
|
|
|
callback(payload.as_ref(), this)
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::GlobalSubscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
type_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
} => self.global_subscriptions.add_or_remove_callback(
|
2022-03-22 15:24:48 +00:00
|
|
|
|
type_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Effect::GlobalEvent { payload } => self.emit_global_event(payload),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::Observation {
|
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
} => self.observations.add_or_remove_callback(
|
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2021-04-28 20:16:03 +00:00
|
|
|
|
Effect::ModelNotification { model_id } => {
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut observations = self.observations.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
observations
|
|
|
|
|
.emit_and_cleanup(model_id, self, |callback, this| callback(this));
|
2021-04-28 20:16:03 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2021-04-28 20:16:03 +00:00
|
|
|
|
Effect::ViewNotification { window_id, view_id } => {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.handle_view_notification_effect(window_id, view_id)
|
2021-04-28 20:16:03 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-22 23:38:17 +00:00
|
|
|
|
Effect::GlobalNotification { type_id } => {
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut subscriptions = self.global_observations.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
subscriptions.emit_and_cleanup(type_id, self, |callback, this| {
|
|
|
|
|
callback(this);
|
|
|
|
|
true
|
|
|
|
|
});
|
2022-03-22 23:38:17 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-23 00:03:24 +00:00
|
|
|
|
Effect::Deferred {
|
|
|
|
|
callback,
|
|
|
|
|
after_window_update,
|
|
|
|
|
} => {
|
|
|
|
|
if after_window_update {
|
|
|
|
|
after_window_update_callbacks.push(callback);
|
|
|
|
|
} else {
|
|
|
|
|
callback(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-08 18:00:54 +00:00
|
|
|
|
Effect::ModelRelease { model_id, model } => {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.handle_entity_release_effect(model_id, model.as_any())
|
2022-03-08 18:00:54 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-03-08 18:00:54 +00:00
|
|
|
|
Effect::ViewRelease { view_id, view } => {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.handle_entity_release_effect(view_id, view.as_any())
|
2022-03-08 18:00:54 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2021-04-28 20:16:03 +00:00
|
|
|
|
Effect::Focus { window_id, view_id } => {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.handle_focus_effect(window_id, view_id);
|
2021-04-28 20:16:03 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
Effect::FocusObservation {
|
|
|
|
|
view_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
} => {
|
2022-08-09 22:10:30 +00:00
|
|
|
|
self.focus_observations.add_or_remove_callback(
|
|
|
|
|
view_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
);
|
2022-04-14 16:22:32 +00:00
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2021-08-27 22:03:37 +00:00
|
|
|
|
Effect::ResizeWindow { window_id } => {
|
|
|
|
|
if let Some(window) = self.cx.windows.get_mut(&window_id) {
|
|
|
|
|
window
|
|
|
|
|
.invalidation
|
|
|
|
|
.get_or_insert(WindowInvalidation::default());
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-07-05 10:15:40 +00:00
|
|
|
|
Effect::WindowActivationObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
2022-08-09 22:10:30 +00:00
|
|
|
|
} => self.window_activation_observations.add_or_remove_callback(
|
2022-07-05 10:15:40 +00:00
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-04-22 21:18:50 +00:00
|
|
|
|
Effect::ActivateWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_active,
|
2022-07-05 10:15:40 +00:00
|
|
|
|
} => self.handle_window_activation_effect(window_id, is_active),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
2022-08-09 22:52:25 +00:00
|
|
|
|
Effect::WindowFullscreenObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
} => self.window_fullscreen_observations.add_or_remove_callback(
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback,
|
|
|
|
|
),
|
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
Effect::FullscreenWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_fullscreen,
|
|
|
|
|
} => self.handle_fullscreen_effect(window_id, is_fullscreen),
|
|
|
|
|
|
2021-08-26 13:00:00 +00:00
|
|
|
|
Effect::RefreshWindows => {
|
2021-08-26 14:36:56 +00:00
|
|
|
|
refreshing = true;
|
2021-08-26 13:00:00 +00:00
|
|
|
|
}
|
2022-08-05 03:55:10 +00:00
|
|
|
|
Effect::DispatchActionFrom {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
action,
|
|
|
|
|
} => {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
self.handle_dispatch_action_from_effect(
|
2022-08-05 03:55:10 +00:00
|
|
|
|
window_id,
|
|
|
|
|
Some(view_id),
|
|
|
|
|
action.as_ref(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-05-30 08:01:23 +00:00
|
|
|
|
Effect::ActionDispatchNotification { action_id } => {
|
|
|
|
|
self.handle_action_dispatch_notification_effect(action_id)
|
|
|
|
|
}
|
2022-06-23 09:43:19 +00:00
|
|
|
|
Effect::WindowShouldCloseSubscription {
|
|
|
|
|
window_id,
|
|
|
|
|
callback,
|
|
|
|
|
} => {
|
|
|
|
|
self.handle_window_should_close_subscription_effect(window_id, callback)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-09-30 21:06:09 +00:00
|
|
|
|
self.pending_notifications.clear();
|
2021-04-28 19:02:39 +00:00
|
|
|
|
self.remove_dropped_entities();
|
2021-04-28 20:16:03 +00:00
|
|
|
|
} else {
|
2021-04-28 19:02:39 +00:00
|
|
|
|
self.remove_dropped_entities();
|
2021-08-26 14:36:56 +00:00
|
|
|
|
if refreshing {
|
2021-08-26 13:00:00 +00:00
|
|
|
|
self.perform_window_refresh();
|
|
|
|
|
} else {
|
|
|
|
|
self.update_windows();
|
|
|
|
|
}
|
2021-04-28 20:16:03 +00:00
|
|
|
|
|
|
|
|
|
if self.pending_effects.is_empty() {
|
2022-03-23 00:03:24 +00:00
|
|
|
|
for callback in after_window_update_callbacks.drain(..) {
|
|
|
|
|
callback(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.pending_effects.is_empty() {
|
|
|
|
|
self.flushing_effects = false;
|
|
|
|
|
self.pending_notifications.clear();
|
2022-03-22 23:38:17 +00:00
|
|
|
|
self.pending_global_notifications.clear();
|
2022-03-23 00:03:24 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-04-28 20:16:03 +00:00
|
|
|
|
}
|
2022-03-23 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
refreshing = false;
|
2021-04-28 19:02:39 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_windows(&mut self) {
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut invalidations: HashMap<_, _> = Default::default();
|
2021-05-28 22:25:15 +00:00
|
|
|
|
for (window_id, window) in &mut self.cx.windows {
|
2021-05-06 04:48:16 +00:00
|
|
|
|
if let Some(invalidation) = window.invalidation.take() {
|
|
|
|
|
invalidations.insert(*window_id, invalidation);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-16 13:51:54 +00:00
|
|
|
|
for (window_id, mut invalidation) in invalidations {
|
2021-04-12 22:42:33 +00:00
|
|
|
|
if let Some((presenter, mut window)) =
|
|
|
|
|
self.presenters_and_platform_windows.remove(&window_id)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
let mut presenter = presenter.borrow_mut();
|
2022-09-14 09:47:43 +00:00
|
|
|
|
presenter.invalidate(&mut invalidation, window.appearance(), self);
|
2022-09-14 13:43:51 +00:00
|
|
|
|
let scene = presenter.build_scene(
|
|
|
|
|
window.content_size(),
|
|
|
|
|
window.scale_factor(),
|
|
|
|
|
false,
|
|
|
|
|
self,
|
|
|
|
|
);
|
2021-04-12 22:42:33 +00:00
|
|
|
|
window.present_scene(scene);
|
|
|
|
|
}
|
|
|
|
|
self.presenters_and_platform_windows
|
|
|
|
|
.insert(window_id, (presenter, window));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 21:18:50 +00:00
|
|
|
|
fn window_was_resized(&mut self, window_id: usize) {
|
2021-08-27 22:03:37 +00:00
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::ResizeWindow { window_id });
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
fn window_was_fullscreen_changed(&mut self, window_id: usize, is_fullscreen: bool) {
|
|
|
|
|
self.pending_effects.push_back(Effect::FullscreenWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_fullscreen,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 21:18:50 +00:00
|
|
|
|
fn window_changed_active_status(&mut self, window_id: usize, is_active: bool) {
|
|
|
|
|
self.pending_effects.push_back(Effect::ActivateWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_active,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 13:00:00 +00:00
|
|
|
|
pub fn refresh_windows(&mut self) {
|
|
|
|
|
self.pending_effects.push_back(Effect::RefreshWindows);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
pub fn dispatch_action_at(&mut self, window_id: usize, view_id: usize, action: impl Action) {
|
|
|
|
|
self.dispatch_any_action_at(window_id, view_id, Box::new(action));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn dispatch_any_action_at(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
action: Box<dyn Action>,
|
|
|
|
|
) {
|
|
|
|
|
self.pending_effects.push_back(Effect::DispatchActionFrom {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
action,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 13:00:00 +00:00
|
|
|
|
fn perform_window_refresh(&mut self) {
|
|
|
|
|
let mut presenters = mem::take(&mut self.presenters_and_platform_windows);
|
|
|
|
|
for (window_id, (presenter, window)) in &mut presenters {
|
2022-03-16 13:51:54 +00:00
|
|
|
|
let mut invalidation = self
|
2021-08-26 13:00:00 +00:00
|
|
|
|
.cx
|
|
|
|
|
.windows
|
2022-08-10 21:39:24 +00:00
|
|
|
|
.get_mut(window_id)
|
2021-08-26 13:00:00 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
.invalidation
|
|
|
|
|
.take();
|
|
|
|
|
let mut presenter = presenter.borrow_mut();
|
2022-03-16 13:51:54 +00:00
|
|
|
|
presenter.refresh(
|
|
|
|
|
invalidation.as_mut().unwrap_or(&mut Default::default()),
|
2022-09-14 09:47:43 +00:00
|
|
|
|
window.appearance(),
|
2022-03-16 13:51:54 +00:00
|
|
|
|
self,
|
|
|
|
|
);
|
2022-09-14 13:43:51 +00:00
|
|
|
|
let scene =
|
|
|
|
|
presenter.build_scene(window.content_size(), window.scale_factor(), true, self);
|
2021-08-26 13:00:00 +00:00
|
|
|
|
window.present_scene(scene);
|
|
|
|
|
}
|
|
|
|
|
self.presenters_and_platform_windows = presenters;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 04:03:01 +00:00
|
|
|
|
fn emit_global_event(&mut self, payload: Box<dyn Any>) {
|
|
|
|
|
let type_id = (&*payload).type_id();
|
|
|
|
|
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut subscriptions = self.global_subscriptions.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
subscriptions.emit_and_cleanup(type_id, self, |callback, this| {
|
|
|
|
|
callback(payload.as_ref(), this);
|
|
|
|
|
true //Always alive
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 14:52:24 +00:00
|
|
|
|
fn handle_view_notification_effect(
|
|
|
|
|
&mut self,
|
|
|
|
|
observed_window_id: usize,
|
|
|
|
|
observed_view_id: usize,
|
|
|
|
|
) {
|
2022-05-31 08:36:10 +00:00
|
|
|
|
if self
|
|
|
|
|
.cx
|
|
|
|
|
.views
|
|
|
|
|
.contains_key(&(observed_window_id, observed_view_id))
|
|
|
|
|
{
|
|
|
|
|
if let Some(window) = self.cx.windows.get_mut(&observed_window_id) {
|
|
|
|
|
window
|
|
|
|
|
.invalidation
|
|
|
|
|
.get_or_insert_with(Default::default)
|
|
|
|
|
.updated
|
|
|
|
|
.insert(observed_view_id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut observations = self.observations.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
observations.emit_and_cleanup(observed_view_id, self, |callback, this| callback(this));
|
2022-03-22 23:38:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 14:52:24 +00:00
|
|
|
|
fn handle_entity_release_effect(&mut self, entity_id: usize, entity: &dyn Any) {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
let callbacks = self.release_observations.lock().remove(&entity_id);
|
|
|
|
|
if let Some(callbacks) = callbacks {
|
2022-04-20 10:54:34 +00:00
|
|
|
|
for (_, callback) in callbacks {
|
2022-03-08 18:00:54 +00:00
|
|
|
|
callback(entity, self);
|
2022-01-21 13:22:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
fn handle_fullscreen_effect(&mut self, window_id: usize, is_fullscreen: bool) {
|
|
|
|
|
//Short circuit evaluation if we're already g2g
|
|
|
|
|
if self
|
|
|
|
|
.cx
|
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map(|w| w.is_fullscreen == is_fullscreen)
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.update(|this| {
|
|
|
|
|
let window = this.cx.windows.get_mut(&window_id)?;
|
|
|
|
|
window.is_fullscreen = is_fullscreen;
|
|
|
|
|
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut observations = this.window_fullscreen_observations.clone();
|
2022-08-09 22:52:25 +00:00
|
|
|
|
observations.emit_and_cleanup(window_id, this, |callback, this| {
|
|
|
|
|
callback(is_fullscreen, this)
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
Some(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 10:15:40 +00:00
|
|
|
|
fn handle_window_activation_effect(&mut self, window_id: usize, active: bool) {
|
2022-08-09 19:34:57 +00:00
|
|
|
|
//Short circuit evaluation if we're already g2g
|
2022-04-22 21:18:50 +00:00
|
|
|
|
if self
|
|
|
|
|
.cx
|
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
2022-08-09 19:34:57 +00:00
|
|
|
|
.map(|w| w.is_active == active)
|
|
|
|
|
.unwrap_or(false)
|
2022-04-22 21:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.update(|this| {
|
|
|
|
|
let window = this.cx.windows.get_mut(&window_id)?;
|
|
|
|
|
window.is_active = active;
|
2022-08-09 19:34:57 +00:00
|
|
|
|
|
|
|
|
|
//Handle focus
|
2022-08-08 02:23:22 +00:00
|
|
|
|
let focused_id = window.focused_view_id?;
|
2022-10-16 17:46:31 +00:00
|
|
|
|
for view_id in this.ancestors(window_id, focused_id).collect::<Vec<_>>() {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
|
|
|
|
|
if active {
|
2022-10-16 17:46:31 +00:00
|
|
|
|
view.focus_in(this, window_id, view_id, focused_id);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
} else {
|
2022-10-16 17:46:31 +00:00
|
|
|
|
view.focus_out(this, window_id, view_id, focused_id);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
}
|
|
|
|
|
this.cx.views.insert((window_id, view_id), view);
|
2022-04-22 21:18:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 23:59:13 +00:00
|
|
|
|
let mut observations = this.window_activation_observations.clone();
|
2022-08-09 22:10:30 +00:00
|
|
|
|
observations.emit_and_cleanup(window_id, this, |callback, this| callback(active, this));
|
2022-07-05 10:15:40 +00:00
|
|
|
|
|
2022-04-22 21:18:50 +00:00
|
|
|
|
Some(())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 14:52:24 +00:00
|
|
|
|
fn handle_focus_effect(&mut self, window_id: usize, focused_id: Option<usize>) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
if self
|
2021-05-28 22:25:15 +00:00
|
|
|
|
.cx
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.windows
|
|
|
|
|
.get(&window_id)
|
2021-05-06 05:21:19 +00:00
|
|
|
|
.map(|w| w.focused_view_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.map_or(false, |cur_focused| cur_focused == focused_id)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
2022-03-04 16:28:53 +00:00
|
|
|
|
let blurred_id = this.cx.windows.get_mut(&window_id).and_then(|window| {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
let blurred_id = window.focused_view_id;
|
|
|
|
|
window.focused_view_id = focused_id;
|
|
|
|
|
blurred_id
|
|
|
|
|
});
|
2021-05-06 04:48:16 +00:00
|
|
|
|
|
2022-08-08 02:23:22 +00:00
|
|
|
|
let blurred_parents = blurred_id
|
2022-10-16 17:46:31 +00:00
|
|
|
|
.map(|blurred_id| this.ancestors(window_id, blurred_id).collect::<Vec<_>>())
|
2022-08-08 02:23:22 +00:00
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
let focused_parents = focused_id
|
2022-10-16 17:46:31 +00:00
|
|
|
|
.map(|focused_id| this.ancestors(window_id, focused_id).collect::<Vec<_>>())
|
2022-08-08 02:23:22 +00:00
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
if let Some(blurred_id) = blurred_id {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
for view_id in blurred_parents.iter().copied() {
|
|
|
|
|
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
|
2022-10-16 17:46:31 +00:00
|
|
|
|
view.focus_out(this, window_id, view_id, blurred_id);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
this.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
}
|
2021-11-29 22:16:19 +00:00
|
|
|
|
}
|
2022-08-08 02:23:22 +00:00
|
|
|
|
|
|
|
|
|
let mut subscriptions = this.focus_observations.clone();
|
|
|
|
|
subscriptions
|
|
|
|
|
.emit_and_cleanup(blurred_id, this, |callback, this| callback(false, this));
|
2021-05-06 04:48:16 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-04 16:28:53 +00:00
|
|
|
|
if let Some(focused_id) = focused_id {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
for view_id in focused_parents {
|
|
|
|
|
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
|
2022-10-16 17:46:31 +00:00
|
|
|
|
view.focus_in(this, window_id, view_id, focused_id);
|
2022-08-09 22:09:38 +00:00
|
|
|
|
this.cx.views.insert((window_id, view_id), view);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
}
|
2022-03-04 16:28:53 +00:00
|
|
|
|
}
|
2022-08-08 02:23:22 +00:00
|
|
|
|
|
|
|
|
|
let mut subscriptions = this.focus_observations.clone();
|
|
|
|
|
subscriptions
|
|
|
|
|
.emit_and_cleanup(focused_id, this, |callback, this| callback(true, this));
|
2021-11-29 22:16:19 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
fn handle_dispatch_action_from_effect(
|
2022-08-05 03:55:10 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: Option<usize>,
|
|
|
|
|
action: &dyn Action,
|
|
|
|
|
) -> bool {
|
|
|
|
|
self.update(|this| {
|
|
|
|
|
if let Some(view_id) = view_id {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
this.halt_action_dispatch = false;
|
2022-08-05 03:55:10 +00:00
|
|
|
|
this.visit_dispatch_path(window_id, view_id, |view_id, capture_phase, this| {
|
|
|
|
|
if let Some(mut view) = this.cx.views.remove(&(window_id, view_id)) {
|
|
|
|
|
let type_id = view.as_any().type_id();
|
|
|
|
|
|
|
|
|
|
if let Some((name, mut handlers)) = this
|
|
|
|
|
.actions_mut(capture_phase)
|
|
|
|
|
.get_mut(&type_id)
|
|
|
|
|
.and_then(|h| h.remove_entry(&action.id()))
|
|
|
|
|
{
|
|
|
|
|
for handler in handlers.iter_mut().rev() {
|
|
|
|
|
this.halt_action_dispatch = true;
|
|
|
|
|
handler(view.as_mut(), action, this, window_id, view_id);
|
|
|
|
|
if this.halt_action_dispatch {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.actions_mut(capture_phase)
|
|
|
|
|
.get_mut(&type_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.insert(name, handlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.cx.views.insert((window_id, view_id), view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
!this.halt_action_dispatch
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !this.halt_action_dispatch {
|
|
|
|
|
this.halt_action_dispatch = this.dispatch_global_action_any(action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.pending_effects
|
|
|
|
|
.push_back(Effect::ActionDispatchNotification {
|
|
|
|
|
action_id: action.id(),
|
|
|
|
|
});
|
|
|
|
|
this.halt_action_dispatch
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 08:01:23 +00:00
|
|
|
|
fn handle_action_dispatch_notification_effect(&mut self, action_id: TypeId) {
|
|
|
|
|
let mut callbacks = mem::take(&mut *self.action_dispatch_observations.lock());
|
2022-08-10 21:39:24 +00:00
|
|
|
|
for callback in callbacks.values_mut() {
|
2022-05-30 08:01:23 +00:00
|
|
|
|
callback(action_id, self);
|
|
|
|
|
}
|
|
|
|
|
self.action_dispatch_observations.lock().extend(callbacks);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 09:43:19 +00:00
|
|
|
|
fn handle_window_should_close_subscription_effect(
|
|
|
|
|
&mut self,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
mut callback: WindowShouldCloseSubscriptionCallback,
|
|
|
|
|
) {
|
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
|
if let Some((_, window)) = self.presenters_and_platform_windows.get_mut(&window_id) {
|
|
|
|
|
window.on_should_close(Box::new(move || app.update(|cx| callback(cx))))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 13:21:02 +00:00
|
|
|
|
pub fn focus(&mut self, window_id: usize, view_id: Option<usize>) {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::Focus { window_id, view_id });
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub fn spawn<F, Fut, T>(&self, f: F) -> Task<T>
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-05-12 21:16:49 +00:00
|
|
|
|
F: FnOnce(AsyncAppContext) -> Fut,
|
|
|
|
|
Fut: 'static + Future<Output = T>,
|
2021-03-19 02:10:32 +00:00
|
|
|
|
T: 'static,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
2022-01-22 01:43:24 +00:00
|
|
|
|
let future = f(self.to_async());
|
2021-06-16 00:22:48 +00:00
|
|
|
|
let cx = self.to_async();
|
2022-01-22 01:43:24 +00:00
|
|
|
|
self.foreground.spawn(async move {
|
|
|
|
|
let result = future.await;
|
|
|
|
|
cx.0.borrow_mut().flush_effects();
|
|
|
|
|
result
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 00:22:48 +00:00
|
|
|
|
pub fn to_async(&self) -> AsyncAppContext {
|
|
|
|
|
AsyncAppContext(self.weak_self.as_ref().unwrap().upgrade().unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 17:03:56 +00:00
|
|
|
|
pub fn write_to_clipboard(&self, item: ClipboardItem) {
|
2021-07-16 15:53:23 +00:00
|
|
|
|
self.cx.platform.write_to_clipboard(item);
|
2021-04-08 03:54:05 +00:00
|
|
|
|
}
|
2021-04-13 12:58:10 +00:00
|
|
|
|
|
2021-04-13 17:03:56 +00:00
|
|
|
|
pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
|
2021-07-16 15:53:23 +00:00
|
|
|
|
self.cx.platform.read_from_clipboard()
|
2021-04-08 03:54:05 +00:00
|
|
|
|
}
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
pub fn leak_detector(&self) -> Arc<Mutex<LeakDetector>> {
|
|
|
|
|
self.cx.ref_counts.lock().leak_detector.clone()
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl ReadModel for MutableAppContext {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
if let Some(model) = self.cx.models.get(&handle.model_id) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
model
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref()
|
2021-05-06 22:19:38 +00:00
|
|
|
|
.expect("downcast is type safe")
|
2021-03-10 04:00:51 +00:00
|
|
|
|
} else {
|
2021-05-06 22:19:38 +00:00
|
|
|
|
panic!("circular model reference");
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpdateModel for MutableAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<T: Entity, V>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
|
|
|
|
|
) -> V {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
if let Some(mut model) = self.cx.models.remove(&handle.model_id) {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let mut cx = ModelContext::new(this, handle.model_id);
|
|
|
|
|
let result = update(
|
|
|
|
|
model
|
|
|
|
|
.as_any_mut()
|
|
|
|
|
.downcast_mut()
|
|
|
|
|
.expect("downcast is type safe"),
|
|
|
|
|
&mut cx,
|
|
|
|
|
);
|
|
|
|
|
this.cx.models.insert(handle.model_id, model);
|
|
|
|
|
result
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
} else {
|
2021-05-06 22:19:38 +00:00
|
|
|
|
panic!("circular model update");
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl UpgradeModelHandle for MutableAppContext {
|
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
2022-02-11 13:41:19 +00:00
|
|
|
|
handle: &WeakModelHandle<T>,
|
2021-08-18 17:45:29 +00:00
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
|
}
|
2022-02-16 01:56:50 +00:00
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.cx.model_handle_is_upgradable(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
self.cx.upgrade_any_model_handle(handle)
|
|
|
|
|
}
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
impl UpgradeViewHandle for MutableAppContext {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
|
|
|
|
|
self.cx.upgrade_view_handle(handle)
|
|
|
|
|
}
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
self.cx.upgrade_any_view_handle(handle)
|
|
|
|
|
}
|
2022-02-11 13:41:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl ReadView for MutableAppContext {
|
|
|
|
|
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
if let Some(view) = self.cx.views.get(&(handle.window_id, handle.view_id)) {
|
2021-05-06 04:48:16 +00:00
|
|
|
|
view.as_any().downcast_ref().expect("downcast is type safe")
|
2021-03-10 04:00:51 +00:00
|
|
|
|
} else {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
panic!("circular view reference for type {}", type_name::<T>());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UpdateView for MutableAppContext {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_view<T, S>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ViewHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
|
) -> S
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
{
|
2021-11-29 22:16:19 +00:00
|
|
|
|
self.update(|this| {
|
|
|
|
|
let mut view = this
|
|
|
|
|
.cx
|
|
|
|
|
.views
|
|
|
|
|
.remove(&(handle.window_id, handle.view_id))
|
|
|
|
|
.expect("circular view update");
|
|
|
|
|
|
|
|
|
|
let mut cx = ViewContext::new(this, handle.window_id, handle.view_id);
|
|
|
|
|
let result = update(
|
|
|
|
|
view.as_any_mut()
|
|
|
|
|
.downcast_mut()
|
|
|
|
|
.expect("downcast is type safe"),
|
|
|
|
|
&mut cx,
|
|
|
|
|
);
|
|
|
|
|
this.cx
|
|
|
|
|
.views
|
|
|
|
|
.insert((handle.window_id, handle.view_id), view);
|
|
|
|
|
result
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 22:58:19 +00:00
|
|
|
|
impl AsRef<AppContext> for MutableAppContext {
|
|
|
|
|
fn as_ref(&self) -> &AppContext {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.cx
|
2021-04-09 22:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:52:08 +00:00
|
|
|
|
impl Deref for MutableAppContext {
|
|
|
|
|
type Target = AppContext;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.cx
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
#[derive(Debug)]
|
2022-08-05 03:55:10 +00:00
|
|
|
|
pub enum ParentId {
|
|
|
|
|
View(usize),
|
|
|
|
|
Root,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct AppContext {
|
|
|
|
|
models: HashMap<usize, Box<dyn AnyModel>>,
|
2021-05-06 04:48:16 +00:00
|
|
|
|
views: HashMap<(usize, usize), Box<dyn AnyView>>,
|
2022-08-05 19:38:05 +00:00
|
|
|
|
pub(crate) parents: HashMap<(usize, usize), ParentId>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
windows: HashMap<usize, Window>,
|
2022-03-17 13:33:01 +00:00
|
|
|
|
globals: HashMap<TypeId, Box<dyn Any>>,
|
2022-02-17 21:43:58 +00:00
|
|
|
|
element_states: HashMap<ElementStateId, Box<dyn Any>>,
|
2021-04-05 23:45:55 +00:00
|
|
|
|
background: Arc<executor::Background>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
2021-04-27 22:51:23 +00:00
|
|
|
|
font_cache: Arc<FontCache>,
|
2021-07-16 15:53:23 +00:00
|
|
|
|
platform: Arc<dyn Platform>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppContext {
|
2022-04-12 02:51:46 +00:00
|
|
|
|
pub(crate) fn root_view(&self, window_id: usize) -> Option<AnyViewHandle> {
|
|
|
|
|
self.windows
|
|
|
|
|
.get(&window_id)
|
|
|
|
|
.map(|window| window.root_view.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
|
|
|
|
|
self.windows
|
|
|
|
|
.get(&window_id)
|
2021-05-06 05:21:19 +00:00
|
|
|
|
.map(|window| window.root_view.id())
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
|
|
|
|
|
self.windows
|
|
|
|
|
.get(&window_id)
|
2022-03-04 16:28:53 +00:00
|
|
|
|
.and_then(|window| window.focused_view_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 13:40:21 +00:00
|
|
|
|
pub fn view_ui_name(&self, window_id: usize, view_id: usize) -> Option<&'static str> {
|
|
|
|
|
Some(self.views.get(&(window_id, view_id))?.ui_name())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
pub fn background(&self) -> &Arc<executor::Background> {
|
2021-04-05 23:45:55 +00:00
|
|
|
|
&self.background
|
|
|
|
|
}
|
2021-04-14 01:15:08 +00:00
|
|
|
|
|
2021-07-16 15:53:23 +00:00
|
|
|
|
pub fn font_cache(&self) -> &Arc<FontCache> {
|
2021-04-27 22:51:23 +00:00
|
|
|
|
&self.font_cache
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 15:53:23 +00:00
|
|
|
|
pub fn platform(&self) -> &Arc<dyn Platform> {
|
|
|
|
|
&self.platform
|
|
|
|
|
}
|
2022-02-28 01:07:46 +00:00
|
|
|
|
|
2022-03-18 13:20:09 +00:00
|
|
|
|
pub fn has_global<T: 'static>(&self) -> bool {
|
|
|
|
|
self.globals.contains_key(&TypeId::of::<T>())
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 13:33:01 +00:00
|
|
|
|
pub fn global<T: 'static>(&self) -> &T {
|
2022-04-06 17:20:57 +00:00
|
|
|
|
if let Some(global) = self.globals.get(&TypeId::of::<T>()) {
|
|
|
|
|
global.downcast_ref().unwrap()
|
|
|
|
|
} else {
|
|
|
|
|
panic!("no global has been added for {}", type_name::<T>());
|
|
|
|
|
}
|
2022-02-28 01:07:46 +00:00
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl ReadModel for AppContext {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
if let Some(model) = self.models.get(&handle.model_id) {
|
|
|
|
|
model
|
|
|
|
|
.as_any()
|
|
|
|
|
.downcast_ref()
|
|
|
|
|
.expect("downcast should be type safe")
|
|
|
|
|
} else {
|
|
|
|
|
panic!("circular model reference");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl UpgradeModelHandle for AppContext {
|
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
2022-02-11 13:41:19 +00:00
|
|
|
|
handle: &WeakModelHandle<T>,
|
2021-08-18 17:45:29 +00:00
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
|
if self.models.contains_key(&handle.model_id) {
|
|
|
|
|
Some(ModelHandle::new(handle.model_id, &self.ref_counts))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-16 01:56:50 +00:00
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.models.contains_key(&handle.model_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
if self.models.contains_key(&handle.model_id) {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
Some(AnyModelHandle::new(
|
|
|
|
|
handle.model_id,
|
|
|
|
|
handle.model_type,
|
|
|
|
|
self.ref_counts.clone(),
|
|
|
|
|
))
|
2022-02-16 01:56:50 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
impl UpgradeViewHandle for AppContext {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
|
|
|
|
|
if self.ref_counts.lock().is_entity_alive(handle.view_id) {
|
|
|
|
|
Some(ViewHandle::new(
|
|
|
|
|
handle.window_id,
|
|
|
|
|
handle.view_id,
|
|
|
|
|
&self.ref_counts,
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
if self.ref_counts.lock().is_entity_alive(handle.view_id) {
|
|
|
|
|
Some(AnyViewHandle::new(
|
|
|
|
|
handle.window_id,
|
|
|
|
|
handle.view_id,
|
|
|
|
|
handle.view_type,
|
|
|
|
|
self.ref_counts.clone(),
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-11 13:41:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl ReadView for AppContext {
|
|
|
|
|
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
2021-05-06 04:48:16 +00:00
|
|
|
|
if let Some(view) = self.views.get(&(handle.window_id, handle.view_id)) {
|
|
|
|
|
view.as_any()
|
|
|
|
|
.downcast_ref()
|
|
|
|
|
.expect("downcast should be type safe")
|
2021-03-10 04:00:51 +00:00
|
|
|
|
} else {
|
2021-05-06 04:48:16 +00:00
|
|
|
|
panic!("circular view reference");
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Window {
|
2021-05-06 05:21:19 +00:00
|
|
|
|
root_view: AnyViewHandle,
|
2022-03-04 16:28:53 +00:00
|
|
|
|
focused_view_id: Option<usize>,
|
2022-04-22 21:18:50 +00:00
|
|
|
|
is_active: bool,
|
2022-08-09 19:34:57 +00:00
|
|
|
|
is_fullscreen: bool,
|
2021-05-06 04:48:16 +00:00
|
|
|
|
invalidation: Option<WindowInvalidation>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
|
pub struct WindowInvalidation {
|
|
|
|
|
pub updated: HashSet<usize>,
|
|
|
|
|
pub removed: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum Effect {
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Subscription {
|
2022-03-22 14:57:30 +00:00
|
|
|
|
entity_id: usize,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: SubscriptionCallback,
|
|
|
|
|
},
|
2021-03-10 04:00:51 +00:00
|
|
|
|
Event {
|
|
|
|
|
entity_id: usize,
|
|
|
|
|
payload: Box<dyn Any>,
|
|
|
|
|
},
|
2022-03-22 15:24:48 +00:00
|
|
|
|
GlobalSubscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
type_id: TypeId,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: GlobalSubscriptionCallback,
|
|
|
|
|
},
|
2022-03-11 04:03:01 +00:00
|
|
|
|
GlobalEvent {
|
|
|
|
|
payload: Box<dyn Any>,
|
|
|
|
|
},
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Observation {
|
|
|
|
|
entity_id: usize,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: ObservationCallback,
|
|
|
|
|
},
|
2021-03-10 04:00:51 +00:00
|
|
|
|
ModelNotification {
|
|
|
|
|
model_id: usize,
|
|
|
|
|
},
|
|
|
|
|
ViewNotification {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
},
|
2022-03-23 00:03:24 +00:00
|
|
|
|
Deferred {
|
|
|
|
|
callback: Box<dyn FnOnce(&mut MutableAppContext)>,
|
|
|
|
|
after_window_update: bool,
|
|
|
|
|
},
|
2022-03-22 23:38:17 +00:00
|
|
|
|
GlobalNotification {
|
|
|
|
|
type_id: TypeId,
|
|
|
|
|
},
|
2022-03-08 18:00:54 +00:00
|
|
|
|
ModelRelease {
|
|
|
|
|
model_id: usize,
|
|
|
|
|
model: Box<dyn AnyModel>,
|
|
|
|
|
},
|
|
|
|
|
ViewRelease {
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view: Box<dyn AnyView>,
|
2022-01-21 13:22:06 +00:00
|
|
|
|
},
|
2021-03-10 04:00:51 +00:00
|
|
|
|
Focus {
|
|
|
|
|
window_id: usize,
|
2022-03-04 16:28:53 +00:00
|
|
|
|
view_id: Option<usize>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
},
|
2022-04-14 16:22:32 +00:00
|
|
|
|
FocusObservation {
|
|
|
|
|
view_id: usize,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: FocusObservationCallback,
|
|
|
|
|
},
|
2021-08-27 22:03:37 +00:00
|
|
|
|
ResizeWindow {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
},
|
2022-08-09 19:34:57 +00:00
|
|
|
|
FullscreenWindow {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
is_fullscreen: bool,
|
|
|
|
|
},
|
2022-04-22 21:18:50 +00:00
|
|
|
|
ActivateWindow {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
is_active: bool,
|
|
|
|
|
},
|
2022-07-05 10:15:40 +00:00
|
|
|
|
WindowActivationObservation {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: WindowActivationCallback,
|
|
|
|
|
},
|
2022-08-09 22:52:25 +00:00
|
|
|
|
WindowFullscreenObservation {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
subscription_id: usize,
|
|
|
|
|
callback: WindowFullscreenCallback,
|
|
|
|
|
},
|
2021-08-26 13:00:00 +00:00
|
|
|
|
RefreshWindows,
|
2022-08-05 03:55:10 +00:00
|
|
|
|
DispatchActionFrom {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
action: Box<dyn Action>,
|
|
|
|
|
},
|
2022-05-30 08:01:23 +00:00
|
|
|
|
ActionDispatchNotification {
|
|
|
|
|
action_id: TypeId,
|
|
|
|
|
},
|
2022-06-23 09:43:19 +00:00
|
|
|
|
WindowShouldCloseSubscription {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
callback: WindowShouldCloseSubscriptionCallback,
|
|
|
|
|
},
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 15:44:10 +00:00
|
|
|
|
impl Debug for Effect {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::Subscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
..
|
|
|
|
|
} => f
|
2022-03-22 14:57:30 +00:00
|
|
|
|
.debug_struct("Effect::Subscribe")
|
|
|
|
|
.field("entity_id", entity_id)
|
2022-03-22 15:11:55 +00:00
|
|
|
|
.field("subscription_id", subscription_id)
|
2022-03-22 14:57:30 +00:00
|
|
|
|
.finish(),
|
2021-07-12 15:44:10 +00:00
|
|
|
|
Effect::Event { entity_id, .. } => f
|
|
|
|
|
.debug_struct("Effect::Event")
|
|
|
|
|
.field("entity_id", entity_id)
|
|
|
|
|
.finish(),
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::GlobalSubscription {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
type_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
..
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::Subscribe")
|
|
|
|
|
.field("type_id", type_id)
|
|
|
|
|
.field("subscription_id", subscription_id)
|
|
|
|
|
.finish(),
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Effect::GlobalEvent { payload, .. } => f
|
|
|
|
|
.debug_struct("Effect::GlobalEvent")
|
|
|
|
|
.field("type_id", &(&*payload).type_id())
|
|
|
|
|
.finish(),
|
2022-03-22 15:24:48 +00:00
|
|
|
|
Effect::Observation {
|
|
|
|
|
entity_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
..
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::Observation")
|
|
|
|
|
.field("entity_id", entity_id)
|
|
|
|
|
.field("subscription_id", subscription_id)
|
|
|
|
|
.finish(),
|
2021-07-12 15:44:10 +00:00
|
|
|
|
Effect::ModelNotification { model_id } => f
|
|
|
|
|
.debug_struct("Effect::ModelNotification")
|
|
|
|
|
.field("model_id", model_id)
|
|
|
|
|
.finish(),
|
|
|
|
|
Effect::ViewNotification { window_id, view_id } => f
|
|
|
|
|
.debug_struct("Effect::ViewNotification")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("view_id", view_id)
|
|
|
|
|
.finish(),
|
2022-03-22 23:38:17 +00:00
|
|
|
|
Effect::GlobalNotification { type_id } => f
|
|
|
|
|
.debug_struct("Effect::GlobalNotification")
|
|
|
|
|
.field("type_id", type_id)
|
|
|
|
|
.finish(),
|
2022-03-23 00:03:24 +00:00
|
|
|
|
Effect::Deferred { .. } => f.debug_struct("Effect::Deferred").finish(),
|
2022-03-08 18:00:54 +00:00
|
|
|
|
Effect::ModelRelease { model_id, .. } => f
|
|
|
|
|
.debug_struct("Effect::ModelRelease")
|
|
|
|
|
.field("model_id", model_id)
|
|
|
|
|
.finish(),
|
|
|
|
|
Effect::ViewRelease { view_id, .. } => f
|
|
|
|
|
.debug_struct("Effect::ViewRelease")
|
|
|
|
|
.field("view_id", view_id)
|
2022-01-21 13:22:06 +00:00
|
|
|
|
.finish(),
|
2021-07-12 15:44:10 +00:00
|
|
|
|
Effect::Focus { window_id, view_id } => f
|
|
|
|
|
.debug_struct("Effect::Focus")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("view_id", view_id)
|
|
|
|
|
.finish(),
|
2022-04-14 16:22:32 +00:00
|
|
|
|
Effect::FocusObservation {
|
|
|
|
|
view_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
..
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::FocusObservation")
|
|
|
|
|
.field("view_id", view_id)
|
|
|
|
|
.field("subscription_id", subscription_id)
|
|
|
|
|
.finish(),
|
2022-08-05 03:55:10 +00:00
|
|
|
|
Effect::DispatchActionFrom {
|
|
|
|
|
window_id, view_id, ..
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::DispatchActionFrom")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("view_id", view_id)
|
|
|
|
|
.finish(),
|
2022-05-30 08:01:23 +00:00
|
|
|
|
Effect::ActionDispatchNotification { action_id, .. } => f
|
|
|
|
|
.debug_struct("Effect::ActionDispatchNotification")
|
|
|
|
|
.field("action_id", action_id)
|
|
|
|
|
.finish(),
|
2021-08-27 22:03:37 +00:00
|
|
|
|
Effect::ResizeWindow { window_id } => f
|
|
|
|
|
.debug_struct("Effect::RefreshWindow")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.finish(),
|
2022-07-05 10:15:40 +00:00
|
|
|
|
Effect::WindowActivationObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
..
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::WindowActivationObservation")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("subscription_id", subscription_id)
|
|
|
|
|
.finish(),
|
2022-04-22 21:18:50 +00:00
|
|
|
|
Effect::ActivateWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_active,
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::ActivateWindow")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("is_active", is_active)
|
|
|
|
|
.finish(),
|
2022-08-09 19:34:57 +00:00
|
|
|
|
Effect::FullscreenWindow {
|
|
|
|
|
window_id,
|
|
|
|
|
is_fullscreen,
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::FullscreenWindow")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("is_fullscreen", is_fullscreen)
|
|
|
|
|
.finish(),
|
2022-08-09 22:52:25 +00:00
|
|
|
|
Effect::WindowFullscreenObservation {
|
|
|
|
|
window_id,
|
|
|
|
|
subscription_id,
|
|
|
|
|
callback: _,
|
|
|
|
|
} => f
|
|
|
|
|
.debug_struct("Effect::WindowFullscreenObservation")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.field("subscription_id", subscription_id)
|
|
|
|
|
.finish(),
|
2021-08-26 13:00:00 +00:00
|
|
|
|
Effect::RefreshWindows => f.debug_struct("Effect::FullViewRefresh").finish(),
|
2022-06-23 09:43:19 +00:00
|
|
|
|
Effect::WindowShouldCloseSubscription { window_id, .. } => f
|
|
|
|
|
.debug_struct("Effect::WindowShouldCloseSubscription")
|
|
|
|
|
.field("window_id", window_id)
|
|
|
|
|
.finish(),
|
2021-07-12 15:44:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 15:04:32 +00:00
|
|
|
|
pub trait AnyModel {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
2021-06-25 16:35:06 +00:00
|
|
|
|
fn release(&mut self, cx: &mut MutableAppContext);
|
2021-11-02 19:16:25 +00:00
|
|
|
|
fn app_will_quit(
|
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> AnyModel for T
|
|
|
|
|
where
|
|
|
|
|
T: Entity,
|
|
|
|
|
{
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
|
|
|
|
fn release(&mut self, cx: &mut MutableAppContext) {
|
|
|
|
|
self.release(cx);
|
|
|
|
|
}
|
2021-11-02 19:16:25 +00:00
|
|
|
|
|
|
|
|
|
fn app_will_quit(
|
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
|
|
|
|
|
self.app_will_quit(cx)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 15:04:32 +00:00
|
|
|
|
pub trait AnyView {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
2021-06-25 16:35:06 +00:00
|
|
|
|
fn release(&mut self, cx: &mut MutableAppContext);
|
2021-11-02 19:16:25 +00:00
|
|
|
|
fn app_will_quit(
|
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>>;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn ui_name(&self) -> &'static str;
|
2022-08-10 21:39:24 +00:00
|
|
|
|
fn render(&mut self, params: RenderParams, cx: &mut MutableAppContext) -> ElementBox;
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_in(
|
2022-08-08 02:23:22 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
focused_id: usize,
|
|
|
|
|
);
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_out(
|
2022-08-08 02:23:22 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
focused_id: usize,
|
|
|
|
|
);
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn key_down(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &KeyDownEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool;
|
|
|
|
|
fn key_up(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &KeyUpEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool;
|
|
|
|
|
fn modifiers_changed(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &ModifiersChangedEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool;
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn keymap_context(&self, cx: &AppContext) -> keymap::Context;
|
2022-04-12 02:51:46 +00:00
|
|
|
|
fn debug_json(&self, cx: &AppContext) -> serde_json::Value;
|
2022-07-20 23:44:26 +00:00
|
|
|
|
|
|
|
|
|
fn text_for_range(&self, range: Range<usize>, cx: &AppContext) -> Option<String>;
|
|
|
|
|
fn selected_text_range(&self, cx: &AppContext) -> Option<Range<usize>>;
|
|
|
|
|
fn marked_text_range(&self, cx: &AppContext) -> Option<Range<usize>>;
|
|
|
|
|
fn unmark_text(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
|
|
|
|
|
fn replace_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
range: Option<Range<usize>>,
|
|
|
|
|
text: &str,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
);
|
|
|
|
|
fn replace_and_mark_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
range: Option<Range<usize>>,
|
|
|
|
|
new_text: &str,
|
|
|
|
|
new_selected_range: Option<Range<usize>>,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
fn any_handle(&self, window_id: usize, view_id: usize, cx: &AppContext) -> AnyViewHandle {
|
|
|
|
|
AnyViewHandle::new(
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
self.as_any().type_id(),
|
|
|
|
|
cx.ref_counts.clone(),
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> AnyView for T
|
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
{
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
fn release(&mut self, cx: &mut MutableAppContext) {
|
|
|
|
|
self.release(cx);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 19:16:25 +00:00
|
|
|
|
fn app_will_quit(
|
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
) -> Option<Pin<Box<dyn 'static + Future<Output = ()>>>> {
|
|
|
|
|
self.app_will_quit(cx)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn ui_name(&self) -> &'static str {
|
|
|
|
|
T::ui_name()
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
fn render(&mut self, params: RenderParams, cx: &mut MutableAppContext) -> ElementBox {
|
2022-05-26 19:22:23 +00:00
|
|
|
|
View::render(self, &mut RenderContext::new(params, cx))
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_in(
|
2022-08-08 02:23:22 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
focused_id: usize,
|
|
|
|
|
) {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
let focused_view_handle: AnyViewHandle = if view_id == focused_id {
|
|
|
|
|
cx.handle().into()
|
|
|
|
|
} else {
|
|
|
|
|
let focused_type = cx
|
|
|
|
|
.views
|
|
|
|
|
.get(&(window_id, focused_id))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_any()
|
|
|
|
|
.type_id();
|
|
|
|
|
AnyViewHandle::new(window_id, focused_id, focused_type, cx.ref_counts.clone())
|
|
|
|
|
};
|
2022-10-16 17:46:31 +00:00
|
|
|
|
View::focus_in(self, focused_view_handle, &mut cx);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_out(
|
2022-08-08 02:23:22 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
blurred_id: usize,
|
|
|
|
|
) {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
2022-08-08 02:23:22 +00:00
|
|
|
|
let blurred_view_handle: AnyViewHandle = if view_id == blurred_id {
|
|
|
|
|
cx.handle().into()
|
|
|
|
|
} else {
|
|
|
|
|
let blurred_type = cx
|
|
|
|
|
.views
|
|
|
|
|
.get(&(window_id, blurred_id))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_any()
|
|
|
|
|
.type_id();
|
|
|
|
|
AnyViewHandle::new(window_id, blurred_id, blurred_type, cx.ref_counts.clone())
|
|
|
|
|
};
|
2022-10-16 17:46:31 +00:00
|
|
|
|
View::focus_out(self, blurred_view_handle, &mut cx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn key_down(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &KeyDownEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::key_down(self, event, &mut cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn key_up(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &KeyUpEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::key_up(self, event, &mut cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn modifiers_changed(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &ModifiersChangedEvent,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::modifiers_changed(self, event, &mut cx)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn keymap_context(&self, cx: &AppContext) -> keymap::Context {
|
|
|
|
|
View::keymap_context(self, cx)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-04-12 02:51:46 +00:00
|
|
|
|
|
|
|
|
|
fn debug_json(&self, cx: &AppContext) -> serde_json::Value {
|
|
|
|
|
View::debug_json(self, cx)
|
|
|
|
|
}
|
2022-07-20 23:44:26 +00:00
|
|
|
|
|
|
|
|
|
fn text_for_range(&self, range: Range<usize>, cx: &AppContext) -> Option<String> {
|
|
|
|
|
View::text_for_range(self, range, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn selected_text_range(&self, cx: &AppContext) -> Option<Range<usize>> {
|
|
|
|
|
View::selected_text_range(self, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn marked_text_range(&self, cx: &AppContext) -> Option<Range<usize>> {
|
|
|
|
|
View::marked_text_range(self, cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unmark_text(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::unmark_text(self, &mut cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
range: Option<Range<usize>>,
|
|
|
|
|
text: &str,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::replace_text_in_range(self, range, text, &mut cx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_and_mark_text_in_range(
|
|
|
|
|
&mut self,
|
|
|
|
|
range: Option<Range<usize>>,
|
|
|
|
|
new_text: &str,
|
|
|
|
|
new_selected_range: Option<Range<usize>>,
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
) {
|
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
|
View::replace_and_mark_text_in_range(self, range, new_text, new_selected_range, &mut cx)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ModelContext<'a, T: ?Sized> {
|
|
|
|
|
app: &'a mut MutableAppContext,
|
|
|
|
|
model_id: usize,
|
|
|
|
|
model_type: PhantomData<T>,
|
|
|
|
|
halt_stream: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T: Entity> ModelContext<'a, T> {
|
|
|
|
|
fn new(app: &'a mut MutableAppContext, model_id: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
app,
|
|
|
|
|
model_id,
|
|
|
|
|
model_type: PhantomData,
|
|
|
|
|
halt_stream: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
pub fn background(&self) -> &Arc<executor::Background> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.app.cx.background
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn halt_stream(&mut self) {
|
|
|
|
|
self.halt_stream = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn model_id(&self) -> usize {
|
|
|
|
|
self.model_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
|
|
|
|
|
where
|
|
|
|
|
S: Entity,
|
|
|
|
|
F: FnOnce(&mut ModelContext<S>) -> S,
|
|
|
|
|
{
|
|
|
|
|
self.app.add_model(build_model)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 23:38:17 +00:00
|
|
|
|
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ModelContext<T>)) {
|
|
|
|
|
let handle = self.handle();
|
2022-03-25 02:24:36 +00:00
|
|
|
|
self.app.defer(move |cx| {
|
2022-03-22 23:38:17 +00:00
|
|
|
|
handle.update(cx, |model, cx| {
|
|
|
|
|
callback(model, cx);
|
|
|
|
|
})
|
2022-03-25 02:24:36 +00:00
|
|
|
|
})
|
2022-03-22 23:38:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn emit(&mut self, payload: T::Event) {
|
|
|
|
|
self.app.pending_effects.push_back(Effect::Event {
|
|
|
|
|
entity_id: self.model_id,
|
|
|
|
|
payload: Box::new(payload),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:58:37 +00:00
|
|
|
|
pub fn notify(&mut self) {
|
2021-09-30 21:06:09 +00:00
|
|
|
|
self.app.notify_model(self.model_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn subscribe<S: Entity, F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<S>,
|
|
|
|
|
mut callback: F,
|
|
|
|
|
) -> Subscription
|
2021-08-23 21:58:37 +00:00
|
|
|
|
where
|
|
|
|
|
S::Event: 'static,
|
|
|
|
|
F: 'static + FnMut(&mut T, ModelHandle<S>, &S::Event, &mut ModelContext<T>),
|
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let subscriber = self.weak_handle();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
self.app
|
2021-08-23 21:58:37 +00:00
|
|
|
|
.subscribe_internal(handle, move |emitter, event, cx| {
|
|
|
|
|
if let Some(subscriber) = subscriber.upgrade(cx) {
|
|
|
|
|
subscriber.update(cx, |subscriber, cx| {
|
|
|
|
|
callback(subscriber, emitter, event, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn observe<S, F>(&mut self, handle: &ModelHandle<S>, mut callback: F) -> Subscription
|
2021-08-23 21:58:37 +00:00
|
|
|
|
where
|
|
|
|
|
S: Entity,
|
|
|
|
|
F: 'static + FnMut(&mut T, ModelHandle<S>, &mut ModelContext<T>),
|
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let observer = self.weak_handle();
|
2021-08-23 21:58:37 +00:00
|
|
|
|
self.app.observe_internal(handle, move |observed, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, observed, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
2021-08-23 21:58:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 08:05:00 +00:00
|
|
|
|
pub fn observe_global<G, F>(&mut self, mut callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
G: Any,
|
|
|
|
|
F: 'static + FnMut(&mut T, &mut ModelContext<T>),
|
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
|
|
|
|
self.app.observe_global::<G, _>(move |cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| callback(observer, cx));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
pub fn observe_release<S, F>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<S>,
|
|
|
|
|
mut callback: F,
|
|
|
|
|
) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
S: Entity,
|
2022-03-08 18:00:54 +00:00
|
|
|
|
F: 'static + FnMut(&mut T, &S, &mut ModelContext<T>),
|
2022-01-21 13:22:06 +00:00
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
2022-03-08 18:00:54 +00:00
|
|
|
|
self.app.observe_release(handle, move |released, cx| {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
2022-03-08 18:00:54 +00:00
|
|
|
|
callback(observer, released, cx);
|
2022-01-21 13:22:06 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 14:32:52 +00:00
|
|
|
|
pub fn handle(&self) -> ModelHandle<T> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
ModelHandle::new(self.model_id, &self.app.cx.ref_counts)
|
2021-04-06 02:04:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 22:12:53 +00:00
|
|
|
|
pub fn weak_handle(&self) -> WeakModelHandle<T> {
|
|
|
|
|
WeakModelHandle::new(self.model_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-05-12 21:28:59 +00:00
|
|
|
|
F: FnOnce(ModelHandle<T>, AsyncAppContext) -> Fut,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
Fut: 'static + Future<Output = S>,
|
|
|
|
|
S: 'static,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
2021-04-06 02:04:04 +00:00
|
|
|
|
let handle = self.handle();
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.app.spawn(|cx| f(handle, cx))
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-07-06 00:37:19 +00:00
|
|
|
|
|
|
|
|
|
pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(WeakModelHandle<T>, AsyncAppContext) -> Fut,
|
|
|
|
|
Fut: 'static + Future<Output = S>,
|
|
|
|
|
S: 'static,
|
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let handle = self.weak_handle();
|
2021-07-06 00:37:19 +00:00
|
|
|
|
self.app.spawn(|cx| f(handle, cx))
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 02:07:25 +00:00
|
|
|
|
impl<M> AsRef<AppContext> for ModelContext<'_, M> {
|
|
|
|
|
fn as_ref(&self) -> &AppContext {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.app.cx
|
2021-04-14 02:07:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<M> AsMut<MutableAppContext> for ModelContext<'_, M> {
|
|
|
|
|
fn as_mut(&mut self) -> &mut MutableAppContext {
|
|
|
|
|
self.app
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl<M> ReadModel for ModelContext<'_, M> {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
|
|
|
|
|
self.app.read_model(handle)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<M> UpdateModel for ModelContext<'_, M> {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<T: Entity, V>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> V,
|
|
|
|
|
) -> V {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
self.app.update_model(handle, update)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl<M> UpgradeModelHandle for ModelContext<'_, M> {
|
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
2022-02-11 13:41:19 +00:00
|
|
|
|
handle: &WeakModelHandle<T>,
|
2021-08-18 17:45:29 +00:00
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
|
}
|
2022-02-16 01:56:50 +00:00
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.cx.model_handle_is_upgradable(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
self.cx.upgrade_any_model_handle(handle)
|
|
|
|
|
}
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 10:59:58 +00:00
|
|
|
|
impl<M> Deref for ModelContext<'_, M> {
|
|
|
|
|
type Target = MutableAppContext;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-08-10 21:39:24 +00:00
|
|
|
|
self.app
|
2021-07-26 10:59:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<M> DerefMut for ModelContext<'_, M> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.app
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct ViewContext<'a, T: ?Sized> {
|
|
|
|
|
app: &'a mut MutableAppContext,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: PhantomData<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T: View> ViewContext<'a, T> {
|
|
|
|
|
fn new(app: &'a mut MutableAppContext, window_id: usize, view_id: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
app,
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
view_type: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 02:04:04 +00:00
|
|
|
|
pub fn handle(&self) -> ViewHandle<T> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
ViewHandle::new(self.window_id, self.view_id, &self.app.cx.ref_counts)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 22:12:53 +00:00
|
|
|
|
pub fn weak_handle(&self) -> WeakViewHandle<T> {
|
|
|
|
|
WeakViewHandle::new(self.window_id, self.view_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn window_id(&self) -> usize {
|
|
|
|
|
self.window_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 20:18:41 +00:00
|
|
|
|
pub fn view_id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 21:33:42 +00:00
|
|
|
|
pub fn foreground(&self) -> &Rc<executor::Foreground> {
|
2021-06-23 23:40:43 +00:00
|
|
|
|
self.app.foreground()
|
2021-04-29 21:33:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 16:50:07 +00:00
|
|
|
|
pub fn background_executor(&self) -> &Arc<executor::Background> {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.app.cx.background
|
2021-03-21 16:50:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 00:40:38 +00:00
|
|
|
|
pub fn platform(&self) -> Arc<dyn Platform> {
|
|
|
|
|
self.app.platform()
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 12:51:22 +00:00
|
|
|
|
pub fn show_character_palette(&self) {
|
|
|
|
|
self.app.show_character_palette(self.window_id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 13:13:30 +00:00
|
|
|
|
pub fn minimize_window(&self) {
|
|
|
|
|
self.app.minimize_window(self.window_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn zoom_window(&self) {
|
|
|
|
|
self.app.zoom_window(self.window_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 09:53:37 +00:00
|
|
|
|
pub fn toggle_full_screen(&self) {
|
|
|
|
|
self.app.toggle_window_full_screen(self.window_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 13:43:51 +00:00
|
|
|
|
pub fn window_bounds(&self) -> RectF {
|
|
|
|
|
self.app.window_bounds(self.window_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
pub fn prompt(
|
|
|
|
|
&self,
|
|
|
|
|
level: PromptLevel,
|
|
|
|
|
msg: &str,
|
|
|
|
|
answers: &[&str],
|
|
|
|
|
) -> oneshot::Receiver<usize> {
|
|
|
|
|
self.app.prompt(self.window_id, level, msg, answers)
|
2021-05-12 09:54:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
pub fn prompt_for_paths(
|
|
|
|
|
&self,
|
|
|
|
|
options: PathPromptOptions,
|
|
|
|
|
) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
|
|
|
|
|
self.app.prompt_for_paths(options)
|
2021-05-05 02:04:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 08:51:29 +00:00
|
|
|
|
pub fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
|
|
|
|
|
self.app.prompt_for_new_path(directory)
|
2021-05-05 02:04:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 02:51:14 +00:00
|
|
|
|
pub fn debug_elements(&self) -> crate::json::Value {
|
|
|
|
|
self.app.debug_elements(self.window_id).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn focus<S>(&mut self, handle: S)
|
|
|
|
|
where
|
|
|
|
|
S: Into<AnyViewHandle>,
|
|
|
|
|
{
|
|
|
|
|
let handle = handle.into();
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.app.focus(handle.window_id, Some(handle.view_id));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn focus_self(&mut self) {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.app.focus(self.window_id, Some(self.view_id));
|
2022-03-04 16:28:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 18:00:11 +00:00
|
|
|
|
pub fn is_self_focused(&self) -> bool {
|
|
|
|
|
self.app.focused_view_id(self.window_id) == Some(self.view_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 22:10:00 +00:00
|
|
|
|
pub fn is_child(&self, view: impl Into<AnyViewHandle>) -> bool {
|
|
|
|
|
let view = view.into();
|
|
|
|
|
if self.window_id != view.window_id {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-10-16 17:46:31 +00:00
|
|
|
|
self.ancestors(view.window_id, view.view_id)
|
2022-10-12 22:10:00 +00:00
|
|
|
|
.any(|parent| parent == self.view_id)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-04 16:28:53 +00:00
|
|
|
|
pub fn blur(&mut self) {
|
2022-04-14 14:52:24 +00:00
|
|
|
|
self.app.focus(self.window_id, None);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 17:45:55 +00:00
|
|
|
|
pub fn set_window_title(&mut self, title: &str) {
|
|
|
|
|
let window_id = self.window_id();
|
|
|
|
|
if let Some((_, window)) = self.presenters_and_platform_windows.get_mut(&window_id) {
|
|
|
|
|
window.set_title(title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 08:59:50 +00:00
|
|
|
|
pub fn set_window_edited(&mut self, edited: bool) {
|
|
|
|
|
let window_id = self.window_id();
|
|
|
|
|
if let Some((_, window)) = self.presenters_and_platform_windows.get_mut(&window_id) {
|
|
|
|
|
window.set_edited(edited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 09:43:19 +00:00
|
|
|
|
pub fn on_window_should_close<F>(&mut self, mut callback: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut T, &mut ViewContext<T>) -> bool,
|
|
|
|
|
{
|
|
|
|
|
let window_id = self.window_id();
|
|
|
|
|
let view = self.weak_handle();
|
|
|
|
|
self.pending_effects
|
|
|
|
|
.push_back(Effect::WindowShouldCloseSubscription {
|
|
|
|
|
window_id,
|
|
|
|
|
callback: Box::new(move |cx| {
|
|
|
|
|
if let Some(view) = view.upgrade(cx) {
|
|
|
|
|
view.update(cx, |view, cx| callback(view, cx))
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn add_model<S, F>(&mut self, build_model: F) -> ModelHandle<S>
|
|
|
|
|
where
|
|
|
|
|
S: Entity,
|
|
|
|
|
F: FnOnce(&mut ModelContext<S>) -> S,
|
|
|
|
|
{
|
|
|
|
|
self.app.add_model(build_model)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_view<S, F>(&mut self, build_view: F) -> ViewHandle<S>
|
|
|
|
|
where
|
|
|
|
|
S: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<S>) -> S,
|
|
|
|
|
{
|
2022-08-05 03:55:10 +00:00
|
|
|
|
self.app
|
|
|
|
|
.build_and_insert_view(self.window_id, ParentId::View(self.view_id), |cx| {
|
|
|
|
|
Some(build_view(cx))
|
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_option_view<S, F>(&mut self, build_view: F) -> Option<ViewHandle<S>>
|
|
|
|
|
where
|
|
|
|
|
S: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<S>) -> Option<S>,
|
|
|
|
|
{
|
2022-08-05 03:55:10 +00:00
|
|
|
|
self.app
|
|
|
|
|
.build_and_insert_view(self.window_id, ParentId::View(self.view_id), build_view)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
pub fn reparent(&mut self, view_handle: impl Into<AnyViewHandle>) {
|
|
|
|
|
let view_handle = view_handle.into();
|
|
|
|
|
if self.window_id != view_handle.window_id {
|
|
|
|
|
panic!("Can't reparent view to a view from a different window");
|
|
|
|
|
}
|
|
|
|
|
self.cx
|
|
|
|
|
.parents
|
|
|
|
|
.remove(&(view_handle.window_id, view_handle.view_id));
|
|
|
|
|
let new_parent_id = self.view_id;
|
|
|
|
|
self.cx.parents.insert(
|
|
|
|
|
(view_handle.window_id, view_handle.view_id),
|
|
|
|
|
ParentId::View(new_parent_id),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 01:02:23 +00:00
|
|
|
|
pub fn replace_root_view<V, F>(&mut self, build_root_view: F) -> ViewHandle<V>
|
|
|
|
|
where
|
|
|
|
|
V: View,
|
|
|
|
|
F: FnOnce(&mut ViewContext<V>) -> V,
|
|
|
|
|
{
|
|
|
|
|
let window_id = self.window_id;
|
|
|
|
|
self.update(|this| {
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let root_view = this
|
|
|
|
|
.build_and_insert_view(window_id, ParentId::Root, |cx| Some(build_root_view(cx)))
|
|
|
|
|
.unwrap();
|
2022-05-17 01:02:23 +00:00
|
|
|
|
let window = this.cx.windows.get_mut(&window_id).unwrap();
|
|
|
|
|
window.root_view = root_view.clone().into();
|
|
|
|
|
window.focused_view_id = Some(root_view.id());
|
|
|
|
|
root_view
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-08-23 21:58:37 +00:00
|
|
|
|
E: Entity,
|
|
|
|
|
E::Event: 'static,
|
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(&mut T, H, &E::Event, &mut ViewContext<T>),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let subscriber = self.weak_handle();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
self.app
|
2021-08-23 21:58:37 +00:00
|
|
|
|
.subscribe_internal(handle, move |emitter, event, cx| {
|
|
|
|
|
if let Some(subscriber) = subscriber.upgrade(cx) {
|
|
|
|
|
subscriber.update(cx, |subscriber, cx| {
|
|
|
|
|
callback(subscriber, emitter, event, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
pub fn observe<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
|
2021-05-03 22:57:13 +00:00
|
|
|
|
where
|
2021-08-23 21:58:37 +00:00
|
|
|
|
E: Entity,
|
|
|
|
|
H: Handle<E>,
|
|
|
|
|
F: 'static + FnMut(&mut T, H, &mut ViewContext<T>),
|
2021-05-03 22:57:13 +00:00
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let observer = self.weak_handle();
|
2021-08-23 21:58:37 +00:00
|
|
|
|
self.app.observe_internal(handle, move |observed, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, observed, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
2021-08-23 21:58:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
pub fn observe_focus<F, V>(&mut self, handle: &ViewHandle<V>, mut callback: F) -> Subscription
|
|
|
|
|
where
|
2022-07-06 13:53:40 +00:00
|
|
|
|
F: 'static + FnMut(&mut T, ViewHandle<V>, bool, &mut ViewContext<T>),
|
2022-04-14 16:22:32 +00:00
|
|
|
|
V: View,
|
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
2022-07-06 13:53:40 +00:00
|
|
|
|
self.app
|
|
|
|
|
.observe_focus(handle, move |observed, focused, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, observed, focused, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-04-14 16:22:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
pub fn observe_release<E, F, H>(&mut self, handle: &H, mut callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
E: Entity,
|
|
|
|
|
H: Handle<E>,
|
2022-03-08 18:00:54 +00:00
|
|
|
|
F: 'static + FnMut(&mut T, &E, &mut ViewContext<T>),
|
2022-01-21 13:22:06 +00:00
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
2022-03-08 18:00:54 +00:00
|
|
|
|
self.app.observe_release(handle, move |released, cx| {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
2022-03-08 18:00:54 +00:00
|
|
|
|
callback(observer, released, cx);
|
2022-01-21 13:22:06 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 08:01:23 +00:00
|
|
|
|
pub fn observe_actions<F>(&mut self, mut callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut T, TypeId, &mut ViewContext<T>),
|
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
|
|
|
|
self.app.observe_actions(move |action_id, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, action_id, cx);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 10:15:40 +00:00
|
|
|
|
pub fn observe_window_activation<F>(&mut self, mut callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut T, bool, &mut ViewContext<T>),
|
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
|
|
|
|
self.app
|
|
|
|
|
.observe_window_activation(self.window_id(), move |active, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, active, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 22:52:25 +00:00
|
|
|
|
pub fn observe_fullscreen<F>(&mut self, mut callback: F) -> Subscription
|
|
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(&mut T, bool, &mut ViewContext<T>),
|
|
|
|
|
{
|
|
|
|
|
let observer = self.weak_handle();
|
|
|
|
|
self.app
|
|
|
|
|
.observe_fullscreen(self.window_id(), move |active, cx| {
|
|
|
|
|
if let Some(observer) = observer.upgrade(cx) {
|
|
|
|
|
observer.update(cx, |observer, cx| {
|
|
|
|
|
callback(observer, active, cx);
|
|
|
|
|
});
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:58:37 +00:00
|
|
|
|
pub fn emit(&mut self, payload: T::Event) {
|
|
|
|
|
self.app.pending_effects.push_back(Effect::Event {
|
|
|
|
|
entity_id: self.view_id,
|
|
|
|
|
payload: Box::new(payload),
|
|
|
|
|
});
|
2021-05-03 22:57:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn notify(&mut self) {
|
2021-04-28 19:02:39 +00:00
|
|
|
|
self.app.notify_view(self.window_id, self.view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2022-10-24 08:47:47 +00:00
|
|
|
|
|
|
|
|
|
pub fn dispatch_action(&mut self, action: impl Action) {
|
|
|
|
|
self.app
|
|
|
|
|
.dispatch_action_at(self.window_id, self.view_id, action)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
pub fn dispatch_any_action(&mut self, action: Box<dyn Action>) {
|
|
|
|
|
self.app
|
|
|
|
|
.dispatch_any_action_at(self.window_id, self.view_id, action)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 20:14:25 +00:00
|
|
|
|
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>)) {
|
|
|
|
|
let handle = self.handle();
|
2022-03-25 02:24:36 +00:00
|
|
|
|
self.app.defer(move |cx| {
|
2022-01-22 20:14:25 +00:00
|
|
|
|
handle.update(cx, |view, cx| {
|
|
|
|
|
callback(view, cx);
|
|
|
|
|
})
|
2022-03-25 02:24:36 +00:00
|
|
|
|
})
|
2022-01-22 20:14:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:03:24 +00:00
|
|
|
|
pub fn after_window_update(
|
|
|
|
|
&mut self,
|
|
|
|
|
callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>),
|
|
|
|
|
) {
|
|
|
|
|
let handle = self.handle();
|
|
|
|
|
self.app.after_window_update(move |cx| {
|
|
|
|
|
handle.update(cx, |view, cx| {
|
|
|
|
|
callback(view, cx);
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn propagate_action(&mut self) {
|
2022-02-25 23:14:16 +00:00
|
|
|
|
self.app.halt_action_dispatch = false;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
|
pub fn spawn<F, Fut, S>(&self, f: F) -> Task<S>
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-05-12 21:28:59 +00:00
|
|
|
|
F: FnOnce(ViewHandle<T>, AsyncAppContext) -> Fut,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
Fut: 'static + Future<Output = S>,
|
|
|
|
|
S: 'static,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
{
|
2021-04-06 02:04:04 +00:00
|
|
|
|
let handle = self.handle();
|
2021-05-28 22:25:15 +00:00
|
|
|
|
self.app.spawn(|cx| f(handle, cx))
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-09-14 18:29:56 +00:00
|
|
|
|
|
|
|
|
|
pub fn spawn_weak<F, Fut, S>(&self, f: F) -> Task<S>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(WeakViewHandle<T>, AsyncAppContext) -> Fut,
|
|
|
|
|
Fut: 'static + Future<Output = S>,
|
|
|
|
|
S: 'static,
|
|
|
|
|
{
|
2021-11-29 22:12:53 +00:00
|
|
|
|
let handle = self.weak_handle();
|
2021-09-14 18:29:56 +00:00
|
|
|
|
self.app.spawn(|cx| f(handle, cx))
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 19:22:23 +00:00
|
|
|
|
pub struct RenderParams {
|
|
|
|
|
pub window_id: usize,
|
|
|
|
|
pub view_id: usize,
|
2021-08-20 17:45:42 +00:00
|
|
|
|
pub titlebar_height: f32,
|
2022-05-27 02:00:01 +00:00
|
|
|
|
pub hovered_region_ids: HashSet<MouseRegionId>,
|
2022-09-09 02:32:38 +00:00
|
|
|
|
pub clicked_region_ids: Option<(HashSet<MouseRegionId>, MouseButton)>,
|
2021-08-26 14:36:56 +00:00
|
|
|
|
pub refreshing: bool,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
pub appearance: Appearance,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 21:55:27 +00:00
|
|
|
|
pub struct RenderContext<'a, T: View> {
|
2022-05-27 00:03:34 +00:00
|
|
|
|
pub(crate) window_id: usize,
|
|
|
|
|
pub(crate) view_id: usize,
|
|
|
|
|
pub(crate) view_type: PhantomData<T>,
|
2022-05-27 02:00:01 +00:00
|
|
|
|
pub(crate) hovered_region_ids: HashSet<MouseRegionId>,
|
2022-09-09 02:32:38 +00:00
|
|
|
|
pub(crate) clicked_region_ids: Option<(HashSet<MouseRegionId>, MouseButton)>,
|
2021-08-27 15:25:13 +00:00
|
|
|
|
pub app: &'a mut MutableAppContext,
|
2021-08-20 17:45:42 +00:00
|
|
|
|
pub titlebar_height: f32,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
pub appearance: Appearance,
|
2021-08-26 14:36:56 +00:00
|
|
|
|
pub refreshing: bool,
|
2021-08-02 21:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 00:09:15 +00:00
|
|
|
|
#[derive(Clone, Default)]
|
2022-05-26 19:22:23 +00:00
|
|
|
|
pub struct MouseState {
|
2022-10-15 00:09:15 +00:00
|
|
|
|
hovered: bool,
|
|
|
|
|
clicked: Option<MouseButton>,
|
|
|
|
|
accessed_hovered: bool,
|
|
|
|
|
accessed_clicked: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MouseState {
|
|
|
|
|
pub fn hovered(&mut self) -> bool {
|
|
|
|
|
self.accessed_hovered = true;
|
|
|
|
|
self.hovered
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn clicked(&mut self) -> Option<MouseButton> {
|
|
|
|
|
self.accessed_clicked = true;
|
|
|
|
|
self.clicked
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn accessed_hovered(&self) -> bool {
|
|
|
|
|
self.accessed_hovered
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn accessed_clicked(&self) -> bool {
|
|
|
|
|
self.accessed_clicked
|
|
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 00:37:28 +00:00
|
|
|
|
impl<'a, V: View> RenderContext<'a, V> {
|
2022-05-26 19:22:23 +00:00
|
|
|
|
fn new(params: RenderParams, app: &'a mut MutableAppContext) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
app,
|
|
|
|
|
window_id: params.window_id,
|
|
|
|
|
view_id: params.view_id,
|
|
|
|
|
view_type: PhantomData,
|
|
|
|
|
titlebar_height: params.titlebar_height,
|
2022-05-27 02:00:01 +00:00
|
|
|
|
hovered_region_ids: params.hovered_region_ids.clone(),
|
2022-08-13 02:00:48 +00:00
|
|
|
|
clicked_region_ids: params.clicked_region_ids.clone(),
|
2022-05-26 19:22:23 +00:00
|
|
|
|
refreshing: params.refreshing,
|
2022-09-14 09:47:43 +00:00
|
|
|
|
appearance: params.appearance,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 00:37:28 +00:00
|
|
|
|
pub fn handle(&self) -> WeakViewHandle<V> {
|
2021-08-02 21:55:27 +00:00
|
|
|
|
WeakViewHandle::new(self.window_id, self.view_id)
|
|
|
|
|
}
|
2021-11-03 22:15:41 +00:00
|
|
|
|
|
2022-08-09 19:34:57 +00:00
|
|
|
|
pub fn window_id(&self) -> usize {
|
|
|
|
|
self.window_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 22:15:41 +00:00
|
|
|
|
pub fn view_id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
2022-05-26 19:22:23 +00:00
|
|
|
|
|
|
|
|
|
pub fn mouse_state<Tag: 'static>(&self, region_id: usize) -> MouseState {
|
2022-09-10 00:29:52 +00:00
|
|
|
|
let region_id = MouseRegionId::new::<Tag>(self.view_id, region_id);
|
2022-05-26 19:22:23 +00:00
|
|
|
|
MouseState {
|
2022-05-27 02:00:01 +00:00
|
|
|
|
hovered: self.hovered_region_ids.contains(®ion_id),
|
2022-08-13 02:00:48 +00:00
|
|
|
|
clicked: self.clicked_region_ids.as_ref().and_then(|(ids, button)| {
|
|
|
|
|
if ids.contains(®ion_id) {
|
|
|
|
|
Some(*button)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}),
|
2022-10-15 00:09:15 +00:00
|
|
|
|
accessed_hovered: false,
|
|
|
|
|
accessed_clicked: false,
|
2022-05-26 19:22:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-27 00:37:28 +00:00
|
|
|
|
|
2022-09-12 21:24:36 +00:00
|
|
|
|
pub fn element_state<Tag: 'static, T: 'static>(
|
2022-05-27 00:37:28 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
element_id: usize,
|
2022-09-12 21:24:36 +00:00
|
|
|
|
initial: T,
|
2022-05-27 00:37:28 +00:00
|
|
|
|
) -> ElementStateHandle<T> {
|
|
|
|
|
let id = ElementStateId {
|
|
|
|
|
view_id: self.view_id(),
|
|
|
|
|
element_id,
|
|
|
|
|
tag: TypeId::of::<Tag>(),
|
|
|
|
|
};
|
|
|
|
|
self.cx
|
|
|
|
|
.element_states
|
|
|
|
|
.entry(id)
|
2022-09-12 21:24:36 +00:00
|
|
|
|
.or_insert_with(|| Box::new(initial));
|
2022-05-27 00:37:28 +00:00
|
|
|
|
ElementStateHandle::new(id, self.frame_count, &self.cx.ref_counts)
|
|
|
|
|
}
|
2022-09-12 21:24:36 +00:00
|
|
|
|
|
|
|
|
|
pub fn default_element_state<Tag: 'static, T: 'static + Default>(
|
|
|
|
|
&mut self,
|
|
|
|
|
element_id: usize,
|
|
|
|
|
) -> ElementStateHandle<T> {
|
|
|
|
|
self.element_state::<Tag, T>(element_id, T::default())
|
|
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 22:57:13 +00:00
|
|
|
|
impl AsRef<AppContext> for &AppContext {
|
|
|
|
|
fn as_ref(&self) -> &AppContext {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 21:55:27 +00:00
|
|
|
|
impl<V: View> Deref for RenderContext<'_, V> {
|
2021-08-27 16:04:21 +00:00
|
|
|
|
type Target = MutableAppContext;
|
2021-08-02 21:55:27 +00:00
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2021-08-27 16:04:21 +00:00
|
|
|
|
self.app
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<V: View> DerefMut for RenderContext<'_, V> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
self.app
|
2021-08-02 21:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 14:36:56 +00:00
|
|
|
|
impl<V: View> ReadModel for RenderContext<'_, V> {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
|
|
|
|
|
self.app.read_model(handle)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 22:39:19 +00:00
|
|
|
|
impl<V: View> UpdateModel for RenderContext<'_, V> {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<T: Entity, O>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
|
|
|
|
|
) -> O {
|
2021-09-17 22:39:19 +00:00
|
|
|
|
self.app.update_model(handle, update)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 16:27:51 +00:00
|
|
|
|
impl<V: View> ReadView for RenderContext<'_, V> {
|
|
|
|
|
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
|
|
|
|
self.app.read_view(handle)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 02:07:25 +00:00
|
|
|
|
impl<M> AsRef<AppContext> for ViewContext<'_, M> {
|
|
|
|
|
fn as_ref(&self) -> &AppContext {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
&self.app.cx
|
2021-04-14 02:07:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:52:08 +00:00
|
|
|
|
impl<M> Deref for ViewContext<'_, M> {
|
|
|
|
|
type Target = MutableAppContext;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2022-08-10 21:39:24 +00:00
|
|
|
|
self.app
|
2021-07-23 16:52:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 17:44:56 +00:00
|
|
|
|
impl<M> DerefMut for ViewContext<'_, M> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.app
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 02:07:25 +00:00
|
|
|
|
impl<M> AsMut<MutableAppContext> for ViewContext<'_, M> {
|
|
|
|
|
fn as_mut(&mut self) -> &mut MutableAppContext {
|
|
|
|
|
self.app
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl<V> ReadModel for ViewContext<'_, V> {
|
|
|
|
|
fn read_model<T: Entity>(&self, handle: &ModelHandle<T>) -> &T {
|
|
|
|
|
self.app.read_model(handle)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl<V> UpgradeModelHandle for ViewContext<'_, V> {
|
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
|
&self,
|
2022-02-11 13:41:19 +00:00
|
|
|
|
handle: &WeakModelHandle<T>,
|
2021-08-18 17:45:29 +00:00
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
|
}
|
2022-02-16 01:56:50 +00:00
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn model_handle_is_upgradable<T: Entity>(&self, handle: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.cx.model_handle_is_upgradable(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
self.cx.upgrade_any_model_handle(handle)
|
|
|
|
|
}
|
2021-08-18 17:45:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
impl<V> UpgradeViewHandle for ViewContext<'_, V> {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
|
|
|
|
|
self.cx.upgrade_view_handle(handle)
|
|
|
|
|
}
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
self.cx.upgrade_any_view_handle(handle)
|
|
|
|
|
}
|
2022-02-11 13:41:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 00:03:34 +00:00
|
|
|
|
impl<V: View> UpgradeViewHandle for RenderContext<'_, V> {
|
|
|
|
|
fn upgrade_view_handle<T: View>(&self, handle: &WeakViewHandle<T>) -> Option<ViewHandle<T>> {
|
|
|
|
|
self.cx.upgrade_view_handle(handle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
self.cx.upgrade_any_view_handle(handle)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<V: View> UpdateModel for ViewContext<'_, V> {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_model<T: Entity, O>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ModelHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ModelContext<T>) -> O,
|
|
|
|
|
) -> O {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
self.app.update_model(handle, update)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
|
impl<V: View> ReadView for ViewContext<'_, V> {
|
|
|
|
|
fn read_view<T: View>(&self, handle: &ViewHandle<T>) -> &T {
|
|
|
|
|
self.app.read_view(handle)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<V: View> UpdateView for ViewContext<'_, V> {
|
2021-10-01 21:23:35 +00:00
|
|
|
|
fn update_view<T, S>(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &ViewHandle<T>,
|
|
|
|
|
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
|
) -> S
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
|
|
|
|
T: View,
|
|
|
|
|
{
|
|
|
|
|
self.app.update_view(handle, update)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait Handle<T> {
|
2021-08-23 21:58:37 +00:00
|
|
|
|
type Weak: 'static;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn id(&self) -> usize;
|
|
|
|
|
fn location(&self) -> EntityLocation;
|
2021-08-23 21:58:37 +00:00
|
|
|
|
fn downgrade(&self) -> Self::Weak;
|
|
|
|
|
fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 19:02:17 +00:00
|
|
|
|
pub trait WeakHandle {
|
|
|
|
|
fn id(&self) -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
|
|
|
|
pub enum EntityLocation {
|
|
|
|
|
Model(usize),
|
|
|
|
|
View(usize, usize),
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
pub struct ModelHandle<T: Entity> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
model_id: usize,
|
|
|
|
|
model_type: PhantomData<T>,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id: usize,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Entity> ModelHandle<T> {
|
|
|
|
|
fn new(model_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
|
ref_counts.lock().inc_model(model_id);
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
let handle_id = ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_created(Some(type_name::<T>()), model_id);
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
Self {
|
|
|
|
|
model_id,
|
|
|
|
|
model_type: PhantomData,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
ref_counts: ref_counts.clone(),
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 16:52:15 +00:00
|
|
|
|
pub fn downgrade(&self) -> WeakModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
WeakModelHandle::new(self.model_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.model_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn read<'a, C: ReadModel>(&self, cx: &'a C) -> &'a T {
|
|
|
|
|
cx.read_model(self)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
|
2021-05-12 21:16:49 +00:00
|
|
|
|
where
|
2021-05-28 22:25:15 +00:00
|
|
|
|
C: ReadModelWith,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
F: FnOnce(&T, &AppContext) -> S,
|
|
|
|
|
{
|
2021-10-01 21:23:35 +00:00
|
|
|
|
let mut read = Some(read);
|
|
|
|
|
cx.read_model_with(self, &mut |model, cx| {
|
|
|
|
|
let read = read.take().unwrap();
|
|
|
|
|
read(model, cx)
|
|
|
|
|
})
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-05-28 22:25:15 +00:00
|
|
|
|
C: UpdateModel,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
|
{
|
2021-10-01 21:23:35 +00:00
|
|
|
|
let mut update = Some(update);
|
|
|
|
|
cx.update_model(self, &mut |model, cx| {
|
|
|
|
|
let update = update.take().unwrap();
|
|
|
|
|
update(model, cx)
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> Clone for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn clone(&self) -> Self {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
Self::new(self.model_id, &self.ref_counts)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> PartialEq for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.model_id == other.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> Eq for ModelHandle<T> {}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> PartialEq<WeakModelHandle<T>> for ModelHandle<T> {
|
2022-02-28 01:07:46 +00:00
|
|
|
|
fn eq(&self, other: &WeakModelHandle<T>) -> bool {
|
|
|
|
|
self.model_id == other.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> Hash for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.model_id.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> std::borrow::Borrow<usize> for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn borrow(&self) -> &usize {
|
|
|
|
|
&self.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> Debug for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_tuple(&format!("ModelHandle<{}>", type_name::<T>()))
|
|
|
|
|
.field(&self.model_id)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
unsafe impl<T: Entity> Send for ModelHandle<T> {}
|
|
|
|
|
unsafe impl<T: Entity> Sync for ModelHandle<T> {}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl<T: Entity> Drop for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn drop(&mut self) {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
let mut ref_counts = self.ref_counts.lock();
|
|
|
|
|
ref_counts.dec_model(self.model_id);
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
ref_counts
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_dropped(self.model_id, self.handle_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:58:37 +00:00
|
|
|
|
impl<T: Entity> Handle<T> for ModelHandle<T> {
|
|
|
|
|
type Weak = WeakModelHandle<T>;
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn id(&self) -> usize {
|
|
|
|
|
self.model_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn location(&self) -> EntityLocation {
|
|
|
|
|
EntityLocation::Model(self.model_id)
|
|
|
|
|
}
|
2021-08-23 21:58:37 +00:00
|
|
|
|
|
|
|
|
|
fn downgrade(&self) -> Self::Weak {
|
|
|
|
|
self.downgrade()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
weak.upgrade(cx)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-08-23 21:58:37 +00:00
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct WeakModelHandle<T> {
|
|
|
|
|
model_id: usize,
|
|
|
|
|
model_type: PhantomData<T>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 19:02:17 +00:00
|
|
|
|
impl<T> WeakHandle for WeakModelHandle<T> {
|
|
|
|
|
fn id(&self) -> usize {
|
|
|
|
|
self.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 15:04:32 +00:00
|
|
|
|
unsafe impl<T> Send for WeakModelHandle<T> {}
|
|
|
|
|
unsafe impl<T> Sync for WeakModelHandle<T> {}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<T: Entity> WeakModelHandle<T> {
|
|
|
|
|
fn new(model_id: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
model_id,
|
|
|
|
|
model_type: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 16:38:37 +00:00
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.model_id
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 01:07:46 +00:00
|
|
|
|
pub fn is_upgradable(&self, cx: &impl UpgradeModelHandle) -> bool {
|
|
|
|
|
cx.model_handle_is_upgradable(self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
|
2021-08-18 17:45:29 +00:00
|
|
|
|
cx.upgrade_model_handle(self)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 19:29:58 +00:00
|
|
|
|
impl<T> Hash for WeakModelHandle<T> {
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.model_id.hash(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PartialEq for WeakModelHandle<T> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.model_id == other.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Eq for WeakModelHandle<T> {}
|
|
|
|
|
|
2022-10-11 13:24:31 +00:00
|
|
|
|
impl<T: Entity> PartialEq<ModelHandle<T>> for WeakModelHandle<T> {
|
|
|
|
|
fn eq(&self, other: &ModelHandle<T>) -> bool {
|
|
|
|
|
self.model_id == other.model_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 22:19:38 +00:00
|
|
|
|
impl<T> Clone for WeakModelHandle<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
model_id: self.model_id,
|
|
|
|
|
model_type: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
|
impl<T> Copy for WeakModelHandle<T> {}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct ViewHandle<T> {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: PhantomData<T>,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id: usize,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: View> ViewHandle<T> {
|
|
|
|
|
fn new(window_id: usize, view_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
|
ref_counts.lock().inc_view(window_id, view_id);
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
let handle_id = ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_created(Some(type_name::<T>()), view_id);
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
Self {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
view_type: PhantomData,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
ref_counts: ref_counts.clone(),
|
2022-03-01 15:11:12 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 02:04:04 +00:00
|
|
|
|
pub fn downgrade(&self) -> WeakViewHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
WeakViewHandle::new(self.window_id, self.view_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn window_id(&self) -> usize {
|
|
|
|
|
self.window_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn read<'a, C: ReadView>(&self, cx: &'a C) -> &'a T {
|
|
|
|
|
cx.read_view(self)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn read_with<C, F, S>(&self, cx: &C, read: F) -> S
|
2021-05-12 21:16:49 +00:00
|
|
|
|
where
|
2021-05-28 22:25:15 +00:00
|
|
|
|
C: ReadViewWith,
|
2021-05-12 21:16:49 +00:00
|
|
|
|
F: FnOnce(&T, &AppContext) -> S,
|
|
|
|
|
{
|
2021-10-01 21:23:35 +00:00
|
|
|
|
let mut read = Some(read);
|
|
|
|
|
cx.read_view_with(self, &mut |view, cx| {
|
|
|
|
|
let read = read.take().unwrap();
|
|
|
|
|
read(view, cx)
|
|
|
|
|
})
|
2021-05-12 21:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn update<C, F, S>(&self, cx: &mut C, update: F) -> S
|
2021-03-10 04:00:51 +00:00
|
|
|
|
where
|
2021-05-28 22:25:15 +00:00
|
|
|
|
C: UpdateView,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
|
{
|
2021-10-01 21:23:35 +00:00
|
|
|
|
let mut update = Some(update);
|
|
|
|
|
cx.update_view(self, &mut |view, cx| {
|
|
|
|
|
let update = update.take().unwrap();
|
|
|
|
|
update(view, cx)
|
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 20:21:59 +00:00
|
|
|
|
pub fn defer<C, F>(&self, cx: &mut C, update: F)
|
|
|
|
|
where
|
|
|
|
|
C: AsMut<MutableAppContext>,
|
|
|
|
|
F: 'static + FnOnce(&mut T, &mut ViewContext<T>),
|
|
|
|
|
{
|
|
|
|
|
let this = self.clone();
|
2022-03-25 02:24:36 +00:00
|
|
|
|
cx.as_mut().defer(move |cx| {
|
2022-01-22 20:21:59 +00:00
|
|
|
|
this.update(cx, |view, cx| update(view, cx));
|
2022-03-25 02:24:36 +00:00
|
|
|
|
});
|
2022-01-22 20:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
pub fn is_focused(&self, cx: &AppContext) -> bool {
|
|
|
|
|
cx.focused_view_id(self.window_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
.map_or(false, |focused_id| focused_id == self.view_id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 15:11:12 +00:00
|
|
|
|
impl<T: View> Clone for ViewHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn clone(&self) -> Self {
|
2022-03-01 15:11:12 +00:00
|
|
|
|
ViewHandle::new(self.window_id, self.view_id, &self.ref_counts)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PartialEq for ViewHandle<T> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.window_id == other.window_id && self.view_id == other.view_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 20:34:04 +00:00
|
|
|
|
impl<T> PartialEq<WeakViewHandle<T>> for ViewHandle<T> {
|
|
|
|
|
fn eq(&self, other: &WeakViewHandle<T>) -> bool {
|
|
|
|
|
self.window_id == other.window_id && self.view_id == other.view_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 10:29:46 +00:00
|
|
|
|
impl<T> PartialEq<ViewHandle<T>> for WeakViewHandle<T> {
|
|
|
|
|
fn eq(&self, other: &ViewHandle<T>) -> bool {
|
|
|
|
|
self.window_id == other.window_id && self.view_id == other.view_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<T> Eq for ViewHandle<T> {}
|
|
|
|
|
|
2022-03-21 15:46:10 +00:00
|
|
|
|
impl<T> Hash for ViewHandle<T> {
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.window_id.hash(state);
|
|
|
|
|
self.view_id.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<T> Debug for ViewHandle<T> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct(&format!("ViewHandle<{}>", type_name::<T>()))
|
|
|
|
|
.field("window_id", &self.window_id)
|
|
|
|
|
.field("view_id", &self.view_id)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for ViewHandle<T> {
|
|
|
|
|
fn drop(&mut self) {
|
2021-05-07 17:47:04 +00:00
|
|
|
|
self.ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.dec_view(self.window_id, self.view_id);
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
self.ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_dropped(self.view_id, self.handle_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 21:58:37 +00:00
|
|
|
|
impl<T: View> Handle<T> for ViewHandle<T> {
|
|
|
|
|
type Weak = WeakViewHandle<T>;
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn location(&self) -> EntityLocation {
|
|
|
|
|
EntityLocation::View(self.window_id, self.view_id)
|
|
|
|
|
}
|
2021-08-23 21:58:37 +00:00
|
|
|
|
|
|
|
|
|
fn downgrade(&self) -> Self::Weak {
|
|
|
|
|
self.downgrade()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upgrade_from(weak: &Self::Weak, cx: &AppContext) -> Option<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
weak.upgrade(cx)
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct AnyViewHandle {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: TypeId,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
2022-03-02 02:21:53 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id: usize,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnyViewHandle {
|
2022-03-01 15:11:12 +00:00
|
|
|
|
fn new(
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: TypeId,
|
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
ref_counts.lock().inc_view(window_id, view_id);
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
let handle_id = ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
2022-03-02 02:21:53 +00:00
|
|
|
|
.handle_created(None, view_id);
|
2022-03-01 15:11:12 +00:00
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
view_type,
|
|
|
|
|
ref_counts,
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 13:40:21 +00:00
|
|
|
|
pub fn window_id(&self) -> usize {
|
|
|
|
|
self.window_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is<T: 'static>(&self) -> bool {
|
|
|
|
|
TypeId::of::<T>() == self.view_type
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 22:20:03 +00:00
|
|
|
|
pub fn is_focused(&self, cx: &AppContext) -> bool {
|
|
|
|
|
cx.focused_view_id(self.window_id)
|
|
|
|
|
.map_or(false, |focused_id| focused_id == self.view_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub fn downcast<T: View>(self) -> Option<ViewHandle<T>> {
|
|
|
|
|
if self.is::<T>() {
|
2021-05-07 17:47:04 +00:00
|
|
|
|
let result = Some(ViewHandle {
|
|
|
|
|
window_id: self.window_id,
|
|
|
|
|
view_id: self.view_id,
|
|
|
|
|
ref_counts: self.ref_counts.clone(),
|
|
|
|
|
view_type: PhantomData,
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id: self.handle_id,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
});
|
|
|
|
|
unsafe {
|
2022-07-05 09:12:18 +00:00
|
|
|
|
Arc::decrement_strong_count(Arc::as_ptr(&self.ref_counts));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
2021-05-07 17:47:04 +00:00
|
|
|
|
std::mem::forget(self);
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 00:53:38 +00:00
|
|
|
|
|
|
|
|
|
pub fn downgrade(&self) -> AnyWeakViewHandle {
|
|
|
|
|
AnyWeakViewHandle {
|
|
|
|
|
window_id: self.window_id,
|
|
|
|
|
view_id: self.view_id,
|
|
|
|
|
view_type: self.view_type,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 13:20:09 +00:00
|
|
|
|
|
|
|
|
|
pub fn view_type(&self) -> TypeId {
|
|
|
|
|
self.view_type
|
|
|
|
|
}
|
2022-04-12 02:51:46 +00:00
|
|
|
|
|
|
|
|
|
pub fn debug_json(&self, cx: &AppContext) -> serde_json::Value {
|
|
|
|
|
cx.views
|
|
|
|
|
.get(&(self.window_id, self.view_id))
|
|
|
|
|
.map_or_else(|| serde_json::Value::Null, |view| view.debug_json(cx))
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 21:05:05 +00:00
|
|
|
|
impl Clone for AnyViewHandle {
|
|
|
|
|
fn clone(&self) -> Self {
|
2022-03-01 15:11:12 +00:00
|
|
|
|
Self::new(
|
|
|
|
|
self.window_id,
|
|
|
|
|
self.view_id,
|
|
|
|
|
self.view_type,
|
|
|
|
|
self.ref_counts.clone(),
|
|
|
|
|
)
|
2021-05-07 21:05:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 08:01:44 +00:00
|
|
|
|
impl From<&AnyViewHandle> for AnyViewHandle {
|
|
|
|
|
fn from(handle: &AnyViewHandle) -> Self {
|
|
|
|
|
handle.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<T: View> From<&ViewHandle<T>> for AnyViewHandle {
|
|
|
|
|
fn from(handle: &ViewHandle<T>) -> Self {
|
2022-03-01 15:11:12 +00:00
|
|
|
|
Self::new(
|
|
|
|
|
handle.window_id,
|
|
|
|
|
handle.view_id,
|
|
|
|
|
TypeId::of::<T>(),
|
|
|
|
|
handle.ref_counts.clone(),
|
|
|
|
|
)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: View> From<ViewHandle<T>> for AnyViewHandle {
|
|
|
|
|
fn from(handle: ViewHandle<T>) -> Self {
|
2021-05-07 17:47:04 +00:00
|
|
|
|
let any_handle = AnyViewHandle {
|
|
|
|
|
window_id: handle.window_id,
|
|
|
|
|
view_id: handle.view_id,
|
|
|
|
|
view_type: TypeId::of::<T>(),
|
|
|
|
|
ref_counts: handle.ref_counts.clone(),
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
handle_id: handle.handle_id,
|
2021-05-07 17:47:04 +00:00
|
|
|
|
};
|
2022-07-05 09:12:18 +00:00
|
|
|
|
|
2021-05-07 17:47:04 +00:00
|
|
|
|
unsafe {
|
2022-07-05 09:12:18 +00:00
|
|
|
|
Arc::decrement_strong_count(Arc::as_ptr(&handle.ref_counts));
|
2021-05-07 17:47:04 +00:00
|
|
|
|
}
|
|
|
|
|
std::mem::forget(handle);
|
|
|
|
|
any_handle
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 05:21:39 +00:00
|
|
|
|
impl Drop for AnyViewHandle {
|
|
|
|
|
fn drop(&mut self) {
|
2021-05-07 17:47:04 +00:00
|
|
|
|
self.ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.dec_view(self.window_id, self.view_id);
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 15:11:12 +00:00
|
|
|
|
self.ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_dropped(self.view_id, self.handle_id);
|
2021-05-06 05:21:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 21:24:48 +00:00
|
|
|
|
pub struct AnyModelHandle {
|
|
|
|
|
model_id: usize,
|
2022-01-07 19:00:12 +00:00
|
|
|
|
model_type: TypeId,
|
2021-05-07 21:24:48 +00:00
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id: usize,
|
2021-05-07 21:24:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 19:00:12 +00:00
|
|
|
|
impl AnyModelHandle {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
fn new(model_id: usize, model_type: TypeId, ref_counts: Arc<Mutex<RefCounts>>) -> Self {
|
|
|
|
|
ref_counts.lock().inc_model(model_id);
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
let handle_id = ref_counts
|
|
|
|
|
.lock()
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_created(None, model_id);
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
model_id,
|
|
|
|
|
model_type,
|
|
|
|
|
ref_counts,
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 19:00:12 +00:00
|
|
|
|
pub fn downcast<T: Entity>(self) -> Option<ModelHandle<T>> {
|
|
|
|
|
if self.is::<T>() {
|
|
|
|
|
let result = Some(ModelHandle {
|
|
|
|
|
model_id: self.model_id,
|
|
|
|
|
model_type: PhantomData,
|
|
|
|
|
ref_counts: self.ref_counts.clone(),
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id: self.handle_id,
|
2022-01-07 19:00:12 +00:00
|
|
|
|
});
|
|
|
|
|
unsafe {
|
2022-07-05 09:12:18 +00:00
|
|
|
|
Arc::decrement_strong_count(Arc::as_ptr(&self.ref_counts));
|
2022-01-07 19:00:12 +00:00
|
|
|
|
}
|
|
|
|
|
std::mem::forget(self);
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
pub fn downgrade(&self) -> AnyWeakModelHandle {
|
|
|
|
|
AnyWeakModelHandle {
|
|
|
|
|
model_id: self.model_id,
|
|
|
|
|
model_type: self.model_type,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 19:00:12 +00:00
|
|
|
|
pub fn is<T: Entity>(&self) -> bool {
|
|
|
|
|
self.model_type == TypeId::of::<T>()
|
|
|
|
|
}
|
2022-03-17 14:54:34 +00:00
|
|
|
|
|
|
|
|
|
pub fn model_type(&self) -> TypeId {
|
|
|
|
|
self.model_type
|
|
|
|
|
}
|
2022-01-07 19:00:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 21:24:48 +00:00
|
|
|
|
impl<T: Entity> From<ModelHandle<T>> for AnyModelHandle {
|
|
|
|
|
fn from(handle: ModelHandle<T>) -> Self {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
Self::new(
|
|
|
|
|
handle.model_id,
|
|
|
|
|
TypeId::of::<T>(),
|
|
|
|
|
handle.ref_counts.clone(),
|
|
|
|
|
)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 01:56:50 +00:00
|
|
|
|
impl Clone for AnyModelHandle {
|
|
|
|
|
fn clone(&self) -> Self {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
Self::new(self.model_id, self.model_type, self.ref_counts.clone())
|
2022-02-16 01:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 21:24:48 +00:00
|
|
|
|
impl Drop for AnyModelHandle {
|
|
|
|
|
fn drop(&mut self) {
|
2022-03-01 02:19:30 +00:00
|
|
|
|
let mut ref_counts = self.ref_counts.lock();
|
|
|
|
|
ref_counts.dec_model(self.model_id);
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
ref_counts
|
|
|
|
|
.leak_detector
|
|
|
|
|
.lock()
|
|
|
|
|
.handle_dropped(self.model_id, self.handle_id);
|
2021-05-07 21:24:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-28 14:19:58 +00:00
|
|
|
|
|
2022-09-02 03:07:30 +00:00
|
|
|
|
#[derive(Hash, PartialEq, Eq, Debug)]
|
2022-02-16 01:56:50 +00:00
|
|
|
|
pub struct AnyWeakModelHandle {
|
|
|
|
|
model_id: usize,
|
|
|
|
|
model_type: TypeId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnyWeakModelHandle {
|
|
|
|
|
pub fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<AnyModelHandle> {
|
|
|
|
|
cx.upgrade_any_model_handle(self)
|
|
|
|
|
}
|
2022-09-02 03:07:30 +00:00
|
|
|
|
pub fn model_type(&self) -> TypeId {
|
|
|
|
|
self.model_type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is<T: 'static>(&self) -> bool {
|
|
|
|
|
TypeId::of::<T>() == self.model_type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn downcast<T: Entity>(&self) -> Option<WeakModelHandle<T>> {
|
|
|
|
|
if self.is::<T>() {
|
|
|
|
|
let result = Some(WeakModelHandle {
|
|
|
|
|
model_id: self.model_id,
|
|
|
|
|
model_type: PhantomData,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-16 01:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 09:49:40 +00:00
|
|
|
|
impl<T: Entity> From<WeakModelHandle<T>> for AnyWeakModelHandle {
|
|
|
|
|
fn from(handle: WeakModelHandle<T>) -> Self {
|
|
|
|
|
AnyWeakModelHandle {
|
|
|
|
|
model_id: handle.model_id,
|
|
|
|
|
model_type: TypeId::of::<T>(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 22:43:15 +00:00
|
|
|
|
#[derive(Debug)]
|
2021-03-10 04:00:51 +00:00
|
|
|
|
pub struct WeakViewHandle<T> {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: PhantomData<T>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 19:02:17 +00:00
|
|
|
|
impl<T> WeakHandle for WeakViewHandle<T> {
|
|
|
|
|
fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl<T: View> WeakViewHandle<T> {
|
|
|
|
|
fn new(window_id: usize, view_id: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
window_id,
|
|
|
|
|
view_id,
|
|
|
|
|
view_type: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 00:59:13 +00:00
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 23:16:40 +00:00
|
|
|
|
pub fn window_id(&self) -> usize {
|
|
|
|
|
self.window_id
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:41:19 +00:00
|
|
|
|
pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<ViewHandle<T>> {
|
|
|
|
|
cx.upgrade_view_handle(self)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Clone for WeakViewHandle<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
window_id: self.window_id,
|
|
|
|
|
view_id: self.view_id,
|
|
|
|
|
view_type: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 14:19:58 +00:00
|
|
|
|
impl<T> PartialEq for WeakViewHandle<T> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.window_id == other.window_id && self.view_id == other.view_id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Eq for WeakViewHandle<T> {}
|
|
|
|
|
|
|
|
|
|
impl<T> Hash for WeakViewHandle<T> {
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.window_id.hash(state);
|
|
|
|
|
self.view_id.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:53:38 +00:00
|
|
|
|
pub struct AnyWeakViewHandle {
|
|
|
|
|
window_id: usize,
|
|
|
|
|
view_id: usize,
|
|
|
|
|
view_type: TypeId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnyWeakViewHandle {
|
2022-10-13 12:53:58 +00:00
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
|
self.view_id
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:53:38 +00:00
|
|
|
|
pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option<AnyViewHandle> {
|
|
|
|
|
cx.upgrade_any_view_handle(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: View> From<WeakViewHandle<T>> for AnyWeakViewHandle {
|
|
|
|
|
fn from(handle: WeakViewHandle<T>) -> Self {
|
|
|
|
|
AnyWeakViewHandle {
|
|
|
|
|
window_id: handle.window_id,
|
|
|
|
|
view_id: handle.view_id,
|
|
|
|
|
view_type: TypeId::of::<T>(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 21:43:58 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct ElementStateId {
|
|
|
|
|
view_id: usize,
|
|
|
|
|
element_id: usize,
|
|
|
|
|
tag: TypeId,
|
2021-08-31 00:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
pub struct ElementStateHandle<T> {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
value_type: PhantomData<T>,
|
2021-08-31 00:59:13 +00:00
|
|
|
|
id: ElementStateId,
|
2021-04-27 03:24:23 +00:00
|
|
|
|
ref_counts: Weak<Mutex<RefCounts>>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
impl<T: 'static> ElementStateHandle<T> {
|
2022-02-17 21:43:58 +00:00
|
|
|
|
fn new(id: ElementStateId, frame_id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
|
|
|
|
|
ref_counts.lock().inc_element_state(id, frame_id);
|
2021-04-27 03:24:23 +00:00
|
|
|
|
Self {
|
|
|
|
|
value_type: PhantomData,
|
|
|
|
|
id,
|
|
|
|
|
ref_counts: Arc::downgrade(ref_counts),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 21:24:36 +00:00
|
|
|
|
pub fn id(&self) -> ElementStateId {
|
|
|
|
|
self.id
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a T {
|
|
|
|
|
cx.element_states
|
2022-02-17 21:43:58 +00:00
|
|
|
|
.get(&self.id)
|
2021-04-28 00:35:24 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
.downcast_ref()
|
2021-08-27 16:04:21 +00:00
|
|
|
|
.unwrap()
|
2021-04-28 00:35:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
pub fn update<C, R>(&self, cx: &mut C, f: impl FnOnce(&mut T, &mut C) -> R) -> R
|
|
|
|
|
where
|
|
|
|
|
C: DerefMut<Target = MutableAppContext>,
|
|
|
|
|
{
|
2022-02-17 21:43:58 +00:00
|
|
|
|
let mut element_state = cx.deref_mut().cx.element_states.remove(&self.id).unwrap();
|
2021-08-27 16:04:21 +00:00
|
|
|
|
let result = f(element_state.downcast_mut().unwrap(), cx);
|
|
|
|
|
cx.deref_mut()
|
2021-07-23 16:52:08 +00:00
|
|
|
|
.cx
|
2021-08-27 16:04:21 +00:00
|
|
|
|
.element_states
|
2022-02-17 21:43:58 +00:00
|
|
|
|
.insert(self.id, element_state);
|
2021-07-23 16:52:08 +00:00
|
|
|
|
result
|
2021-04-27 03:24:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
|
impl<T> Drop for ElementStateHandle<T> {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
if let Some(ref_counts) = self.ref_counts.upgrade() {
|
2022-02-17 21:43:58 +00:00
|
|
|
|
ref_counts.lock().dec_element_state(self.id);
|
2021-04-27 03:24:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
|
#[must_use]
|
|
|
|
|
pub enum Subscription {
|
|
|
|
|
Subscription {
|
|
|
|
|
id: usize,
|
|
|
|
|
entity_id: usize,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
subscriptions: Option<Weak<Mapping<usize, SubscriptionCallback>>>,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
},
|
2022-03-11 04:03:01 +00:00
|
|
|
|
GlobalSubscription {
|
|
|
|
|
id: usize,
|
|
|
|
|
type_id: TypeId,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
subscriptions: Option<Weak<Mapping<TypeId, GlobalSubscriptionCallback>>>,
|
2022-03-11 04:03:01 +00:00
|
|
|
|
},
|
2021-08-23 22:54:24 +00:00
|
|
|
|
Observation {
|
|
|
|
|
id: usize,
|
|
|
|
|
entity_id: usize,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Option<Weak<Mapping<usize, ObservationCallback>>>,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
},
|
2022-03-22 23:38:17 +00:00
|
|
|
|
GlobalObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
type_id: TypeId,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Option<Weak<Mapping<TypeId, GlobalObservationCallback>>>,
|
2022-03-22 23:38:17 +00:00
|
|
|
|
},
|
2022-04-14 16:22:32 +00:00
|
|
|
|
FocusObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
view_id: usize,
|
2022-08-09 23:59:13 +00:00
|
|
|
|
observations: Option<Weak<Mapping<usize, FocusObservationCallback>>>,
|
|
|
|
|
},
|
|
|
|
|
WindowActivationObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
observations: Option<Weak<Mapping<usize, WindowActivationCallback>>>,
|
|
|
|
|
},
|
|
|
|
|
WindowFullscreenObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
window_id: usize,
|
|
|
|
|
observations: Option<Weak<Mapping<usize, WindowFullscreenCallback>>>,
|
2022-04-14 16:22:32 +00:00
|
|
|
|
},
|
2022-08-09 23:59:13 +00:00
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
ReleaseObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
entity_id: usize,
|
2022-08-10 21:39:24 +00:00
|
|
|
|
#[allow(clippy::type_complexity)]
|
2022-01-21 13:22:06 +00:00
|
|
|
|
observations:
|
|
|
|
|
Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, ReleaseObservationCallback>>>>>,
|
|
|
|
|
},
|
2022-05-30 08:01:23 +00:00
|
|
|
|
ActionObservation {
|
|
|
|
|
id: usize,
|
|
|
|
|
observations: Option<Weak<Mutex<BTreeMap<usize, ActionObservationCallback>>>>,
|
|
|
|
|
},
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Subscription {
|
|
|
|
|
pub fn detach(&mut self) {
|
|
|
|
|
match self {
|
|
|
|
|
Subscription::Subscription { subscriptions, .. } => {
|
|
|
|
|
subscriptions.take();
|
|
|
|
|
}
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Subscription::GlobalSubscription { subscriptions, .. } => {
|
|
|
|
|
subscriptions.take();
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
Subscription::Observation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-03-22 23:38:17 +00:00
|
|
|
|
Subscription::GlobalObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-01-21 13:22:06 +00:00
|
|
|
|
Subscription::ReleaseObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-04-14 16:22:32 +00:00
|
|
|
|
Subscription::FocusObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-05-30 08:01:23 +00:00
|
|
|
|
Subscription::ActionObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-07-05 10:15:40 +00:00
|
|
|
|
Subscription::WindowActivationObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2022-08-09 22:52:25 +00:00
|
|
|
|
Subscription::WindowFullscreenObservation { observations, .. } => {
|
|
|
|
|
observations.take();
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Subscription {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
match self {
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Subscription::Subscription {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
id,
|
|
|
|
|
entity_id,
|
2022-03-11 04:03:01 +00:00
|
|
|
|
subscriptions,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
} => {
|
2022-03-11 04:03:01 +00:00
|
|
|
|
if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
match subscriptions
|
|
|
|
|
.lock()
|
|
|
|
|
.entry(*entity_id)
|
|
|
|
|
.or_default()
|
|
|
|
|
.entry(*id)
|
|
|
|
|
{
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.insert(None);
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.remove();
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Subscription::GlobalSubscription {
|
|
|
|
|
id,
|
|
|
|
|
type_id,
|
|
|
|
|
subscriptions,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
|
2022-03-11 23:38:01 +00:00
|
|
|
|
match subscriptions.lock().entry(*type_id).or_default().entry(*id) {
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.insert(None);
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.remove();
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2022-03-11 04:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Subscription::Observation {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
id,
|
|
|
|
|
entity_id,
|
|
|
|
|
observations,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
match observations
|
|
|
|
|
.lock()
|
|
|
|
|
.entry(*entity_id)
|
|
|
|
|
.or_default()
|
|
|
|
|
.entry(*id)
|
|
|
|
|
{
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.insert(None);
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2022-03-22 14:57:30 +00:00
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
2022-03-11 23:34:04 +00:00
|
|
|
|
entry.remove();
|
2022-03-11 23:38:01 +00:00
|
|
|
|
}
|
2022-01-21 13:22:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-22 23:38:17 +00:00
|
|
|
|
Subscription::GlobalObservation {
|
|
|
|
|
id,
|
|
|
|
|
type_id,
|
|
|
|
|
observations,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
|
match observations.lock().entry(*type_id).or_default().entry(*id) {
|
|
|
|
|
collections::btree_map::Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(None);
|
|
|
|
|
}
|
|
|
|
|
collections::btree_map::Entry::Occupied(entry) => {
|
|
|
|
|
entry.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-11 04:03:01 +00:00
|
|
|
|
Subscription::ReleaseObservation {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
id,
|
|
|
|
|
entity_id,
|
2022-03-11 04:03:01 +00:00
|
|
|
|
observations,
|
2021-08-23 22:54:24 +00:00
|
|
|
|
} => {
|
2022-03-11 04:03:01 +00:00
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
|
if let Some(observations) = observations.lock().get_mut(entity_id) {
|
|
|
|
|
observations.remove(id);
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-14 16:22:32 +00:00
|
|
|
|
Subscription::FocusObservation {
|
|
|
|
|
id,
|
|
|
|
|
view_id,
|
|
|
|
|
observations,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
|
match observations.lock().entry(*view_id).or_default().entry(*id) {
|
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(None);
|
|
|
|
|
}
|
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
|
|
|
|
entry.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-30 08:01:23 +00:00
|
|
|
|
Subscription::ActionObservation { id, observations } => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
2022-08-10 21:39:24 +00:00
|
|
|
|
observations.lock().remove(id);
|
2022-05-30 08:01:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-05 10:15:40 +00:00
|
|
|
|
Subscription::WindowActivationObservation {
|
|
|
|
|
id,
|
|
|
|
|
window_id,
|
|
|
|
|
observations,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
|
match observations
|
|
|
|
|
.lock()
|
|
|
|
|
.entry(*window_id)
|
|
|
|
|
.or_default()
|
|
|
|
|
.entry(*id)
|
|
|
|
|
{
|
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(None);
|
|
|
|
|
}
|
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
|
|
|
|
entry.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-09 22:52:25 +00:00
|
|
|
|
Subscription::WindowFullscreenObservation {
|
|
|
|
|
id,
|
|
|
|
|
window_id,
|
|
|
|
|
observations,
|
|
|
|
|
} => {
|
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
|
match observations
|
|
|
|
|
.lock()
|
|
|
|
|
.entry(*window_id)
|
|
|
|
|
.or_default()
|
|
|
|
|
.entry(*id)
|
|
|
|
|
{
|
|
|
|
|
btree_map::Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(None);
|
|
|
|
|
}
|
|
|
|
|
btree_map::Entry::Occupied(entry) => {
|
|
|
|
|
entry.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-23 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 15:00:40 +00:00
|
|
|
|
lazy_static! {
|
|
|
|
|
static ref LEAK_BACKTRACE: bool =
|
|
|
|
|
std::env::var("LEAK_BACKTRACE").map_or(false, |b| !b.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct LeakDetector {
|
|
|
|
|
next_handle_id: usize,
|
2022-08-10 21:39:24 +00:00
|
|
|
|
#[allow(clippy::type_complexity)]
|
2022-03-01 16:01:52 +00:00
|
|
|
|
handle_backtraces: HashMap<
|
|
|
|
|
usize,
|
|
|
|
|
(
|
|
|
|
|
Option<&'static str>,
|
|
|
|
|
HashMap<usize, Option<backtrace::Backtrace>>,
|
|
|
|
|
),
|
|
|
|
|
>,
|
2022-03-01 02:19:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
impl LeakDetector {
|
|
|
|
|
fn handle_created(&mut self, type_name: Option<&'static str>, entity_id: usize) -> usize {
|
|
|
|
|
let handle_id = post_inc(&mut self.next_handle_id);
|
|
|
|
|
let entry = self.handle_backtraces.entry(entity_id).or_default();
|
2022-03-01 15:00:40 +00:00
|
|
|
|
let backtrace = if *LEAK_BACKTRACE {
|
2022-03-01 16:01:52 +00:00
|
|
|
|
Some(backtrace::Backtrace::new_unresolved())
|
2022-03-01 15:00:40 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2022-03-01 02:19:30 +00:00
|
|
|
|
if let Some(type_name) = type_name {
|
|
|
|
|
entry.0.get_or_insert(type_name);
|
|
|
|
|
}
|
2022-03-01 15:00:40 +00:00
|
|
|
|
entry.1.insert(handle_id, backtrace);
|
2022-03-01 02:19:30 +00:00
|
|
|
|
handle_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_dropped(&mut self, entity_id: usize, handle_id: usize) {
|
|
|
|
|
if let Some((_, backtraces)) = self.handle_backtraces.get_mut(&entity_id) {
|
|
|
|
|
assert!(backtraces.remove(&handle_id).is_some());
|
|
|
|
|
if backtraces.is_empty() {
|
|
|
|
|
self.handle_backtraces.remove(&entity_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 19:02:17 +00:00
|
|
|
|
pub fn assert_dropped(&mut self, entity_id: usize) {
|
|
|
|
|
if let Some((type_name, backtraces)) = self.handle_backtraces.get_mut(&entity_id) {
|
2022-08-10 21:39:24 +00:00
|
|
|
|
for trace in backtraces.values_mut().flatten() {
|
|
|
|
|
trace.resolve();
|
|
|
|
|
eprintln!("{:?}", crate::util::CwdBacktrace(trace));
|
2022-04-05 19:02:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hint = if *LEAK_BACKTRACE {
|
|
|
|
|
""
|
|
|
|
|
} else {
|
|
|
|
|
" – set LEAK_BACKTRACE=1 for more information"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
panic!(
|
|
|
|
|
"{} handles to {} {} still exist{}",
|
|
|
|
|
backtraces.len(),
|
|
|
|
|
type_name.unwrap_or("entity"),
|
|
|
|
|
entity_id,
|
|
|
|
|
hint
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-01 02:19:30 +00:00
|
|
|
|
pub fn detect(&mut self) {
|
|
|
|
|
let mut found_leaks = false;
|
|
|
|
|
for (id, (type_name, backtraces)) in self.handle_backtraces.iter_mut() {
|
|
|
|
|
eprintln!(
|
2022-04-05 19:02:17 +00:00
|
|
|
|
"leaked {} handles to {} {}",
|
2022-03-01 02:19:30 +00:00
|
|
|
|
backtraces.len(),
|
|
|
|
|
type_name.unwrap_or("entity"),
|
|
|
|
|
id
|
|
|
|
|
);
|
2022-08-10 21:39:24 +00:00
|
|
|
|
for trace in backtraces.values_mut().flatten() {
|
|
|
|
|
trace.resolve();
|
|
|
|
|
eprintln!("{:?}", crate::util::CwdBacktrace(trace));
|
2022-03-01 02:19:30 +00:00
|
|
|
|
}
|
|
|
|
|
found_leaks = true;
|
|
|
|
|
}
|
2022-03-01 15:00:40 +00:00
|
|
|
|
|
|
|
|
|
let hint = if *LEAK_BACKTRACE {
|
|
|
|
|
""
|
|
|
|
|
} else {
|
|
|
|
|
" – set LEAK_BACKTRACE=1 for more information"
|
|
|
|
|
};
|
|
|
|
|
assert!(!found_leaks, "detected leaked handles{}", hint);
|
2022-03-01 02:19:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct RefCounts {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
entity_counts: HashMap<usize, usize>,
|
2022-02-17 21:43:58 +00:00
|
|
|
|
element_state_counts: HashMap<ElementStateId, ElementStateRefCount>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
dropped_models: HashSet<usize>,
|
|
|
|
|
dropped_views: HashSet<(usize, usize)>,
|
2022-02-17 21:43:58 +00:00
|
|
|
|
dropped_element_states: HashSet<ElementStateId>,
|
2022-03-01 02:19:30 +00:00
|
|
|
|
|
2022-03-01 16:01:52 +00:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-01 02:19:30 +00:00
|
|
|
|
leak_detector: Arc<Mutex<LeakDetector>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 22:04:27 +00:00
|
|
|
|
struct ElementStateRefCount {
|
|
|
|
|
ref_count: usize,
|
|
|
|
|
frame_id: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
|
impl RefCounts {
|
2021-05-13 01:29:17 +00:00
|
|
|
|
fn inc_model(&mut self, model_id: usize) {
|
|
|
|
|
match self.entity_counts.entry(model_id) {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
|
*entry.get_mut() += 1;
|
|
|
|
|
}
|
2021-05-13 01:29:17 +00:00
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(1);
|
|
|
|
|
self.dropped_models.remove(&model_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn inc_view(&mut self, window_id: usize, view_id: usize) {
|
|
|
|
|
match self.entity_counts.entry(view_id) {
|
|
|
|
|
Entry::Occupied(mut entry) => *entry.get_mut() += 1,
|
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(1);
|
|
|
|
|
self.dropped_views.remove(&(window_id, view_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-27 03:24:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 21:43:58 +00:00
|
|
|
|
fn inc_element_state(&mut self, id: ElementStateId, frame_id: usize) {
|
|
|
|
|
match self.element_state_counts.entry(id) {
|
2022-02-14 22:04:27 +00:00
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
|
let entry = entry.get_mut();
|
|
|
|
|
if entry.frame_id == frame_id || entry.ref_count >= 2 {
|
|
|
|
|
panic!("used the same element state more than once in the same frame");
|
|
|
|
|
}
|
|
|
|
|
entry.ref_count += 1;
|
|
|
|
|
entry.frame_id = frame_id;
|
|
|
|
|
}
|
2021-08-31 00:59:13 +00:00
|
|
|
|
Entry::Vacant(entry) => {
|
2022-02-14 22:04:27 +00:00
|
|
|
|
entry.insert(ElementStateRefCount {
|
|
|
|
|
ref_count: 1,
|
|
|
|
|
frame_id,
|
|
|
|
|
});
|
2022-02-17 21:43:58 +00:00
|
|
|
|
self.dropped_element_states.remove(&id);
|
2021-08-31 00:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dec_model(&mut self, model_id: usize) {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
let count = self.entity_counts.get_mut(&model_id).unwrap();
|
|
|
|
|
*count -= 1;
|
|
|
|
|
if *count == 0 {
|
|
|
|
|
self.entity_counts.remove(&model_id);
|
|
|
|
|
self.dropped_models.insert(model_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dec_view(&mut self, window_id: usize, view_id: usize) {
|
2021-04-27 03:24:23 +00:00
|
|
|
|
let count = self.entity_counts.get_mut(&view_id).unwrap();
|
|
|
|
|
*count -= 1;
|
|
|
|
|
if *count == 0 {
|
|
|
|
|
self.entity_counts.remove(&view_id);
|
|
|
|
|
self.dropped_views.insert((window_id, view_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 21:43:58 +00:00
|
|
|
|
fn dec_element_state(&mut self, id: ElementStateId) {
|
|
|
|
|
let entry = self.element_state_counts.get_mut(&id).unwrap();
|
2022-02-14 22:04:27 +00:00
|
|
|
|
entry.ref_count -= 1;
|
|
|
|
|
if entry.ref_count == 0 {
|
2022-02-17 21:43:58 +00:00
|
|
|
|
self.element_state_counts.remove(&id);
|
|
|
|
|
self.dropped_element_states.insert(id);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 17:47:04 +00:00
|
|
|
|
fn is_entity_alive(&self, entity_id: usize) -> bool {
|
|
|
|
|
self.entity_counts.contains_key(&entity_id)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 03:52:18 +00:00
|
|
|
|
fn take_dropped(
|
|
|
|
|
&mut self,
|
|
|
|
|
) -> (
|
|
|
|
|
HashSet<usize>,
|
|
|
|
|
HashSet<(usize, usize)>,
|
2022-02-17 21:43:58 +00:00
|
|
|
|
HashSet<ElementStateId>,
|
2021-04-27 03:52:18 +00:00
|
|
|
|
) {
|
2021-11-29 22:16:19 +00:00
|
|
|
|
(
|
|
|
|
|
std::mem::take(&mut self.dropped_models),
|
|
|
|
|
std::mem::take(&mut self.dropped_views),
|
|
|
|
|
std::mem::take(&mut self.dropped_element_states),
|
|
|
|
|
)
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2022-07-18 06:19:32 +00:00
|
|
|
|
use crate::{actions, elements::*, impl_actions, MouseButton, MouseButtonEvent};
|
2022-04-08 22:32:56 +00:00
|
|
|
|
use serde::Deserialize;
|
2021-04-20 15:21:29 +00:00
|
|
|
|
use smol::future::poll_once;
|
2022-01-21 13:22:06 +00:00
|
|
|
|
use std::{
|
|
|
|
|
cell::Cell,
|
2022-03-23 00:03:24 +00:00
|
|
|
|
sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
|
2022-01-21 13:22:06 +00:00
|
|
|
|
};
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_model_handles(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct Model {
|
|
|
|
|
other: Option<ModelHandle<Model>>,
|
|
|
|
|
events: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Model {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn new(other: Option<ModelHandle<Self>>, cx: &mut ModelContext<Self>) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
if let Some(other) = other.as_ref() {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.observe(other, |me, _, _| {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
me.events.push("notified".into());
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-08-23 21:58:37 +00:00
|
|
|
|
cx.subscribe(other, |me, _, event, _| {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
me.events.push(format!("observed event {}", event));
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
other,
|
|
|
|
|
events: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let handle_1 = cx.add_model(|cx| Model::new(None, cx));
|
|
|
|
|
let handle_2 = cx.add_model(|cx| Model::new(Some(handle_1.clone()), cx));
|
|
|
|
|
assert_eq!(cx.cx.models.len(), 2);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_1.update(cx, |model, cx| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.events.push("updated".into());
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.emit(1);
|
|
|
|
|
cx.notify();
|
|
|
|
|
cx.emit(2);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.read(cx).events,
|
2021-05-12 16:21:32 +00:00
|
|
|
|
vec![
|
|
|
|
|
"observed event 1".to_string(),
|
|
|
|
|
"notified".to_string(),
|
|
|
|
|
"observed event 2".to_string(),
|
|
|
|
|
]
|
|
|
|
|
);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |model, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
drop(handle_1);
|
|
|
|
|
model.other.take();
|
2021-04-09 22:58:19 +00:00
|
|
|
|
});
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(cx.cx.models.len(), 1);
|
2022-08-09 23:59:13 +00:00
|
|
|
|
assert!(cx.subscriptions.is_empty());
|
|
|
|
|
assert!(cx.observations.is_empty());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2022-03-08 08:30:32 +00:00
|
|
|
|
fn test_model_events(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Model {
|
|
|
|
|
events: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let handle_1 = cx.add_model(|_| Model::default());
|
|
|
|
|
let handle_2 = cx.add_model(|_| Model::default());
|
2022-03-22 14:57:30 +00:00
|
|
|
|
|
2022-03-08 08:30:32 +00:00
|
|
|
|
handle_1.update(cx, |_, cx| {
|
|
|
|
|
cx.subscribe(&handle_2, move |model: &mut Model, emitter, event, cx| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.events.push(*event);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-08 08:30:32 +00:00
|
|
|
|
cx.subscribe(&emitter, |model, _, event, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.events.push(*event * 2);
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |_, c| c.emit(7));
|
2022-03-09 09:48:52 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |_, c| c.emit(5));
|
2022-03-09 09:48:52 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 14:57:30 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_model_emit_before_subscribe_in_same_update_cycle(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.add_model(|cx| {
|
|
|
|
|
drop(cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("dropped before flush")
|
|
|
|
|
}));
|
|
|
|
|
cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("before emit")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
cx.emit(());
|
|
|
|
|
cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("after emit")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
Model
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(*events.borrow(), ["before emit"]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_observe_and_notify_from_model(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Model {
|
|
|
|
|
count: usize,
|
|
|
|
|
events: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let handle_1 = cx.add_model(|_| Model::default());
|
|
|
|
|
let handle_2 = cx.add_model(|_| Model::default());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_1.update(cx, |_, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
c.observe(&handle_2, move |model, observed, c| {
|
|
|
|
|
model.events.push(observed.read(c).count);
|
2022-03-01 16:01:52 +00:00
|
|
|
|
c.observe(&observed, |model, observed, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.events.push(observed.read(c).count * 2);
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |model, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.count = 7;
|
|
|
|
|
c.notify()
|
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |model, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.count = 5;
|
|
|
|
|
c.notify()
|
|
|
|
|
});
|
2021-08-25 15:35:27 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10])
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 15:24:48 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_model_notify_before_observe_in_same_update_cycle(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.add_model(|cx| {
|
|
|
|
|
drop(cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("dropped before flush")
|
|
|
|
|
}));
|
|
|
|
|
cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("before notify")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
cx.notify();
|
|
|
|
|
cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("after notify")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
Model
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(*events.borrow(), ["before notify"]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 00:03:24 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_defer_and_after_window_update(cx: &mut MutableAppContext) {
|
|
|
|
|
struct View {
|
|
|
|
|
render_count: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
post_inc(&mut self.render_count);
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (_, view) = cx.add_window(Default::default(), |_| View { render_count: 0 });
|
|
|
|
|
let called_defer = Rc::new(AtomicBool::new(false));
|
|
|
|
|
let called_after_window_update = Rc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
|
|
view.update(cx, |this, cx| {
|
|
|
|
|
assert_eq!(this.render_count, 1);
|
|
|
|
|
cx.defer({
|
|
|
|
|
let called_defer = called_defer.clone();
|
|
|
|
|
move |this, _| {
|
|
|
|
|
assert_eq!(this.render_count, 1);
|
|
|
|
|
called_defer.store(true, SeqCst);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
cx.after_window_update({
|
|
|
|
|
let called_after_window_update = called_after_window_update.clone();
|
|
|
|
|
move |this, cx| {
|
|
|
|
|
assert_eq!(this.render_count, 2);
|
|
|
|
|
called_after_window_update.store(true, SeqCst);
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
assert!(!called_defer.load(SeqCst));
|
|
|
|
|
assert!(!called_after_window_update.load(SeqCst));
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert!(called_defer.load(SeqCst));
|
|
|
|
|
assert!(called_after_window_update.load(SeqCst));
|
|
|
|
|
assert_eq!(view.read(cx).render_count, 3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_view_handles(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct View {
|
|
|
|
|
other: Option<ViewHandle<View>>,
|
|
|
|
|
events: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn new(other: Option<ViewHandle<View>>, cx: &mut ViewContext<Self>) -> Self {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
if let Some(other) = other.as_ref() {
|
2021-08-23 22:07:50 +00:00
|
|
|
|
cx.subscribe(other, |me, _, event, _| {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
me.events.push(format!("observed event {}", event));
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
Self {
|
|
|
|
|
other,
|
|
|
|
|
events: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, root_view) = cx.add_window(Default::default(), |cx| View::new(None, cx));
|
|
|
|
|
let handle_1 = cx.add_view(&root_view, |cx| View::new(None, cx));
|
|
|
|
|
let handle_2 = cx.add_view(&root_view, |cx| View::new(Some(handle_1.clone()), cx));
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(cx.cx.views.len(), 3);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_1.update(cx, |view, cx| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
view.events.push("updated".into());
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.emit(1);
|
|
|
|
|
cx.emit(2);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec!["updated".to_string()]);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.read(cx).events,
|
2021-05-12 16:21:32 +00:00
|
|
|
|
vec![
|
|
|
|
|
"observed event 1".to_string(),
|
|
|
|
|
"observed event 2".to_string(),
|
|
|
|
|
]
|
|
|
|
|
);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |view, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
drop(handle_1);
|
|
|
|
|
view.other.take();
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(cx.cx.views.len(), 2);
|
2022-08-09 23:59:13 +00:00
|
|
|
|
assert!(cx.subscriptions.is_empty());
|
|
|
|
|
assert!(cx.observations.is_empty());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 15:44:10 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_add_window(cx: &mut MutableAppContext) {
|
|
|
|
|
struct View {
|
|
|
|
|
mouse_down_count: Arc<AtomicUsize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2022-09-10 00:29:52 +00:00
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
enum Handler {}
|
2021-07-12 15:44:10 +00:00
|
|
|
|
let mouse_down_count = self.mouse_down_count.clone();
|
2022-09-10 00:29:52 +00:00
|
|
|
|
MouseEventHandler::<Handler>::new(0, cx, |_, _| Empty::new().boxed())
|
|
|
|
|
.on_down(MouseButton::Left, move |_, _| {
|
2021-07-12 15:44:10 +00:00
|
|
|
|
mouse_down_count.fetch_add(1, SeqCst);
|
|
|
|
|
})
|
|
|
|
|
.boxed()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mouse_down_count = Arc::new(AtomicUsize::new(0));
|
2021-08-05 17:48:35 +00:00
|
|
|
|
let (window_id, _) = cx.add_window(Default::default(), |_| View {
|
2021-07-12 15:44:10 +00:00
|
|
|
|
mouse_down_count: mouse_down_count.clone(),
|
|
|
|
|
});
|
|
|
|
|
let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
|
|
|
|
|
// Ensure window's root element is in a valid lifecycle state.
|
|
|
|
|
presenter.borrow_mut().dispatch_event(
|
2022-07-18 06:19:32 +00:00
|
|
|
|
Event::MouseDown(MouseButtonEvent {
|
2021-07-12 15:44:10 +00:00
|
|
|
|
position: Default::default(),
|
2022-07-06 23:37:56 +00:00
|
|
|
|
button: MouseButton::Left,
|
2022-10-23 09:36:04 +00:00
|
|
|
|
modifiers: Default::default(),
|
2021-11-23 17:50:17 +00:00
|
|
|
|
click_count: 1,
|
2022-07-06 22:36:42 +00:00
|
|
|
|
}),
|
2022-08-13 02:00:48 +00:00
|
|
|
|
false,
|
2021-07-12 15:44:10 +00:00
|
|
|
|
cx,
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(mouse_down_count.load(SeqCst), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_entity_release_hooks(cx: &mut MutableAppContext) {
|
|
|
|
|
struct Model {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
released: Rc<Cell<bool>>,
|
2021-06-25 16:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct View {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
released: Rc<Cell<bool>>,
|
2021-06-25 16:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
|
|
|
|
|
fn release(&mut self, _: &mut MutableAppContext) {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
self.released.set(true);
|
2021-06-25 16:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
|
|
|
|
|
fn release(&mut self, _: &mut MutableAppContext) {
|
2022-01-21 13:22:06 +00:00
|
|
|
|
self.released.set(true);
|
2021-06-25 16:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-06-25 16:35:06 +00:00
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
let model_released = Rc::new(Cell::new(false));
|
|
|
|
|
let model_release_observed = Rc::new(Cell::new(false));
|
|
|
|
|
let view_released = Rc::new(Cell::new(false));
|
|
|
|
|
let view_release_observed = Rc::new(Cell::new(false));
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
|
|
|
|
let model = cx.add_model(|_| Model {
|
|
|
|
|
released: model_released.clone(),
|
|
|
|
|
});
|
2022-01-21 13:22:06 +00:00
|
|
|
|
let (window_id, view) = cx.add_window(Default::default(), |_| View {
|
2021-06-25 16:35:06 +00:00
|
|
|
|
released: view_released.clone(),
|
|
|
|
|
});
|
2022-01-21 13:22:06 +00:00
|
|
|
|
assert!(!model_released.get());
|
|
|
|
|
assert!(!view_released.get());
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
cx.observe_release(&model, {
|
|
|
|
|
let model_release_observed = model_release_observed.clone();
|
2022-03-08 18:00:54 +00:00
|
|
|
|
move |_, _| model_release_observed.set(true)
|
2022-01-21 13:22:06 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
cx.observe_release(&view, {
|
|
|
|
|
let view_release_observed = view_release_observed.clone();
|
2022-03-08 18:00:54 +00:00
|
|
|
|
move |_, _| view_release_observed.set(true)
|
2022-01-21 13:22:06 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
cx.update(move |_| {
|
2021-06-25 16:35:06 +00:00
|
|
|
|
drop(model);
|
|
|
|
|
});
|
2022-01-21 13:22:06 +00:00
|
|
|
|
assert!(model_released.get());
|
|
|
|
|
assert!(model_release_observed.get());
|
2021-06-25 16:35:06 +00:00
|
|
|
|
|
2022-01-21 13:22:06 +00:00
|
|
|
|
drop(view);
|
|
|
|
|
cx.remove_window(window_id);
|
|
|
|
|
assert!(view_released.get());
|
|
|
|
|
assert!(view_release_observed.get());
|
2021-06-25 16:35:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2022-03-08 08:30:32 +00:00
|
|
|
|
fn test_view_events(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct View {
|
|
|
|
|
events: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, handle_1) = cx.add_window(Default::default(), |_| View::default());
|
|
|
|
|
let handle_2 = cx.add_view(&handle_1, |_| View::default());
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let handle_3 = cx.add_model(|_| Model);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-08 08:30:32 +00:00
|
|
|
|
handle_1.update(cx, |_, cx| {
|
|
|
|
|
cx.subscribe(&handle_2, move |me, emitter, event, cx| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
me.events.push(*event);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-08 08:30:32 +00:00
|
|
|
|
cx.subscribe(&emitter, |me, _, event, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
me.events.push(*event * 2);
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-08 08:30:32 +00:00
|
|
|
|
cx.subscribe(&handle_3, |me, _, event, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
me.events.push(*event);
|
|
|
|
|
})
|
2021-08-23 22:54:24 +00:00
|
|
|
|
.detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |_, c| c.emit(7));
|
2022-03-09 09:48:52 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_2.update(cx, |_, c| c.emit(5));
|
2022-03-09 09:48:52 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
handle_3.update(cx, |_, c| c.emit(9));
|
2022-03-09 09:48:52 +00:00
|
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10, 9]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 23:34:04 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_global_events(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
struct GlobalEvent(u64);
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
let first_subscription;
|
|
|
|
|
let second_subscription;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
first_subscription = cx.subscribe_global(move |e: &GlobalEvent, _| {
|
|
|
|
|
events.borrow_mut().push(("First", e.clone()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
second_subscription = cx.subscribe_global(move |e: &GlobalEvent, _| {
|
|
|
|
|
events.borrow_mut().push(("Second", e.clone()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(1));
|
|
|
|
|
cx.emit_global(GlobalEvent(2));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
drop(first_subscription);
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(3));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
drop(second_subscription);
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(4));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
&*events.borrow(),
|
|
|
|
|
&[
|
|
|
|
|
("First", GlobalEvent(1)),
|
|
|
|
|
("Second", GlobalEvent(1)),
|
|
|
|
|
("First", GlobalEvent(2)),
|
|
|
|
|
("Second", GlobalEvent(2)),
|
|
|
|
|
("Second", GlobalEvent(3)),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 15:11:55 +00:00
|
|
|
|
#[crate::test(self)]
|
2022-03-22 15:24:48 +00:00
|
|
|
|
fn test_global_events_emitted_before_subscription_in_same_update_cycle(
|
|
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
|
) {
|
2022-03-22 15:11:55 +00:00
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
drop(cx.subscribe_global(move |_: &(), _| {
|
|
|
|
|
events.borrow_mut().push("dropped before emit");
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
cx.subscribe_global(move |_: &(), _| {
|
|
|
|
|
events.borrow_mut().push("before emit");
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cx.emit_global(());
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
cx.subscribe_global(move |_: &(), _| {
|
|
|
|
|
events.borrow_mut().push("after emit");
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(*events.borrow(), ["before emit"]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 23:34:04 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_global_nested_events(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
struct GlobalEvent(u64);
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
cx.subscribe_global(move |e: &GlobalEvent, cx| {
|
|
|
|
|
events.borrow_mut().push(("Outer", e.clone()));
|
|
|
|
|
|
2022-03-23 08:14:33 +00:00
|
|
|
|
if e.0 == 1 {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
cx.subscribe_global(move |e: &GlobalEvent, _| {
|
|
|
|
|
events.borrow_mut().push(("Inner", e.clone()));
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
}
|
2022-03-11 23:38:01 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2022-03-11 23:34:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(1));
|
|
|
|
|
cx.emit_global(GlobalEvent(2));
|
|
|
|
|
cx.emit_global(GlobalEvent(3));
|
|
|
|
|
});
|
2022-03-23 08:14:33 +00:00
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(4));
|
|
|
|
|
});
|
2022-03-11 23:34:04 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
&*events.borrow(),
|
|
|
|
|
&[
|
|
|
|
|
("Outer", GlobalEvent(1)),
|
|
|
|
|
("Outer", GlobalEvent(2)),
|
|
|
|
|
("Outer", GlobalEvent(3)),
|
2022-03-23 08:14:33 +00:00
|
|
|
|
("Outer", GlobalEvent(4)),
|
|
|
|
|
("Inner", GlobalEvent(4)),
|
2022-03-11 23:34:04 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 17:27:27 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_global(cx: &mut MutableAppContext) {
|
|
|
|
|
type Global = usize;
|
|
|
|
|
|
|
|
|
|
let observation_count = Rc::new(RefCell::new(0));
|
|
|
|
|
let subscription = cx.observe_global::<Global, _>({
|
|
|
|
|
let observation_count = observation_count.clone();
|
2022-05-23 16:23:25 +00:00
|
|
|
|
move |_| {
|
2022-03-23 17:27:27 +00:00
|
|
|
|
*observation_count.borrow_mut() += 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert!(!cx.has_global::<Global>());
|
|
|
|
|
assert_eq!(cx.default_global::<Global>(), &0);
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 1);
|
|
|
|
|
assert!(cx.has_global::<Global>());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
cx.update_global::<Global, _, _>(|global, _| {
|
|
|
|
|
*global = 1;
|
|
|
|
|
"Update Result"
|
|
|
|
|
}),
|
|
|
|
|
"Update Result"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 2);
|
|
|
|
|
assert_eq!(cx.global::<Global>(), &1);
|
|
|
|
|
|
|
|
|
|
drop(subscription);
|
|
|
|
|
cx.update_global::<Global, _, _>(|global, _| {
|
|
|
|
|
*global = 2;
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 2);
|
|
|
|
|
|
|
|
|
|
type OtherGlobal = f32;
|
|
|
|
|
|
|
|
|
|
let observation_count = Rc::new(RefCell::new(0));
|
|
|
|
|
cx.observe_global::<OtherGlobal, _>({
|
|
|
|
|
let observation_count = observation_count.clone();
|
2022-05-23 16:23:25 +00:00
|
|
|
|
move |_| {
|
2022-03-23 17:27:27 +00:00
|
|
|
|
*observation_count.borrow_mut() += 1;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
cx.update_default_global::<OtherGlobal, _, _>(|global, _| {
|
|
|
|
|
assert_eq!(global, &0.0);
|
|
|
|
|
*global = 2.0;
|
|
|
|
|
"Default update result"
|
|
|
|
|
}),
|
|
|
|
|
"Default update result"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(cx.global::<OtherGlobal>(), &2.0);
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_dropping_subscribers(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct View;
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, root_view) = cx.add_window(Default::default(), |_| View);
|
|
|
|
|
let observing_view = cx.add_view(&root_view, |_| View);
|
|
|
|
|
let emitting_view = cx.add_view(&root_view, |_| View);
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let observing_model = cx.add_model(|_| Model);
|
|
|
|
|
let observed_model = cx.add_model(|_| Model);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
observing_view.update(cx, |_, cx| {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
cx.subscribe(&emitting_view, |_, _, _, _| {}).detach();
|
|
|
|
|
cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
observing_model.update(cx, |_, cx| {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
cx.subscribe(&observed_model, |_, _, _, _| {}).detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
cx.update(|_| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
drop(observing_view);
|
|
|
|
|
drop(observing_model);
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
emitting_view.update(cx, |_, cx| cx.emit(()));
|
|
|
|
|
observed_model.update(cx, |_, cx| cx.emit(()));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 14:57:30 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_view_emit_before_subscribe_in_same_update_cycle(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct TestView;
|
|
|
|
|
|
|
|
|
|
impl Entity for TestView {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View for TestView {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"TestView"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.add_window(Default::default(), |cx| {
|
|
|
|
|
drop(cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("dropped before flush")
|
|
|
|
|
}));
|
|
|
|
|
cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("before emit")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
cx.emit(());
|
|
|
|
|
cx.subscribe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _, _| events.borrow_mut().push("after emit")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
TestView
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(*events.borrow(), ["before emit"]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_observe_and_notify_from_view(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct View {
|
|
|
|
|
events: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = usize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Model {
|
|
|
|
|
count: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
|
let (_, view) = cx.add_window(Default::default(), |_| View::default());
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let model = cx.add_model(|_| Model::default());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
view.update(cx, |_, c| {
|
2021-08-23 22:07:50 +00:00
|
|
|
|
c.observe(&model, |me, observed, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
me.events.push(observed.read(c).count)
|
2021-08-23 22:54:24 +00:00
|
|
|
|
})
|
|
|
|
|
.detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
model.update(cx, |model, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
model.count = 11;
|
|
|
|
|
c.notify();
|
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
assert_eq!(view.read(cx).events, vec![11]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 15:24:48 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_view_notify_before_observe_in_same_update_cycle(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct TestView;
|
|
|
|
|
|
|
|
|
|
impl Entity for TestView {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View for TestView {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"TestView"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.add_window(Default::default(), |cx| {
|
|
|
|
|
drop(cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("dropped before flush")
|
|
|
|
|
}));
|
|
|
|
|
cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("before notify")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
cx.notify();
|
|
|
|
|
cx.observe(&cx.handle(), {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |_, _, _| events.borrow_mut().push("after notify")
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
TestView
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(*events.borrow(), ["before notify"]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_dropping_observers(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct View;
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, root_view) = cx.add_window(Default::default(), |_| View);
|
|
|
|
|
let observing_view = cx.add_view(root_view, |_| View);
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let observing_model = cx.add_model(|_| Model);
|
|
|
|
|
let observed_model = cx.add_model(|_| Model);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
observing_view.update(cx, |_, cx| {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
cx.observe(&observed_model, |_, _, _| {}).detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
|
observing_model.update(cx, |_, cx| {
|
2021-08-23 22:54:24 +00:00
|
|
|
|
cx.observe(&observed_model, |_, _, _| {}).detach();
|
2021-05-12 16:21:32 +00:00
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-11-29 22:16:19 +00:00
|
|
|
|
cx.update(|_| {
|
2021-05-12 16:21:32 +00:00
|
|
|
|
drop(observing_view);
|
|
|
|
|
drop(observing_model);
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
observed_model.update(cx, |_, cx| cx.notify());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 23:34:04 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_dropping_subscriptions_during_callback(cx: &mut MutableAppContext) {
|
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
|
type Event = u64;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Events
|
|
|
|
|
let observing_model = cx.add_model(|_| Model);
|
|
|
|
|
let observed_model = cx.add_model(|_| Model);
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
|
|
observing_model.update(cx, |_, cx| {
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
let subscription = Rc::new(RefCell::new(None));
|
|
|
|
|
*subscription.borrow_mut() = Some(cx.subscribe(&observed_model, {
|
|
|
|
|
let subscription = subscription.clone();
|
|
|
|
|
move |_, _, e, _| {
|
|
|
|
|
subscription.borrow_mut().take();
|
2022-08-10 21:39:24 +00:00
|
|
|
|
events.borrow_mut().push(*e);
|
2022-03-11 23:34:04 +00:00
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observed_model.update(cx, |_, cx| {
|
|
|
|
|
cx.emit(1);
|
|
|
|
|
cx.emit(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(*events.borrow(), [1]);
|
|
|
|
|
|
|
|
|
|
// Global Events
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
|
struct GlobalEvent(u64);
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
let subscription = Rc::new(RefCell::new(None));
|
|
|
|
|
*subscription.borrow_mut() = Some(cx.subscribe_global({
|
|
|
|
|
let subscription = subscription.clone();
|
|
|
|
|
move |e: &GlobalEvent, _| {
|
|
|
|
|
subscription.borrow_mut().take();
|
|
|
|
|
events.borrow_mut().push(e.clone());
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.emit_global(GlobalEvent(1));
|
|
|
|
|
cx.emit_global(GlobalEvent(2));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(*events.borrow(), [GlobalEvent(1)]);
|
|
|
|
|
|
|
|
|
|
// Model Observation
|
|
|
|
|
let observing_model = cx.add_model(|_| Model);
|
|
|
|
|
let observed_model = cx.add_model(|_| Model);
|
|
|
|
|
|
|
|
|
|
let observation_count = Rc::new(RefCell::new(0));
|
|
|
|
|
|
|
|
|
|
observing_model.update(cx, |_, cx| {
|
|
|
|
|
let observation_count = observation_count.clone();
|
|
|
|
|
let subscription = Rc::new(RefCell::new(None));
|
|
|
|
|
*subscription.borrow_mut() = Some(cx.observe(&observed_model, {
|
|
|
|
|
let subscription = subscription.clone();
|
|
|
|
|
move |_, _, _| {
|
|
|
|
|
subscription.borrow_mut().take();
|
|
|
|
|
*observation_count.borrow_mut() += 1;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observed_model.update(cx, |_, cx| {
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observed_model.update(cx, |_, cx| {
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 1);
|
|
|
|
|
|
|
|
|
|
// View Observation
|
|
|
|
|
struct View;
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, root_view) = cx.add_window(Default::default(), |_| View);
|
|
|
|
|
let observing_view = cx.add_view(&root_view, |_| View);
|
|
|
|
|
let observed_view = cx.add_view(&root_view, |_| View);
|
2022-03-11 23:34:04 +00:00
|
|
|
|
|
|
|
|
|
let observation_count = Rc::new(RefCell::new(0));
|
|
|
|
|
observing_view.update(cx, |_, cx| {
|
|
|
|
|
let observation_count = observation_count.clone();
|
|
|
|
|
let subscription = Rc::new(RefCell::new(None));
|
|
|
|
|
*subscription.borrow_mut() = Some(cx.observe(&observed_view, {
|
|
|
|
|
let subscription = subscription.clone();
|
|
|
|
|
move |_, _, _| {
|
|
|
|
|
subscription.borrow_mut().take();
|
|
|
|
|
*observation_count.borrow_mut() += 1;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observed_view.update(cx, |_, cx| {
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observed_view.update(cx, |_, cx| {
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 1);
|
2022-03-22 23:38:17 +00:00
|
|
|
|
|
|
|
|
|
// Global Observation
|
2022-03-23 17:27:27 +00:00
|
|
|
|
let observation_count = Rc::new(RefCell::new(0));
|
|
|
|
|
let subscription = Rc::new(RefCell::new(None));
|
|
|
|
|
*subscription.borrow_mut() = Some(cx.observe_global::<(), _>({
|
|
|
|
|
let observation_count = observation_count.clone();
|
|
|
|
|
let subscription = subscription.clone();
|
2022-05-23 16:23:25 +00:00
|
|
|
|
move |_| {
|
2022-03-23 17:27:27 +00:00
|
|
|
|
subscription.borrow_mut().take();
|
|
|
|
|
*observation_count.borrow_mut() += 1;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
cx.default_global::<()>();
|
|
|
|
|
cx.set_global(());
|
|
|
|
|
assert_eq!(*observation_count.borrow(), 1);
|
2022-03-11 23:34:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_focus(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct View {
|
2021-05-07 15:43:07 +00:00
|
|
|
|
name: String,
|
|
|
|
|
events: Arc<Mutex<Vec<String>>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
2021-05-07 15:43:07 +00:00
|
|
|
|
type Event = ();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_in(&mut self, focused: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
if cx.handle().id() == focused.id() {
|
|
|
|
|
self.events.lock().push(format!("{} focused", &self.name));
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 17:46:31 +00:00
|
|
|
|
fn focus_out(&mut self, blurred: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
2022-08-08 02:23:22 +00:00
|
|
|
|
if cx.handle().id() == blurred.id() {
|
|
|
|
|
self.events.lock().push(format!("{} blurred", &self.name));
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
let view_events: Arc<Mutex<Vec<String>>> = Default::default();
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, view_1) = cx.add_window(Default::default(), |_| View {
|
2022-04-14 16:22:32 +00:00
|
|
|
|
events: view_events.clone(),
|
2021-05-12 16:21:32 +00:00
|
|
|
|
name: "view 1".to_string(),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
});
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let view_2 = cx.add_view(&view_1, |_| View {
|
2022-04-14 16:22:32 +00:00
|
|
|
|
events: view_events.clone(),
|
2021-05-12 16:21:32 +00:00
|
|
|
|
name: "view 2".to_string(),
|
2021-03-10 04:00:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
2022-04-14 16:22:32 +00:00
|
|
|
|
let observed_events: Arc<Mutex<Vec<String>>> = Default::default();
|
|
|
|
|
view_1.update(cx, |_, cx| {
|
|
|
|
|
cx.observe_focus(&view_2, {
|
|
|
|
|
let observed_events = observed_events.clone();
|
2022-07-06 13:53:40 +00:00
|
|
|
|
move |this, view, focused, cx| {
|
|
|
|
|
let label = if focused { "focus" } else { "blur" };
|
2022-04-14 16:22:32 +00:00
|
|
|
|
observed_events.lock().push(format!(
|
2022-07-06 13:53:40 +00:00
|
|
|
|
"{} observed {}'s {}",
|
2022-04-14 16:22:32 +00:00
|
|
|
|
this.name,
|
2022-07-06 13:53:40 +00:00
|
|
|
|
view.read(cx).name,
|
|
|
|
|
label
|
2022-04-14 16:22:32 +00:00
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
});
|
|
|
|
|
view_2.update(cx, |_, cx| {
|
|
|
|
|
cx.observe_focus(&view_1, {
|
|
|
|
|
let observed_events = observed_events.clone();
|
2022-07-06 13:53:40 +00:00
|
|
|
|
move |this, view, focused, cx| {
|
|
|
|
|
let label = if focused { "focus" } else { "blur" };
|
2022-04-14 16:22:32 +00:00
|
|
|
|
observed_events.lock().push(format!(
|
2022-07-06 13:53:40 +00:00
|
|
|
|
"{} observed {}'s {}",
|
2022-04-14 16:22:32 +00:00
|
|
|
|
this.name,
|
2022-07-06 13:53:40 +00:00
|
|
|
|
view.read(cx).name,
|
|
|
|
|
label
|
2022-04-14 16:22:32 +00:00
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
});
|
2022-07-06 13:53:40 +00:00
|
|
|
|
assert_eq!(mem::take(&mut *view_events.lock()), ["view 1 focused"]);
|
|
|
|
|
assert_eq!(mem::take(&mut *observed_events.lock()), Vec::<&str>::new());
|
2022-04-14 16:22:32 +00:00
|
|
|
|
|
|
|
|
|
view_1.update(cx, |_, cx| {
|
2022-10-12 22:57:29 +00:00
|
|
|
|
// Ensure focus events are sent for all intermediate focuses
|
2022-04-14 16:22:32 +00:00
|
|
|
|
cx.focus(&view_2);
|
|
|
|
|
cx.focus(&view_1);
|
|
|
|
|
cx.focus(&view_2);
|
|
|
|
|
});
|
2022-07-06 13:53:40 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *view_events.lock()),
|
2022-10-12 22:57:29 +00:00
|
|
|
|
[
|
|
|
|
|
"view 1 blurred",
|
|
|
|
|
"view 2 focused",
|
|
|
|
|
"view 2 blurred",
|
|
|
|
|
"view 1 focused",
|
|
|
|
|
"view 1 blurred",
|
|
|
|
|
"view 2 focused"
|
|
|
|
|
],
|
2022-07-06 13:53:40 +00:00
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *observed_events.lock()),
|
|
|
|
|
[
|
2022-10-12 22:57:29 +00:00
|
|
|
|
"view 2 observed view 1's blur",
|
|
|
|
|
"view 1 observed view 2's focus",
|
|
|
|
|
"view 1 observed view 2's blur",
|
|
|
|
|
"view 2 observed view 1's focus",
|
2022-07-06 13:53:40 +00:00
|
|
|
|
"view 2 observed view 1's blur",
|
|
|
|
|
"view 1 observed view 2's focus"
|
|
|
|
|
]
|
|
|
|
|
);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
2022-07-06 13:53:40 +00:00
|
|
|
|
view_1.update(cx, |_, cx| cx.focus(&view_1));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *view_events.lock()),
|
|
|
|
|
["view 2 blurred", "view 1 focused"],
|
|
|
|
|
);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(
|
2022-07-06 13:53:40 +00:00
|
|
|
|
mem::take(&mut *observed_events.lock()),
|
2021-05-12 16:21:32 +00:00
|
|
|
|
[
|
2022-07-06 13:53:40 +00:00
|
|
|
|
"view 1 observed view 2's blur",
|
|
|
|
|
"view 2 observed view 1's focus"
|
|
|
|
|
]
|
2021-05-12 16:21:32 +00:00
|
|
|
|
);
|
2022-07-06 13:53:40 +00:00
|
|
|
|
|
|
|
|
|
view_1.update(cx, |_, cx| cx.focus(&view_2));
|
2022-04-14 16:22:32 +00:00
|
|
|
|
assert_eq!(
|
2022-07-06 13:53:40 +00:00
|
|
|
|
mem::take(&mut *view_events.lock()),
|
|
|
|
|
["view 1 blurred", "view 2 focused"],
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *observed_events.lock()),
|
2022-04-14 16:22:32 +00:00
|
|
|
|
[
|
2022-07-06 13:53:40 +00:00
|
|
|
|
"view 2 observed view 1's blur",
|
|
|
|
|
"view 1 observed view 2's focus"
|
2022-04-14 16:22:32 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2022-07-06 13:53:40 +00:00
|
|
|
|
|
|
|
|
|
view_1.update(cx, |_, _| drop(view_2));
|
|
|
|
|
assert_eq!(mem::take(&mut *view_events.lock()), ["view 1 focused"]);
|
|
|
|
|
assert_eq!(mem::take(&mut *observed_events.lock()), Vec::<&str>::new());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_deserialize_actions(cx: &mut MutableAppContext) {
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
|
|
|
|
|
pub struct ComplexAction {
|
|
|
|
|
arg: String,
|
|
|
|
|
count: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actions!(test::something, [SimpleAction]);
|
|
|
|
|
impl_actions!(test::something, [ComplexAction]);
|
|
|
|
|
|
|
|
|
|
cx.add_global_action(move |_: &SimpleAction, _: &mut MutableAppContext| {});
|
|
|
|
|
cx.add_global_action(move |_: &ComplexAction, _: &mut MutableAppContext| {});
|
|
|
|
|
|
|
|
|
|
let action1 = cx
|
|
|
|
|
.deserialize_action(
|
|
|
|
|
"test::something::ComplexAction",
|
|
|
|
|
Some(r#"{"arg": "a", "count": 5}"#),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let action2 = cx
|
|
|
|
|
.deserialize_action("test::something::SimpleAction", None)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
action1.as_any().downcast_ref::<ComplexAction>().unwrap(),
|
|
|
|
|
&ComplexAction {
|
|
|
|
|
arg: "a".to_string(),
|
|
|
|
|
count: 5,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
action2.as_any().downcast_ref::<SimpleAction>().unwrap(),
|
|
|
|
|
&SimpleAction
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_dispatch_action(cx: &mut MutableAppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
|
struct ViewA {
|
|
|
|
|
id: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for ViewA {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View for ViewA {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ViewB {
|
|
|
|
|
id: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for ViewB {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View for ViewB {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 07:29:42 +00:00
|
|
|
|
#[derive(Clone, Default, Deserialize, PartialEq)]
|
2022-04-08 22:32:56 +00:00
|
|
|
|
pub struct Action(pub String);
|
2022-04-07 23:20:49 +00:00
|
|
|
|
|
|
|
|
|
impl_actions!(test, [Action]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
let actions = Rc::new(RefCell::new(Vec::new()));
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_global_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |_: &Action, _: &mut MutableAppContext| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
actions.borrow_mut().push("global".to_string());
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut ViewA, action: &Action, cx| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
assert_eq!(action.0, "bar");
|
|
|
|
|
cx.propagate_action();
|
|
|
|
|
actions.borrow_mut().push(format!("{} a", view.id));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut ViewA, _: &Action, cx| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
if view.id != 1 {
|
|
|
|
|
cx.add_view(|cx| {
|
|
|
|
|
cx.propagate_action(); // Still works on a nested ViewContext
|
|
|
|
|
ViewB { id: 5 }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
actions.borrow_mut().push(format!("{} b", view.id));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut ViewB, _: &Action, cx| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
cx.propagate_action();
|
|
|
|
|
actions.borrow_mut().push(format!("{} c", view.id));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut ViewB, _: &Action, cx| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
cx.propagate_action();
|
|
|
|
|
actions.borrow_mut().push(format!("{} d", view.id));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-02-27 23:14:40 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.capture_action({
|
2022-02-27 23:14:40 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut ViewA, _: &Action, cx| {
|
2022-02-27 23:14:40 +00:00
|
|
|
|
cx.propagate_action();
|
|
|
|
|
actions.borrow_mut().push(format!("{} capture", view.id));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-05-30 08:01:23 +00:00
|
|
|
|
let observed_actions = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
cx.observe_actions({
|
|
|
|
|
let observed_actions = observed_actions.clone();
|
|
|
|
|
move |action_id, _| observed_actions.borrow_mut().push(action_id)
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
|
|
|
|
|
let view_2 = cx.add_view(&view_1, |_| ViewB { id: 2 });
|
|
|
|
|
let view_3 = cx.add_view(&view_2, |_| ViewA { id: 3 });
|
|
|
|
|
let view_4 = cx.add_view(&view_3, |_| ViewB { id: 4 });
|
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
cx.handle_dispatch_action_from_effect(
|
2021-05-12 16:21:32 +00:00
|
|
|
|
window_id,
|
2022-08-05 03:55:10 +00:00
|
|
|
|
Some(view_4.id()),
|
2022-04-08 22:32:56 +00:00
|
|
|
|
&Action("bar".to_string()),
|
2021-05-12 16:21:32 +00:00
|
|
|
|
);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
*actions.borrow(),
|
2022-02-27 23:14:40 +00:00
|
|
|
|
vec![
|
|
|
|
|
"1 capture",
|
|
|
|
|
"3 capture",
|
|
|
|
|
"4 d",
|
|
|
|
|
"4 c",
|
|
|
|
|
"3 b",
|
|
|
|
|
"3 a",
|
|
|
|
|
"2 d",
|
|
|
|
|
"2 c",
|
|
|
|
|
"1 b"
|
|
|
|
|
]
|
2021-05-12 16:21:32 +00:00
|
|
|
|
);
|
2022-05-30 08:01:23 +00:00
|
|
|
|
assert_eq!(*observed_actions.borrow(), [Action::default().id()]);
|
2021-03-19 03:33:16 +00:00
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
// Remove view_1, which doesn't propagate the action
|
2022-08-05 03:55:10 +00:00
|
|
|
|
|
2022-08-05 19:38:05 +00:00
|
|
|
|
let (window_id, view_2) = cx.add_window(Default::default(), |_| ViewB { id: 2 });
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let view_3 = cx.add_view(&view_2, |_| ViewA { id: 3 });
|
|
|
|
|
let view_4 = cx.add_view(&view_3, |_| ViewB { id: 4 });
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
actions.borrow_mut().clear();
|
2022-08-05 19:38:05 +00:00
|
|
|
|
cx.handle_dispatch_action_from_effect(
|
2021-05-12 16:21:32 +00:00
|
|
|
|
window_id,
|
2022-08-05 03:55:10 +00:00
|
|
|
|
Some(view_4.id()),
|
2022-04-08 22:32:56 +00:00
|
|
|
|
&Action("bar".to_string()),
|
2021-05-12 16:21:32 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*actions.borrow(),
|
2022-02-27 23:14:40 +00:00
|
|
|
|
vec![
|
|
|
|
|
"3 capture",
|
|
|
|
|
"4 d",
|
|
|
|
|
"4 c",
|
|
|
|
|
"3 b",
|
|
|
|
|
"3 a",
|
|
|
|
|
"2 d",
|
|
|
|
|
"2 c",
|
|
|
|
|
"global"
|
|
|
|
|
]
|
2021-05-12 16:21:32 +00:00
|
|
|
|
);
|
2022-05-30 08:01:23 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
*observed_actions.borrow(),
|
|
|
|
|
[Action::default().id(), Action::default().id()]
|
|
|
|
|
);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn test_dispatch_keystroke(cx: &mut MutableAppContext) {
|
2022-06-06 07:29:42 +00:00
|
|
|
|
#[derive(Clone, Deserialize, PartialEq)]
|
2022-04-08 22:32:56 +00:00
|
|
|
|
pub struct Action(String);
|
2022-04-07 23:20:49 +00:00
|
|
|
|
|
|
|
|
|
impl_actions!(test, [Action]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
|
|
struct View {
|
|
|
|
|
id: usize,
|
|
|
|
|
keymap_context: keymap::Context,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-03-18 19:13:31 +00:00
|
|
|
|
Empty::new().boxed()
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"View"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
|
|
|
|
|
self.keymap_context.clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View {
|
|
|
|
|
fn new(id: usize) -> Self {
|
|
|
|
|
View {
|
|
|
|
|
id,
|
|
|
|
|
keymap_context: keymap::Context::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
let mut view_1 = View::new(1);
|
|
|
|
|
let mut view_2 = View::new(2);
|
|
|
|
|
let mut view_3 = View::new(3);
|
|
|
|
|
view_1.keymap_context.set.insert("a".into());
|
2021-12-24 20:05:30 +00:00
|
|
|
|
view_2.keymap_context.set.insert("a".into());
|
2021-05-12 16:21:32 +00:00
|
|
|
|
view_2.keymap_context.set.insert("b".into());
|
2021-12-24 20:05:30 +00:00
|
|
|
|
view_3.keymap_context.set.insert("a".into());
|
|
|
|
|
view_3.keymap_context.set.insert("b".into());
|
2021-05-12 16:21:32 +00:00
|
|
|
|
view_3.keymap_context.set.insert("c".into());
|
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
|
let (window_id, view_1) = cx.add_window(Default::default(), |_| view_1);
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let view_2 = cx.add_view(&view_1, |_| view_2);
|
2022-08-10 23:26:53 +00:00
|
|
|
|
let _view_3 = cx.add_view(&view_2, |cx| {
|
2022-08-05 19:38:05 +00:00
|
|
|
|
cx.focus_self();
|
|
|
|
|
view_3
|
|
|
|
|
});
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
|
|
|
|
// This keymap's only binding dispatches an action on view 2 because that view will have
|
|
|
|
|
// "a" and "b" in its context, but not "c".
|
2021-08-22 17:58:19 +00:00
|
|
|
|
cx.add_bindings(vec![keymap::Binding::new(
|
|
|
|
|
"a",
|
2022-04-08 22:32:56 +00:00
|
|
|
|
Action("a".to_string()),
|
2021-08-22 17:58:19 +00:00
|
|
|
|
Some("a && b && !c"),
|
|
|
|
|
)]);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_bindings(vec![keymap::Binding::new(
|
|
|
|
|
"b",
|
|
|
|
|
Action("b".to_string()),
|
|
|
|
|
None,
|
|
|
|
|
)]);
|
2022-03-06 16:46:33 +00:00
|
|
|
|
|
|
|
|
|
let actions = Rc::new(RefCell::new(Vec::new()));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
cx.add_action({
|
2022-03-06 16:46:33 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |view: &mut View, action: &Action, cx| {
|
2022-03-06 16:46:33 +00:00
|
|
|
|
if action.0 == "a" {
|
|
|
|
|
actions.borrow_mut().push(format!("{} a", view.id));
|
|
|
|
|
} else {
|
|
|
|
|
actions
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.push(format!("{} {}", view.id, action.0));
|
|
|
|
|
cx.propagate_action();
|
|
|
|
|
}
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cx.add_global_action({
|
2022-03-06 16:46:33 +00:00
|
|
|
|
let actions = actions.clone();
|
2022-04-08 22:32:56 +00:00
|
|
|
|
move |action: &Action, _| {
|
2022-03-06 16:46:33 +00:00
|
|
|
|
actions.borrow_mut().push(format!("global {}", action.0));
|
2022-04-08 22:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
cx.dispatch_keystroke(window_id, &Keystroke::parse("a").unwrap());
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
2022-03-06 16:46:33 +00:00
|
|
|
|
assert_eq!(&*actions.borrow(), &["2 a"]);
|
|
|
|
|
|
|
|
|
|
actions.borrow_mut().clear();
|
2022-08-10 23:26:53 +00:00
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
cx.dispatch_keystroke(window_id, &Keystroke::parse("b").unwrap());
|
2022-03-06 16:46:33 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(&*actions.borrow(), &["3 b", "2 b", "1 b", "global b"]);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_model_condition(cx: &mut TestAppContext) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
struct Counter(usize);
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Counter {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Counter {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn inc(&mut self, cx: &mut ModelContext<Self>) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
self.0 += 1;
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.notify();
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let model = cx.add_model(|_| Counter(0));
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
let condition1 = model.condition(cx, |model, _| model.0 == 2);
|
|
|
|
|
let condition2 = model.condition(cx, |model, _| model.0 == 3);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
smol::pin!(condition1, condition2);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
model.update(cx, |model, cx| model.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition1).await, None);
|
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, None);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
model.update(cx, |model, cx| model.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition1).await, Some(()));
|
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, None);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
model.update(cx, |model, cx| model.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, Some(()));
|
2021-04-20 16:43:13 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
model.update(cx, |_, cx| cx.notify());
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-04-20 15:21:29 +00:00
|
|
|
|
#[should_panic]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_model_condition_timeout(cx: &mut TestAppContext) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let model = cx.add_model(|_| Model);
|
2022-08-10 21:39:24 +00:00
|
|
|
|
model.condition(cx, |_, _| false).await;
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-04-20 16:36:54 +00:00
|
|
|
|
#[should_panic(expected = "model dropped with pending condition")]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_model_condition_panic_on_drop(cx: &mut TestAppContext) {
|
2021-04-20 16:36:54 +00:00
|
|
|
|
struct Model;
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Model {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let model = cx.add_model(|_| Model);
|
2022-08-10 21:39:24 +00:00
|
|
|
|
let condition = model.condition(cx, |_, _| false);
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.update(|_| drop(model));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
condition.await;
|
2021-04-20 16:36:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_view_condition(cx: &mut TestAppContext) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
struct Counter(usize);
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Counter {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for Counter {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"test view"
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Counter {
|
2021-05-28 22:25:15 +00:00
|
|
|
|
fn inc(&mut self, cx: &mut ViewContext<Self>) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
self.0 += 1;
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.notify();
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let (_, view) = cx.add_window(|_| Counter(0));
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
let condition1 = view.condition(cx, |view, _| view.0 == 2);
|
|
|
|
|
let condition2 = view.condition(cx, |view, _| view.0 == 3);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
smol::pin!(condition1, condition2);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
view.update(cx, |view, cx| view.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition1).await, None);
|
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, None);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
view.update(cx, |view, cx| view.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition1).await, Some(()));
|
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, None);
|
2021-04-20 15:21:29 +00:00
|
|
|
|
|
2022-03-01 11:01:02 +00:00
|
|
|
|
view.update(cx, |view, cx| view.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
assert_eq!(poll_once(&mut condition2).await, Some(()));
|
2022-03-01 11:01:02 +00:00
|
|
|
|
view.update(cx, |_, cx| cx.notify());
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-04-20 15:21:29 +00:00
|
|
|
|
#[should_panic]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_view_condition_timeout(cx: &mut TestAppContext) {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
struct View;
|
|
|
|
|
|
|
|
|
|
impl super::Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"test view"
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-04-20 15:21:29 +00:00
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
|
let (_, view) = cx.add_window(|_| View);
|
2022-08-10 21:39:24 +00:00
|
|
|
|
view.condition(cx, |_, _| false).await;
|
2021-04-20 15:21:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
|
#[crate::test(self)]
|
2021-05-06 20:18:41 +00:00
|
|
|
|
#[should_panic(expected = "view dropped with pending condition")]
|
2022-03-01 11:01:02 +00:00
|
|
|
|
async fn test_view_condition_panic_on_drop(cx: &mut TestAppContext) {
|
2021-04-20 16:36:54 +00:00
|
|
|
|
struct View;
|
|
|
|
|
|
|
|
|
|
impl super::Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"test view"
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 15:51:26 +00:00
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2021-04-20 16:36:54 +00:00
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let (_, root_view) = cx.add_window(|_| View);
|
|
|
|
|
let view = cx.add_view(&root_view, |_| View);
|
2021-04-20 16:36:54 +00:00
|
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
|
let condition = view.condition(cx, |_, _| false);
|
2021-05-28 22:25:15 +00:00
|
|
|
|
cx.update(|_| drop(view));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
condition.await;
|
2021-04-20 16:36:54 +00:00
|
|
|
|
}
|
2022-03-16 13:51:54 +00:00
|
|
|
|
|
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_refresh_windows(cx: &mut MutableAppContext) {
|
|
|
|
|
struct View(usize);
|
|
|
|
|
|
|
|
|
|
impl super::Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"test view"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
Empty::new().named(format!("render count: {}", post_inc(&mut self.0)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (window_id, root_view) = cx.add_window(Default::default(), |_| View(0));
|
|
|
|
|
let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&root_view.id()].name(),
|
|
|
|
|
Some("render count: 0")
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-05 03:55:10 +00:00
|
|
|
|
let view = cx.add_view(&root_view, |cx| {
|
2022-03-16 13:51:54 +00:00
|
|
|
|
cx.refresh_windows();
|
|
|
|
|
View(0)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&root_view.id()].name(),
|
|
|
|
|
Some("render count: 1")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&view.id()].name(),
|
|
|
|
|
Some("render count: 0")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| cx.refresh_windows());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&root_view.id()].name(),
|
|
|
|
|
Some("render count: 2")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&view.id()].name(),
|
|
|
|
|
Some("render count: 1")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
|
|
|
|
cx.refresh_windows();
|
|
|
|
|
drop(view);
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(
|
|
|
|
|
presenter.borrow().rendered_views[&root_view.id()].name(),
|
|
|
|
|
Some("render count: 3")
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(presenter.borrow().rendered_views.len(), 1);
|
|
|
|
|
}
|
2022-07-05 13:47:45 +00:00
|
|
|
|
|
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
async fn test_window_activation(cx: &mut TestAppContext) {
|
|
|
|
|
struct View(&'static str);
|
|
|
|
|
|
|
|
|
|
impl super::Entity for View {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"test view"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
let (window_1, _) = cx.add_window(|cx: &mut ViewContext<View>| {
|
|
|
|
|
cx.observe_window_activation({
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |this, active, _| events.borrow_mut().push((this.0, active))
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
View("window 1")
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(mem::take(&mut *events.borrow_mut()), [("window 1", true)]);
|
|
|
|
|
|
|
|
|
|
let (window_2, _) = cx.add_window(|cx: &mut ViewContext<View>| {
|
|
|
|
|
cx.observe_window_activation({
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |this, active, _| events.borrow_mut().push((this.0, active))
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
View("window 2")
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *events.borrow_mut()),
|
|
|
|
|
[("window 1", false), ("window 2", true)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let (window_3, _) = cx.add_window(|cx: &mut ViewContext<View>| {
|
|
|
|
|
cx.observe_window_activation({
|
|
|
|
|
let events = events.clone();
|
|
|
|
|
move |this, active, _| events.borrow_mut().push((this.0, active))
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
View("window 3")
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *events.borrow_mut()),
|
|
|
|
|
[("window 2", false), ("window 3", true)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.simulate_window_activation(Some(window_2));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *events.borrow_mut()),
|
|
|
|
|
[("window 3", false), ("window 2", true)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.simulate_window_activation(Some(window_1));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *events.borrow_mut()),
|
|
|
|
|
[("window 2", false), ("window 1", true)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.simulate_window_activation(Some(window_3));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
mem::take(&mut *events.borrow_mut()),
|
|
|
|
|
[("window 1", false), ("window 3", true)]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cx.simulate_window_activation(Some(window_3));
|
|
|
|
|
assert_eq!(mem::take(&mut *events.borrow_mut()), []);
|
|
|
|
|
}
|
2022-10-13 13:59:43 +00:00
|
|
|
|
|
|
|
|
|
#[crate::test(self)]
|
|
|
|
|
fn test_child_view(cx: &mut MutableAppContext) {
|
|
|
|
|
struct Child {
|
|
|
|
|
rendered: Rc<Cell<bool>>,
|
|
|
|
|
dropped: Rc<Cell<bool>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Child {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for Child {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"child view"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
self.rendered.set(true);
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Child {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.dropped.set(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Parent {
|
|
|
|
|
child: Option<ViewHandle<Child>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::Entity for Parent {
|
|
|
|
|
type Event = ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl super::View for Parent {
|
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
|
"parent view"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
|
if let Some(child) = self.child.as_ref() {
|
|
|
|
|
ChildView::new(child, cx).boxed()
|
|
|
|
|
} else {
|
|
|
|
|
Empty::new().boxed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let child_rendered = Rc::new(Cell::new(false));
|
|
|
|
|
let child_dropped = Rc::new(Cell::new(false));
|
|
|
|
|
let (_, root_view) = cx.add_window(Default::default(), |cx| Parent {
|
|
|
|
|
child: Some(cx.add_view(|_| Child {
|
|
|
|
|
rendered: child_rendered.clone(),
|
|
|
|
|
dropped: child_dropped.clone(),
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
assert!(child_rendered.take());
|
|
|
|
|
assert!(!child_dropped.take());
|
|
|
|
|
|
|
|
|
|
root_view.update(cx, |view, cx| {
|
|
|
|
|
view.child.take();
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
assert!(!child_rendered.take());
|
|
|
|
|
assert!(child_dropped.take());
|
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
}
|