Add close menu to Panel. (#196)

This commit is contained in:
Jason Lee 2024-09-01 21:33:50 +08:00 committed by GitHub
parent 9c4411ff2b
commit 6564c502c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 19 deletions

View file

@ -39,7 +39,6 @@ pub fn init(_app_state: Arc<AppState>, cx: &mut AppContext) {
pub struct StoryWorkspace { pub struct StoryWorkspace {
locale_selector: View<LocaleSelector>, locale_selector: View<LocaleSelector>,
// stack_panel: View<StackPanel>,
dock_area: View<DockArea>, dock_area: View<DockArea>,
} }

View file

@ -91,6 +91,10 @@ Dock:
en: Unnamed en: Unnamed
zh-CN: 未命名 zh-CN: 未命名
zh-HK: 未命名 zh-HK: 未命名
Close:
en: Close
zh-CN: 关闭
zh-HK: 關閉
Zoom In: Zoom In:
en: Zoom In en: Zoom In
zh-CN: 放大 zh-CN: 放大

View file

@ -3,21 +3,19 @@ mod stack_panel;
mod tab_panel; mod tab_panel;
use gpui::{ use gpui::{
actions, div, prelude::FluentBuilder, AnyView, InteractiveElement as _, IntoElement, actions, div, prelude::FluentBuilder, AnyWeakView, InteractiveElement as _, IntoElement,
ParentElement as _, Render, Styled, View, ViewContext, ParentElement as _, Render, Styled, View, ViewContext,
}; };
pub use panel::*; pub use panel::*;
pub use stack_panel::*; pub use stack_panel::*;
pub use tab_panel::*; pub use tab_panel::*;
use crate::theme::ActiveTheme; actions!(dock, [ToggleZoom, ClosePanel]);
actions!(dock, [ToggleZoom]);
/// The main area of the dock. /// The main area of the dock.
pub struct DockArea { pub struct DockArea {
root: View<StackPanel>, root: View<StackPanel>,
zoom_view: Option<AnyView>, zoom_view: Option<AnyWeakView>,
} }
impl DockArea { impl DockArea {
@ -33,28 +31,24 @@ impl DockArea {
if self.zoom_view.is_some() { if self.zoom_view.is_some() {
self.zoom_view = None; self.zoom_view = None;
} else { } else {
self.zoom_view = Some(panel.into()); self.zoom_view = Some(panel.downgrade().into());
} }
cx.notify(); cx.notify();
} }
} }
impl Render for DockArea { impl Render for DockArea {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement { fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
div() div()
.id("dock-area") .id("dock-area")
.size_full() .size_full()
.overflow_hidden() .overflow_hidden()
.map(|this| match self.zoom_view.clone() { .map(|this| {
Some(view) => this.bg(cx.theme().tab_bar).p_3().child( if let Some(zoom_view) = self.zoom_view.as_ref().and_then(|view| view.upgrade()) {
div() this.child(zoom_view)
.size_full() } else {
.border_1() this.child(self.root.clone())
.border_color(cx.theme().border) }
.shadow_lg()
.child(view),
),
None => this.child(self.root.clone()),
}) })
} }
} }

View file

@ -18,7 +18,7 @@ use crate::{
v_flex, AxisExt, IconName, Placement, Selectable, Sizable, StyledExt, 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 { pub enum PanelEvent {
ZoomIn, ZoomIn,
@ -195,6 +195,8 @@ impl TabPanel {
}, },
Box::new(ToggleZoom), Box::new(ToggleZoom),
) )
.separator()
.menu(t!("Dock.Close"), Box::new(ClosePanel))
}) })
.anchor(AnchorCorner::TopRight), .anchor(AnchorCorner::TopRight),
) )
@ -488,6 +490,12 @@ impl TabPanel {
cx.emit(PanelEvent::ZoomOut) cx.emit(PanelEvent::ZoomOut)
} }
} }
fn on_action_close_panel(&mut self, _: &ClosePanel, cx: &mut ViewContext<Self>) {
if let Some(panel) = self.active_panel() {
self.remove_panel(panel, cx);
}
}
} }
impl Panel for TabPanel {} impl Panel for TabPanel {}
@ -505,6 +513,7 @@ impl Render for TabPanel {
.id("tab-panel") .id("tab-panel")
.track_focus(&self.focus_handle) .track_focus(&self.focus_handle)
.on_action(cx.listener(Self::on_action_toggle_zoom)) .on_action(cx.listener(Self::on_action_toggle_zoom))
.on_action(cx.listener(Self::on_action_close_panel))
.size_full() .size_full()
.overflow_hidden() .overflow_hidden()
.bg(cx.theme().background) .bg(cx.theme().background)