+ManagedView
And some games with rust traits
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use gpui::{
|
||||
div, prelude::*, px, AnyView, Div, EventEmitter, FocusHandle, Render, Subscription, View,
|
||||
ViewContext, WindowContext,
|
||||
div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, View,
|
||||
ViewContext,
|
||||
};
|
||||
use ui::{h_stack, v_stack};
|
||||
|
||||
@@ -15,14 +15,6 @@ pub struct ModalLayer {
|
||||
active_modal: Option<ActiveModal>,
|
||||
}
|
||||
|
||||
pub trait Modal: Render + EventEmitter<ModalEvent> {
|
||||
fn focus(&self, cx: &mut WindowContext);
|
||||
}
|
||||
|
||||
pub enum ModalEvent {
|
||||
Dismissed,
|
||||
}
|
||||
|
||||
impl ModalLayer {
|
||||
pub fn new() -> Self {
|
||||
Self { active_modal: None }
|
||||
@@ -30,7 +22,7 @@ impl ModalLayer {
|
||||
|
||||
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
|
||||
where
|
||||
V: Modal,
|
||||
V: ManagedView,
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
if let Some(active_modal) = &self.active_modal {
|
||||
@@ -46,17 +38,15 @@ impl ModalLayer {
|
||||
|
||||
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
|
||||
where
|
||||
V: Modal,
|
||||
V: ManagedView,
|
||||
{
|
||||
self.active_modal = Some(ActiveModal {
|
||||
modal: new_modal.clone().into(),
|
||||
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| match e {
|
||||
ModalEvent::Dismissed => this.hide_modal(cx),
|
||||
}),
|
||||
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| this.hide_modal(cx)),
|
||||
previous_focus_handle: cx.focused(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
});
|
||||
new_modal.update(cx, |modal, cx| modal.focus(cx));
|
||||
cx.focus_view(&new_modal);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@ use futures::{
|
||||
use gpui::{
|
||||
actions, div, point, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, AsyncAppContext,
|
||||
AsyncWindowContext, Bounds, Context, Div, Entity, EntityId, EventEmitter, FocusHandle,
|
||||
FocusableView, GlobalPixels, InteractiveComponent, KeyContext, Model, ModelContext,
|
||||
ParentComponent, Point, Render, Size, Styled, Subscription, Task, View, ViewContext,
|
||||
VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle, WindowOptions,
|
||||
FocusableView, GlobalPixels, InteractiveComponent, KeyContext, ManagedView, Model,
|
||||
ModelContext, ParentComponent, Point, Render, Size, Styled, Subscription, Task, View,
|
||||
ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle, WindowOptions,
|
||||
};
|
||||
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
|
||||
use itertools::Itertools;
|
||||
@@ -3380,11 +3380,14 @@ impl Workspace {
|
||||
div
|
||||
}
|
||||
|
||||
pub fn active_modal<V: Modal + 'static>(&mut self, cx: &ViewContext<Self>) -> Option<View<V>> {
|
||||
pub fn active_modal<V: ManagedView + 'static>(
|
||||
&mut self,
|
||||
cx: &ViewContext<Self>,
|
||||
) -> Option<View<V>> {
|
||||
self.modal_layer.read(cx).active_modal()
|
||||
}
|
||||
|
||||
pub fn toggle_modal<V: Modal, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
|
||||
pub fn toggle_modal<V: ManagedView, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
|
||||
where
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user