Checkpoint
This commit is contained in:
parent
769a04517f
commit
7e49c7d782
3 changed files with 133 additions and 126 deletions
|
@ -1,6 +1,10 @@
|
||||||
|
mod model_context;
|
||||||
|
|
||||||
|
pub use model_context::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
current_platform, AnyWindowHandle, Context, LayoutId, MainThreadOnly, Platform, Reference,
|
current_platform, AnyWindowHandle, Context, LayoutId, MainThreadOnly, Platform, RootView,
|
||||||
RootView, TextSystem, Window, WindowContext, WindowHandle, WindowId,
|
TextSystem, Window, WindowContext, WindowHandle, WindowId,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use collections::{HashMap, VecDeque};
|
use collections::{HashMap, VecDeque};
|
||||||
|
@ -274,94 +278,6 @@ impl AsyncContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ModelContext<'a, T> {
|
|
||||||
app: Reference<'a, AppContext>,
|
|
||||||
entity_type: PhantomData<T>,
|
|
||||||
entity_id: EntityId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
|
|
||||||
pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self {
|
|
||||||
Self {
|
|
||||||
app: Reference::Mutable(app),
|
|
||||||
entity_type: PhantomData,
|
|
||||||
entity_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo!
|
|
||||||
// fn update<R>(&mut self, update: impl FnOnce(&mut T, &mut Self) -> R) -> R {
|
|
||||||
// let mut entity = self
|
|
||||||
// .app
|
|
||||||
// .entities
|
|
||||||
// .get_mut(self.entity_id)
|
|
||||||
// .unwrap()
|
|
||||||
// .take()
|
|
||||||
// .unwrap();
|
|
||||||
// let result = update(entity.downcast_mut::<T>().unwrap(), self);
|
|
||||||
// self.app
|
|
||||||
// .entities
|
|
||||||
// .get_mut(self.entity_id)
|
|
||||||
// .unwrap()
|
|
||||||
// .replace(entity);
|
|
||||||
// result
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn handle(&self) -> WeakHandle<T> {
|
|
||||||
WeakHandle {
|
|
||||||
id: self.entity_id,
|
|
||||||
entity_type: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn observe<E: Send + Sync + 'static>(
|
|
||||||
&mut self,
|
|
||||||
handle: &Handle<E>,
|
|
||||||
on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T>) + Send + Sync + 'static,
|
|
||||||
) {
|
|
||||||
let this = self.handle();
|
|
||||||
let handle = handle.downgrade();
|
|
||||||
self.app
|
|
||||||
.observers
|
|
||||||
.entry(handle.id)
|
|
||||||
.or_default()
|
|
||||||
.push(Arc::new(move |cx| {
|
|
||||||
if let Some((this, handle)) = this.upgrade(cx).zip(handle.upgrade(cx)) {
|
|
||||||
this.update(cx, |this, cx| on_notify(this, handle, cx));
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn notify(&mut self) {
|
|
||||||
self.app
|
|
||||||
.pending_effects
|
|
||||||
.push_back(Effect::Notify(self.entity_id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: 'static> Context for ModelContext<'a, T> {
|
|
||||||
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ModelContext<'b, U>;
|
|
||||||
type Result<U> = U;
|
|
||||||
|
|
||||||
fn entity<U: Send + Sync + 'static>(
|
|
||||||
&mut self,
|
|
||||||
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
|
|
||||||
) -> Handle<U> {
|
|
||||||
self.app.entity(build_entity)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_entity<U: Send + Sync + 'static, R>(
|
|
||||||
&mut self,
|
|
||||||
handle: &Handle<U>,
|
|
||||||
update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
|
|
||||||
) -> R {
|
|
||||||
self.app.update_entity(handle, update)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slotmap::new_key_type! { pub struct EntityId; }
|
slotmap::new_key_type! { pub struct EntityId; }
|
||||||
|
|
||||||
pub struct Handle<T> {
|
pub struct Handle<T> {
|
||||||
|
|
90
crates/gpui3/src/app/model_context.rs
Normal file
90
crates/gpui3/src/app/model_context.rs
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle};
|
||||||
|
use std::{marker::PhantomData, sync::Arc};
|
||||||
|
|
||||||
|
pub struct ModelContext<'a, T> {
|
||||||
|
app: Reference<'a, AppContext>,
|
||||||
|
entity_type: PhantomData<T>,
|
||||||
|
entity_id: EntityId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
|
||||||
|
pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self {
|
||||||
|
Self {
|
||||||
|
app: Reference::Mutable(app),
|
||||||
|
entity_type: PhantomData,
|
||||||
|
entity_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo!
|
||||||
|
// fn update<R>(&mut self, update: impl FnOnce(&mut T, &mut Self) -> R) -> R {
|
||||||
|
// let mut entity = self
|
||||||
|
// .app
|
||||||
|
// .entities
|
||||||
|
// .get_mut(self.entity_id)
|
||||||
|
// .unwrap()
|
||||||
|
// .take()
|
||||||
|
// .unwrap();
|
||||||
|
// let result = update(entity.downcast_mut::<T>().unwrap(), self);
|
||||||
|
// self.app
|
||||||
|
// .entities
|
||||||
|
// .get_mut(self.entity_id)
|
||||||
|
// .unwrap()
|
||||||
|
// .replace(entity);
|
||||||
|
// result
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn handle(&self) -> WeakHandle<T> {
|
||||||
|
WeakHandle {
|
||||||
|
id: self.entity_id,
|
||||||
|
entity_type: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn observe<E: Send + Sync + 'static>(
|
||||||
|
&mut self,
|
||||||
|
handle: &Handle<E>,
|
||||||
|
on_notify: impl Fn(&mut T, Handle<E>, &mut ModelContext<'_, T>) + Send + Sync + 'static,
|
||||||
|
) {
|
||||||
|
let this = self.handle();
|
||||||
|
let handle = handle.downgrade();
|
||||||
|
self.app
|
||||||
|
.observers
|
||||||
|
.entry(handle.id)
|
||||||
|
.or_default()
|
||||||
|
.push(Arc::new(move |cx| {
|
||||||
|
if let Some((this, handle)) = this.upgrade(cx).zip(handle.upgrade(cx)) {
|
||||||
|
this.update(cx, |this, cx| on_notify(this, handle, cx));
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn notify(&mut self) {
|
||||||
|
self.app
|
||||||
|
.pending_effects
|
||||||
|
.push_back(Effect::Notify(self.entity_id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'static> Context for ModelContext<'a, T> {
|
||||||
|
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ModelContext<'b, U>;
|
||||||
|
type Result<U> = U;
|
||||||
|
|
||||||
|
fn entity<U: Send + Sync + 'static>(
|
||||||
|
&mut self,
|
||||||
|
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, U>) -> U,
|
||||||
|
) -> Handle<U> {
|
||||||
|
self.app.entity(build_entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_entity<U: Send + Sync + 'static, R>(
|
||||||
|
&mut self,
|
||||||
|
handle: &Handle<U>,
|
||||||
|
update: impl FnOnce(&mut U, &mut Self::EntityContext<'_, '_, U>) -> R,
|
||||||
|
) -> R {
|
||||||
|
self.app.update_entity(handle, update)
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,53 +9,54 @@ impl TestPlatform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo!("implement out what our tests needed in GPUI 1")
|
||||||
impl Platform for TestPlatform {
|
impl Platform for TestPlatform {
|
||||||
fn dispatcher(&self) -> std::sync::Arc<dyn crate::PlatformDispatcher> {
|
fn dispatcher(&self) -> std::sync::Arc<dyn crate::PlatformDispatcher> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_system(&self) -> std::sync::Arc<dyn crate::PlatformTextSystem> {
|
fn text_system(&self) -> std::sync::Arc<dyn crate::PlatformTextSystem> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
|
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quit(&self) {
|
fn quit(&self) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn restart(&self) {
|
fn restart(&self) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate(&self, _ignoring_other_apps: bool) {
|
fn activate(&self, _ignoring_other_apps: bool) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hide(&self) {
|
fn hide(&self) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hide_other_apps(&self) {
|
fn hide_other_apps(&self) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unhide_other_apps(&self) {
|
fn unhide_other_apps(&self) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn screens(&self) -> Vec<std::rc::Rc<dyn crate::PlatformScreen>> {
|
fn screens(&self) -> Vec<std::rc::Rc<dyn crate::PlatformScreen>> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn screen_by_id(&self, _id: ScreenId) -> Option<std::rc::Rc<dyn crate::PlatformScreen>> {
|
fn screen_by_id(&self, _id: ScreenId) -> Option<std::rc::Rc<dyn crate::PlatformScreen>> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main_window(&self) -> Option<crate::AnyWindowHandle> {
|
fn main_window(&self) -> Option<crate::AnyWindowHandle> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_window(
|
fn open_window(
|
||||||
|
@ -63,93 +64,93 @@ impl Platform for TestPlatform {
|
||||||
_handle: crate::AnyWindowHandle,
|
_handle: crate::AnyWindowHandle,
|
||||||
_options: crate::WindowOptions,
|
_options: crate::WindowOptions,
|
||||||
) -> Box<dyn crate::PlatformWindow> {
|
) -> Box<dyn crate::PlatformWindow> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_url(&self, _url: &str) {
|
fn open_url(&self, _url: &str) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_open_urls(&self, _callback: Box<dyn FnMut(Vec<String>)>) {
|
fn on_open_urls(&self, _callback: Box<dyn FnMut(Vec<String>)>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_for_paths(
|
fn prompt_for_paths(
|
||||||
&self,
|
&self,
|
||||||
_options: crate::PathPromptOptions,
|
_options: crate::PathPromptOptions,
|
||||||
) -> futures::channel::oneshot::Receiver<Option<Vec<std::path::PathBuf>>> {
|
) -> futures::channel::oneshot::Receiver<Option<Vec<std::path::PathBuf>>> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_for_new_path(
|
fn prompt_for_new_path(
|
||||||
&self,
|
&self,
|
||||||
_directory: &std::path::Path,
|
_directory: &std::path::Path,
|
||||||
) -> futures::channel::oneshot::Receiver<Option<std::path::PathBuf>> {
|
) -> futures::channel::oneshot::Receiver<Option<std::path::PathBuf>> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reveal_path(&self, _path: &std::path::Path) {
|
fn reveal_path(&self, _path: &std::path::Path) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_become_active(&self, _callback: Box<dyn FnMut()>) {
|
fn on_become_active(&self, _callback: Box<dyn FnMut()>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_resign_active(&self, _callback: Box<dyn FnMut()>) {
|
fn on_resign_active(&self, _callback: Box<dyn FnMut()>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_quit(&self, _callback: Box<dyn FnMut()>) {
|
fn on_quit(&self, _callback: Box<dyn FnMut()>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_reopen(&self, _callback: Box<dyn FnMut()>) {
|
fn on_reopen(&self, _callback: Box<dyn FnMut()>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(&self, _callback: Box<dyn FnMut(crate::Event) -> bool>) {
|
fn on_event(&self, _callback: Box<dyn FnMut(crate::Event) -> bool>) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn os_name(&self) -> &'static str {
|
fn os_name(&self) -> &'static str {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn os_version(&self) -> anyhow::Result<crate::SemanticVersion> {
|
fn os_version(&self) -> anyhow::Result<crate::SemanticVersion> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app_version(&self) -> anyhow::Result<crate::SemanticVersion> {
|
fn app_version(&self) -> anyhow::Result<crate::SemanticVersion> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app_path(&self) -> anyhow::Result<std::path::PathBuf> {
|
fn app_path(&self) -> anyhow::Result<std::path::PathBuf> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn local_timezone(&self) -> time::UtcOffset {
|
fn local_timezone(&self) -> time::UtcOffset {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_for_auxiliary_executable(&self, _name: &str) -> anyhow::Result<std::path::PathBuf> {
|
fn path_for_auxiliary_executable(&self, _name: &str) -> anyhow::Result<std::path::PathBuf> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cursor_style(&self, _style: crate::CursorStyle) {
|
fn set_cursor_style(&self, _style: crate::CursorStyle) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_auto_hide_scrollbars(&self) -> bool {
|
fn should_auto_hide_scrollbars(&self) -> bool {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to_clipboard(&self, _item: crate::ClipboardItem) {
|
fn write_to_clipboard(&self, _item: crate::ClipboardItem) {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_from_clipboard(&self) -> Option<crate::ClipboardItem> {
|
fn read_from_clipboard(&self) -> Option<crate::ClipboardItem> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_credentials(
|
fn write_credentials(
|
||||||
|
@ -158,14 +159,14 @@ impl Platform for TestPlatform {
|
||||||
_username: &str,
|
_username: &str,
|
||||||
_password: &[u8],
|
_password: &[u8],
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_credentials(&self, _url: &str) -> anyhow::Result<Option<(String, Vec<u8>)>> {
|
fn read_credentials(&self, _url: &str) -> anyhow::Result<Option<(String, Vec<u8>)>> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_credentials(&self, _url: &str) -> anyhow::Result<()> {
|
fn delete_credentials(&self, _url: &str) -> anyhow::Result<()> {
|
||||||
todo!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue