Refactor GoToLine to use cx.observe_new_views()
This commit is contained in:
parent
cbdd4aca89
commit
1b9f76c01d
11 changed files with 136 additions and 65 deletions
|
@ -1,25 +1,15 @@
|
|||
use crate::Workspace;
|
||||
use gpui::{
|
||||
div, px, AnyView, AppContext, Component, Div, EventEmitter, ParentElement, Render,
|
||||
StatelessInteractive, Styled, Subscription, View, ViewContext, WeakView,
|
||||
div, px, AnyView, Component, Div, EventEmitter, ParentElement, Render, StatelessInteractive,
|
||||
Styled, Subscription, View, ViewContext,
|
||||
};
|
||||
use std::{any::TypeId, sync::Arc};
|
||||
use ui::v_stack;
|
||||
|
||||
pub struct ModalRegistry {
|
||||
registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
|
||||
}
|
||||
|
||||
pub struct ModalLayer {
|
||||
workspace: WeakView<Workspace>,
|
||||
open_modal: Option<AnyView>,
|
||||
subscription: Option<Subscription>,
|
||||
}
|
||||
|
||||
pub fn init_modal_registry(cx: &mut AppContext) {
|
||||
cx.set_global(ModalRegistry {
|
||||
registered_modals: Vec::new(),
|
||||
});
|
||||
registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
|
||||
}
|
||||
|
||||
pub enum ModalEvent {
|
||||
|
@ -30,7 +20,15 @@ pub trait Modal: EventEmitter + Render {
|
|||
fn to_modal_event(&self, _: &Self::Event) -> Option<ModalEvent>;
|
||||
}
|
||||
|
||||
impl ModalRegistry {
|
||||
impl ModalLayer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
open_modal: None,
|
||||
subscription: None,
|
||||
registered_modals: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
|
||||
where
|
||||
V: Modal,
|
||||
|
@ -44,32 +42,19 @@ impl ModalRegistry {
|
|||
let build_view = build_view.clone();
|
||||
|
||||
div.on_action(move |workspace, event: &A, cx| {
|
||||
let Some(new_modal) =
|
||||
(build_view)(workspace, cx) else {
|
||||
return
|
||||
};
|
||||
workspace.modal_layer.update(cx, |modal_layer, cx| {
|
||||
modal_layer.show_modal(new_modal, cx);
|
||||
})
|
||||
let Some(new_modal) = (build_view)(workspace, cx) else {
|
||||
return;
|
||||
};
|
||||
workspace.modal_layer().show_modal(new_modal, cx);
|
||||
})
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
impl ModalLayer {
|
||||
pub fn new(workspace: WeakView<Workspace>) -> Self {
|
||||
Self {
|
||||
workspace,
|
||||
open_modal: None,
|
||||
subscription: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_modal<V: Modal>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>) {
|
||||
pub fn show_modal<V: Modal>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Workspace>) {
|
||||
self.subscription = Some(cx.subscribe(&new_modal, |this, modal, e, cx| {
|
||||
match modal.read(cx).to_modal_event(e) {
|
||||
Some(ModalEvent::Dismissed) => this.hide_modal(cx),
|
||||
Some(ModalEvent::Dismissed) => this.modal_layer().hide_modal(cx),
|
||||
None => {}
|
||||
}
|
||||
}));
|
||||
|
@ -77,16 +62,16 @@ impl ModalLayer {
|
|||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
|
||||
pub fn hide_modal(&mut self, cx: &mut ViewContext<Workspace>) {
|
||||
self.open_modal.take();
|
||||
self.subscription.take();
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn render(&self, cx: &ViewContext<Workspace>) -> Div<Workspace> {
|
||||
pub fn wrapper_element(&self, cx: &ViewContext<Workspace>) -> Div<Workspace> {
|
||||
let mut parent = div().relative().size_full();
|
||||
|
||||
for (_, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
|
||||
for (_, action) in self.registered_modals.iter() {
|
||||
parent = (action)(parent);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue