Merge branch 'main' into picker

This commit is contained in:
Max Brunsfeld
2023-11-07 11:27:14 -08:00
38 changed files with 1117 additions and 585 deletions
+113
View File
@@ -0,0 +1,113 @@
use std::{any::TypeId, sync::Arc};
use gpui::{
div, AnyView, AppContext, Component, Div, ParentElement, Render, StatelessInteractive, View,
ViewContext,
};
use crate::Workspace;
pub struct ModalRegistry {
registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
}
pub trait Modal {}
#[derive(Clone)]
pub struct ModalLayer {
open_modal: Option<AnyView>,
}
pub fn init_modal_registry(cx: &mut AppContext) {
cx.set_global(ModalRegistry {
registered_modals: Vec::new(),
});
}
struct ToggleModal {
name: String,
}
// complete change of plan?
// on_action(ToggleModal{ name})
// register_modal(name, |workspace, cx| { ... })
impl ModalRegistry {
pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
where
V: Render,
B: Fn(&Workspace, &mut ViewContext<Workspace>) -> View<V> + 'static,
{
let build_view = Arc::new(build_view);
dbg!("yonder");
self.registered_modals.push((
TypeId::of::<A>(),
Box::new(move |mut div| {
let build_view = build_view.clone();
dbg!("this point");
div.on_action(
move |workspace: &mut Workspace, event: &A, cx: &mut ViewContext<Workspace>| {
let new_modal = (build_view)(workspace, cx);
workspace.modal_layer.update(cx, |modal_layer, _| {
modal_layer.open_modal = Some(new_modal.into());
});
cx.notify();
},
)
}),
));
}
}
impl ModalLayer {
pub fn new() -> Self {
Self { open_modal: None }
}
pub fn render(&self, cx: &ViewContext<Workspace>) -> impl Component<Workspace> {
dbg!("rendering ModalLayer");
let mut div = div();
// div, c workspace.toggle_modal()div.on_action()) {
//
// }
// for (type_id, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
// div = div.useful_on_action(*type_id, action)
// }
for (_, action) in cx.global::<ModalRegistry>().registered_modals.iter() {
div = (action)(div);
}
div.children(self.open_modal.clone())
}
}
// 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();
// }),
// )
// }
// div
// }
// }
+10 -84
View File
@@ -10,6 +10,7 @@ mod persistence;
pub mod searchable;
// todo!()
// pub mod shared_screen;
mod modal_layer;
mod status_bar;
mod toolbar;
mod workspace_settings;
@@ -45,6 +46,8 @@ use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings,
use itertools::Itertools;
use language2::LanguageRegistry;
use lazy_static::lazy_static;
pub use modal_layer::ModalRegistry;
use modal_layer::{init_modal_registry, ModalLayer};
use node_runtime::NodeRuntime;
use notifications::{simple_message_notification::MessageNotification, NotificationHandle};
pub use pane::*;
@@ -81,26 +84,6 @@ lazy_static! {
.and_then(parse_pixel_position_env_var);
}
// pub trait Modal: View {
// fn has_focus(&self) -> bool;
// fn dismiss_on_event(event: &Self::Event) -> bool;
// }
// trait ModalHandle {
// fn as_any(&self) -> &AnyViewHandle;
// fn has_focus(&self, cx: &WindowContext) -> bool;
// }
// impl<T: Modal> ModalHandle for View<T> {
// fn as_any(&self) -> &AnyViewHandle {
// self
// }
// fn has_focus(&self, cx: &WindowContext) -> bool {
// self.read(cx).has_focus()
// }
// }
// #[derive(Clone, PartialEq)]
// pub struct RemoveWorktreeFromProject(pub WorktreeId);
@@ -243,6 +226,7 @@ pub fn init_settings(cx: &mut AppContext) {
pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {
init_settings(cx);
init_modal_registry(cx);
pane::init(cx);
notifications::init(cx);
@@ -550,7 +534,6 @@ pub enum Event {
pub struct Workspace {
weak_self: WeakView<Self>,
focus_handle: FocusHandle,
// modal: Option<ActiveModal>,
zoomed: Option<AnyWeakView>,
zoomed_position: Option<DockPosition>,
center: PaneGroup,
@@ -563,6 +546,7 @@ pub struct Workspace {
last_active_center_pane: Option<WeakView<Pane>>,
last_active_view_id: Option<proto::ViewId>,
status_bar: View<StatusBar>,
modal_layer: View<ModalLayer>,
// titlebar_item: Option<AnyViewHandle>,
notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>,
project: Model<Project>,
@@ -580,11 +564,6 @@ pub struct Workspace {
pane_history_timestamp: Arc<AtomicUsize>,
}
// struct ActiveModal {
// view: Box<dyn ModalHandle>,
// previously_focused_view_id: Option<usize>,
// }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ViewId {
pub creator: PeerId,
@@ -718,6 +697,8 @@ impl Workspace {
status_bar
});
let modal_layer = cx.build_view(|cx| ModalLayer::new());
// todo!()
// cx.update_default_global::<DragAndDrop<Workspace>, _, _>(|drag_and_drop, _| {
// drag_and_drop.register_container(weak_handle.clone());
@@ -769,7 +750,6 @@ impl Workspace {
Workspace {
weak_self: weak_handle.clone(),
focus_handle: cx.focus_handle(),
// modal: None,
zoomed: None,
zoomed_position: None,
center: PaneGroup::new(center_pane.clone()),
@@ -779,6 +759,7 @@ impl Workspace {
last_active_center_pane: Some(center_pane.downgrade()),
last_active_view_id: None,
status_bar,
modal_layer,
// titlebar_item: None,
notifications: Default::default(),
left_dock,
@@ -1620,63 +1601,6 @@ impl Workspace {
})
}
// /// Returns the modal that was toggled closed if it was open.
// pub fn toggle_modal<V, F>(
// &mut self,
// cx: &mut ViewContext<Self>,
// build_view: F,
// ) -> Option<View<V>>
// where
// V: 'static + Modal,
// F: FnOnce(&mut Self, &mut ViewContext<Self>) -> View<V>,
// {
// cx.notify();
// // Whatever modal was visible is getting clobbered. If its the same type as V, then return
// // it. Otherwise, create a new modal and set it as active.
// if let Some(already_open_modal) = self
// .dismiss_modal(cx)
// .and_then(|modal| modal.downcast::<V>())
// {
// cx.focus_self();
// Some(already_open_modal)
// } else {
// let modal = build_view(self, cx);
// cx.subscribe(&modal, |this, _, event, cx| {
// if V::dismiss_on_event(event) {
// this.dismiss_modal(cx);
// }
// })
// .detach();
// let previously_focused_view_id = cx.focused_view_id();
// cx.focus(&modal);
// self.modal = Some(ActiveModal {
// view: Box::new(modal),
// previously_focused_view_id,
// });
// None
// }
// }
// pub fn modal<V: 'static + View>(&self) -> Option<View<V>> {
// self.modal
// .as_ref()
// .and_then(|modal| modal.view.as_any().clone().downcast::<V>())
// }
// pub fn dismiss_modal(&mut self, cx: &mut ViewContext<Self>) -> Option<AnyViewHandle> {
// if let Some(modal) = self.modal.take() {
// if let Some(previously_focused_view_id) = modal.previously_focused_view_id {
// if modal.view.has_focus(cx) {
// cx.window_context().focus(Some(previously_focused_view_id));
// }
// }
// cx.notify();
// Some(modal.view.as_any().clone())
// } else {
// None
// }
// }
pub fn items<'a>(
&'a self,
cx: &'a AppContext,
@@ -3916,6 +3840,8 @@ impl Render for Workspace {
// .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))),
// ),
)
// .child(self.modal_layer.clone())
.child(self.modal_layer.read(cx).render(cx))
}
}