dock: Add panel_style to support always show TabBar. (#444)

Close #441 

```rs
DockArea::new(MAIN_DOCK_AREA.id, Some(MAIN_DOCK_AREA.version), cx).panel_style(PanelStyle::TabBar)
```

<img width="1433" alt="image"
src="https://github.com/user-attachments/assets/2013188e-990b-4709-ac7e-43c2de8bc895">
This commit is contained in:
Jason Lee 2024-11-27 21:34:27 +08:00 committed by GitHub
parent 53fe5823ef
commit 7fe91e4e12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 9 deletions

View file

@ -37,28 +37,31 @@ pub enum DockEvent {
/// The main area of the dock. /// The main area of the dock.
pub struct DockArea { pub struct DockArea {
id: SharedString, id: SharedString,
/// The version is used to special the default layout, this is like the `panel_version` in `trait Panel`. /// The version is used to special the default layout, this is like the `panel_version` in [`Panel`](Panel).
version: Option<usize>, version: Option<usize>,
pub(crate) bounds: Bounds<Pixels>, pub(crate) bounds: Bounds<Pixels>,
/// The center view of the dockarea. /// The center view of the dockarea.
items: DockItem, items: DockItem,
/// The entity_id of the TabPanel where each toggle button should be displayed, /// The entity_id of the [`TabPanel`](TabPanel) where each toggle button should be displayed,
toggle_button_panels: Edges<Option<EntityId>>, toggle_button_panels: Edges<Option<EntityId>>,
/// The left dock of the dockarea. /// The left dock of the dock_area.
left_dock: Option<View<Dock>>, left_dock: Option<View<Dock>>,
/// The bottom dock of the dockarea. /// The bottom dock of the dock_area.
bottom_dock: Option<View<Dock>>, bottom_dock: Option<View<Dock>>,
/// The right dock of the dockarea. /// The right dock of the dock_area.
right_dock: Option<View<Dock>>, right_dock: Option<View<Dock>>,
/// The top zoom view of the dockarea, if any. /// The top zoom view of the dock_area, if any.
zoom_view: Option<AnyView>, zoom_view: Option<AnyView>,
/// Lock panels layout, but allow to resize. /// Lock panels layout, but allow to resize.
is_locked: bool, is_locked: bool,
/// The panel style, default is [`PanelStyle::Default`](PanelStyle::Default).
pub(crate) panel_style: PanelStyle,
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
} }
@ -296,6 +299,7 @@ impl DockArea {
right_dock: None, right_dock: None,
bottom_dock: None, bottom_dock: None,
is_locked: false, is_locked: false,
panel_style: PanelStyle::Default,
_subscriptions: vec![], _subscriptions: vec![],
}; };
@ -304,6 +308,12 @@ impl DockArea {
this this
} }
/// Set the panel style of the dock area.
pub fn panel_style(mut self, style: PanelStyle) -> Self {
self.panel_style = style;
self
}
/// Set version of the dock area. /// Set version of the dock area.
pub fn set_version(&mut self, version: usize, cx: &mut ViewContext<Self>) { pub fn set_version(&mut self, version: usize, cx: &mut ViewContext<Self>) {
self.version = Some(version); self.version = Some(version);

View file

@ -16,6 +16,15 @@ pub enum PanelEvent {
LayoutChanged, LayoutChanged,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PanelStyle {
/// Display the TabBar when there are multiple tabs, otherwise display the simple title.
Default,
/// Always display the tab bar.
TabBar,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TitleStyle { pub struct TitleStyle {
pub background: Hsla, pub background: Hsla,
pub foreground: Hsla, pub foreground: Hsla,

View file

@ -20,8 +20,8 @@ use crate::{
}; };
use super::{ use super::{
ClosePanel, DockArea, DockItemState, DockPlacement, Panel, PanelEvent, PanelView, StackPanel, ClosePanel, DockArea, DockItemState, DockPlacement, Panel, PanelEvent, PanelStyle, PanelView,
ToggleZoom, StackPanel, ToggleZoom,
}; };
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -455,11 +455,16 @@ impl TabPanel {
fn render_title_bar(&self, state: TabState, cx: &mut ViewContext<Self>) -> impl IntoElement { fn render_title_bar(&self, state: TabState, cx: &mut ViewContext<Self>) -> impl IntoElement {
let view = cx.view().clone(); let view = cx.view().clone();
let Some(dock_area) = self.dock_area.upgrade() else {
return div().into_any_element();
};
let panel_style = dock_area.read(cx).panel_style;
let left_dock_button = self.render_dock_toggle_button(DockPlacement::Left, cx); let left_dock_button = self.render_dock_toggle_button(DockPlacement::Left, cx);
let bottom_dock_button = self.render_dock_toggle_button(DockPlacement::Bottom, cx); let bottom_dock_button = self.render_dock_toggle_button(DockPlacement::Bottom, cx);
let right_dock_button = self.render_dock_toggle_button(DockPlacement::Right, cx); let right_dock_button = self.render_dock_toggle_button(DockPlacement::Right, cx);
if self.panels.len() == 1 { if self.panels.len() == 1 && panel_style == PanelStyle::Default {
let panel = self.panels.get(0).unwrap(); let panel = self.panels.get(0).unwrap();
let title_style = panel.title_style(cx); let title_style = panel.title_style(cx);