Merge branch 'main' into project-panel2
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
use crate::Workspace;
|
||||
use gpui::{
|
||||
div, px, AnyView, Component, Div, EventEmitter, ParentElement, Render, StatelessInteractive,
|
||||
Styled, Subscription, View, ViewContext,
|
||||
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 {
|
||||
modal: AnyView,
|
||||
subscription: Subscription,
|
||||
previous_focus_handle: Option<FocusHandle>,
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
pub struct ModalLayer {
|
||||
open_modal: Option<AnyView>,
|
||||
subscription: Option<Subscription>,
|
||||
registered_modals: Vec<(TypeId, Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>)>,
|
||||
active_modal: Option<ActiveModal>,
|
||||
}
|
||||
|
||||
pub trait Modal: Render + EventEmitter<ModalEvent> {
|
||||
fn focus(&self, cx: &mut WindowContext);
|
||||
}
|
||||
|
||||
pub enum ModalEvent {
|
||||
@@ -18,74 +25,82 @@ pub enum ModalEvent {
|
||||
|
||||
impl ModalLayer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
open_modal: None,
|
||||
subscription: None,
|
||||
registered_modals: Vec::new(),
|
||||
}
|
||||
Self { active_modal: None }
|
||||
}
|
||||
|
||||
pub fn register_modal<A: 'static, V, B>(&mut self, action: A, build_view: B)
|
||||
pub fn toggle_modal<V, B>(&mut self, cx: &mut ViewContext<Self>, build_view: B)
|
||||
where
|
||||
V: EventEmitter<ModalEvent> + Render,
|
||||
B: Fn(&mut Workspace, &mut ViewContext<Workspace>) -> Option<View<V>> + 'static,
|
||||
V: Modal,
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
let build_view = Arc::new(build_view);
|
||||
let previous_focus = cx.focused();
|
||||
|
||||
self.registered_modals.push((
|
||||
TypeId::of::<A>(),
|
||||
Box::new(move |mut div| {
|
||||
let build_view = build_view.clone();
|
||||
if let Some(active_modal) = &self.active_modal {
|
||||
let is_close = active_modal.modal.clone().downcast::<V>().is_ok();
|
||||
self.hide_modal(cx);
|
||||
if is_close {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let new_modal = cx.build_view(build_view);
|
||||
self.show_modal(new_modal, cx);
|
||||
}
|
||||
|
||||
div.on_action(move |workspace, event: &A, cx| {
|
||||
let Some(new_modal) = (build_view)(workspace, cx) else {
|
||||
return;
|
||||
};
|
||||
workspace.modal_layer().show_modal(new_modal, cx);
|
||||
})
|
||||
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, |this, modal, e, cx| match e {
|
||||
ModalEvent::Dismissed => this.hide_modal(cx),
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Workspace>)
|
||||
where
|
||||
V: EventEmitter<ModalEvent> + Render,
|
||||
{
|
||||
self.subscription = Some(cx.subscribe(&new_modal, |this, modal, e, cx| match e {
|
||||
ModalEvent::Dismissed => this.modal_layer().hide_modal(cx),
|
||||
}));
|
||||
self.open_modal = Some(new_modal.into());
|
||||
previous_focus_handle: cx.focused(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
});
|
||||
new_modal.update(cx, |modal, cx| modal.focus(cx));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn hide_modal(&mut self, cx: &mut ViewContext<Workspace>) {
|
||||
self.open_modal.take();
|
||||
self.subscription.take();
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn wrapper_element(&self, cx: &ViewContext<Workspace>) -> Div<Workspace> {
|
||||
let mut parent = div().relative().size_full();
|
||||
|
||||
for (_, action) in self.registered_modals.iter() {
|
||||
parent = (action)(parent);
|
||||
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) {
|
||||
previous_focus.focus(cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parent.when_some(self.open_modal.as_ref(), |parent, open_modal| {
|
||||
let container1 = div()
|
||||
.absolute()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.items_center()
|
||||
.size_full()
|
||||
.top_0()
|
||||
.left_0()
|
||||
.z_index(400);
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
// transparent layer
|
||||
let container2 = v_stack().h(px(0.0)).relative().top_20();
|
||||
impl Render for ModalLayer {
|
||||
type Element = Div<Self>;
|
||||
|
||||
parent.child(container1.child(container2.child(open_modal.clone())))
|
||||
})
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
let Some(active_modal) = &self.active_modal else {
|
||||
return 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()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+166
-160
@@ -36,11 +36,12 @@ use futures::{
|
||||
Future, FutureExt, StreamExt,
|
||||
};
|
||||
use gpui::{
|
||||
actions, div, point, rems, size, AnyModel, AnyView, AnyWeakView, AppContext, AsyncAppContext,
|
||||
AsyncWindowContext, Bounds, Component, Div, Entity, EntityId, EventEmitter, FocusHandle,
|
||||
GlobalPixels, Model, ModelContext, ParentElement, Point, Render, Size, StatefulInteractive,
|
||||
StatelessInteractive, Styled, Subscription, Task, View, ViewContext, VisualContext, WeakView,
|
||||
WindowBounds, WindowContext, WindowHandle, WindowOptions,
|
||||
actions, div, point, rems, size, Action, AnyModel, AnyView, AnyWeakView, AppContext,
|
||||
AsyncAppContext, AsyncWindowContext, Bounds, Component, DispatchContext, Div, Entity, EntityId,
|
||||
EventEmitter, FocusHandle, GlobalPixels, Model, ModelContext, ParentElement, Point, Render,
|
||||
Size, StatefulInteractive, StatefulInteractivity, StatelessInteractive, Styled, Subscription,
|
||||
Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle,
|
||||
WindowOptions,
|
||||
};
|
||||
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
|
||||
use itertools::Itertools;
|
||||
@@ -434,6 +435,13 @@ pub enum Event {
|
||||
pub struct Workspace {
|
||||
weak_self: WeakView<Self>,
|
||||
focus_handle: FocusHandle,
|
||||
workspace_actions: Vec<
|
||||
Box<
|
||||
dyn Fn(
|
||||
Div<Workspace, StatefulInteractivity<Workspace>>,
|
||||
) -> Div<Workspace, StatefulInteractivity<Workspace>>,
|
||||
>,
|
||||
>,
|
||||
zoomed: Option<AnyWeakView>,
|
||||
zoomed_position: Option<DockPosition>,
|
||||
center: PaneGroup,
|
||||
@@ -446,7 +454,7 @@ pub struct Workspace {
|
||||
last_active_center_pane: Option<WeakView<Pane>>,
|
||||
last_active_view_id: Option<proto::ViewId>,
|
||||
status_bar: View<StatusBar>,
|
||||
modal_layer: ModalLayer,
|
||||
modal_layer: View<ModalLayer>,
|
||||
// titlebar_item: Option<AnyViewHandle>,
|
||||
notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>,
|
||||
project: Model<Project>,
|
||||
@@ -598,7 +606,7 @@ impl Workspace {
|
||||
});
|
||||
|
||||
let workspace_handle = cx.view().downgrade();
|
||||
let modal_layer = ModalLayer::new();
|
||||
let modal_layer = cx.build_view(|cx| ModalLayer::new());
|
||||
|
||||
// todo!()
|
||||
// cx.update_default_global::<DragAndDrop<Workspace>, _, _>(|drag_and_drop, _| {
|
||||
@@ -679,13 +687,10 @@ impl Workspace {
|
||||
leader_updates_tx,
|
||||
subscriptions,
|
||||
pane_history_timestamp,
|
||||
workspace_actions: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn modal_layer(&mut self) -> &mut ModalLayer {
|
||||
&mut self.modal_layer
|
||||
}
|
||||
|
||||
fn new_local(
|
||||
abs_paths: Vec<PathBuf>,
|
||||
app_state: Arc<AppState>,
|
||||
@@ -3356,23 +3361,27 @@ impl Workspace {
|
||||
// #[cfg(any(test, feature = "test-support"))]
|
||||
// pub fn test_new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
|
||||
// use node_runtime::FakeNodeRuntime;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub fn test_new(project: Model<Project>, cx: &mut ViewContext<Self>) -> Self {
|
||||
use gpui::Context;
|
||||
use node_runtime::FakeNodeRuntime;
|
||||
|
||||
// let client = project.read(cx).client();
|
||||
// let user_store = project.read(cx).user_store();
|
||||
let client = project.read(cx).client();
|
||||
let user_store = project.read(cx).user_store();
|
||||
|
||||
// let workspace_store = cx.add_model(|cx| WorkspaceStore::new(client.clone(), cx));
|
||||
// let app_state = Arc::new(AppState {
|
||||
// languages: project.read(cx).languages().clone(),
|
||||
// workspace_store,
|
||||
// client,
|
||||
// user_store,
|
||||
// fs: project.read(cx).fs().clone(),
|
||||
// build_window_options: |_, _, _| Default::default(),
|
||||
// initialize_workspace: |_, _, _, _| Task::ready(Ok(())),
|
||||
// node_runtime: FakeNodeRuntime::new(),
|
||||
// });
|
||||
// Self::new(0, project, app_state, cx)
|
||||
// }
|
||||
let workspace_store = cx.build_model(|cx| WorkspaceStore::new(client.clone(), cx));
|
||||
let app_state = Arc::new(AppState {
|
||||
languages: project.read(cx).languages().clone(),
|
||||
workspace_store,
|
||||
client,
|
||||
user_store,
|
||||
fs: project.read(cx).fs().clone(),
|
||||
build_window_options: |_, _, _| Default::default(),
|
||||
initialize_workspace: |_, _, _, _| Task::ready(Ok(())),
|
||||
node_runtime: FakeNodeRuntime::new(),
|
||||
});
|
||||
Self::new(0, project, app_state, cx)
|
||||
}
|
||||
|
||||
// fn render_dock(&self, position: DockPosition, cx: &WindowContext) -> Option<AnyElement<Self>> {
|
||||
// let dock = match position {
|
||||
@@ -3404,6 +3413,35 @@ impl Workspace {
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
pub fn register_action<A: Action>(
|
||||
&mut self,
|
||||
callback: impl Fn(&mut Self, &A, &mut ViewContext<Self>) + 'static,
|
||||
) {
|
||||
let callback = Arc::new(callback);
|
||||
|
||||
self.workspace_actions.push(Box::new(move |div| {
|
||||
let callback = callback.clone();
|
||||
div.on_action(move |workspace, event, cx| (callback.clone())(workspace, event, cx))
|
||||
}));
|
||||
}
|
||||
|
||||
fn add_workspace_actions_listeners(
|
||||
&self,
|
||||
mut div: Div<Workspace, StatefulInteractivity<Workspace>>,
|
||||
) -> Div<Workspace, StatefulInteractivity<Workspace>> {
|
||||
for action in self.workspace_actions.iter() {
|
||||
div = (action)(div)
|
||||
}
|
||||
div
|
||||
}
|
||||
|
||||
pub fn toggle_modal<V: Modal, B>(&mut self, cx: &mut ViewContext<Self>, build: B)
|
||||
where
|
||||
B: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
self.modal_layer
|
||||
.update(cx, |modal_layer, cx| modal_layer.toggle_modal(cx, build))
|
||||
}
|
||||
}
|
||||
|
||||
fn window_bounds_env_override(cx: &AsyncAppContext) -> Option<WindowBounds> {
|
||||
@@ -3618,142 +3656,110 @@ impl Render for Workspace {
|
||||
type Element = Div<Self>;
|
||||
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
div()
|
||||
.relative()
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.font("Zed Sans")
|
||||
.gap_0()
|
||||
.justify_start()
|
||||
.items_start()
|
||||
.text_color(cx.theme().colors().text)
|
||||
.bg(cx.theme().colors().background)
|
||||
.child(self.render_titlebar(cx))
|
||||
.child(Workspace::actions(
|
||||
// todo! should this be a component a view?
|
||||
self.modal_layer
|
||||
.wrapper_element(cx)
|
||||
.relative()
|
||||
.flex_1()
|
||||
.w_full()
|
||||
.flex()
|
||||
.overflow_hidden()
|
||||
.border_t()
|
||||
.border_b()
|
||||
.border_color(cx.theme().colors().border)
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_row()
|
||||
.flex_1()
|
||||
.h_full()
|
||||
.child(div().flex().flex_1().child(self.left_dock.clone()))
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.flex_1()
|
||||
.child(self.center.render(
|
||||
&self.project,
|
||||
&self.follower_states,
|
||||
self.active_call(),
|
||||
&self.active_pane,
|
||||
self.zoomed.as_ref(),
|
||||
&self.app_state,
|
||||
cx,
|
||||
))
|
||||
.child(div().flex().flex_1().child(self.bottom_dock.clone())),
|
||||
)
|
||||
.child(div().flex().flex_1().child(self.right_dock.clone())),
|
||||
), // .children(
|
||||
// Some(
|
||||
// Panel::new("chat-panel-outer", cx)
|
||||
// .side(PanelSide::Right)
|
||||
// .child(ChatPanel::new("chat-panel-inner").messages(vec![
|
||||
// ChatMessage::new(
|
||||
// "osiewicz".to_string(),
|
||||
// "is this thing on?".to_string(),
|
||||
// DateTime::parse_from_rfc3339("2023-09-27T15:40:52.707Z")
|
||||
// .unwrap()
|
||||
// .naive_local(),
|
||||
// ),
|
||||
// ChatMessage::new(
|
||||
// "maxdeviant".to_string(),
|
||||
// "Reading you loud and clear!".to_string(),
|
||||
// DateTime::parse_from_rfc3339("2023-09-28T15:40:52.707Z")
|
||||
// .unwrap()
|
||||
// .naive_local(),
|
||||
// ),
|
||||
// ])),
|
||||
// )
|
||||
// .filter(|_| self.is_chat_panel_open()),
|
||||
// )
|
||||
// .children(
|
||||
// Some(
|
||||
// Panel::new("notifications-panel-outer", cx)
|
||||
// .side(PanelSide::Right)
|
||||
// .child(NotificationsPanel::new("notifications-panel-inner")),
|
||||
// )
|
||||
// .filter(|_| self.is_notifications_panel_open()),
|
||||
// )
|
||||
// .children(
|
||||
// Some(
|
||||
// Panel::new("assistant-panel-outer", cx)
|
||||
// .child(AssistantPanel::new("assistant-panel-inner")),
|
||||
// )
|
||||
// .filter(|_| self.is_assistant_panel_open()),
|
||||
// ),
|
||||
))
|
||||
.child(self.status_bar.clone())
|
||||
// .when(self.debug.show_toast, |this| {
|
||||
// this.child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast")))
|
||||
// })
|
||||
// .children(
|
||||
// Some(
|
||||
// div()
|
||||
// .absolute()
|
||||
// .top(px(50.))
|
||||
// .left(px(640.))
|
||||
// .z_index(8)
|
||||
// .child(LanguageSelector::new("language-selector")),
|
||||
// )
|
||||
// .filter(|_| self.is_language_selector_open()),
|
||||
// )
|
||||
.z_index(8)
|
||||
// Debug
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.z_index(9)
|
||||
.absolute()
|
||||
.top_20()
|
||||
.left_1_4()
|
||||
.w_40()
|
||||
.gap_2(), // .when(self.show_debug, |this| {
|
||||
// this.child(Button::<Workspace>::new("Toggle User Settings").on_click(
|
||||
// Arc::new(|workspace, cx| workspace.debug_toggle_user_settings(cx)),
|
||||
// ))
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Toasts").on_click(Arc::new(
|
||||
// |workspace, cx| workspace.debug_toggle_toast(cx),
|
||||
// )),
|
||||
// )
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Livestream").on_click(Arc::new(
|
||||
// |workspace, cx| workspace.debug_toggle_livestream(cx),
|
||||
// )),
|
||||
// )
|
||||
// })
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Debug")
|
||||
// .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))),
|
||||
// ),
|
||||
)
|
||||
let mut context = DispatchContext::default();
|
||||
context.insert("Workspace");
|
||||
cx.with_key_dispatch_context(context, |cx| {
|
||||
div()
|
||||
.relative()
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.font("Zed Sans")
|
||||
.gap_0()
|
||||
.justify_start()
|
||||
.items_start()
|
||||
.text_color(cx.theme().colors().text)
|
||||
.bg(cx.theme().colors().background)
|
||||
.child(self.render_titlebar(cx))
|
||||
.child(
|
||||
// todo! should this be a component a view?
|
||||
self.add_workspace_actions_listeners(div().id("workspace"))
|
||||
.relative()
|
||||
.flex_1()
|
||||
.w_full()
|
||||
.flex()
|
||||
.overflow_hidden()
|
||||
.border_t()
|
||||
.border_b()
|
||||
.border_color(cx.theme().colors().border)
|
||||
.child(self.modal_layer.clone())
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_row()
|
||||
.flex_1()
|
||||
.h_full()
|
||||
.child(div().flex().flex_1().child(self.left_dock.clone()))
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.flex_1()
|
||||
.child(self.center.render(
|
||||
&self.project,
|
||||
&self.follower_states,
|
||||
self.active_call(),
|
||||
&self.active_pane,
|
||||
self.zoomed.as_ref(),
|
||||
&self.app_state,
|
||||
cx,
|
||||
))
|
||||
.child(
|
||||
div().flex().flex_1().child(self.bottom_dock.clone()),
|
||||
),
|
||||
)
|
||||
.child(div().flex().flex_1().child(self.right_dock.clone())),
|
||||
),
|
||||
)
|
||||
.child(self.status_bar.clone())
|
||||
// .when(self.debug.show_toast, |this| {
|
||||
// this.child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast")))
|
||||
// })
|
||||
// .children(
|
||||
// Some(
|
||||
// div()
|
||||
// .absolute()
|
||||
// .top(px(50.))
|
||||
// .left(px(640.))
|
||||
// .z_index(8)
|
||||
// .child(LanguageSelector::new("language-selector")),
|
||||
// )
|
||||
// .filter(|_| self.is_language_selector_open()),
|
||||
// )
|
||||
.z_index(8)
|
||||
// Debug
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.z_index(9)
|
||||
.absolute()
|
||||
.top_20()
|
||||
.left_1_4()
|
||||
.w_40()
|
||||
.gap_2(), // .when(self.show_debug, |this| {
|
||||
// this.child(Button::<Workspace>::new("Toggle User Settings").on_click(
|
||||
// Arc::new(|workspace, cx| workspace.debug_toggle_user_settings(cx)),
|
||||
// ))
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Toasts").on_click(Arc::new(
|
||||
// |workspace, cx| workspace.debug_toggle_toast(cx),
|
||||
// )),
|
||||
// )
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Livestream").on_click(Arc::new(
|
||||
// |workspace, cx| workspace.debug_toggle_livestream(cx),
|
||||
// )),
|
||||
// )
|
||||
// })
|
||||
// .child(
|
||||
// Button::<Workspace>::new("Toggle Debug")
|
||||
// .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))),
|
||||
// ),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// todo!()
|
||||
// impl Entity for Workspace {
|
||||
// type Event = Event;
|
||||
|
||||
Reference in New Issue
Block a user