From 6564c502c9c2723a08f438051ea3877e060e599c Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 1 Sep 2024 21:33:50 +0800 Subject: [PATCH] Add close menu to Panel. (#196) --- crates/app/src/story_workspace.rs | 1 - crates/ui/locales/ui.yml | 4 ++++ crates/ui/src/dock/mod.rs | 28 +++++++++++----------------- crates/ui/src/dock/tab_panel.rs | 11 ++++++++++- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/crates/app/src/story_workspace.rs b/crates/app/src/story_workspace.rs index c6792033..c8785bd9 100644 --- a/crates/app/src/story_workspace.rs +++ b/crates/app/src/story_workspace.rs @@ -39,7 +39,6 @@ pub fn init(_app_state: Arc, cx: &mut AppContext) { pub struct StoryWorkspace { locale_selector: View, - // stack_panel: View, dock_area: View, } diff --git a/crates/ui/locales/ui.yml b/crates/ui/locales/ui.yml index 5e032477..c72ac401 100644 --- a/crates/ui/locales/ui.yml +++ b/crates/ui/locales/ui.yml @@ -91,6 +91,10 @@ Dock: en: Unnamed zh-CN: 未命名 zh-HK: 未命名 + Close: + en: Close + zh-CN: 关闭 + zh-HK: 關閉 Zoom In: en: Zoom In zh-CN: 放大 diff --git a/crates/ui/src/dock/mod.rs b/crates/ui/src/dock/mod.rs index dc8fe939..fd8785ad 100644 --- a/crates/ui/src/dock/mod.rs +++ b/crates/ui/src/dock/mod.rs @@ -3,21 +3,19 @@ mod stack_panel; mod tab_panel; use gpui::{ - actions, div, prelude::FluentBuilder, AnyView, InteractiveElement as _, IntoElement, + actions, div, prelude::FluentBuilder, AnyWeakView, InteractiveElement as _, IntoElement, ParentElement as _, Render, Styled, View, ViewContext, }; pub use panel::*; pub use stack_panel::*; pub use tab_panel::*; -use crate::theme::ActiveTheme; - -actions!(dock, [ToggleZoom]); +actions!(dock, [ToggleZoom, ClosePanel]); /// The main area of the dock. pub struct DockArea { root: View, - zoom_view: Option, + zoom_view: Option, } impl DockArea { @@ -33,28 +31,24 @@ impl DockArea { if self.zoom_view.is_some() { self.zoom_view = None; } else { - self.zoom_view = Some(panel.into()); + self.zoom_view = Some(panel.downgrade().into()); } cx.notify(); } } impl Render for DockArea { - fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { div() .id("dock-area") .size_full() .overflow_hidden() - .map(|this| match self.zoom_view.clone() { - Some(view) => this.bg(cx.theme().tab_bar).p_3().child( - div() - .size_full() - .border_1() - .border_color(cx.theme().border) - .shadow_lg() - .child(view), - ), - None => this.child(self.root.clone()), + .map(|this| { + if let Some(zoom_view) = self.zoom_view.as_ref().and_then(|view| view.upgrade()) { + this.child(zoom_view) + } else { + this.child(self.root.clone()) + } }) } } diff --git a/crates/ui/src/dock/tab_panel.rs b/crates/ui/src/dock/tab_panel.rs index bd10b5b0..bc38e968 100644 --- a/crates/ui/src/dock/tab_panel.rs +++ b/crates/ui/src/dock/tab_panel.rs @@ -18,7 +18,7 @@ use crate::{ v_flex, AxisExt, IconName, Placement, Selectable, Sizable, StyledExt, }; -use super::{DockArea, Panel, PanelView, StackPanel, ToggleZoom}; +use super::{ClosePanel, DockArea, Panel, PanelView, StackPanel, ToggleZoom}; pub enum PanelEvent { ZoomIn, @@ -195,6 +195,8 @@ impl TabPanel { }, Box::new(ToggleZoom), ) + .separator() + .menu(t!("Dock.Close"), Box::new(ClosePanel)) }) .anchor(AnchorCorner::TopRight), ) @@ -488,6 +490,12 @@ impl TabPanel { cx.emit(PanelEvent::ZoomOut) } } + + fn on_action_close_panel(&mut self, _: &ClosePanel, cx: &mut ViewContext) { + if let Some(panel) = self.active_panel() { + self.remove_panel(panel, cx); + } + } } impl Panel for TabPanel {} @@ -505,6 +513,7 @@ impl Render for TabPanel { .id("tab-panel") .track_focus(&self.focus_handle) .on_action(cx.listener(Self::on_action_toggle_zoom)) + .on_action(cx.listener(Self::on_action_close_panel)) .size_full() .overflow_hidden() .bg(cx.theme().background)