Refactor to make ModalLayer a View

This commit is contained in:
Conrad Irwin 2023-11-09 22:11:11 -07:00
parent d4b1d1b528
commit 5a711886d4
3 changed files with 162 additions and 185 deletions

View file

@ -1,10 +1,7 @@
use crate::Workspace;
use gpui::{
div, px, AnyView, Component, Div, EventEmitter, FocusHandle, ParentElement, Render,
StatefulInteractivity, StatelessInteractive, Styled, Subscription, View, ViewContext,
VisualContext, WindowContext,
div, px, AnyView, Div, EventEmitter, FocusHandle, ParentElement, Render, StatelessInteractive,
Styled, Subscription, View, ViewContext, VisualContext, WindowContext,
};
use std::{any::TypeId, sync::Arc};
use ui::v_stack;
pub struct ActiveModal {
@ -31,7 +28,7 @@ impl ModalLayer {
Self { active_modal: None }
}
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Workspace>, build_view: B)
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
where
V: Modal,
B: FnOnce(&mut ViewContext<V>) -> V,
@ -48,14 +45,14 @@ impl ModalLayer {
self.show_modal(new_modal, cx);
}
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Workspace>)
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
where
V: Modal,
{
self.active_modal = Some(ActiveModal {
modal: new_modal.clone().into(),
subscription: cx.subscribe(&new_modal, |workspace, modal, e, cx| match e {
ModalEvent::Dismissed => workspace.modal_layer.hide_modal(cx),
subscription: cx.subscribe(&new_modal, |this, modal, e, cx| match e {
ModalEvent::Dismissed => this.hide_modal(cx),
}),
previous_focus_handle: cx.focused(),
focus_handle: cx.focus_handle(),
@ -64,7 +61,7 @@ impl ModalLayer {
cx.notify();
}
pub fn hide_modal(&mut self, cx: &mut ViewContext<Workspace>) {
pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
if let Some(active_modal) = self.active_modal.take() {
if let Some(previous_focus) = active_modal.previous_focus_handle {
if active_modal.focus_handle.contains_focused(cx) {
@ -75,57 +72,34 @@ impl ModalLayer {
cx.notify();
}
pub fn wrapper_element(
&self,
cx: &ViewContext<Workspace>,
) -> Div<Workspace, StatefulInteractivity<Workspace>> {
let parent = div().id("boop");
parent.when_some(self.active_modal.as_ref(), |parent, open_modal| {
let container1 = div()
.absolute()
.flex()
.flex_col()
.items_center()
.size_full()
.top_0()
.left_0()
.z_index(400);
let container2 = v_stack()
.h(px(0.0))
.relative()
.top_20()
.track_focus(&open_modal.focus_handle)
.on_mouse_down_out(|workspace: &mut Workspace, event, cx| {
workspace.modal_layer.hide_modal(cx);
});
parent.child(container1.child(container2.child(open_modal.modal.clone())))
})
}
}
// impl Render for ModalLayer {
// type Element = Div<Self>;
impl Render for ModalLayer {
type Element = Div<Self>;
// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
// let mut div = div();
// for (type_id, build_view) in cx.global::<ModalRegistry>().registered_modals {
// div = div.useful_on_action(
// type_id,
// Box::new(|this, _: dyn Any, phase, cx: &mut ViewContext<Self>| {
// if phase == DispatchPhase::Capture {
// return;
// }
// self.workspace.update(cx, |workspace, cx| {
// self.open_modal = Some(build_view(workspace, cx));
// });
// cx.notify();
// }),
// )
// }
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let Some(active_modal) = &self.active_modal else {
return div();
};
// div
// }
// }
div()
.absolute()
.flex()
.flex_col()
.items_center()
.size_full()
.top_0()
.left_0()
.z_index(400)
.child(
v_stack()
.h(px(0.0))
.top_20()
.track_focus(&active_modal.focus_handle)
.on_mouse_down_out(|this: &mut Self, event, cx| {
this.hide_modal(cx);
})
.child(active_modal.modal.clone()),
)
}
}