WIP
This commit is contained in:
parent
e8857d959b
commit
147db607c7
7 changed files with 79 additions and 176 deletions
|
@ -18,8 +18,8 @@ use crate::{
|
||||||
AppMetadata, AssetSource, ClipboardItem, Context, DispatchPhase, DisplayId, Entity, Executor,
|
AppMetadata, AssetSource, ClipboardItem, Context, DispatchPhase, DisplayId, Entity, Executor,
|
||||||
FocusEvent, FocusHandle, FocusId, KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly,
|
FocusEvent, FocusHandle, FocusId, KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly,
|
||||||
Pixels, Platform, PlatformDisplay, Point, Render, SharedString, SubscriberSet, Subscription,
|
Pixels, Platform, PlatformDisplay, Point, Render, SharedString, SubscriberSet, Subscription,
|
||||||
SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, ViewContext, Window,
|
SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, Window, WindowContext,
|
||||||
WindowContext, WindowHandle, WindowId,
|
WindowHandle, WindowId,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use collections::{HashMap, HashSet, VecDeque};
|
use collections::{HashMap, HashSet, VecDeque};
|
||||||
|
@ -267,27 +267,6 @@ impl AppContext {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_window_root<V, R>(
|
|
||||||
&mut self,
|
|
||||||
handle: &WindowHandle<V>,
|
|
||||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
|
|
||||||
) -> Result<R>
|
|
||||||
where
|
|
||||||
V: 'static + Send,
|
|
||||||
{
|
|
||||||
self.update_window(handle.any_handle, |cx| {
|
|
||||||
let root_view = cx
|
|
||||||
.window
|
|
||||||
.root_view
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.clone()
|
|
||||||
.downcast()
|
|
||||||
.unwrap();
|
|
||||||
root_view.update(cx, update)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn push_effect(&mut self, effect: Effect) {
|
pub(crate) fn push_effect(&mut self, effect: Effect) {
|
||||||
match &effect {
|
match &effect {
|
||||||
Effect::Notify { emitter } => {
|
Effect::Notify { emitter } => {
|
||||||
|
@ -354,7 +333,7 @@ impl AppContext {
|
||||||
.collect::<SmallVec<[_; 8]>>();
|
.collect::<SmallVec<[_; 8]>>();
|
||||||
|
|
||||||
for dirty_window_handle in dirty_window_ids {
|
for dirty_window_handle in dirty_window_ids {
|
||||||
self.update_window(dirty_window_handle, |cx| cx.draw())
|
self.update_window(dirty_window_handle, |_, cx| cx.draw())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -384,7 +363,7 @@ impl AppContext {
|
||||||
/// a window blur handler to restore focus to some logical element.
|
/// a window blur handler to restore focus to some logical element.
|
||||||
fn release_dropped_focus_handles(&mut self) {
|
fn release_dropped_focus_handles(&mut self) {
|
||||||
for window_handle in self.windows() {
|
for window_handle in self.windows() {
|
||||||
self.update_window(window_handle, |cx| {
|
self.update_window(window_handle, |_, cx| {
|
||||||
let mut blur_window = false;
|
let mut blur_window = false;
|
||||||
let focus = cx.window.focus;
|
let focus = cx.window.focus;
|
||||||
cx.window.focus_handles.write().retain(|handle_id, count| {
|
cx.window.focus_handles.write().retain(|handle_id, count| {
|
||||||
|
@ -424,7 +403,7 @@ impl AppContext {
|
||||||
window_handle: AnyWindowHandle,
|
window_handle: AnyWindowHandle,
|
||||||
focused: Option<FocusId>,
|
focused: Option<FocusId>,
|
||||||
) {
|
) {
|
||||||
self.update_window(window_handle, |cx| {
|
self.update_window(window_handle, |_, cx| {
|
||||||
if cx.window.focus == focused {
|
if cx.window.focus == focused {
|
||||||
let mut listeners = mem::take(&mut cx.window.focus_listeners);
|
let mut listeners = mem::take(&mut cx.window.focus_listeners);
|
||||||
let focused =
|
let focused =
|
||||||
|
@ -764,7 +743,7 @@ impl Context for AppContext {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
|
fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
self.update(|cx| {
|
self.update(|cx| {
|
||||||
let mut window = cx
|
let mut window = cx
|
||||||
|
@ -774,8 +753,8 @@ impl Context for AppContext {
|
||||||
.take()
|
.take()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let result = update(&mut WindowContext::new(cx, &mut window));
|
let root_view = window.root_view.clone().unwrap();
|
||||||
|
let result = update(root_view, &mut WindowContext::new(cx, &mut window));
|
||||||
cx.windows
|
cx.windows
|
||||||
.get_mut(handle.id)
|
.get_mut(handle.id)
|
||||||
.ok_or_else(|| anyhow!("window not found"))?
|
.ok_or_else(|| anyhow!("window not found"))?
|
||||||
|
@ -854,39 +833,6 @@ impl MainThread<AppContext> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_window<R>(
|
|
||||||
&mut self,
|
|
||||||
handle: AnyWindowHandle,
|
|
||||||
update: impl FnOnce(&mut MainThread<WindowContext>) -> R,
|
|
||||||
) -> Result<R> {
|
|
||||||
self.0.update_window(handle, |cx| {
|
|
||||||
update(unsafe {
|
|
||||||
std::mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_window_root<V, R>(
|
|
||||||
&mut self,
|
|
||||||
handle: &WindowHandle<V>,
|
|
||||||
update: impl FnOnce(&mut V, &mut MainThread<ViewContext<'_, V>>) -> R,
|
|
||||||
) -> Result<R>
|
|
||||||
where
|
|
||||||
V: 'static + Send,
|
|
||||||
{
|
|
||||||
self.update_window(handle.any_handle, |cx| {
|
|
||||||
let root_view = cx
|
|
||||||
.window
|
|
||||||
.root_view
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.clone()
|
|
||||||
.downcast()
|
|
||||||
.unwrap();
|
|
||||||
root_view.update(cx, update)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Opens a new window with the given option and the root view returned by the given function.
|
/// Opens a new window with the given option and the root view returned by the given function.
|
||||||
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
|
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
|
||||||
/// functionality.
|
/// functionality.
|
||||||
|
|
|
@ -42,7 +42,7 @@ impl Context for AsyncAppContext {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
let app = self.app.upgrade().context("app was released")?;
|
let app = self.app.upgrade().context("app was released")?;
|
||||||
let mut lock = app.lock(); // Need this to compile
|
let mut lock = app.lock(); // Need this to compile
|
||||||
|
@ -68,29 +68,6 @@ impl AsyncAppContext {
|
||||||
Ok(f(&mut *lock))
|
Ok(f(&mut *lock))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_window<R>(
|
|
||||||
&self,
|
|
||||||
window: AnyWindowHandle,
|
|
||||||
update: impl FnOnce(&mut WindowContext) -> R,
|
|
||||||
) -> Result<R> {
|
|
||||||
let app = self.app.upgrade().context("app was released")?;
|
|
||||||
let mut app_context = app.lock();
|
|
||||||
app_context.update_window(window, update)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_window_root<V, R>(
|
|
||||||
&mut self,
|
|
||||||
handle: &WindowHandle<V>,
|
|
||||||
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
|
|
||||||
) -> Result<R>
|
|
||||||
where
|
|
||||||
V: 'static + Send,
|
|
||||||
{
|
|
||||||
let app = self.app.upgrade().context("app was released")?;
|
|
||||||
let mut app_context = app.lock();
|
|
||||||
app_context.update_window_root(handle, update)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
|
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
|
||||||
where
|
where
|
||||||
Fut: Future<Output = R> + Send + 'static,
|
Fut: Future<Output = R> + Send + 'static,
|
||||||
|
@ -191,22 +168,25 @@ impl AsyncWindowContext {
|
||||||
Self { app, window }
|
Self { app, window }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update<R>(&self, update: impl FnOnce(&mut WindowContext) -> R) -> Result<R> {
|
pub fn update<R>(
|
||||||
|
&mut self,
|
||||||
|
update: impl FnOnce(AnyView, &mut WindowContext) -> R,
|
||||||
|
) -> Result<R> {
|
||||||
self.app.update_window(self.window, update)
|
self.app.update_window(self.window, update)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
|
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.on_next_frame(f))
|
.update_window(self.window, |_root, cx| cx.on_next_frame(f))
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_global<G: 'static, R>(
|
pub fn read_global<G: 'static, R>(
|
||||||
&self,
|
&mut self,
|
||||||
read: impl FnOnce(&G, &WindowContext) -> R,
|
read: impl FnOnce(&G, &WindowContext) -> R,
|
||||||
) -> Result<R> {
|
) -> Result<R> {
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| read(cx.global(), cx))
|
.update_window(self.window, |_, cx| read(cx.global(), cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_global<G, R>(
|
pub fn update_global<G, R>(
|
||||||
|
@ -217,7 +197,7 @@ impl AsyncWindowContext {
|
||||||
G: 'static,
|
G: 'static,
|
||||||
{
|
{
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.update_global(update))
|
.update_window(self.window, |_, cx| cx.update_global(update))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn<Fut, R>(
|
pub fn spawn<Fut, R>(
|
||||||
|
@ -245,13 +225,13 @@ impl AsyncWindowContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_on_main<R>(
|
pub fn run_on_main<R>(
|
||||||
&self,
|
&mut self,
|
||||||
f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
|
f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
|
||||||
) -> Task<Result<R>>
|
) -> Task<Result<R>>
|
||||||
where
|
where
|
||||||
R: Send + 'static,
|
R: Send + 'static,
|
||||||
{
|
{
|
||||||
self.update(|cx| cx.run_on_main(f))
|
self.update(|_, cx| cx.run_on_main(f))
|
||||||
.unwrap_or_else(|error| Task::ready(Err(error)))
|
.unwrap_or_else(|error| Task::ready(Err(error)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,7 +250,7 @@ impl Context for AsyncWindowContext {
|
||||||
T: 'static + Send,
|
T: 'static + Send,
|
||||||
{
|
{
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.build_model(build_model))
|
.update_window(self.window, |_, cx| cx.build_model(build_model))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_model<T: 'static, R>(
|
fn update_model<T: 'static, R>(
|
||||||
|
@ -279,12 +259,12 @@ impl Context for AsyncWindowContext {
|
||||||
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
|
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
|
||||||
) -> Result<R> {
|
) -> Result<R> {
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.update_model(handle, update))
|
.update_window(self.window, |_, cx| cx.update_model(handle, update))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
self.app.update_window(window, update)
|
self.app.update_window(window, update)
|
||||||
}
|
}
|
||||||
|
@ -293,10 +273,6 @@ impl Context for AsyncWindowContext {
|
||||||
impl VisualContext for AsyncWindowContext {
|
impl VisualContext for AsyncWindowContext {
|
||||||
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
|
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
|
||||||
|
|
||||||
fn root_view(&self) -> Result<AnyView> {
|
|
||||||
self.app.update_window(self.window, |cx| cx.root_view())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_view<V>(
|
fn build_view<V>(
|
||||||
&mut self,
|
&mut self,
|
||||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
||||||
|
@ -305,7 +281,7 @@ impl VisualContext for AsyncWindowContext {
|
||||||
V: 'static + Send,
|
V: 'static + Send,
|
||||||
{
|
{
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.build_view(build_view_state))
|
.update_window(self.window, |_, cx| cx.build_view(build_view_state))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_view<V: 'static, R>(
|
fn update_view<V: 'static, R>(
|
||||||
|
@ -314,7 +290,7 @@ impl VisualContext for AsyncWindowContext {
|
||||||
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
|
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
|
||||||
) -> Self::Result<R> {
|
) -> Self::Result<R> {
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.update_view(view, update))
|
.update_window(self.window, |_, cx| cx.update_view(view, update))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_root_view<V>(
|
fn replace_root_view<V>(
|
||||||
|
@ -325,7 +301,7 @@ impl VisualContext for AsyncWindowContext {
|
||||||
V: 'static + Send + Render,
|
V: 'static + Send + Render,
|
||||||
{
|
{
|
||||||
self.app
|
self.app
|
||||||
.update_window(self.window, |cx| cx.replace_root_view(build_view))
|
.update_window(self.window, |_, cx| cx.replace_root_view(build_view))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter,
|
AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId,
|
||||||
MainThread, Model, Subscription, Task, WeakModel, WindowContext,
|
EventEmitter, MainThread, Model, Subscription, Task, WeakModel, WindowContext,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
|
@ -253,7 +253,7 @@ impl<'a, T> Context for ModelContext<'a, T> {
|
||||||
|
|
||||||
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
|
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> R,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> R,
|
||||||
{
|
{
|
||||||
self.app.update_window(window, update)
|
self.app.update_window(window, update)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, MainThread, Model,
|
AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, MainThread, Model,
|
||||||
ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
|
ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
|
||||||
};
|
};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
@ -38,7 +38,7 @@ impl Context for TestAppContext {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
let mut lock = self.app.lock();
|
let mut lock = self.app.lock();
|
||||||
lock.update_window(window, f)
|
lock.update_window(window, f)
|
||||||
|
@ -76,15 +76,6 @@ impl TestAppContext {
|
||||||
f(&mut *lock)
|
f(&mut *lock)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_window<R>(
|
|
||||||
&self,
|
|
||||||
handle: AnyWindowHandle,
|
|
||||||
update: impl FnOnce(&mut WindowContext) -> R,
|
|
||||||
) -> R {
|
|
||||||
let mut app = self.app.lock();
|
|
||||||
app.update_window(handle, update).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
|
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
|
||||||
where
|
where
|
||||||
Fut: Future<Output = R> + Send + 'static,
|
Fut: Future<Output = R> + Send + 'static,
|
||||||
|
|
|
@ -98,7 +98,7 @@ pub trait Context {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T;
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait VisualContext: Context {
|
pub trait VisualContext: Context {
|
||||||
|
@ -199,15 +199,15 @@ impl<C: Context> Context for MainThread<C> {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
self.0.update_window(window, |cx| {
|
self.0.update_window(window, |root, cx| {
|
||||||
let cx = unsafe {
|
let cx = unsafe {
|
||||||
mem::transmute::<&mut C::WindowContext<'_>, &mut MainThread<C::WindowContext<'_>>>(
|
mem::transmute::<&mut C::WindowContext<'_>, &mut MainThread<C::WindowContext<'_>>>(
|
||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
update(cx)
|
update(root, cx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -215,10 +215,6 @@ impl<C: Context> Context for MainThread<C> {
|
||||||
impl<C: VisualContext> VisualContext for MainThread<C> {
|
impl<C: VisualContext> VisualContext for MainThread<C> {
|
||||||
type ViewContext<'a, V: 'static> = MainThread<C::ViewContext<'a, V>>;
|
type ViewContext<'a, V: 'static> = MainThread<C::ViewContext<'a, V>>;
|
||||||
|
|
||||||
fn root_view(&self) -> AnyView {
|
|
||||||
self.0.root_view()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_view<V>(
|
fn build_view<V>(
|
||||||
&mut self,
|
&mut self,
|
||||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
|
||||||
Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
|
Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
|
||||||
Entity, EntityId, EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId, GlobalElementId,
|
Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId, GlyphId,
|
||||||
GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke,
|
Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
|
||||||
LayoutId, MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite,
|
MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
|
||||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
|
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow,
|
||||||
PlatformWindow, Point, PolychromeSprite, Quad, Render, RenderGlyphParams, RenderImageParams,
|
Point, PolychromeSprite, Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams,
|
||||||
RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
|
ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine,
|
||||||
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView,
|
Task, Underline, UnderlineStyle, View, VisualContext, WeakView, WindowOptions,
|
||||||
WindowOptions, SUBPIXEL_VARIANTS,
|
SUBPIXEL_VARIANTS,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
|
@ -204,9 +204,9 @@ impl Window {
|
||||||
let content_size = platform_window.content_size();
|
let content_size = platform_window.content_size();
|
||||||
let scale_factor = platform_window.scale_factor();
|
let scale_factor = platform_window.scale_factor();
|
||||||
platform_window.on_resize(Box::new({
|
platform_window.on_resize(Box::new({
|
||||||
let cx = cx.to_async();
|
let mut cx = cx.to_async();
|
||||||
move |content_size, scale_factor| {
|
move |content_size, scale_factor| {
|
||||||
cx.update_window(handle, |cx| {
|
cx.update_window(handle, |_, cx| {
|
||||||
cx.window.scale_factor = scale_factor;
|
cx.window.scale_factor = scale_factor;
|
||||||
cx.window.scene_builder = SceneBuilder::new();
|
cx.window.scene_builder = SceneBuilder::new();
|
||||||
cx.window.content_size = content_size;
|
cx.window.content_size = content_size;
|
||||||
|
@ -223,9 +223,9 @@ impl Window {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
platform_window.on_input({
|
platform_window.on_input({
|
||||||
let cx = cx.to_async();
|
let mut cx = cx.to_async();
|
||||||
Box::new(move |event| {
|
Box::new(move |event| {
|
||||||
cx.update_window(handle, |cx| cx.dispatch_event(event))
|
cx.update_window(handle, |_, cx| cx.dispatch_event(event))
|
||||||
.log_err()
|
.log_err()
|
||||||
.unwrap_or(true)
|
.unwrap_or(true)
|
||||||
})
|
})
|
||||||
|
@ -372,7 +372,7 @@ impl<'a> WindowContext<'a> {
|
||||||
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static + Send) {
|
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static + Send) {
|
||||||
let window = self.window.handle;
|
let window = self.window.handle;
|
||||||
self.app.defer(move |cx| {
|
self.app.defer(move |cx| {
|
||||||
cx.update_window(window, f).ok();
|
cx.update_window(window, |_, cx| f(cx)).ok();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,7 +391,7 @@ impl<'a> WindowContext<'a> {
|
||||||
self.app.event_listeners.insert(
|
self.app.event_listeners.insert(
|
||||||
entity_id,
|
entity_id,
|
||||||
Box::new(move |event, cx| {
|
Box::new(move |event, cx| {
|
||||||
cx.update_window(window_handle, |cx| {
|
cx.update_window(window_handle, |_, cx| {
|
||||||
if let Some(handle) = E::upgrade_from(&entity) {
|
if let Some(handle) = E::upgrade_from(&entity) {
|
||||||
let event = event.downcast_ref().expect("invalid event type");
|
let event = event.downcast_ref().expect("invalid event type");
|
||||||
on_event(handle, event, cx);
|
on_event(handle, event, cx);
|
||||||
|
@ -421,7 +421,8 @@ impl<'a> WindowContext<'a> {
|
||||||
})))
|
})))
|
||||||
} else {
|
} else {
|
||||||
let handle = self.window.handle;
|
let handle = self.window.handle;
|
||||||
self.app.run_on_main(move |cx| cx.update_window(handle, f))
|
self.app
|
||||||
|
.run_on_main(move |cx| cx.update_window(handle, |_, cx| f(cx)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,12 +444,12 @@ impl<'a> WindowContext<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let async_cx = cx.to_async();
|
let mut async_cx = cx.to_async();
|
||||||
cx.next_frame_callbacks.insert(display_id, vec![f]);
|
cx.next_frame_callbacks.insert(display_id, vec![f]);
|
||||||
cx.platform().set_display_link_output_callback(
|
cx.platform().set_display_link_output_callback(
|
||||||
display_id,
|
display_id,
|
||||||
Box::new(move |_current_time, _output_time| {
|
Box::new(move |_current_time, _output_time| {
|
||||||
let _ = async_cx.update(|cx| {
|
let _ = async_cx.update(|_, cx| {
|
||||||
let callbacks = cx
|
let callbacks = cx
|
||||||
.next_frame_callbacks
|
.next_frame_callbacks
|
||||||
.get_mut(&display_id)
|
.get_mut(&display_id)
|
||||||
|
@ -1181,7 +1182,7 @@ impl<'a> WindowContext<'a> {
|
||||||
let window_handle = self.window.handle;
|
let window_handle = self.window.handle;
|
||||||
self.global_observers.insert(
|
self.global_observers.insert(
|
||||||
TypeId::of::<G>(),
|
TypeId::of::<G>(),
|
||||||
Box::new(move |cx| cx.update_window(window_handle, |cx| f(cx)).is_ok()),
|
Box::new(move |cx| cx.update_window(window_handle, |_, cx| f(cx)).is_ok()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1297,10 +1298,11 @@ impl Context for WindowContext<'_> {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
if window == self.window.handle {
|
if window == self.window.handle {
|
||||||
Ok(update(self))
|
let root_view = self.window.root_view.clone().unwrap();
|
||||||
|
Ok(update(root_view, self))
|
||||||
} else {
|
} else {
|
||||||
self.app.update_window(window, update)
|
self.app.update_window(window, update)
|
||||||
}
|
}
|
||||||
|
@ -1310,13 +1312,6 @@ impl Context for WindowContext<'_> {
|
||||||
impl VisualContext for WindowContext<'_> {
|
impl VisualContext for WindowContext<'_> {
|
||||||
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
|
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
|
||||||
|
|
||||||
fn root_view(&self) -> Self::Result<AnyView> {
|
|
||||||
self.window
|
|
||||||
.root_view
|
|
||||||
.clone()
|
|
||||||
.expect("we only take the root_view value when we draw")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_view<V>(
|
fn build_view<V>(
|
||||||
&mut self,
|
&mut self,
|
||||||
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
|
||||||
|
@ -1654,7 +1649,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
self.app.observers.insert(
|
self.app.observers.insert(
|
||||||
entity_id,
|
entity_id,
|
||||||
Box::new(move |cx| {
|
Box::new(move |cx| {
|
||||||
cx.update_window(window_handle, |cx| {
|
cx.update_window(window_handle, |_, cx| {
|
||||||
if let Some(handle) = E::upgrade_from(&entity) {
|
if let Some(handle) = E::upgrade_from(&entity) {
|
||||||
view.update(cx, |this, cx| on_notify(this, handle, cx))
|
view.update(cx, |this, cx| on_notify(this, handle, cx))
|
||||||
.is_ok()
|
.is_ok()
|
||||||
|
@ -1683,7 +1678,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
self.app.event_listeners.insert(
|
self.app.event_listeners.insert(
|
||||||
entity_id,
|
entity_id,
|
||||||
Box::new(move |event, cx| {
|
Box::new(move |event, cx| {
|
||||||
cx.update_window(window_handle, |cx| {
|
cx.update_window(window_handle, |_, cx| {
|
||||||
if let Some(handle) = E::upgrade_from(&handle) {
|
if let Some(handle) = E::upgrade_from(&handle) {
|
||||||
let event = event.downcast_ref().expect("invalid event type");
|
let event = event.downcast_ref().expect("invalid event type");
|
||||||
view.update(cx, |this, cx| on_event(this, handle, event, cx))
|
view.update(cx, |this, cx| on_event(this, handle, event, cx))
|
||||||
|
@ -1707,7 +1702,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
Box::new(move |this, cx| {
|
Box::new(move |this, cx| {
|
||||||
let this = this.downcast_mut().expect("invalid entity type");
|
let this = this.downcast_mut().expect("invalid entity type");
|
||||||
// todo!("are we okay with silently swallowing the error?")
|
// todo!("are we okay with silently swallowing the error?")
|
||||||
let _ = cx.update_window(window_handle, |cx| on_release(this, cx));
|
let _ = cx.update_window(window_handle, |_, cx| on_release(this, cx));
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1729,7 +1724,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
entity_id,
|
entity_id,
|
||||||
Box::new(move |entity, cx| {
|
Box::new(move |entity, cx| {
|
||||||
let entity = entity.downcast_mut().expect("invalid entity type");
|
let entity = entity.downcast_mut().expect("invalid entity type");
|
||||||
let _ = cx.update_window(window_handle, |cx| {
|
let _ = cx.update_window(window_handle, |_, cx| {
|
||||||
view.update(cx, |this, cx| on_release(this, entity, cx))
|
view.update(cx, |this, cx| on_release(this, entity, cx))
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
@ -1892,7 +1887,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
self.global_observers.insert(
|
self.global_observers.insert(
|
||||||
TypeId::of::<G>(),
|
TypeId::of::<G>(),
|
||||||
Box::new(move |cx| {
|
Box::new(move |cx| {
|
||||||
cx.update_window(window_handle, |cx| {
|
cx.update_window(window_handle, |_, cx| {
|
||||||
view.update(cx, |view, cx| f(view, cx)).is_ok()
|
view.update(cx, |view, cx| f(view, cx)).is_ok()
|
||||||
})
|
})
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
|
@ -1962,7 +1957,7 @@ impl<V> Context for ViewContext<'_, V> {
|
||||||
|
|
||||||
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self::WindowContext<'_>) -> T,
|
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
|
||||||
{
|
{
|
||||||
self.window_cx.update_window(window, update)
|
self.window_cx.update_window(window, update)
|
||||||
}
|
}
|
||||||
|
@ -1971,10 +1966,6 @@ impl<V> Context for ViewContext<'_, V> {
|
||||||
impl<V: 'static> VisualContext for ViewContext<'_, V> {
|
impl<V: 'static> VisualContext for ViewContext<'_, V> {
|
||||||
type ViewContext<'a, W: 'static> = ViewContext<'a, W>;
|
type ViewContext<'a, W: 'static> = ViewContext<'a, W>;
|
||||||
|
|
||||||
fn root_view(&self) -> Self::Result<AnyView> {
|
|
||||||
self.window_cx.root_view()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_view<W: 'static + Send>(
|
fn build_view<W: 'static + Send>(
|
||||||
&mut self,
|
&mut self,
|
||||||
build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
|
build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
|
||||||
|
@ -2043,21 +2034,24 @@ impl<V: 'static + Render> WindowHandle<V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_root<C, R>(
|
pub fn update_root<C, R, W>(
|
||||||
&self,
|
&self,
|
||||||
cx: &mut C,
|
cx: &mut C,
|
||||||
update: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
|
update: impl FnOnce(
|
||||||
|
&mut V,
|
||||||
|
&mut <C::WindowContext<'_> as VisualContext>::ViewContext<'_, V>,
|
||||||
|
) -> R,
|
||||||
) -> Result<R>
|
) -> Result<R>
|
||||||
where
|
where
|
||||||
C: Context,
|
C: for<'a> Context<WindowContext<'a> = W>,
|
||||||
|
W: VisualContext<Result<R> = R>,
|
||||||
{
|
{
|
||||||
cx.update_window(self.any_handle, |cx| {
|
cx.update_window(self.any_handle, |root_view, cx| {
|
||||||
let x = Ok(cx.root_view()).flatten();
|
let view = root_view
|
||||||
|
.downcast::<V>()
|
||||||
// let root_view = x.unwrap().downcast::<V>().unwrap();
|
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
|
||||||
// root_view.update(cx, update)
|
Ok(view.update(cx, update))
|
||||||
todo!()
|
})?
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ use crate::persistence::model::{
|
||||||
DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup,
|
DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup,
|
||||||
SerializedWorkspace,
|
SerializedWorkspace,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context as _, Result};
|
||||||
use call2::ActiveCall;
|
use call2::ActiveCall;
|
||||||
use client2::{
|
use client2::{
|
||||||
proto::{self, PeerId},
|
proto::{self, PeerId},
|
||||||
|
@ -29,7 +29,7 @@ use futures::{
|
||||||
};
|
};
|
||||||
use gpui2::{
|
use gpui2::{
|
||||||
div, point, size, AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, Bounds,
|
div, point, size, AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, Bounds,
|
||||||
Div, EventEmitter, GlobalPixels, MainThread, Model, ModelContext, Point, Render, Size,
|
Context, Div, EventEmitter, GlobalPixels, MainThread, Model, ModelContext, Point, Render, Size,
|
||||||
Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext,
|
Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext,
|
||||||
WindowHandle, WindowOptions,
|
WindowHandle, WindowOptions,
|
||||||
};
|
};
|
||||||
|
@ -837,7 +837,7 @@ impl Workspace {
|
||||||
};
|
};
|
||||||
|
|
||||||
let window = if let Some(window) = requesting_window {
|
let window = if let Some(window) = requesting_window {
|
||||||
cx.update_window(window.into(), |cx| {
|
cx.update_window(window.into(), |old_workspace, cx| {
|
||||||
cx.replace_root_view(|cx| {
|
cx.replace_root_view(|cx| {
|
||||||
Workspace::new(workspace_id, project_handle.clone(), app_state.clone(), cx)
|
Workspace::new(workspace_id, project_handle.clone(), app_state.clone(), cx)
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue