Checkpoint
This commit is contained in:
parent
9798d65cf9
commit
8f1000ea10
4 changed files with 19 additions and 59 deletions
|
@ -267,20 +267,6 @@ impl AppContext {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn read_window<R>(
|
|
||||||
&mut self,
|
|
||||||
handle: AnyWindowHandle,
|
|
||||||
read: impl FnOnce(&WindowContext) -> R,
|
|
||||||
) -> Result<R> {
|
|
||||||
let window = self
|
|
||||||
.windows
|
|
||||||
.get(handle.id)
|
|
||||||
.ok_or_else(|| anyhow!("window not found"))?
|
|
||||||
.as_ref()
|
|
||||||
.unwrap();
|
|
||||||
Ok(read(&WindowContext::immutable(self, &window)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn update_window<R>(
|
pub(crate) fn update_window<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
|
|
|
@ -58,16 +58,6 @@ impl AsyncAppContext {
|
||||||
Ok(f(&mut *lock))
|
Ok(f(&mut *lock))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_window<R>(
|
|
||||||
&self,
|
|
||||||
handle: AnyWindowHandle,
|
|
||||||
update: impl FnOnce(&WindowContext) -> R,
|
|
||||||
) -> Result<R> {
|
|
||||||
let app = self.app.upgrade().context("app was released")?;
|
|
||||||
let mut app_context = app.lock();
|
|
||||||
app_context.read_window(handle, update)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_window<R>(
|
pub fn update_window<R>(
|
||||||
&self,
|
&self,
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
|
@ -183,7 +173,7 @@ impl AsyncWindowContext {
|
||||||
read: impl FnOnce(&G, &WindowContext) -> R,
|
read: impl FnOnce(&G, &WindowContext) -> R,
|
||||||
) -> Result<R> {
|
) -> Result<R> {
|
||||||
self.app
|
self.app
|
||||||
.read_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>(
|
||||||
|
|
|
@ -67,15 +67,6 @@ impl TestAppContext {
|
||||||
f(&mut *lock)
|
f(&mut *lock)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_window<R>(
|
|
||||||
&self,
|
|
||||||
handle: AnyWindowHandle,
|
|
||||||
read: impl FnOnce(&WindowContext) -> R,
|
|
||||||
) -> R {
|
|
||||||
let mut app_context = self.app.lock();
|
|
||||||
app_context.read_window(handle, read).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_window<R>(
|
pub fn update_window<R>(
|
||||||
&self,
|
&self,
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
|
|
|
@ -305,20 +305,13 @@ impl ContentMask<Pixels> {
|
||||||
/// Provides access to application state in the context of a single window. Derefs
|
/// Provides access to application state in the context of a single window. Derefs
|
||||||
/// to an `AppContext`, so you can also pass a `WindowContext` to any method that takes
|
/// to an `AppContext`, so you can also pass a `WindowContext` to any method that takes
|
||||||
/// an `AppContext` and call any `AppContext` methods.
|
/// an `AppContext` and call any `AppContext` methods.
|
||||||
pub struct WindowContext<'a, 'w> {
|
pub struct WindowContext<'a> {
|
||||||
pub(crate) app: Reference<'a, AppContext>,
|
pub(crate) app: Reference<'a, AppContext>,
|
||||||
pub(crate) window: Reference<'w, Window>,
|
pub(crate) window: Reference<'a, Window>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> WindowContext<'a, 'w> {
|
impl<'a> WindowContext<'a> {
|
||||||
pub(crate) fn immutable(app: &'a AppContext, window: &'w Window) -> Self {
|
pub(crate) fn mutable(app: &'a mut AppContext, window: &'a mut Window) -> Self {
|
||||||
Self {
|
|
||||||
app: Reference::Immutable(app),
|
|
||||||
window: Reference::Immutable(window),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
app: Reference::Mutable(app),
|
app: Reference::Mutable(app),
|
||||||
window: Reference::Mutable(window),
|
window: Reference::Mutable(window),
|
||||||
|
@ -388,7 +381,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
pub fn subscribe<Emitter, E>(
|
pub fn subscribe<Emitter, E>(
|
||||||
&mut self,
|
&mut self,
|
||||||
entity: &E,
|
entity: &E,
|
||||||
mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_, '_>) + Send + 'static,
|
mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_>) + Send + 'static,
|
||||||
) -> Subscription
|
) -> Subscription
|
||||||
where
|
where
|
||||||
Emitter: EventEmitter,
|
Emitter: EventEmitter,
|
||||||
|
@ -419,7 +412,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
/// of the window.
|
/// of the window.
|
||||||
pub fn run_on_main<R>(
|
pub fn run_on_main<R>(
|
||||||
&mut 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,
|
||||||
|
@ -1185,7 +1178,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
/// is updated.
|
/// is updated.
|
||||||
pub fn observe_global<G: 'static>(
|
pub fn observe_global<G: 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
f: impl Fn(&mut WindowContext<'_, '_>) + Send + 'static,
|
f: impl Fn(&mut WindowContext<'_>) + Send + 'static,
|
||||||
) -> Subscription {
|
) -> Subscription {
|
||||||
let window_handle = self.window.handle;
|
let window_handle = self.window.handle;
|
||||||
self.global_observers.insert(
|
self.global_observers.insert(
|
||||||
|
@ -1273,7 +1266,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context for WindowContext<'_, '_> {
|
impl Context for WindowContext<'_> {
|
||||||
type ModelContext<'a, T> = ModelContext<'a, T>;
|
type ModelContext<'a, T> = ModelContext<'a, T>;
|
||||||
type Result<T> = T;
|
type Result<T> = T;
|
||||||
|
|
||||||
|
@ -1304,7 +1297,7 @@ 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 build_view<V>(
|
fn build_view<V>(
|
||||||
|
@ -1338,7 +1331,7 @@ impl VisualContext for WindowContext<'_, '_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
|
impl<'a> std::ops::Deref for WindowContext<'a> {
|
||||||
type Target = AppContext;
|
type Target = AppContext;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
|
@ -1346,19 +1339,19 @@ impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
|
impl<'a> std::ops::DerefMut for WindowContext<'a> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.app
|
&mut self.app
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> Borrow<AppContext> for WindowContext<'a, 'w> {
|
impl<'a> Borrow<AppContext> for WindowContext<'a> {
|
||||||
fn borrow(&self) -> &AppContext {
|
fn borrow(&self) -> &AppContext {
|
||||||
&self.app
|
&self.app
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> BorrowMut<AppContext> for WindowContext<'a, 'w> {
|
impl<'a> BorrowMut<AppContext> for WindowContext<'a> {
|
||||||
fn borrow_mut(&mut self) -> &mut AppContext {
|
fn borrow_mut(&mut self) -> &mut AppContext {
|
||||||
&mut self.app
|
&mut self.app
|
||||||
}
|
}
|
||||||
|
@ -1526,13 +1519,13 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Borrow<Window> for WindowContext<'_, '_> {
|
impl Borrow<Window> for WindowContext<'_> {
|
||||||
fn borrow(&self) -> &Window {
|
fn borrow(&self) -> &Window {
|
||||||
&self.window
|
&self.window
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BorrowMut<Window> for WindowContext<'_, '_> {
|
impl BorrowMut<Window> for WindowContext<'_> {
|
||||||
fn borrow_mut(&mut self) -> &mut Window {
|
fn borrow_mut(&mut self) -> &mut Window {
|
||||||
&mut self.window
|
&mut self.window
|
||||||
}
|
}
|
||||||
|
@ -1541,7 +1534,7 @@ impl BorrowMut<Window> for WindowContext<'_, '_> {
|
||||||
impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
|
impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
|
||||||
|
|
||||||
pub struct ViewContext<'a, V> {
|
pub struct ViewContext<'a, V> {
|
||||||
window_cx: WindowContext<'a, 'a>,
|
window_cx: WindowContext<'a>,
|
||||||
view: &'a View<V>,
|
view: &'a View<V>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1740,7 +1733,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
|
||||||
move |event: &dyn Any,
|
move |event: &dyn Any,
|
||||||
context_stack: &[&DispatchContext],
|
context_stack: &[&DispatchContext],
|
||||||
phase: DispatchPhase,
|
phase: DispatchPhase,
|
||||||
cx: &mut WindowContext<'_, '_>| {
|
cx: &mut WindowContext<'_>| {
|
||||||
handle
|
handle
|
||||||
.update(cx, |view, cx| {
|
.update(cx, |view, cx| {
|
||||||
listener(view, event, context_stack, phase, cx)
|
listener(view, event, context_stack, phase, cx)
|
||||||
|
@ -1952,7 +1945,7 @@ impl<V: 'static> VisualContext for ViewContext<'_, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, V> std::ops::Deref for ViewContext<'a, V> {
|
impl<'a, V> std::ops::Deref for ViewContext<'a, V> {
|
||||||
type Target = WindowContext<'a, 'a>;
|
type Target = WindowContext<'a>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.window_cx
|
&self.window_cx
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue