2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
2021-03-22 02:54:23 +00:00
|
|
|
elements::ElementBox,
|
2021-04-02 18:03:35 +00:00
|
|
|
executor,
|
2021-03-10 04:00:51 +00:00
|
|
|
keymap::{self, Keystroke},
|
2021-06-11 05:12:04 +00:00
|
|
|
platform::{self, Platform, PromptLevel, WindowOptions},
|
2021-03-21 16:50:07 +00:00
|
|
|
presenter::Presenter,
|
2021-04-20 15:21:29 +00:00
|
|
|
util::{post_inc, timeout},
|
2021-07-23 16:52:08 +00:00
|
|
|
AssetCache, AssetSource, ClipboardItem, EventContext, FontCache, PathPromptOptions,
|
|
|
|
TextLayoutCache,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
use anyhow::{anyhow, Result};
|
2021-05-12 21:16:49 +00:00
|
|
|
use async_task::Task;
|
2021-03-10 04:00:51 +00:00
|
|
|
use keymap::MatchResult;
|
2021-04-28 00:35:24 +00:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2021-03-21 17:38:23 +00:00
|
|
|
use platform::Event;
|
2021-08-20 14:24:33 +00:00
|
|
|
use postage::{mpsc, sink::Sink as _, stream::Stream as _};
|
2021-04-02 19:53:06 +00:00
|
|
|
use smol::prelude::*;
|
2021-03-10 04:00:51 +00:00
|
|
|
use std::{
|
|
|
|
any::{type_name, Any, TypeId},
|
|
|
|
cell::RefCell,
|
2021-08-25 15:35:27 +00:00
|
|
|
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque},
|
2021-03-10 04:00:51 +00:00
|
|
|
fmt::{self, Debug},
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
marker::PhantomData,
|
2021-07-23 17:44:56 +00:00
|
|
|
ops::{Deref, DerefMut},
|
2021-05-05 02:04:11 +00:00
|
|
|
path::{Path, PathBuf},
|
2021-03-10 04:00:51 +00:00
|
|
|
rc::{self, Rc},
|
|
|
|
sync::{Arc, Weak},
|
2021-04-20 15:21:29 +00:00
|
|
|
time::Duration,
|
2021-03-10 04:00:51 +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-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-02 21:55:27 +00:00
|
|
|
fn render(&self, cx: &RenderContext<'_, Self>) -> ElementBox;
|
2021-05-28 22:25:15 +00:00
|
|
|
fn on_focus(&mut self, _: &mut ViewContext<Self>) {}
|
|
|
|
fn on_blur(&mut self, _: &mut ViewContext<Self>) {}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn read_model_with<E: Entity, F: FnOnce(&E, &AppContext) -> T, T>(
|
|
|
|
&self,
|
|
|
|
handle: &ModelHandle<E>,
|
|
|
|
read: F,
|
|
|
|
) -> T;
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub trait UpdateModel {
|
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S;
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
pub trait UpgradeModelHandle {
|
|
|
|
fn upgrade_model_handle<T: Entity>(&self, handle: WeakModelHandle<T>)
|
|
|
|
-> Option<ModelHandle<T>>;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fn read_view_with<V, F, T>(&self, handle: &ViewHandle<V>, read: F) -> T
|
|
|
|
where
|
|
|
|
V: View,
|
|
|
|
F: FnOnce(&V, &AppContext) -> T;
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub trait UpdateView {
|
|
|
|
fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S;
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
pub trait Action: 'static + AnyAction {
|
|
|
|
type Argument: 'static + Clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AnyAction {
|
|
|
|
fn id(&self) -> TypeId;
|
2021-08-23 12:45:13 +00:00
|
|
|
fn name(&self) -> &'static str;
|
2021-08-23 10:00:31 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
2021-08-22 17:58:19 +00:00
|
|
|
fn boxed_clone(&self) -> Box<dyn AnyAction>;
|
|
|
|
fn boxed_clone_as_any(&self) -> Box<dyn Any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! action {
|
|
|
|
($name:ident, $arg:ty) => {
|
2021-08-23 03:02:48 +00:00
|
|
|
#[derive(Clone)]
|
2021-08-22 17:58:19 +00:00
|
|
|
pub struct $name(pub $arg);
|
|
|
|
|
|
|
|
impl $crate::Action for $name {
|
|
|
|
type Argument = $arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::AnyAction for $name {
|
|
|
|
fn id(&self) -> std::any::TypeId {
|
|
|
|
std::any::TypeId::of::<$name>()
|
|
|
|
}
|
|
|
|
|
2021-08-23 12:45:13 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
stringify!($name)
|
|
|
|
}
|
|
|
|
|
2021-08-23 10:00:31 +00:00
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
self
|
2021-08-22 17:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($name:ident) => {
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct $name;
|
|
|
|
|
|
|
|
impl $crate::Action for $name {
|
|
|
|
type Argument = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::AnyAction for $name {
|
|
|
|
fn id(&self) -> std::any::TypeId {
|
|
|
|
std::any::TypeId::of::<$name>()
|
|
|
|
}
|
|
|
|
|
2021-08-23 12:45:13 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
stringify!($name)
|
|
|
|
}
|
|
|
|
|
2021-08-23 10:00:31 +00:00
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
|
|
self
|
2021-08-22 17:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
2021-08-23 12:45:13 +00:00
|
|
|
Box::new(self.clone())
|
2021-08-22 17:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
2021-08-23 12:45:13 +00:00
|
|
|
Box::new(self.clone())
|
2021-08-22 17:58:19 +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> {
|
|
|
|
Action {
|
|
|
|
name: &'a str,
|
|
|
|
keystroke: Option<&'a str>,
|
2021-08-22 17:58:19 +00:00
|
|
|
action: Box<dyn AnyAction>,
|
2021-04-08 22:56:21 +00:00
|
|
|
},
|
|
|
|
Separator,
|
|
|
|
}
|
|
|
|
|
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>>);
|
|
|
|
|
2021-07-09 23:27:33 +00:00
|
|
|
pub struct BackgroundAppContext(*const RefCell<MutableAppContext>);
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
#[derive(Clone)]
|
2021-06-07 23:32:03 +00:00
|
|
|
pub struct TestAppContext {
|
|
|
|
cx: Rc<RefCell<MutableAppContext>>,
|
2021-06-07 23:42:49 +00:00
|
|
|
foreground_platform: Rc<platform::test::ForegroundPlatform>,
|
2021-06-07 23:32:03 +00:00
|
|
|
}
|
2021-04-10 06:05:09 +00:00
|
|
|
|
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())),
|
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-05-28 22:25:15 +00:00
|
|
|
let cx = app.0.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
foreground_platform.on_menu_command(Box::new(move |action| {
|
2021-05-28 22:25:15 +00:00
|
|
|
let mut cx = cx.borrow_mut();
|
2021-07-16 15:53:23 +00:00
|
|
|
if let Some(key_window_id) = cx.cx.platform.key_window_id() {
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some((presenter, _)) = cx.presenters_and_platform_windows.get(&key_window_id)
|
2021-04-12 22:42:33 +00:00
|
|
|
{
|
|
|
|
let presenter = presenter.clone();
|
2021-05-28 22:25:15 +00:00
|
|
|
let path = presenter.borrow().dispatch_path(cx.as_ref());
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.dispatch_action_any(key_window_id, &path, action);
|
2021-04-14 12:47:18 +00:00
|
|
|
} else {
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.dispatch_global_action_any(action);
|
2021-04-12 22:42:33 +00:00
|
|
|
}
|
2021-04-14 12:47:18 +00:00
|
|
|
} else {
|
2021-08-22 17:58:19 +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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_event<F>(self, mut callback: F) -> Self
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_open_files<F>(self, mut callback: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + FnMut(Vec<PathBuf>, &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-04-09 19:38:09 +00:00
|
|
|
.on_open_files(Box::new(move |paths| {
|
2021-05-28 22:25:15 +00:00
|
|
|
callback(paths, &mut *cx.borrow_mut())
|
2021-04-09 19:38:09 +00:00
|
|
|
}));
|
|
|
|
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();
|
|
|
|
on_finish_launching(&mut *cx);
|
2021-04-10 03:33:17 +00:00
|
|
|
}))
|
2021-04-09 19:38:09 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = callback(&mut *state);
|
|
|
|
state.flush_effects();
|
|
|
|
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
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
impl TestAppContext {
|
2021-07-08 19:03:00 +00:00
|
|
|
pub fn new(
|
2021-07-29 14:53:10 +00:00
|
|
|
foreground_platform: Rc<platform::test::ForegroundPlatform>,
|
|
|
|
platform: Arc<dyn Platform>,
|
2021-07-08 19:03:00 +00:00
|
|
|
foreground: Rc<executor::Foreground>,
|
|
|
|
background: Arc<executor::Background>,
|
2021-07-29 14:53:10 +00:00
|
|
|
font_cache: Arc<FontCache>,
|
2021-07-08 19:03:00 +00:00
|
|
|
first_entity_id: usize,
|
|
|
|
) -> Self {
|
2021-07-08 00:18:39 +00:00
|
|
|
let mut cx = MutableAppContext::new(
|
|
|
|
foreground.clone(),
|
2021-07-08 19:03:00 +00:00
|
|
|
background,
|
2021-07-08 00:18:39 +00:00
|
|
|
platform,
|
|
|
|
foreground_platform.clone(),
|
2021-07-29 14:53:10 +00:00
|
|
|
font_cache,
|
2021-07-08 00:18:39 +00:00
|
|
|
(),
|
|
|
|
);
|
|
|
|
cx.next_entity_id = first_entity_id;
|
2021-06-23 23:34:36 +00:00
|
|
|
let cx = TestAppContext {
|
2021-07-08 00:18:39 +00:00
|
|
|
cx: Rc::new(RefCell::new(cx)),
|
2021-06-23 23:34:36 +00:00
|
|
|
foreground_platform,
|
|
|
|
};
|
|
|
|
cx.cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx.cx));
|
|
|
|
cx
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
pub fn dispatch_action<A: Action>(
|
2021-03-10 04:00:51 +00:00
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
responder_chain: Vec<usize>,
|
2021-08-22 17:58:19 +00:00
|
|
|
action: A,
|
2021-03-10 04:00:51 +00:00
|
|
|
) {
|
2021-08-22 17:58:19 +00:00
|
|
|
self.cx
|
|
|
|
.borrow_mut()
|
|
|
|
.dispatch_action_any(window_id, &responder_chain, &action);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
pub fn dispatch_global_action<A: Action>(&self, action: A) {
|
|
|
|
self.cx.borrow_mut().dispatch_global_action(action);
|
2021-06-09 22:38:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn dispatch_keystroke(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
responder_chain: Vec<usize>,
|
|
|
|
keystroke: &Keystroke,
|
|
|
|
) -> Result<bool> {
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.dispatch_keystroke(window_id, responder_chain, keystroke)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut ModelContext<T>) -> T,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.pending_flushes += 1;
|
|
|
|
let handle = state.add_model(build_model);
|
|
|
|
state.flush_effects();
|
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
{
|
2021-08-05 17:48:35 +00:00
|
|
|
self.cx
|
|
|
|
.borrow_mut()
|
|
|
|
.add_window(Default::default(), build_root_view)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn window_ids(&self) -> Vec<usize> {
|
2021-06-07 23:32:03 +00:00
|
|
|
self.cx.borrow().window_ids().collect()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root_view<T: View>(&self, window_id: usize) -> Option<ViewHandle<T>> {
|
2021-06-07 23:32:03 +00:00
|
|
|
self.cx.borrow().root_view(window_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.pending_flushes += 1;
|
|
|
|
let handle = state.add_view(window_id, build_view);
|
|
|
|
state.flush_effects();
|
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_option_view<T, F>(
|
|
|
|
&mut self,
|
|
|
|
window_id: usize,
|
|
|
|
build_view: F,
|
|
|
|
) -> Option<ViewHandle<T>>
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.pending_flushes += 1;
|
|
|
|
let handle = state.add_option_view(window_id, build_view);
|
|
|
|
state.flush_effects();
|
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
pub fn read<T, F: FnOnce(&AppContext) -> T>(&self, callback: F) -> T {
|
2021-06-07 23:32:03 +00:00
|
|
|
callback(self.cx.borrow().as_ref())
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-04-10 06:11:13 +00:00
|
|
|
// Don't increment pending flushes in order to effects to be flushed before the callback
|
|
|
|
// completes, which is helpful in tests.
|
2021-03-10 04:00:51 +00:00
|
|
|
let result = callback(&mut *state);
|
2021-04-10 06:11:13 +00:00
|
|
|
// Flush effects after the callback just in case there are any. This can happen in edge
|
|
|
|
// cases such as the closure dropping handles.
|
2021-03-10 04:00:51 +00:00
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:07:09 +00:00
|
|
|
pub fn to_async(&self) -> AsyncAppContext {
|
|
|
|
AsyncAppContext(self.cx.clone())
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:51:28 +00:00
|
|
|
pub fn font_cache(&self) -> Arc<FontCache> {
|
2021-06-07 23:32:03 +00:00
|
|
|
self.cx.borrow().cx.font_cache.clone()
|
2021-03-19 18:12:10 +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.borrow().cx.platform.clone()
|
2021-03-19 18:12:10 +00:00
|
|
|
}
|
2021-05-05 18:04:39 +00:00
|
|
|
|
2021-06-23 23:40:43 +00:00
|
|
|
pub fn foreground(&self) -> Rc<executor::Foreground> {
|
|
|
|
self.cx.borrow().foreground().clone()
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:35:06 +00:00
|
|
|
pub fn background(&self) -> Arc<executor::Background> {
|
|
|
|
self.cx.borrow().background().clone()
|
2021-06-23 23:40:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 18:04:39 +00:00
|
|
|
pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
|
2021-06-07 23:42:49 +00:00
|
|
|
self.foreground_platform.simulate_new_path_selection(result);
|
2021-05-05 18:04:39 +00:00
|
|
|
}
|
2021-05-07 04:06:20 +00:00
|
|
|
|
|
|
|
pub fn did_prompt_for_new_path(&self) -> bool {
|
2021-06-07 23:42:49 +00:00
|
|
|
self.foreground_platform.as_ref().did_prompt_for_new_path()
|
2021-05-07 04:06:20 +00:00
|
|
|
}
|
2021-05-12 13:19:46 +00:00
|
|
|
|
|
|
|
pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) {
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-05-12 13:19:46 +00:00
|
|
|
let (_, window) = state
|
|
|
|
.presenters_and_platform_windows
|
|
|
|
.get_mut(&window_id)
|
|
|
|
.unwrap();
|
|
|
|
let test_window = window
|
|
|
|
.as_any_mut()
|
|
|
|
.downcast_mut::<platform::test::Window>()
|
|
|
|
.unwrap();
|
|
|
|
let callback = test_window
|
|
|
|
.last_prompt
|
|
|
|
.take()
|
|
|
|
.expect("prompt was not called");
|
|
|
|
(callback)(answer);
|
|
|
|
}
|
2021-03-10 04:00:51 +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()))
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
pub fn read<T, F: FnOnce(&AppContext) -> T>(&mut self, callback: F) -> T {
|
|
|
|
callback(self.0.borrow().as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update<T, F: FnOnce(&mut MutableAppContext) -> T>(&mut self, callback: F) -> T {
|
|
|
|
let mut state = self.0.borrow_mut();
|
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = callback(&mut *state);
|
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
{
|
|
|
|
let mut state = self.0.borrow_mut();
|
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = state.update_model(handle, update);
|
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
impl UpgradeModelHandle for AsyncAppContext {
|
|
|
|
fn upgrade_model_handle<T: Entity>(
|
|
|
|
&self,
|
|
|
|
handle: WeakModelHandle<T>,
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
self.0.borrow_mut().upgrade_model_handle(handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
impl ReadModelWith for AsyncAppContext {
|
|
|
|
fn read_model_with<E: Entity, F: FnOnce(&E, &AppContext) -> T, T>(
|
|
|
|
&self,
|
|
|
|
handle: &ModelHandle<E>,
|
|
|
|
read: F,
|
|
|
|
) -> 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 {
|
|
|
|
fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
{
|
|
|
|
let mut state = self.0.borrow_mut();
|
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = state.update_view(handle, update);
|
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadViewWith for AsyncAppContext {
|
|
|
|
fn read_view_with<V, F, T>(&self, handle: &ViewHandle<V>, read: F) -> T
|
|
|
|
where
|
|
|
|
V: View,
|
|
|
|
F: FnOnce(&V, &AppContext) -> 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
impl UpdateModel for TestAppContext {
|
2021-03-10 04:00:51 +00:00
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = state.update_model(handle, update);
|
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
impl ReadModelWith for TestAppContext {
|
|
|
|
fn read_model_with<E: Entity, F: FnOnce(&E, &AppContext) -> T, T>(
|
|
|
|
&self,
|
|
|
|
handle: &ModelHandle<E>,
|
|
|
|
read: F,
|
|
|
|
) -> T {
|
2021-06-07 23:32:03 +00:00
|
|
|
let cx = self.cx.borrow();
|
2021-05-28 22:25:15 +00:00
|
|
|
let cx = cx.as_ref();
|
|
|
|
read(handle.read(cx), cx)
|
2021-05-12 21:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:05:09 +00:00
|
|
|
impl UpdateView for TestAppContext {
|
2021-03-10 04:00:51 +00:00
|
|
|
fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut state = self.cx.borrow_mut();
|
2021-03-10 04:00:51 +00:00
|
|
|
state.pending_flushes += 1;
|
|
|
|
let result = state.update_view(handle, update);
|
|
|
|
state.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:16:49 +00:00
|
|
|
impl ReadViewWith for TestAppContext {
|
|
|
|
fn read_view_with<V, F, T>(&self, handle: &ViewHandle<V>, read: F) -> T
|
|
|
|
where
|
|
|
|
V: View,
|
|
|
|
F: FnOnce(&V, &AppContext) -> T,
|
|
|
|
{
|
2021-06-07 23:32:03 +00:00
|
|
|
let cx = self.cx.borrow();
|
2021-05-28 22:25:15 +00:00
|
|
|
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 =
|
2021-08-22 17:58:19 +00:00
|
|
|
dyn FnMut(&mut dyn AnyView, &dyn AnyAction, &mut MutableAppContext, usize, usize) -> bool;
|
|
|
|
type GlobalActionCallback = dyn FnMut(&dyn AnyAction, &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>;
|
|
|
|
type ObservationCallback = Box<dyn FnMut(&mut MutableAppContext) -> bool>;
|
|
|
|
|
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,
|
2021-08-22 17:58:19 +00:00
|
|
|
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
|
|
|
global_actions: HashMap<TypeId, Vec<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,
|
2021-08-25 15:35:27 +00:00
|
|
|
subscriptions: Arc<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>,
|
|
|
|
observations: Arc<Mutex<HashMap<usize, BTreeMap<usize, ObservationCallback>>>>,
|
2021-04-12 22:42:33 +00:00
|
|
|
presenters_and_platform_windows:
|
|
|
|
HashMap<usize, (Rc<RefCell<Presenter>>, Box<dyn platform::Window>)>,
|
2021-04-08 02:51:14 +00:00
|
|
|
debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
foreground: Rc<executor::Foreground>,
|
|
|
|
pending_effects: VecDeque<Effect>,
|
|
|
|
pending_flushes: usize,
|
|
|
|
flushing_effects: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
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(),
|
2021-04-27 03:24:23 +00:00
|
|
|
windows: Default::default(),
|
|
|
|
values: Default::default(),
|
2021-03-10 04:00:51 +00:00
|
|
|
ref_counts: Arc::new(Mutex::new(RefCounts::default())),
|
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
|
|
|
},
|
|
|
|
actions: HashMap::new(),
|
|
|
|
global_actions: HashMap::new(),
|
|
|
|
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,
|
|
|
|
subscriptions: Default::default(),
|
|
|
|
observations: Default::default(),
|
2021-04-12 22:42:33 +00:00
|
|
|
presenters_and_platform_windows: HashMap::new(),
|
2021-04-08 02:51:14 +00:00
|
|
|
debug_elements_callbacks: HashMap::new(),
|
2021-03-10 04:00:51 +00:00
|
|
|
foreground,
|
|
|
|
pending_effects: VecDeque::new(),
|
|
|
|
pending_flushes: 0,
|
|
|
|
flushing_effects: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
pub fn upgrade(&self) -> App {
|
|
|
|
App(self.weak_self.as_ref().unwrap().upgrade().unwrap())
|
|
|
|
}
|
|
|
|
|
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 on_debug_elements<F>(&mut self, window_id: usize, callback: F)
|
|
|
|
where
|
|
|
|
F: 'static + Fn(&AppContext) -> crate::json::Value,
|
|
|
|
{
|
|
|
|
self.debug_elements_callbacks
|
|
|
|
.insert(window_id, Box::new(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
|
|
|
|
self.debug_elements_callbacks
|
|
|
|
.get(&window_id)
|
2021-05-28 22:25:15 +00:00
|
|
|
.map(|debug_elements| debug_elements(&self.cx))
|
2021-04-08 02:51:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
pub fn add_action<A, V, 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,
|
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,
|
2021-08-22 17:58:19 +00:00
|
|
|
action: &dyn AnyAction,
|
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,
|
|
|
|
);
|
|
|
|
cx.halt_action_dispatch
|
2021-03-10 04:00:51 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
self.actions
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-22 17:58:19 +00:00
|
|
|
let handler = Box::new(move |action: &dyn AnyAction, 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
|
|
|
});
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
self.global_actions
|
|
|
|
.entry(TypeId::of::<A>())
|
|
|
|
.or_default()
|
|
|
|
.push(handler);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root_view_id(&self, window_id: usize) -> Option<usize> {
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.root_view_id(window_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focused_view_id(&self, window_id: usize) -> Option<usize> {
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.focused_view_id(window_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:45:42 +00:00
|
|
|
pub fn render_views(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
titlebar_height: f32,
|
|
|
|
) -> HashMap<usize, ElementBox> {
|
|
|
|
self.cx.render_views(window_id, titlebar_height)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 22:58:19 +00:00
|
|
|
pub fn update<T, F: FnOnce() -> T>(&mut self, callback: F) -> T {
|
|
|
|
self.pending_flushes += 1;
|
|
|
|
let result = callback();
|
|
|
|
self.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-06-07 23:02:24 +00:00
|
|
|
pub fn set_menus(&mut self, menus: Vec<Menu>) {
|
2021-06-07 23:42:49 +00:00
|
|
|
self.foreground_platform.set_menus(menus);
|
2021-04-12 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 09:57:05 +00:00
|
|
|
fn prompt<F>(
|
2021-05-12 09:54:48 +00:00
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
level: PromptLevel,
|
|
|
|
msg: &str,
|
|
|
|
answers: &[&str],
|
|
|
|
done_fn: F,
|
|
|
|
) where
|
|
|
|
F: 'static + FnOnce(usize, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
|
|
|
|
let foreground = self.foreground.clone();
|
|
|
|
let (_, window) = &self.presenters_and_platform_windows[&window_id];
|
|
|
|
window.prompt(
|
|
|
|
level,
|
|
|
|
msg,
|
|
|
|
answers,
|
|
|
|
Box::new(move |answer| {
|
|
|
|
foreground
|
|
|
|
.spawn(async move { (done_fn)(answer, &mut *app.borrow_mut()) })
|
|
|
|
.detach();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-14 14:30:03 +00:00
|
|
|
pub fn prompt_for_paths<F>(&self, options: PathPromptOptions, done_fn: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnOnce(Option<Vec<PathBuf>>, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
|
|
|
|
let foreground = self.foreground.clone();
|
2021-06-07 23:42:49 +00:00
|
|
|
self.foreground_platform.prompt_for_paths(
|
2021-04-14 14:30:03 +00:00
|
|
|
options,
|
|
|
|
Box::new(move |paths| {
|
|
|
|
foreground
|
|
|
|
.spawn(async move { (done_fn)(paths, &mut *app.borrow_mut()) })
|
|
|
|
.detach();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:04:11 +00:00
|
|
|
pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
let app = self.weak_self.as_ref().unwrap().upgrade().unwrap();
|
|
|
|
let foreground = self.foreground.clone();
|
2021-06-07 23:42:49 +00:00
|
|
|
self.foreground_platform.prompt_for_new_path(
|
2021-05-05 02:04:11 +00:00
|
|
|
directory,
|
|
|
|
Box::new(move |path| {
|
|
|
|
foreground
|
|
|
|
.spawn(async move { (done_fn)(path, &mut *app.borrow_mut()) })
|
|
|
|
.detach();
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
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
|
|
|
{
|
2021-08-23 22:54:24 +00:00
|
|
|
let id = post_inc(&mut self.next_subscription_id);
|
2021-08-23 21:58:37 +00:00
|
|
|
let emitter = handle.downgrade();
|
2021-08-23 22:02:30 +00:00
|
|
|
self.subscriptions
|
2021-08-23 22:54:24 +00:00
|
|
|
.lock()
|
2021-08-23 22:02:30 +00:00
|
|
|
.entry(handle.id())
|
|
|
|
.or_default()
|
2021-08-23 22:54:24 +00:00
|
|
|
.insert(
|
|
|
|
id,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
Subscription::Subscription {
|
|
|
|
id,
|
|
|
|
entity_id: handle.id(),
|
|
|
|
subscriptions: Some(Arc::downgrade(&self.subscriptions)),
|
|
|
|
}
|
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
|
|
|
{
|
2021-08-23 22:54:24 +00:00
|
|
|
let id = post_inc(&mut self.next_subscription_id);
|
2021-08-23 21:58:37 +00:00
|
|
|
let observed = handle.downgrade();
|
2021-08-23 22:02:30 +00:00
|
|
|
self.observations
|
2021-08-23 22:54:24 +00:00
|
|
|
.lock()
|
2021-08-23 22:02:30 +00:00
|
|
|
.entry(handle.id())
|
|
|
|
.or_default()
|
2021-08-23 22:54:24 +00:00
|
|
|
.insert(
|
|
|
|
id,
|
|
|
|
Box::new(move |cx| {
|
|
|
|
if let Some(observed) = H::upgrade_from(&observed, cx) {
|
|
|
|
callback(observed, cx)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
Subscription::Observation {
|
|
|
|
id,
|
|
|
|
entity_id: handle.id(),
|
|
|
|
observations: Some(Arc::downgrade(&self.observations)),
|
|
|
|
}
|
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) {
|
|
|
|
self.pending_effects
|
|
|
|
.push_back(Effect::ViewNotification { window_id, view_id });
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:57:10 +00:00
|
|
|
pub(crate) fn notify_all_views(&mut self) {
|
|
|
|
let notifications = self
|
|
|
|
.views
|
|
|
|
.keys()
|
|
|
|
.copied()
|
|
|
|
.map(|(window_id, view_id)| Effect::ViewNotification { window_id, view_id })
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.pending_effects.extend(notifications);
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
pub fn dispatch_action<A: Action>(
|
2021-04-09 22:58:19 +00:00
|
|
|
&mut self,
|
|
|
|
window_id: usize,
|
|
|
|
responder_chain: Vec<usize>,
|
2021-08-22 17:58:19 +00:00
|
|
|
action: &A,
|
2021-04-09 22:58:19 +00:00
|
|
|
) {
|
2021-08-22 17:58:19 +00:00
|
|
|
self.dispatch_action_any(window_id, &responder_chain, action);
|
2021-04-09 22:58:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 19:02:39 +00:00
|
|
|
pub(crate) fn dispatch_action_any(
|
2021-03-10 04:00:51 +00:00
|
|
|
&mut self,
|
|
|
|
window_id: usize,
|
2021-03-21 15:44:14 +00:00
|
|
|
path: &[usize],
|
2021-08-22 17:58:19 +00:00
|
|
|
action: &dyn AnyAction,
|
2021-03-10 04:00:51 +00:00
|
|
|
) -> bool {
|
|
|
|
self.pending_flushes += 1;
|
|
|
|
let mut halted_dispatch = false;
|
|
|
|
|
2021-03-21 15:44:14 +00:00
|
|
|
for view_id in path.iter().rev() {
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some(mut view) = self.cx.views.remove(&(window_id, *view_id)) {
|
2021-03-10 04:00:51 +00:00
|
|
|
let type_id = view.as_any().type_id();
|
|
|
|
|
|
|
|
if let Some((name, mut handlers)) = self
|
|
|
|
.actions
|
|
|
|
.get_mut(&type_id)
|
2021-08-22 17:58:19 +00:00
|
|
|
.and_then(|h| h.remove_entry(&action.id()))
|
2021-03-10 04:00:51 +00:00
|
|
|
{
|
|
|
|
for handler in handlers.iter_mut().rev() {
|
2021-08-22 17:58:19 +00:00
|
|
|
let halt_dispatch =
|
|
|
|
handler(view.as_mut(), action, self, window_id, *view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
if halt_dispatch {
|
|
|
|
halted_dispatch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.actions
|
|
|
|
.get_mut(&type_id)
|
|
|
|
.unwrap()
|
|
|
|
.insert(name, handlers);
|
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.views.insert((window_id, *view_id), view);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
if halted_dispatch {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !halted_dispatch {
|
2021-08-22 17:58:19 +00:00
|
|
|
self.dispatch_global_action_any(action);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flush_effects();
|
|
|
|
halted_dispatch
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) {
|
|
|
|
if let Some((name, mut handlers)) = self.global_actions.remove_entry(&action.id()) {
|
2021-03-10 04:00:51 +00:00
|
|
|
self.pending_flushes += 1;
|
|
|
|
for handler in handlers.iter_mut().rev() {
|
2021-08-22 17:58:19 +00:00
|
|
|
handler(action, self);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
self.global_actions.insert(name, handlers);
|
|
|
|
self.flush_effects();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dispatch_keystroke(
|
|
|
|
&mut self,
|
|
|
|
window_id: usize,
|
|
|
|
responder_chain: Vec<usize>,
|
|
|
|
keystroke: &Keystroke,
|
|
|
|
) -> Result<bool> {
|
|
|
|
let mut context_chain = Vec::new();
|
|
|
|
let mut context = keymap::Context::default();
|
|
|
|
for view_id in &responder_chain {
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some(view) = self.cx.views.get(&(window_id, *view_id)) {
|
2021-04-10 06:14:26 +00:00
|
|
|
context.extend(view.keymap_context(self.as_ref()));
|
2021-03-10 04:00:51 +00:00
|
|
|
context_chain.push(context.clone());
|
|
|
|
} else {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"View {} in responder chain does not exist",
|
|
|
|
view_id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pending = false;
|
2021-05-28 22:25:15 +00:00
|
|
|
for (i, cx) in context_chain.iter().enumerate().rev() {
|
2021-03-10 04:00:51 +00:00
|
|
|
match self
|
|
|
|
.keystroke_matcher
|
2021-05-28 22:25:15 +00:00
|
|
|
.push_keystroke(keystroke.clone(), responder_chain[i], cx)
|
2021-03-10 04:00:51 +00:00
|
|
|
{
|
|
|
|
MatchResult::None => {}
|
|
|
|
MatchResult::Pending => pending = true,
|
2021-08-22 17:58:19 +00:00
|
|
|
MatchResult::Action(action) => {
|
|
|
|
if self.dispatch_action_any(window_id, &responder_chain[0..=i], action.as_ref())
|
|
|
|
{
|
2021-03-10 04:00:51 +00:00
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(pending)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut ModelContext<T>) -> T,
|
|
|
|
{
|
|
|
|
self.pending_flushes += 1;
|
|
|
|
let model_id = post_inc(&mut self.next_entity_id);
|
2021-05-28 22:25:15 +00:00
|
|
|
let handle = ModelHandle::new(model_id, &self.cx.ref_counts);
|
|
|
|
let mut cx = ModelContext::new(self, model_id);
|
|
|
|
let model = build_model(&mut cx);
|
|
|
|
self.cx.models.insert(model_id, Box::new(model));
|
2021-03-10 04:00:51 +00:00
|
|
|
self.flush_effects();
|
2021-05-06 22:19:38 +00:00
|
|
|
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-04-06 12:29:42 +00:00
|
|
|
self.pending_flushes += 1;
|
2021-03-10 04:00:51 +00:00
|
|
|
let window_id = post_inc(&mut self.next_window_id);
|
2021-05-06 05:21:19 +00:00
|
|
|
let root_view = self.add_view(window_id, build_root_view);
|
2021-05-07 15:43:07 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.windows.insert(
|
2021-05-06 05:21:19 +00:00
|
|
|
window_id,
|
|
|
|
Window {
|
|
|
|
root_view: root_view.clone().into(),
|
|
|
|
focused_view_id: root_view.id(),
|
|
|
|
invalidation: None,
|
|
|
|
},
|
|
|
|
);
|
2021-08-05 17:48:35 +00:00
|
|
|
self.open_platform_window(window_id, window_options);
|
2021-07-12 15:44:10 +00:00
|
|
|
root_view.update(self, |view, cx| {
|
|
|
|
view.on_focus(cx);
|
|
|
|
cx.notify();
|
|
|
|
});
|
2021-04-06 12:29:42 +00:00
|
|
|
self.flush_effects();
|
|
|
|
|
2021-05-06 05:21:19 +00:00
|
|
|
(window_id, root_view)
|
2021-04-06 12:29:42 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
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);
|
2021-05-06 05:21:19 +00:00
|
|
|
self.remove_dropped_entities();
|
2021-05-05 17:34:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
fn open_platform_window(&mut self, window_id: usize, window_options: WindowOptions) {
|
|
|
|
let mut window =
|
|
|
|
self.cx
|
|
|
|
.platform
|
|
|
|
.open_window(window_id, window_options, self.foreground.clone());
|
2021-08-23 13:20:23 +00:00
|
|
|
let presenter = Rc::new(RefCell::new(
|
|
|
|
self.build_presenter(window_id, window.titlebar_height()),
|
|
|
|
));
|
2021-03-19 03:33:16 +00:00
|
|
|
|
2021-04-12 22:14:25 +00:00
|
|
|
{
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
let presenter = presenter.clone();
|
|
|
|
window.on_event(Box::new(move |event| {
|
2021-05-28 22:25:15 +00:00
|
|
|
app.update(|cx| {
|
2021-04-12 22:14:25 +00:00
|
|
|
if let Event::KeyDown { keystroke, .. } = &event {
|
2021-05-28 22:25:15 +00:00
|
|
|
if cx
|
2021-04-12 22:14:25 +00:00
|
|
|
.dispatch_keystroke(
|
|
|
|
window_id,
|
2021-05-28 22:25:15 +00:00
|
|
|
presenter.borrow().dispatch_path(cx.as_ref()),
|
2021-04-12 22:14:25 +00:00
|
|
|
keystroke,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 15:44:14 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
presenter.borrow_mut().dispatch_event(event, cx);
|
2021-04-12 22:14:25 +00:00
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
2021-03-21 04:15:04 +00:00
|
|
|
|
2021-04-12 22:14:25 +00:00
|
|
|
{
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
let presenter = presenter.clone();
|
|
|
|
window.on_resize(Box::new(move |window| {
|
2021-05-28 22:25:15 +00:00
|
|
|
app.update(|cx| {
|
2021-04-12 22:14:25 +00:00
|
|
|
let scene = presenter.borrow_mut().build_scene(
|
|
|
|
window.size(),
|
|
|
|
window.scale_factor(),
|
2021-05-28 22:25:15 +00:00
|
|
|
cx,
|
2021-04-12 22:14:25 +00:00
|
|
|
);
|
|
|
|
window.present_scene(scene);
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
2021-04-08 02:51:14 +00:00
|
|
|
|
2021-05-05 17:34:49 +00:00
|
|
|
{
|
|
|
|
let mut app = self.upgrade();
|
|
|
|
window.on_close(Box::new(move || {
|
2021-05-28 22:25:15 +00:00
|
|
|
app.update(|cx| cx.remove_window(window_id));
|
2021-05-05 17:34:49 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-04-12 22:42:33 +00:00
|
|
|
self.presenters_and_platform_windows
|
|
|
|
.insert(window_id, (presenter.clone(), window));
|
2021-04-12 22:14:25 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
self.on_debug_elements(window_id, move |cx| {
|
|
|
|
presenter.borrow().debug_elements(cx).unwrap()
|
2021-04-12 22:14:25 +00:00
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 13:20:23 +00:00
|
|
|
pub fn build_presenter(&self, window_id: usize, titlebar_height: f32) -> Presenter {
|
|
|
|
Presenter::new(
|
|
|
|
window_id,
|
|
|
|
titlebar_height,
|
|
|
|
self.cx.font_cache.clone(),
|
|
|
|
TextLayoutCache::new(self.cx.platform.fonts()),
|
|
|
|
self.assets.clone(),
|
|
|
|
self,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> T,
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
self.add_option_view(window_id, |cx| Some(build_view(cx)))
|
2021-03-10 04:00:51 +00:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_option_view<T, F>(
|
|
|
|
&mut self,
|
|
|
|
window_id: usize,
|
|
|
|
build_view: F,
|
|
|
|
) -> Option<ViewHandle<T>>
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut ViewContext<T>) -> Option<T>,
|
|
|
|
{
|
|
|
|
let view_id = post_inc(&mut self.next_entity_id);
|
|
|
|
self.pending_flushes += 1;
|
2021-05-28 22:25:15 +00:00
|
|
|
let handle = ViewHandle::new(window_id, view_id, &self.cx.ref_counts);
|
|
|
|
let mut cx = ViewContext::new(self, window_id, view_id);
|
|
|
|
let handle = if let Some(view) = build_view(&mut cx) {
|
|
|
|
self.cx.views.insert((window_id, view_id), Box::new(view));
|
|
|
|
if let Some(window) = self.cx.windows.get_mut(&window_id) {
|
2021-05-06 04:48:16 +00:00
|
|
|
window
|
|
|
|
.invalidation
|
|
|
|
.get_or_insert_with(Default::default)
|
|
|
|
.updated
|
|
|
|
.insert(view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-05-06 21:18:09 +00:00
|
|
|
Some(handle)
|
2021-03-10 04:00:51 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
self.flush_effects();
|
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_dropped_entities(&mut self) {
|
|
|
|
loop {
|
2021-04-27 03:52:18 +00:00
|
|
|
let (dropped_models, dropped_views, dropped_values) =
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.ref_counts.lock().take_dropped();
|
2021-04-27 03:24:23 +00:00
|
|
|
if dropped_models.is_empty() && dropped_views.is_empty() && dropped_values.is_empty() {
|
2021-03-10 04:00:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for model_id in dropped_models {
|
2021-08-23 22:54:24 +00:00
|
|
|
self.subscriptions.lock().remove(&model_id);
|
|
|
|
self.observations.lock().remove(&model_id);
|
2021-06-25 16:35:06 +00:00
|
|
|
let mut model = self.cx.models.remove(&model_id).unwrap();
|
|
|
|
model.release(self);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (window_id, view_id) in dropped_views {
|
2021-08-23 22:54:24 +00:00
|
|
|
self.subscriptions.lock().remove(&view_id);
|
|
|
|
self.observations.lock().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);
|
2021-05-06 22:30:10 +00:00
|
|
|
if window.focused_view_id == view_id {
|
|
|
|
Some(window.root_view.id())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(view_id) = change_focus_to {
|
|
|
|
self.focus(window_id, view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-27 03:24:23 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let mut values = self.cx.values.write();
|
2021-04-27 03:24:23 +00:00
|
|
|
for key in dropped_values {
|
|
|
|
values.remove(&key);
|
|
|
|
}
|
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);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
if !self.flushing_effects && self.pending_flushes == 0 {
|
|
|
|
self.flushing_effects = true;
|
|
|
|
|
2021-04-28 20:16:03 +00:00
|
|
|
loop {
|
|
|
|
if let Some(effect) = self.pending_effects.pop_front() {
|
|
|
|
match effect {
|
|
|
|
Effect::Event { entity_id, payload } => self.emit_event(entity_id, payload),
|
|
|
|
Effect::ModelNotification { model_id } => {
|
|
|
|
self.notify_model_observers(model_id)
|
|
|
|
}
|
|
|
|
Effect::ViewNotification { window_id, view_id } => {
|
|
|
|
self.notify_view_observers(window_id, view_id)
|
|
|
|
}
|
|
|
|
Effect::Focus { window_id, view_id } => {
|
|
|
|
self.focus(window_id, view_id);
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
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();
|
|
|
|
self.update_windows();
|
2021-04-28 20:16:03 +00:00
|
|
|
|
|
|
|
if self.pending_effects.is_empty() {
|
|
|
|
self.flushing_effects = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-04-28 19:02:39 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_windows(&mut self) {
|
|
|
|
let mut invalidations = HashMap::new();
|
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
|
|
|
|
|
|
|
for (window_id, 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();
|
2021-08-23 13:20:23 +00:00
|
|
|
presenter.invalidate(invalidation, self.as_ref());
|
|
|
|
let scene = presenter.build_scene(window.size(), window.scale_factor(), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_event(&mut self, entity_id: usize, payload: Box<dyn Any>) {
|
2021-08-23 22:54:24 +00:00
|
|
|
let callbacks = self.subscriptions.lock().remove(&entity_id);
|
|
|
|
if let Some(callbacks) = callbacks {
|
|
|
|
for (id, mut callback) in callbacks {
|
2021-08-23 21:58:37 +00:00
|
|
|
let alive = callback(payload.as_ref(), self);
|
2021-03-10 04:00:51 +00:00
|
|
|
if alive {
|
|
|
|
self.subscriptions
|
2021-08-23 22:54:24 +00:00
|
|
|
.lock()
|
2021-03-10 04:00:51 +00:00
|
|
|
.entry(entity_id)
|
|
|
|
.or_default()
|
2021-08-23 22:54:24 +00:00
|
|
|
.insert(id, callback);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_model_observers(&mut self, observed_id: usize) {
|
2021-08-23 22:54:24 +00:00
|
|
|
let callbacks = self.observations.lock().remove(&observed_id);
|
|
|
|
if let Some(callbacks) = callbacks {
|
2021-05-28 22:25:15 +00:00
|
|
|
if self.cx.models.contains_key(&observed_id) {
|
2021-08-23 22:54:24 +00:00
|
|
|
for (id, mut callback) in callbacks {
|
2021-08-23 21:58:37 +00:00
|
|
|
let alive = callback(self);
|
2021-03-10 04:00:51 +00:00
|
|
|
if alive {
|
2021-08-23 22:02:30 +00:00
|
|
|
self.observations
|
2021-08-23 22:54:24 +00:00
|
|
|
.lock()
|
2021-03-10 04:00:51 +00:00
|
|
|
.entry(observed_id)
|
|
|
|
.or_default()
|
2021-08-23 22:54:24 +00:00
|
|
|
.insert(id, callback);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:02:30 +00:00
|
|
|
fn notify_view_observers(&mut self, observed_window_id: usize, observed_view_id: usize) {
|
|
|
|
if let Some(window) = self.cx.windows.get_mut(&observed_window_id) {
|
2021-05-06 04:48:16 +00:00
|
|
|
window
|
|
|
|
.invalidation
|
|
|
|
.get_or_insert_with(Default::default)
|
|
|
|
.updated
|
2021-08-23 22:02:30 +00:00
|
|
|
.insert(observed_view_id);
|
2021-05-06 04:48:16 +00:00
|
|
|
}
|
2021-04-20 10:28:30 +00:00
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
let callbacks = self.observations.lock().remove(&observed_view_id);
|
|
|
|
if let Some(callbacks) = callbacks {
|
2021-08-23 22:02:30 +00:00
|
|
|
if self
|
|
|
|
.cx
|
|
|
|
.views
|
|
|
|
.contains_key(&(observed_window_id, observed_view_id))
|
|
|
|
{
|
2021-08-23 22:54:24 +00:00
|
|
|
for (id, mut callback) in callbacks {
|
2021-08-23 21:58:37 +00:00
|
|
|
let alive = callback(self);
|
|
|
|
if alive {
|
|
|
|
self.observations
|
2021-08-23 22:54:24 +00:00
|
|
|
.lock()
|
2021-08-23 21:58:37 +00:00
|
|
|
.entry(observed_view_id)
|
|
|
|
.or_default()
|
2021-08-23 22:54:24 +00:00
|
|
|
.insert(id, callback);
|
2021-05-03 22:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn focus(&mut self, window_id: usize, focused_id: usize) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.pending_flushes += 1;
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let blurred_id = self.cx.windows.get_mut(&window_id).map(|window| {
|
2021-05-06 05:21:19 +00:00
|
|
|
let blurred_id = window.focused_view_id;
|
|
|
|
window.focused_view_id = focused_id;
|
2021-05-06 04:48:16 +00:00
|
|
|
blurred_id
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(blurred_id) = blurred_id {
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some(mut blurred_view) = self.cx.views.remove(&(window_id, blurred_id)) {
|
2021-05-06 04:48:16 +00:00
|
|
|
blurred_view.on_blur(self, window_id, blurred_id);
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.views.insert((window_id, blurred_id), blurred_view);
|
2021-05-06 04:48:16 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some(mut focused_view) = self.cx.views.remove(&(window_id, focused_id)) {
|
2021-05-06 04:48:16 +00:00
|
|
|
focused_view.on_focus(self, window_id, focused_id);
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.views.insert((window_id, focused_id), focused_view);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.flush_effects();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-06-16 00:22:48 +00:00
|
|
|
let cx = self.to_async();
|
2021-05-28 22:25:15 +00:00
|
|
|
self.foreground.spawn(f(cx))
|
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
|
|
|
}
|
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 {
|
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
if let Some(mut model) = self.cx.models.remove(&handle.model_id) {
|
2021-03-10 04:00:51 +00:00
|
|
|
self.pending_flushes += 1;
|
2021-05-28 22:25:15 +00:00
|
|
|
let mut cx = ModelContext::new(self, handle.model_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
let result = update(
|
|
|
|
model
|
|
|
|
.as_any_mut()
|
|
|
|
.downcast_mut()
|
2021-05-06 22:19:38 +00:00
|
|
|
.expect("downcast is type safe"),
|
2021-05-28 22:25:15 +00:00
|
|
|
&mut cx,
|
2021-03-10 04:00:51 +00:00
|
|
|
);
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx.models.insert(handle.model_id, model);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.flush_effects();
|
|
|
|
result
|
|
|
|
} 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,
|
|
|
|
handle: WeakModelHandle<T>,
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-06 04:48:16 +00:00
|
|
|
panic!("circular view reference");
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateView for MutableAppContext {
|
|
|
|
fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
{
|
|
|
|
self.pending_flushes += 1;
|
2021-05-06 04:48:16 +00:00
|
|
|
let mut view = self
|
2021-05-28 22:25:15 +00:00
|
|
|
.cx
|
2021-05-06 04:48:16 +00:00
|
|
|
.views
|
|
|
|
.remove(&(handle.window_id, handle.view_id))
|
|
|
|
.expect("circular view update");
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let mut cx = ViewContext::new(self, handle.window_id, handle.view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
let result = update(
|
|
|
|
view.as_any_mut()
|
|
|
|
.downcast_mut()
|
2021-05-06 22:19:38 +00:00
|
|
|
.expect("downcast is type safe"),
|
2021-05-28 22:25:15 +00:00
|
|
|
&mut cx,
|
2021-03-10 04:00:51 +00:00
|
|
|
);
|
2021-05-28 22:25:15 +00:00
|
|
|
self.cx
|
2021-03-10 04:00:51 +00:00
|
|
|
.views
|
2021-05-06 04:48:16 +00:00
|
|
|
.insert((handle.window_id, handle.view_id), view);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.flush_effects();
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
windows: HashMap<usize, Window>,
|
2021-04-28 00:35:24 +00:00
|
|
|
values: RwLock<HashMap<(TypeId, usize), 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 {
|
|
|
|
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)
|
2021-05-06 05:21:19 +00:00
|
|
|
.map(|window| window.focused_view_id)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:45:42 +00:00
|
|
|
pub fn render_view(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
titlebar_height: f32,
|
|
|
|
) -> Result<ElementBox> {
|
2021-05-06 04:48:16 +00:00
|
|
|
self.views
|
|
|
|
.get(&(window_id, view_id))
|
2021-08-20 17:45:42 +00:00
|
|
|
.map(|v| v.render(window_id, view_id, titlebar_height, self))
|
2021-03-10 04:00:51 +00:00
|
|
|
.ok_or(anyhow!("view not found"))
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:45:42 +00:00
|
|
|
pub fn render_views(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
titlebar_height: f32,
|
|
|
|
) -> HashMap<usize, ElementBox> {
|
2021-05-06 04:48:16 +00:00
|
|
|
self.views
|
|
|
|
.iter()
|
|
|
|
.filter_map(|((win_id, view_id), view)| {
|
|
|
|
if *win_id == window_id {
|
2021-08-20 17:45:42 +00:00
|
|
|
Some((
|
|
|
|
*view_id,
|
|
|
|
view.render(*win_id, *view_id, titlebar_height, self),
|
|
|
|
))
|
2021-05-06 04:48:16 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
})
|
2021-05-06 04:48:16 +00:00
|
|
|
.collect::<HashMap<_, ElementBox>>()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-04-05 23:45:55 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-27 03:52:18 +00:00
|
|
|
pub fn value<Tag: 'static, T: 'static + Default>(&self, id: usize) -> ValueHandle<T> {
|
2021-04-27 03:24:23 +00:00
|
|
|
let key = (TypeId::of::<Tag>(), id);
|
2021-07-23 16:52:08 +00:00
|
|
|
self.values
|
|
|
|
.write()
|
|
|
|
.entry(key)
|
|
|
|
.or_insert_with(|| Box::new(T::default()));
|
2021-04-27 03:24:23 +00:00
|
|
|
ValueHandle::new(TypeId::of::<Tag>(), id, &self.ref_counts)
|
|
|
|
}
|
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,
|
|
|
|
handle: WeakModelHandle<T>,
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
if self.models.contains_key(&handle.model_id) {
|
|
|
|
Some(ModelHandle::new(handle.model_id, &self.ref_counts))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
focused_view_id: usize,
|
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 {
|
|
|
|
Event {
|
|
|
|
entity_id: usize,
|
|
|
|
payload: Box<dyn Any>,
|
|
|
|
},
|
|
|
|
ModelNotification {
|
|
|
|
model_id: usize,
|
|
|
|
},
|
|
|
|
ViewNotification {
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
},
|
|
|
|
Focus {
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:44:10 +00:00
|
|
|
impl Debug for Effect {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Effect::Event { entity_id, .. } => f
|
|
|
|
.debug_struct("Effect::Event")
|
|
|
|
.field("entity_id", entity_id)
|
|
|
|
.finish(),
|
|
|
|
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(),
|
|
|
|
Effect::Focus { window_id, view_id } => f
|
|
|
|
.debug_struct("Effect::Focus")
|
|
|
|
.field("window_id", window_id)
|
|
|
|
.field("view_id", view_id)
|
|
|
|
.finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-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-03-10 04:00:51 +00:00
|
|
|
fn ui_name(&self) -> &'static str;
|
2021-08-20 17:45:42 +00:00
|
|
|
fn render<'a>(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
titlebar_height: f32,
|
|
|
|
cx: &AppContext,
|
|
|
|
) -> ElementBox;
|
2021-05-28 22:25:15 +00:00
|
|
|
fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
|
|
|
|
fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
|
|
|
|
fn keymap_context(&self, cx: &AppContext) -> keymap::Context;
|
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-03-10 04:00:51 +00:00
|
|
|
fn ui_name(&self) -> &'static str {
|
|
|
|
T::ui_name()
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:45:42 +00:00
|
|
|
fn render<'a>(
|
|
|
|
&self,
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
titlebar_height: f32,
|
|
|
|
cx: &AppContext,
|
|
|
|
) -> ElementBox {
|
2021-08-02 21:55:27 +00:00
|
|
|
View::render(
|
|
|
|
self,
|
|
|
|
&RenderContext {
|
|
|
|
window_id,
|
|
|
|
view_id,
|
|
|
|
app: cx,
|
|
|
|
view_type: PhantomData::<T>,
|
2021-08-20 17:45:42 +00:00
|
|
|
titlebar_height,
|
2021-08-02 21:55:27 +00:00
|
|
|
},
|
|
|
|
)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
View::on_focus(self, &mut cx);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize) {
|
|
|
|
let mut cx = ViewContext::new(cx, window_id, view_id);
|
|
|
|
View::on_blur(self, &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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-10 04:00:51 +00:00
|
|
|
self.app
|
2021-08-23 21:58:37 +00:00
|
|
|
.pending_effects
|
|
|
|
.push_back(Effect::ModelNotification {
|
2021-03-10 04:00:51 +00:00
|
|
|
model_id: self.model_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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>),
|
|
|
|
{
|
|
|
|
let subscriber = self.handle().downgrade();
|
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>),
|
|
|
|
{
|
|
|
|
let observer = self.handle().downgrade();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-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,
|
|
|
|
{
|
|
|
|
let handle = self.handle().downgrade();
|
|
|
|
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> {
|
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
handle: WeakModelHandle<T>,
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 10:59:58 +00:00
|
|
|
impl<M> Deref for ModelContext<'_, M> {
|
|
|
|
type Target = MutableAppContext;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.app
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
|
|
|
halt_action_dispatch: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
halt_action_dispatch: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:54:48 +00:00
|
|
|
pub fn prompt<F>(&self, level: PromptLevel, msg: &str, answers: &[&str], done_fn: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnOnce(usize, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
self.app
|
|
|
|
.prompt(self.window_id, level, msg, answers, done_fn)
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:04:11 +00:00
|
|
|
pub fn prompt_for_paths<F>(&self, options: PathPromptOptions, done_fn: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnOnce(Option<Vec<PathBuf>>, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
self.app.prompt_for_paths(options, done_fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prompt_for_new_path<F>(&self, directory: &Path, done_fn: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnOnce(Option<PathBuf>, &mut MutableAppContext),
|
|
|
|
{
|
|
|
|
self.app.prompt_for_new_path(directory, done_fn)
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
self.app.pending_effects.push_back(Effect::Focus {
|
|
|
|
window_id: handle.window_id,
|
|
|
|
view_id: handle.view_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_self(&mut self) {
|
|
|
|
self.app.pending_effects.push_back(Effect::Focus {
|
|
|
|
window_id: self.window_id,
|
|
|
|
view_id: self.view_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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_view<S, F>(&mut self, build_view: F) -> ViewHandle<S>
|
|
|
|
where
|
|
|
|
S: View,
|
|
|
|
F: FnOnce(&mut ViewContext<S>) -> S,
|
|
|
|
{
|
|
|
|
self.app.add_view(self.window_id, build_view)
|
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
|
|
|
{
|
|
|
|
self.app.add_option_view(self.window_id, build_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-08-23 21:58:37 +00:00
|
|
|
let subscriber = self.handle().downgrade();
|
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-08-23 21:58:37 +00:00
|
|
|
let observer = self.handle().downgrade();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-02 22:57:10 +00:00
|
|
|
pub fn notify_all(&mut self) {
|
|
|
|
self.app.notify_all_views();
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn propagate_action(&mut self) {
|
|
|
|
self.halt_action_dispatch = false;
|
|
|
|
}
|
|
|
|
|
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-08-02 21:55:27 +00:00
|
|
|
pub struct RenderContext<'a, T: View> {
|
|
|
|
pub app: &'a AppContext,
|
2021-08-20 17:45:42 +00:00
|
|
|
pub titlebar_height: f32,
|
2021-08-02 21:55:27 +00:00
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
view_type: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: View> RenderContext<'a, T> {
|
|
|
|
pub fn handle(&self) -> WeakViewHandle<T> {
|
|
|
|
WeakViewHandle::new(self.window_id, self.view_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
type Target = AppContext;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.app
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
&self.app
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
handle: WeakModelHandle<T>,
|
|
|
|
) -> Option<ModelHandle<T>> {
|
|
|
|
self.cx.upgrade_model_handle(handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
impl<V: View> UpdateModel for ViewContext<'_, V> {
|
|
|
|
fn update_model<T, F, S>(&mut self, handle: &ModelHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: Entity,
|
|
|
|
F: FnOnce(&mut T, &mut ModelContext<T>) -> S,
|
|
|
|
{
|
|
|
|
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> {
|
|
|
|
fn update_view<T, F, S>(&mut self, handle: &ViewHandle<T>, update: F) -> S
|
|
|
|
where
|
|
|
|
T: View,
|
|
|
|
F: FnOnce(&mut T, &mut ViewContext<T>) -> S,
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
|
|
|
pub enum EntityLocation {
|
|
|
|
Model(usize),
|
|
|
|
View(usize, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ModelHandle<T> {
|
|
|
|
model_id: usize,
|
|
|
|
model_type: PhantomData<T>,
|
2021-05-07 17:47:04 +00:00
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
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);
|
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(),
|
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
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
pub fn read_with<'a, 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-05-28 22:25:15 +00:00
|
|
|
cx.read_model_with(self, read)
|
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-05-28 22:25:15 +00:00
|
|
|
cx.update_model(self, update)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-04-20 04:01:54 +00:00
|
|
|
|
2021-08-24 20:12:02 +00:00
|
|
|
pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
|
|
|
|
let (mut tx, mut rx) = mpsc::channel(1);
|
|
|
|
let mut cx = cx.cx.borrow_mut();
|
|
|
|
let subscription = cx.observe(self, move |_, _| {
|
|
|
|
tx.blocking_send(()).ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
let duration = if std::env::var("CI").is_ok() {
|
|
|
|
Duration::from_secs(5)
|
|
|
|
} else {
|
|
|
|
Duration::from_secs(1)
|
|
|
|
};
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let notification = timeout(duration, rx.recv())
|
|
|
|
.await
|
|
|
|
.expect("next notification timed out");
|
|
|
|
drop(subscription);
|
|
|
|
notification.expect("model dropped while test was waiting for its next notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_event(&self, cx: &TestAppContext) -> impl Future<Output = T::Event>
|
|
|
|
where
|
|
|
|
T::Event: Clone,
|
|
|
|
{
|
|
|
|
let (mut tx, mut rx) = mpsc::channel(1);
|
|
|
|
let mut cx = cx.cx.borrow_mut();
|
|
|
|
let subscription = cx.subscribe(self, move |_, event, _| {
|
|
|
|
tx.blocking_send(event.clone()).ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
let duration = if std::env::var("CI").is_ok() {
|
|
|
|
Duration::from_secs(5)
|
|
|
|
} else {
|
|
|
|
Duration::from_secs(1)
|
|
|
|
};
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let event = timeout(duration, rx.recv())
|
|
|
|
.await
|
|
|
|
.expect("next event timed out");
|
|
|
|
drop(subscription);
|
|
|
|
event.expect("model dropped while test was waiting for its next event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 04:01:54 +00:00
|
|
|
pub fn condition(
|
|
|
|
&self,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &TestAppContext,
|
2021-04-28 19:42:34 +00:00
|
|
|
mut predicate: impl FnMut(&T, &AppContext) -> bool,
|
|
|
|
) -> impl Future<Output = ()> {
|
2021-05-06 20:18:41 +00:00
|
|
|
let (tx, mut rx) = mpsc::channel(1024);
|
|
|
|
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut cx = cx.cx.borrow_mut();
|
2021-08-23 22:54:24 +00:00
|
|
|
let subscriptions = (
|
|
|
|
cx.observe(self, {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
move |_, _| {
|
|
|
|
tx.blocking_send(()).ok();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
cx.subscribe(self, {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
move |_, _, _| {
|
|
|
|
tx.blocking_send(()).ok();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2021-05-06 20:18:41 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
|
2021-04-20 16:36:54 +00:00
|
|
|
let handle = self.downgrade();
|
2021-05-19 12:50:28 +00:00
|
|
|
let duration = if std::env::var("CI").is_ok() {
|
2021-06-22 00:07:56 +00:00
|
|
|
Duration::from_secs(5)
|
2021-05-19 12:50:28 +00:00
|
|
|
} else {
|
2021-06-22 00:07:56 +00:00
|
|
|
Duration::from_secs(1)
|
2021-05-19 12:50:28 +00:00
|
|
|
};
|
2021-04-20 04:01:54 +00:00
|
|
|
|
|
|
|
async move {
|
2021-04-28 19:42:34 +00:00
|
|
|
timeout(duration, async move {
|
2021-04-20 15:21:29 +00:00
|
|
|
loop {
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
let cx = cx.borrow();
|
|
|
|
let cx = cx.as_ref();
|
2021-04-20 16:36:54 +00:00
|
|
|
if predicate(
|
|
|
|
handle
|
2021-05-28 22:25:15 +00:00
|
|
|
.upgrade(cx)
|
2021-04-20 16:36:54 +00:00
|
|
|
.expect("model dropped with pending condition")
|
2021-05-28 22:25:15 +00:00
|
|
|
.read(cx),
|
|
|
|
cx,
|
2021-04-20 16:36:54 +00:00
|
|
|
) {
|
2021-04-20 15:21:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:36:54 +00:00
|
|
|
rx.recv()
|
|
|
|
.await
|
|
|
|
.expect("model dropped with pending condition");
|
2021-04-20 04:01:54 +00:00
|
|
|
}
|
2021-04-20 15:21:29 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.expect("condition timed out");
|
2021-08-23 22:54:24 +00:00
|
|
|
drop(subscriptions);
|
2021-04-20 04:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for ModelHandle<T> {
|
|
|
|
fn clone(&self) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
self.ref_counts.lock().inc_model(self.model_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
Self {
|
|
|
|
model_id: self.model_id,
|
|
|
|
model_type: PhantomData,
|
|
|
|
ref_counts: self.ref_counts.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PartialEq for ModelHandle<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.model_id == other.model_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Eq for ModelHandle<T> {}
|
|
|
|
|
|
|
|
impl<T> Hash for ModelHandle<T> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.model_id.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
impl<T> std::borrow::Borrow<usize> for ModelHandle<T> {
|
2021-03-10 04:00:51 +00:00
|
|
|
fn borrow(&self) -> &usize {
|
|
|
|
&self.model_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Debug for ModelHandle<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_tuple(&format!("ModelHandle<{}>", type_name::<T>()))
|
|
|
|
.field(&self.model_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> Send for ModelHandle<T> {}
|
|
|
|
unsafe impl<T> Sync for ModelHandle<T> {}
|
|
|
|
|
|
|
|
impl<T> Drop for ModelHandle<T> {
|
|
|
|
fn drop(&mut self) {
|
2021-05-07 17:47:04 +00:00
|
|
|
self.ref_counts.lock().dec_model(self.model_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>,
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
pub fn upgrade(self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<T>> {
|
|
|
|
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> {}
|
|
|
|
|
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>>,
|
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);
|
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(),
|
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-05-28 22:25:15 +00:00
|
|
|
cx.read_view_with(self, read)
|
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-05-28 22:25:15 +00:00
|
|
|
cx.update_view(self, update)
|
2021-03-10 04:00:51 +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)
|
|
|
|
}
|
2021-04-20 04:01:54 +00:00
|
|
|
|
|
|
|
pub fn condition(
|
|
|
|
&self,
|
2021-05-28 22:25:15 +00:00
|
|
|
cx: &TestAppContext,
|
2021-05-06 20:18:41 +00:00
|
|
|
mut predicate: impl FnMut(&T, &AppContext) -> bool,
|
|
|
|
) -> impl Future<Output = ()> {
|
|
|
|
let (tx, mut rx) = mpsc::channel(1024);
|
|
|
|
|
2021-06-07 23:32:03 +00:00
|
|
|
let mut cx = cx.cx.borrow_mut();
|
2021-08-23 22:54:24 +00:00
|
|
|
let subscriptions = self.update(&mut *cx, |_, cx| {
|
|
|
|
(
|
|
|
|
cx.observe(self, {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
move |_, _, _| {
|
|
|
|
tx.blocking_send(()).ok();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
cx.subscribe(self, {
|
|
|
|
let mut tx = tx.clone();
|
|
|
|
move |_, _, _, _| {
|
|
|
|
tx.blocking_send(()).ok();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
2021-05-06 20:18:41 +00:00
|
|
|
});
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let cx = cx.weak_self.as_ref().unwrap().upgrade().unwrap();
|
2021-04-20 16:36:54 +00:00
|
|
|
let handle = self.downgrade();
|
2021-05-19 12:50:28 +00:00
|
|
|
let duration = if std::env::var("CI").is_ok() {
|
|
|
|
Duration::from_secs(2)
|
2021-05-19 12:42:10 +00:00
|
|
|
} else {
|
|
|
|
Duration::from_millis(500)
|
|
|
|
};
|
2021-04-20 04:01:54 +00:00
|
|
|
|
|
|
|
async move {
|
2021-05-06 20:18:41 +00:00
|
|
|
timeout(duration, async move {
|
2021-04-20 15:21:29 +00:00
|
|
|
loop {
|
|
|
|
{
|
2021-05-28 22:25:15 +00:00
|
|
|
let cx = cx.borrow();
|
|
|
|
let cx = cx.as_ref();
|
2021-04-20 16:36:54 +00:00
|
|
|
if predicate(
|
|
|
|
handle
|
2021-05-28 22:25:15 +00:00
|
|
|
.upgrade(cx)
|
2021-05-06 20:18:41 +00:00
|
|
|
.expect("view dropped with pending condition")
|
2021-05-28 22:25:15 +00:00
|
|
|
.read(cx),
|
|
|
|
cx,
|
2021-04-20 16:36:54 +00:00
|
|
|
) {
|
2021-04-20 15:21:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:36:54 +00:00
|
|
|
rx.recv()
|
|
|
|
.await
|
2021-05-06 20:18:41 +00:00
|
|
|
.expect("view dropped with pending condition");
|
2021-04-20 04:01:54 +00:00
|
|
|
}
|
2021-04-20 15:21:29 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.expect("condition timed out");
|
2021-08-23 22:54:24 +00:00
|
|
|
drop(subscriptions);
|
2021-04-20 04:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for ViewHandle<T> {
|
|
|
|
fn clone(&self) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
self.ref_counts
|
|
|
|
.lock()
|
|
|
|
.inc_view(self.window_id, self.view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
Self {
|
|
|
|
window_id: self.window_id,
|
|
|
|
view_id: self.view_id,
|
|
|
|
view_type: PhantomData,
|
|
|
|
ref_counts: self.ref_counts.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PartialEq for ViewHandle<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.window_id == other.window_id && self.view_id == other.view_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Eq for ViewHandle<T> {}
|
|
|
|
|
|
|
|
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);
|
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>>,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnyViewHandle {
|
|
|
|
pub fn id(&self) -> usize {
|
|
|
|
self.view_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is<T: 'static>(&self) -> bool {
|
|
|
|
TypeId::of::<T>() == self.view_type
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
unsafe {
|
|
|
|
Arc::decrement_strong_count(&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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:05:05 +00:00
|
|
|
impl Clone for AnyViewHandle {
|
|
|
|
fn clone(&self) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
self.ref_counts
|
|
|
|
.lock()
|
|
|
|
.inc_view(self.window_id, self.view_id);
|
2021-05-07 21:05:05 +00:00
|
|
|
Self {
|
|
|
|
window_id: self.window_id,
|
|
|
|
view_id: self.view_id,
|
|
|
|
view_type: self.view_type,
|
|
|
|
ref_counts: self.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-13 01:29:17 +00:00
|
|
|
handle
|
|
|
|
.ref_counts
|
|
|
|
.lock()
|
|
|
|
.inc_view(handle.window_id, handle.view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
AnyViewHandle {
|
|
|
|
window_id: handle.window_id,
|
|
|
|
view_id: handle.view_id,
|
|
|
|
view_type: TypeId::of::<T>(),
|
|
|
|
ref_counts: handle.ref_counts.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
Arc::decrement_strong_count(&handle.ref_counts);
|
|
|
|
}
|
|
|
|
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);
|
2021-05-06 05:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:24:48 +00:00
|
|
|
pub struct AnyModelHandle {
|
|
|
|
model_id: usize,
|
|
|
|
ref_counts: Arc<Mutex<RefCounts>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Entity> From<ModelHandle<T>> for AnyModelHandle {
|
|
|
|
fn from(handle: ModelHandle<T>) -> Self {
|
2021-05-13 01:29:17 +00:00
|
|
|
handle.ref_counts.lock().inc_model(handle.model_id);
|
2021-05-07 21:24:48 +00:00
|
|
|
Self {
|
|
|
|
model_id: handle.model_id,
|
|
|
|
ref_counts: handle.ref_counts.clone(),
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 21:24:48 +00:00
|
|
|
impl Drop for AnyModelHandle {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.ref_counts.lock().dec_model(self.model_id);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
pub struct WeakViewHandle<T> {
|
|
|
|
window_id: usize,
|
|
|
|
view_id: usize,
|
|
|
|
view_type: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: View> WeakViewHandle<T> {
|
|
|
|
fn new(window_id: usize, view_id: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
window_id,
|
|
|
|
view_id,
|
|
|
|
view_type: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:52:08 +00:00
|
|
|
pub fn upgrade(&self, cx: &AppContext) -> Option<ViewHandle<T>> {
|
2021-05-28 22:25:15 +00:00
|
|
|
if cx.ref_counts.lock().is_entity_alive(self.view_id) {
|
2021-03-10 04:00:51 +00:00
|
|
|
Some(ViewHandle::new(
|
|
|
|
self.window_id,
|
|
|
|
self.view_id,
|
2021-05-28 22:25:15 +00:00
|
|
|
&cx.ref_counts,
|
2021-03-10 04:00:51 +00:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for WeakViewHandle<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
window_id: self.window_id,
|
|
|
|
view_id: self.view_id,
|
|
|
|
view_type: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 03:24:23 +00:00
|
|
|
pub struct ValueHandle<T> {
|
|
|
|
value_type: PhantomData<T>,
|
|
|
|
tag_type_id: TypeId,
|
|
|
|
id: usize,
|
|
|
|
ref_counts: Weak<Mutex<RefCounts>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: 'static> ValueHandle<T> {
|
|
|
|
fn new(tag_type_id: TypeId, id: usize, ref_counts: &Arc<Mutex<RefCounts>>) -> Self {
|
|
|
|
ref_counts.lock().inc_value(tag_type_id, id);
|
|
|
|
Self {
|
|
|
|
value_type: PhantomData,
|
|
|
|
tag_type_id,
|
|
|
|
id,
|
|
|
|
ref_counts: Arc::downgrade(ref_counts),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
pub fn read<R>(&self, cx: &AppContext, f: impl FnOnce(&T) -> R) -> R {
|
|
|
|
f(cx.values
|
2021-04-28 00:35:24 +00:00
|
|
|
.read()
|
|
|
|
.get(&(self.tag_type_id, self.id))
|
|
|
|
.unwrap()
|
|
|
|
.downcast_ref()
|
|
|
|
.unwrap())
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:52:08 +00:00
|
|
|
pub fn update<R>(
|
|
|
|
&self,
|
|
|
|
cx: &mut EventContext,
|
|
|
|
f: impl FnOnce(&mut T, &mut EventContext) -> R,
|
|
|
|
) -> R {
|
|
|
|
let mut value = cx
|
|
|
|
.app
|
|
|
|
.cx
|
|
|
|
.values
|
2021-04-28 00:35:24 +00:00
|
|
|
.write()
|
2021-07-23 16:52:08 +00:00
|
|
|
.remove(&(self.tag_type_id, self.id))
|
|
|
|
.unwrap();
|
|
|
|
let result = f(value.downcast_mut().unwrap(), cx);
|
|
|
|
cx.app
|
|
|
|
.cx
|
|
|
|
.values
|
|
|
|
.write()
|
|
|
|
.insert((self.tag_type_id, self.id), value);
|
|
|
|
result
|
2021-04-27 03:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for ValueHandle<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(ref_counts) = self.ref_counts.upgrade() {
|
|
|
|
ref_counts.lock().dec_value(self.tag_type_id, self.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 22:54:24 +00:00
|
|
|
#[must_use]
|
|
|
|
pub enum Subscription {
|
|
|
|
Subscription {
|
|
|
|
id: usize,
|
|
|
|
entity_id: usize,
|
2021-08-25 15:35:27 +00:00
|
|
|
subscriptions: Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, SubscriptionCallback>>>>>,
|
2021-08-23 22:54:24 +00:00
|
|
|
},
|
|
|
|
Observation {
|
|
|
|
id: usize,
|
|
|
|
entity_id: usize,
|
2021-08-25 15:35:27 +00:00
|
|
|
observations: Option<Weak<Mutex<HashMap<usize, BTreeMap<usize, ObservationCallback>>>>>,
|
2021-08-23 22:54:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Subscription {
|
|
|
|
pub fn detach(&mut self) {
|
|
|
|
match self {
|
|
|
|
Subscription::Subscription { subscriptions, .. } => {
|
|
|
|
subscriptions.take();
|
|
|
|
}
|
|
|
|
Subscription::Observation { observations, .. } => {
|
|
|
|
observations.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Subscription {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self {
|
|
|
|
Subscription::Observation {
|
|
|
|
id,
|
|
|
|
entity_id,
|
|
|
|
observations,
|
|
|
|
} => {
|
|
|
|
if let Some(observations) = observations.as_ref().and_then(Weak::upgrade) {
|
|
|
|
if let Some(observations) = observations.lock().get_mut(entity_id) {
|
|
|
|
observations.remove(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Subscription::Subscription {
|
|
|
|
id,
|
|
|
|
entity_id,
|
|
|
|
subscriptions,
|
|
|
|
} => {
|
|
|
|
if let Some(subscriptions) = subscriptions.as_ref().and_then(Weak::upgrade) {
|
|
|
|
if let Some(subscriptions) = subscriptions.lock().get_mut(entity_id) {
|
|
|
|
subscriptions.remove(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
value_counts: HashMap<(TypeId, usize), usize>,
|
2021-03-10 04:00:51 +00:00
|
|
|
dropped_models: HashSet<usize>,
|
|
|
|
dropped_views: HashSet<(usize, usize)>,
|
2021-04-27 03:24:23 +00:00
|
|
|
dropped_values: HashSet<(TypeId, 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) {
|
|
|
|
Entry::Occupied(mut entry) => *entry.get_mut() += 1,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn inc_value(&mut self, tag_type_id: TypeId, id: usize) {
|
|
|
|
*self.value_counts.entry((tag_type_id, id)).or_insert(0) += 1;
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dec_value(&mut self, tag_type_id: TypeId, id: usize) {
|
|
|
|
let key = (tag_type_id, id);
|
|
|
|
let count = self.value_counts.get_mut(&key).unwrap();
|
|
|
|
*count -= 1;
|
|
|
|
if *count == 0 {
|
|
|
|
self.value_counts.remove(&key);
|
|
|
|
self.dropped_values.insert(key);
|
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)>,
|
|
|
|
HashSet<(TypeId, usize)>,
|
|
|
|
) {
|
2021-03-10 04:00:51 +00:00
|
|
|
let mut dropped_models = HashSet::new();
|
|
|
|
let mut dropped_views = HashSet::new();
|
2021-04-27 03:24:23 +00:00
|
|
|
let mut dropped_values = HashSet::new();
|
2021-03-10 04:00:51 +00:00
|
|
|
std::mem::swap(&mut self.dropped_models, &mut dropped_models);
|
|
|
|
std::mem::swap(&mut self.dropped_views, &mut dropped_views);
|
2021-04-27 03:24:23 +00:00
|
|
|
std::mem::swap(&mut self.dropped_values, &mut dropped_values);
|
|
|
|
(dropped_models, dropped_views, dropped_values)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::elements::*;
|
2021-04-20 15:21:29 +00:00
|
|
|
use smol::future::poll_once;
|
2021-07-12 15:44:10 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
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);
|
2021-08-23 22:54:24 +00:00
|
|
|
assert!(cx.subscriptions.lock().is_empty());
|
|
|
|
assert!(cx.observations.lock().is_empty());
|
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_subscribe_and_emit_from_model(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());
|
2021-05-12 16:21:32 +00:00
|
|
|
let handle_2b = handle_2.clone();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
handle_1.update(cx, |_, c| {
|
2021-08-23 21:58:37 +00:00
|
|
|
c.subscribe(&handle_2, move |model: &mut Model, _, event, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
model.events.push(*event);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-08-23 21:58:37 +00:00
|
|
|
c.subscribe(&handle_2b, |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));
|
|
|
|
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));
|
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
|
|
|
}
|
|
|
|
|
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-05-12 16:21:32 +00:00
|
|
|
let handle_2b = handle_2.clone();
|
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);
|
|
|
|
c.observe(&handle_2b, |model, observed, c| {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, _) = cx.add_window(Default::default(), |cx| View::new(None, cx));
|
2021-05-28 22:25:15 +00:00
|
|
|
let handle_1 = cx.add_view(window_id, |cx| View::new(None, cx));
|
|
|
|
let handle_2 = cx.add_view(window_id, |cx| View::new(Some(handle_1.clone()), cx));
|
|
|
|
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);
|
2021-08-23 22:54:24 +00:00
|
|
|
assert!(cx.subscriptions.lock().is_empty());
|
|
|
|
assert!(cx.observations.lock().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 {
|
2021-08-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &RenderContext<Self>) -> ElementBox {
|
2021-07-12 15:44:10 +00:00
|
|
|
let mouse_down_count = self.mouse_down_count.clone();
|
|
|
|
EventHandler::new(Empty::new().boxed())
|
|
|
|
.on_mouse_down(move |_| {
|
|
|
|
mouse_down_count.fetch_add(1, SeqCst);
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.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(
|
|
|
|
Event::LeftMouseDown {
|
|
|
|
position: Default::default(),
|
|
|
|
cmd: false,
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
released: Arc<Mutex<bool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct View {
|
|
|
|
released: Arc<Mutex<bool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for Model {
|
|
|
|
type Event = ();
|
|
|
|
|
|
|
|
fn release(&mut self, _: &mut MutableAppContext) {
|
|
|
|
*self.released.lock() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for View {
|
|
|
|
type Event = ();
|
|
|
|
|
|
|
|
fn release(&mut self, _: &mut MutableAppContext) {
|
|
|
|
*self.released.lock() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::View for View {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"View"
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &RenderContext<Self>) -> ElementBox {
|
2021-06-25 16:35:06 +00:00
|
|
|
Empty::new().boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let model_released = Arc::new(Mutex::new(false));
|
|
|
|
let view_released = Arc::new(Mutex::new(false));
|
|
|
|
|
|
|
|
let model = cx.add_model(|_| Model {
|
|
|
|
released: model_released.clone(),
|
|
|
|
});
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, _) = cx.add_window(Default::default(), |_| View {
|
2021-06-25 16:35:06 +00:00
|
|
|
released: view_released.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(!*model_released.lock());
|
|
|
|
assert!(!*view_released.lock());
|
|
|
|
|
|
|
|
cx.update(move || {
|
|
|
|
drop(model);
|
|
|
|
});
|
|
|
|
assert!(*model_released.lock());
|
|
|
|
|
|
|
|
drop(cx.remove_window(window_id));
|
|
|
|
assert!(*view_released.lock());
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
#[crate::test(self)]
|
2021-05-28 22:25:15 +00:00
|
|
|
fn test_subscribe_and_emit_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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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;
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, handle_1) = cx.add_window(Default::default(), |_| View::default());
|
2021-05-28 22:25:15 +00:00
|
|
|
let handle_2 = cx.add_view(window_id, |_| View::default());
|
2021-05-12 16:21:32 +00:00
|
|
|
let handle_2b = handle_2.clone();
|
2021-05-28 22:25:15 +00:00
|
|
|
let handle_3 = cx.add_model(|_| Model);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
handle_1.update(cx, |_, c| {
|
2021-08-23 22:07:50 +00:00
|
|
|
c.subscribe(&handle_2, move |me, _, event, c| {
|
2021-05-12 16:21:32 +00:00
|
|
|
me.events.push(*event);
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-08-23 22:07:50 +00:00
|
|
|
c.subscribe(&handle_2b, |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
|
|
|
|
2021-08-23 22:07:50 +00:00
|
|
|
c.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));
|
|
|
|
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));
|
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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
handle_3.update(cx, |_, c| c.emit(9));
|
2021-08-25 15:35:27 +00:00
|
|
|
assert_eq!(handle_1.read(cx).events, vec![7, 5, 10, 9]);
|
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_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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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 = ();
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, _) = cx.add_window(Default::default(), |_| View);
|
2021-05-28 22:25:15 +00:00
|
|
|
let observing_view = cx.add_view(window_id, |_| View);
|
|
|
|
let emitting_view = cx.add_view(window_id, |_| View);
|
|
|
|
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-05-28 22:25:15 +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
|
|
|
}
|
|
|
|
|
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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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
|
|
|
}
|
|
|
|
|
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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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 = ();
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, _) = cx.add_window(Default::default(), |_| View);
|
2021-05-28 22:25:15 +00:00
|
|
|
let observing_view = cx.add_view(window_id, |_| View);
|
|
|
|
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-05-28 22:25:15 +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
|
|
|
}
|
|
|
|
|
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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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"
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:43:07 +00:00
|
|
|
fn on_focus(&mut self, _: &mut ViewContext<Self>) {
|
|
|
|
self.events.lock().push(format!("{} focused", &self.name));
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 15:43:07 +00:00
|
|
|
fn on_blur(&mut self, _: &mut ViewContext<Self>) {
|
|
|
|
self.events.lock().push(format!("{} blurred", &self.name));
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let events: Arc<Mutex<Vec<String>>> = Default::default();
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, view_1) = cx.add_window(Default::default(), |_| View {
|
2021-05-12 16:21:32 +00:00
|
|
|
events: events.clone(),
|
|
|
|
name: "view 1".to_string(),
|
2021-03-10 04:00:51 +00:00
|
|
|
});
|
2021-05-28 22:25:15 +00:00
|
|
|
let view_2 = cx.add_view(window_id, |_| View {
|
2021-05-12 16:21:32 +00:00
|
|
|
events: events.clone(),
|
|
|
|
name: "view 2".to_string(),
|
2021-03-10 04:00:51 +00:00
|
|
|
});
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
view_1.update(cx, |_, cx| cx.focus(&view_2));
|
|
|
|
view_1.update(cx, |_, cx| cx.focus(&view_1));
|
|
|
|
view_1.update(cx, |_, cx| cx.focus(&view_2));
|
|
|
|
view_1.update(cx, |_, _| drop(view_2));
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
*events.lock(),
|
|
|
|
[
|
|
|
|
"view 1 focused".to_string(),
|
|
|
|
"view 1 blurred".to_string(),
|
|
|
|
"view 2 focused".to_string(),
|
|
|
|
"view 2 blurred".to_string(),
|
|
|
|
"view 1 focused".to_string(),
|
|
|
|
"view 1 blurred".to_string(),
|
|
|
|
"view 2 focused".to_string(),
|
|
|
|
"view 1 focused".to_string(),
|
|
|
|
],
|
|
|
|
);
|
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_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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
action!(Action, &'static str);
|
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
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
2021-05-12 16:21:32 +00:00
|
|
|
actions_clone.borrow_mut().push("global a".to_string());
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
2021-05-12 16:21:32 +00:00
|
|
|
actions_clone.borrow_mut().push("global b".to_string());
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.add_action(move |view: &mut ViewA, action: &Action, cx| {
|
|
|
|
assert_eq!(action.0, "bar");
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.propagate_action();
|
2021-05-12 16:21:32 +00:00
|
|
|
actions_clone.borrow_mut().push(format!("{} a", view.id));
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.add_action(move |view: &mut ViewA, _: &Action, cx| {
|
2021-05-12 16:21:32 +00:00
|
|
|
if view.id != 1 {
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.propagate_action();
|
2021-05-12 16:21:32 +00:00
|
|
|
}
|
|
|
|
actions_clone.borrow_mut().push(format!("{} b", view.id));
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 19:29:54 +00:00
|
|
|
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.propagate_action();
|
2021-05-12 16:21:32 +00:00
|
|
|
actions_clone.borrow_mut().push(format!("{} c", view.id));
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
let actions_clone = actions.clone();
|
2021-08-22 19:29:54 +00:00
|
|
|
cx.add_action(move |view: &mut ViewB, _: &Action, cx| {
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.propagate_action();
|
2021-05-12 16:21:32 +00:00
|
|
|
actions_clone.borrow_mut().push(format!("{} d", view.id));
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-08-05 17:48:35 +00:00
|
|
|
let (window_id, view_1) = cx.add_window(Default::default(), |_| ViewA { id: 1 });
|
2021-05-28 22:25:15 +00:00
|
|
|
let view_2 = cx.add_view(window_id, |_| ViewB { id: 2 });
|
|
|
|
let view_3 = cx.add_view(window_id, |_| ViewA { id: 3 });
|
|
|
|
let view_4 = cx.add_view(window_id, |_| ViewB { id: 4 });
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.dispatch_action(
|
2021-05-12 16:21:32 +00:00
|
|
|
window_id,
|
|
|
|
vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()],
|
2021-08-22 17:58:19 +00:00
|
|
|
&Action("bar"),
|
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(),
|
|
|
|
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "1 b"]
|
|
|
|
);
|
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
|
|
|
|
actions.borrow_mut().clear();
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.dispatch_action(
|
2021-05-12 16:21:32 +00:00
|
|
|
window_id,
|
|
|
|
vec![view_2.id(), view_3.id(), view_4.id()],
|
2021-08-22 17:58:19 +00:00
|
|
|
&Action("bar"),
|
2021-05-12 16:21:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
*actions.borrow(),
|
|
|
|
vec!["4 d", "4 c", "3 b", "3 a", "2 d", "2 c", "global b", "global a"]
|
|
|
|
);
|
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) {
|
2021-03-10 04:00:51 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
action!(Action, &'static str);
|
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-02 21:55:27 +00:00
|
|
|
fn render<'a>(&self, _: &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());
|
|
|
|
view_2.keymap_context.set.insert("b".into());
|
|
|
|
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);
|
2021-05-28 22:25:15 +00:00
|
|
|
let view_2 = cx.add_view(window_id, |_| view_2);
|
|
|
|
let view_3 = cx.add_view(window_id, |_| 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",
|
|
|
|
Action("a"),
|
|
|
|
Some("a && b && !c"),
|
|
|
|
)]);
|
2021-05-12 16:21:32 +00:00
|
|
|
|
|
|
|
let handled_action = Rc::new(Cell::new(false));
|
|
|
|
let handled_action_clone = handled_action.clone();
|
2021-08-22 17:58:19 +00:00
|
|
|
cx.add_action(move |view: &mut View, action: &Action, _| {
|
2021-05-12 16:21:32 +00:00
|
|
|
handled_action_clone.set(true);
|
|
|
|
assert_eq!(view.id, 2);
|
2021-08-22 17:58:19 +00:00
|
|
|
assert_eq!(action.0, "a");
|
2021-05-12 16:21:32 +00:00
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
cx.dispatch_keystroke(
|
2021-05-12 16:21:32 +00:00
|
|
|
window_id,
|
|
|
|
vec![view_1.id(), view_2.id(), view_3.id()],
|
|
|
|
&Keystroke::parse("a").unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2021-05-12 16:21:32 +00:00
|
|
|
assert!(handled_action.get());
|
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
|
|
|
async fn test_model_condition(mut cx: 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
|
|
|
|
2021-05-28 22:25:15 +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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
model.update(&mut 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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
model.update(&mut 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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
model.update(&mut 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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
model.update(&mut 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]
|
2021-05-28 22:25:15 +00:00
|
|
|
async fn test_model_condition_timeout(mut cx: 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);
|
|
|
|
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")]
|
2021-05-28 22:25:15 +00:00
|
|
|
async fn test_model_condition_panic_on_drop(mut cx: 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);
|
|
|
|
let condition = model.condition(&cx, |_, _| false);
|
|
|
|
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)]
|
2021-05-28 22:25:15 +00:00
|
|
|
async fn test_view_condition(mut cx: 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-02 21:55:27 +00:00
|
|
|
fn render(&self, _: &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
|
|
|
|
2021-05-28 22:25:15 +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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
view.update(&mut 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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
view.update(&mut 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
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
view.update(&mut cx, |view, cx| view.inc(cx));
|
2021-05-12 16:21:32 +00:00
|
|
|
assert_eq!(poll_once(&mut condition2).await, Some(()));
|
2021-05-28 22:25:15 +00:00
|
|
|
view.update(&mut 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]
|
2021-05-28 22:25:15 +00:00
|
|
|
async fn test_view_condition_timeout(mut cx: 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-02 21:55:27 +00:00
|
|
|
fn render(&self, _: &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);
|
|
|
|
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")]
|
2021-05-28 22:25:15 +00:00
|
|
|
async fn test_view_condition_panic_on_drop(mut cx: 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-02 21:55:27 +00:00
|
|
|
fn render(&self, _: &RenderContext<Self>) -> ElementBox {
|
2021-04-20 16:36:54 +00:00
|
|
|
Empty::new().boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let window_id = cx.add_window(|_| View).0;
|
|
|
|
let view = cx.add_view(window_id, |_| View);
|
2021-04-20 16:36:54 +00:00
|
|
|
|
2021-05-28 22:25:15 +00:00
|
|
|
let condition = view.condition(&cx, |_, _| false);
|
|
|
|
cx.update(|_| drop(view));
|
2021-05-12 16:21:32 +00:00
|
|
|
condition.await;
|
2021-04-20 16:36:54 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|