feat: out-of-box experience #2
+18
-96
@@ -24,6 +24,12 @@ impl Render for ResizePanel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where to place a panel.
|
||||
///
|
||||
/// The [`DockArea`] has a fixed left dock and a center area. `Left` targets
|
||||
/// the left dock; `Center` adds a tab to the center; `Right` and `Bottom`
|
||||
/// split the center so the new panel lands on the given side of the existing
|
||||
/// center content.
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum DockPlacement {
|
||||
#[serde(rename = "center")]
|
||||
@@ -37,7 +43,8 @@ pub enum DockPlacement {
|
||||
}
|
||||
|
||||
impl DockPlacement {
|
||||
fn axis(&self) -> Axis {
|
||||
/// The split axis used when the placement splits the center area.
|
||||
pub(crate) fn axis(&self) -> Axis {
|
||||
match self {
|
||||
Self::Left | Self::Right => Axis::Horizontal,
|
||||
Self::Bottom => Axis::Vertical,
|
||||
@@ -48,24 +55,16 @@ impl DockPlacement {
|
||||
pub fn is_left(&self) -> bool {
|
||||
matches!(self, Self::Left)
|
||||
}
|
||||
|
||||
pub fn is_bottom(&self) -> bool {
|
||||
matches!(self, Self::Bottom)
|
||||
}
|
||||
|
||||
pub fn is_right(&self) -> bool {
|
||||
matches!(self, Self::Right)
|
||||
}
|
||||
}
|
||||
|
||||
/// The Dock is a fixed container that places at left, bottom, right of the Windows.
|
||||
/// The Dock is a fixed container that places at the left side of the window.
|
||||
///
|
||||
/// This is unlike Panel, it can't be move or add any other panel.
|
||||
pub struct Dock {
|
||||
pub(super) placement: DockPlacement,
|
||||
dock_area: WeakEntity<DockArea>,
|
||||
pub(crate) panel: DockItem,
|
||||
/// The size is means the width or height of the Dock, if the placement is left or right, the size is width, otherwise the size is height.
|
||||
/// The width of the dock.
|
||||
pub(super) size: Pixels,
|
||||
pub(super) open: bool,
|
||||
/// Whether the Dock is collapsible, default: true
|
||||
@@ -117,22 +116,6 @@ impl Dock {
|
||||
Self::new(dock_area, DockPlacement::Left, window, cx)
|
||||
}
|
||||
|
||||
pub fn bottom(
|
||||
dock_area: WeakEntity<DockArea>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
Self::new(dock_area, DockPlacement::Bottom, window, cx)
|
||||
}
|
||||
|
||||
pub fn right(
|
||||
dock_area: WeakEntity<DockArea>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
Self::new(dock_area, DockPlacement::Right, window, cx)
|
||||
}
|
||||
|
||||
/// Update the Dock to be collapsible or not.
|
||||
///
|
||||
/// And if the Dock is not collapsible, it will be open.
|
||||
@@ -212,16 +195,6 @@ impl Dock {
|
||||
}
|
||||
});
|
||||
}
|
||||
DockItem::Tiles { view, .. } => {
|
||||
window.defer(cx, {
|
||||
let view = view.clone();
|
||||
move |window, cx| {
|
||||
_ = dock_area.update(cx, |this, cx| {
|
||||
this.subscribe_panel(&view, window, cx);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
DockItem::Panel { .. } => {
|
||||
// Not supported
|
||||
}
|
||||
@@ -275,8 +248,7 @@ impl Dock {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.panel
|
||||
.add_panel(panel, &self.dock_area, None, window, cx);
|
||||
self.panel.add_panel(panel, &self.dock_area, window, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -328,52 +300,10 @@ impl Dock {
|
||||
.expect("DockArea is missing")
|
||||
.read(cx);
|
||||
let area_bounds = dock_area.bounds;
|
||||
let mut left_dock_size = px(0.0);
|
||||
let mut right_dock_size = px(0.0);
|
||||
|
||||
// Get the size of the left dock if it's open and not the current dock
|
||||
if let Some(left_dock) = &dock_area.left_dock
|
||||
&& left_dock.entity_id() != cx.entity().entity_id()
|
||||
{
|
||||
let left_dock_read = left_dock.read(cx);
|
||||
if left_dock_read.is_open() {
|
||||
left_dock_size = left_dock_read.size;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the size of the right dock if it's open and not the current dock
|
||||
if let Some(right_dock) = &dock_area.right_dock
|
||||
&& right_dock.entity_id() != cx.entity().entity_id()
|
||||
{
|
||||
let right_dock_read = right_dock.read(cx);
|
||||
if right_dock_read.is_open() {
|
||||
right_dock_size = right_dock_read.size;
|
||||
}
|
||||
}
|
||||
|
||||
let size = match self.placement {
|
||||
DockPlacement::Left => mouse_position.x - area_bounds.left(),
|
||||
DockPlacement::Right => area_bounds.right() - mouse_position.x,
|
||||
DockPlacement::Bottom => area_bounds.bottom() - mouse_position.y,
|
||||
DockPlacement::Center => unreachable!(),
|
||||
};
|
||||
match self.placement {
|
||||
DockPlacement::Left => {
|
||||
let max_size =
|
||||
(area_bounds.size.width - PANEL_MIN_SIZE - right_dock_size).max(PANEL_MIN_SIZE);
|
||||
self.size = size.clamp(PANEL_MIN_SIZE, max_size);
|
||||
}
|
||||
DockPlacement::Right => {
|
||||
let max_size =
|
||||
(area_bounds.size.width - PANEL_MIN_SIZE - left_dock_size).max(PANEL_MIN_SIZE);
|
||||
self.size = size.clamp(PANEL_MIN_SIZE, max_size);
|
||||
}
|
||||
DockPlacement::Bottom => {
|
||||
let max_size = (area_bounds.size.height - PANEL_MIN_SIZE).max(PANEL_MIN_SIZE);
|
||||
self.size = size.clamp(PANEL_MIN_SIZE, max_size);
|
||||
}
|
||||
DockPlacement::Center => unreachable!(),
|
||||
}
|
||||
let size = mouse_position.x - area_bounds.left();
|
||||
let max_size = (area_bounds.size.width - PANEL_MIN_SIZE).max(PANEL_MIN_SIZE);
|
||||
self.size = size.clamp(PANEL_MIN_SIZE, max_size);
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
@@ -394,7 +324,7 @@ impl Dock {
|
||||
|
||||
impl Render for Dock {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl gpui::IntoElement {
|
||||
if !self.open && !self.placement.is_bottom() {
|
||||
if !self.open {
|
||||
return div();
|
||||
}
|
||||
|
||||
@@ -403,21 +333,13 @@ impl Render for Dock {
|
||||
div()
|
||||
.relative()
|
||||
.overflow_hidden()
|
||||
.map(|this| match self.placement {
|
||||
DockPlacement::Left | DockPlacement::Right => this.h_flex().h_full().w(self.size),
|
||||
DockPlacement::Bottom => this.w_full().h(self.size),
|
||||
DockPlacement::Center => unreachable!(),
|
||||
})
|
||||
// Bottom Dock should keep the title bar, then user can click the Toggle button
|
||||
.when(!self.open && self.placement.is_bottom(), |this| {
|
||||
this.h(px(29.))
|
||||
})
|
||||
.h_flex()
|
||||
.h_full()
|
||||
.w(self.size)
|
||||
.map(|this| match &self.panel {
|
||||
DockItem::Split { view, .. } => this.child(view.clone()),
|
||||
DockItem::Tabs { view, .. } => this.child(view.clone()),
|
||||
DockItem::Panel { view, .. } => this.child(view.clone().view().cached(cache_style)),
|
||||
// Not support to render Tiles and Tile into Dock
|
||||
DockItem::Tiles { .. } => this,
|
||||
})
|
||||
.child(self.render_resize_handle(window, cx))
|
||||
.child(DockElement {
|
||||
|
||||
@@ -169,7 +169,10 @@
|
||||
],
|
||||
"info": {
|
||||
"stack": {
|
||||
"sizes": [704.0, 263.0],
|
||||
"sizes": [
|
||||
704.0,
|
||||
263.0
|
||||
],
|
||||
"axis": 1
|
||||
}
|
||||
}
|
||||
@@ -198,64 +201,5 @@
|
||||
"size": 350.0,
|
||||
"open": true,
|
||||
"resizeable": true
|
||||
},
|
||||
"right_dock": {
|
||||
"panel": {
|
||||
"panel_name": "TabPanel",
|
||||
"children": [
|
||||
{
|
||||
"panel_name": "StoryContainer",
|
||||
"children": [],
|
||||
"info": {
|
||||
"panel": {
|
||||
"story_klass": "ImageStory"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"info": {
|
||||
"tabs": {
|
||||
"active_index": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"placement": "right",
|
||||
"size": 320.0,
|
||||
"open": true,
|
||||
"resizeable": true
|
||||
},
|
||||
"bottom_dock": {
|
||||
"panel": {
|
||||
"panel_name": "TabPanel",
|
||||
"children": [
|
||||
{
|
||||
"panel_name": "StoryContainer",
|
||||
"children": [],
|
||||
"info": {
|
||||
"panel": {
|
||||
"story_klass": "TextStory"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"panel_name": "StoryContainer",
|
||||
"children": [],
|
||||
"info": {
|
||||
"panel": {
|
||||
"story_klass": "IconStory"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"info": {
|
||||
"tabs": {
|
||||
"active_index": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"placement": "bottom",
|
||||
"size": 200.0,
|
||||
"open": true,
|
||||
"resizeable": true
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
-372
@@ -5,7 +5,6 @@ mod resize_handle;
|
||||
mod stack_panel;
|
||||
mod state;
|
||||
mod tab_panel;
|
||||
mod tiles;
|
||||
mod window_controls;
|
||||
|
||||
use std::sync::Arc;
|
||||
@@ -23,7 +22,6 @@ pub use panel::*;
|
||||
pub use stack_panel::*;
|
||||
pub use state::*;
|
||||
pub use tab_panel::*;
|
||||
pub use tiles::*;
|
||||
|
||||
/// Initialize the dock, registering the [`PanelRegistry`] global.
|
||||
///
|
||||
@@ -54,6 +52,20 @@ pub(crate) fn t(key: &'static str) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// A host-owned drag item, dragged into the dock by the application.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AnyDrag {
|
||||
pub value: Arc<dyn std::any::Any + Send + Sync>,
|
||||
}
|
||||
|
||||
impl AnyDrag {
|
||||
pub fn new<T: 'static + Send + Sync>(value: T) -> Self {
|
||||
Self {
|
||||
value: Arc::new(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum DockEvent {
|
||||
/// The layout of the dock has changed, subscribers this to save the layout.
|
||||
///
|
||||
@@ -68,10 +80,6 @@ pub enum DockEvent {
|
||||
/// Where a host-owned drag landed, and how much the container can say about it.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum DropTarget {
|
||||
/// Dropped on a [`Tiles`] canvas, where the landing position is just the
|
||||
/// cursor position and the host can read it directly.
|
||||
Canvas,
|
||||
|
||||
/// Dropped on a [`TabPanel`] in a split layout. A split layout has no free
|
||||
/// coordinates, so the container reports the panel and the edge it resolved
|
||||
/// from the cursor instead.
|
||||
@@ -95,10 +103,6 @@ pub struct DockArea {
|
||||
center: DockItem,
|
||||
/// The left dock of the dock_area.
|
||||
left_dock: Option<Entity<Dock>>,
|
||||
/// The bottom dock of the dock_area.
|
||||
bottom_dock: Option<Entity<Dock>>,
|
||||
/// The right dock of the dock_area.
|
||||
right_dock: Option<Entity<Dock>>,
|
||||
|
||||
/// The entity_id of the [`TabPanel`](TabPanel) where each toggle button should be displayed,
|
||||
toggle_button_panels: Edges<Option<EntityId>>,
|
||||
@@ -144,13 +148,6 @@ pub enum DockItem {
|
||||
size: Option<Pixels>,
|
||||
view: Arc<dyn PanelView>,
|
||||
},
|
||||
/// Tiles layout
|
||||
Tiles {
|
||||
/// Self size, only used for build split panels
|
||||
size: Option<Pixels>,
|
||||
items: Vec<TileItem>,
|
||||
view: Entity<Tiles>,
|
||||
},
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DockItem {
|
||||
@@ -172,7 +169,6 @@ impl std::fmt::Debug for DockItem {
|
||||
.field("active_ix", active_ix)
|
||||
.finish(),
|
||||
DockItem::Panel { .. } => f.debug_struct("Panel").finish(),
|
||||
DockItem::Tiles { .. } => f.debug_struct("Tiles").finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,7 +180,6 @@ impl DockItem {
|
||||
Self::Split { size, .. } => *size,
|
||||
Self::Tabs { size, .. } => *size,
|
||||
Self::Panel { size, .. } => *size,
|
||||
Self::Tiles { size, .. } => *size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +189,6 @@ impl DockItem {
|
||||
match self {
|
||||
Self::Split { ref mut size, .. } => *size = new_size,
|
||||
Self::Tabs { ref mut size, .. } => *size = new_size,
|
||||
Self::Tiles { ref mut size, .. } => *size = new_size,
|
||||
Self::Panel { ref mut size, .. } => *size = new_size,
|
||||
}
|
||||
self
|
||||
@@ -303,60 +297,6 @@ impl DockItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create DockItem with tiles layout
|
||||
///
|
||||
/// This items and metas should have the same length.
|
||||
pub fn tiles(
|
||||
items: Vec<DockItem>,
|
||||
metas: Vec<impl Into<TileMeta> + Copy>,
|
||||
dock_area: &WeakEntity<DockArea>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Self {
|
||||
assert!(items.len() == metas.len());
|
||||
|
||||
let tile_panel = cx.new(|cx| {
|
||||
let mut tiles = Tiles::new(window, cx);
|
||||
for (ix, item) in items.clone().into_iter().enumerate() {
|
||||
match item {
|
||||
DockItem::Tabs { view, .. } => {
|
||||
let meta: TileMeta = metas[ix].into();
|
||||
let tile_item =
|
||||
TileItem::new(Arc::new(view), meta.bounds).z_index(meta.z_index);
|
||||
tiles.add_item(tile_item, dock_area, window, cx);
|
||||
}
|
||||
DockItem::Panel { view, .. } => {
|
||||
let meta: TileMeta = metas[ix].into();
|
||||
let tile_item =
|
||||
TileItem::new(view.clone(), meta.bounds).z_index(meta.z_index);
|
||||
tiles.add_item(tile_item, dock_area, window, cx);
|
||||
}
|
||||
_ => {
|
||||
// Ignore non-tabs items
|
||||
}
|
||||
}
|
||||
}
|
||||
tiles
|
||||
});
|
||||
|
||||
window.defer(cx, {
|
||||
let tile_panel = tile_panel.clone();
|
||||
let dock_area = dock_area.clone();
|
||||
move |window, cx| {
|
||||
_ = dock_area.update(cx, |this, cx| {
|
||||
this.subscribe_panel(&tile_panel, window, cx);
|
||||
this.subscribe_tiles_item_drop(&tile_panel, window, cx);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Self::Tiles {
|
||||
size: None,
|
||||
items: tile_panel.read(cx).panels.clone(),
|
||||
view: tile_panel,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create DockItem with tabs layout, items are displayed as tabs.
|
||||
///
|
||||
/// The `active_ix` is the index of the active tab, if `None` the first tab is active.
|
||||
@@ -412,7 +352,6 @@ impl DockItem {
|
||||
match self {
|
||||
Self::Split { view, .. } => Arc::new(view.clone()),
|
||||
Self::Tabs { view, .. } => Arc::new(view.clone()),
|
||||
Self::Tiles { view, .. } => Arc::new(view.clone()),
|
||||
Self::Panel { view, .. } => view.clone(),
|
||||
}
|
||||
}
|
||||
@@ -440,13 +379,6 @@ impl DockItem {
|
||||
if let Ok(tabs) = view.clone().downcast::<TabPanel>() {
|
||||
return tabs.read(cx).panels.iter().all(|panel| is_empty(panel, cx));
|
||||
}
|
||||
if let Ok(tiles) = view.downcast::<Tiles>() {
|
||||
return tiles
|
||||
.read(cx)
|
||||
.panels()
|
||||
.iter()
|
||||
.all(|item| is_empty(&item.panel, cx));
|
||||
}
|
||||
|
||||
!panel.visible(cx)
|
||||
}
|
||||
@@ -462,15 +394,6 @@ impl DockItem {
|
||||
}
|
||||
Self::Tabs { items, .. } => items.iter().find(|item| *item == &panel).cloned(),
|
||||
Self::Panel { view, .. } => Some(view.clone()),
|
||||
Self::Tiles { items, .. } => items.iter().find_map(|item| {
|
||||
// `==` on `Arc<dyn PanelView>` moves the captured `panel`
|
||||
// inside the closure; `eq` borrows both sides.
|
||||
if item.panel.eq(&panel) {
|
||||
Some(item.panel.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -479,7 +402,6 @@ impl DockItem {
|
||||
&mut self,
|
||||
panel: Arc<dyn PanelView>,
|
||||
dock_area: &WeakEntity<DockArea>,
|
||||
bounds: Option<Bounds<Pixels>>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
@@ -508,21 +430,6 @@ impl DockItem {
|
||||
stack_panel.add_panel(new_item.view(), None, dock_area.clone(), window, cx);
|
||||
});
|
||||
}
|
||||
Self::Tiles { view, items, .. } => {
|
||||
let tile_item = TileItem::new(
|
||||
Arc::new(cx.new(|cx| {
|
||||
let mut tab_panel = TabPanel::new(None, dock_area.clone(), window, cx);
|
||||
tab_panel.add_panel(panel.clone(), window, cx);
|
||||
tab_panel
|
||||
})),
|
||||
bounds.unwrap_or_else(|| TileMeta::default().bounds),
|
||||
);
|
||||
|
||||
items.push(tile_item.clone());
|
||||
view.update(cx, |tiles, cx| {
|
||||
tiles.add_item(tile_item, dock_area, window, cx);
|
||||
});
|
||||
}
|
||||
Self::Panel { .. } => {}
|
||||
}
|
||||
}
|
||||
@@ -544,11 +451,6 @@ impl DockItem {
|
||||
split.remove_panel(panel, window, cx);
|
||||
});
|
||||
}
|
||||
DockItem::Tiles { view, .. } => {
|
||||
view.update(cx, |tiles, cx| {
|
||||
tiles.remove(panel, window, cx);
|
||||
});
|
||||
}
|
||||
DockItem::Panel { .. } => {}
|
||||
}
|
||||
}
|
||||
@@ -566,7 +468,6 @@ impl DockItem {
|
||||
item.set_collapsed(collapsed, window, cx);
|
||||
}
|
||||
}
|
||||
DockItem::Tiles { .. } => {}
|
||||
DockItem::Panel { view, .. } => view.set_active(!collapsed, window, cx),
|
||||
}
|
||||
}
|
||||
@@ -576,17 +477,6 @@ impl DockItem {
|
||||
match self {
|
||||
DockItem::Tabs { view, .. } => Some(view.clone()),
|
||||
DockItem::Split { view, .. } => view.read(cx).left_top_tab_panel(true, cx),
|
||||
DockItem::Tiles { .. } => None,
|
||||
DockItem::Panel { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively traverses to find the right-most and top-most TabPanel.
|
||||
pub(crate) fn right_top_tab_panel(&self, cx: &App) -> Option<Entity<TabPanel>> {
|
||||
match self {
|
||||
DockItem::Tabs { view, .. } => Some(view.clone()),
|
||||
DockItem::Split { view, .. } => view.read(cx).right_top_tab_panel(true, cx),
|
||||
DockItem::Tiles { .. } => None,
|
||||
DockItem::Panel { .. } => None,
|
||||
}
|
||||
}
|
||||
@@ -615,8 +505,6 @@ impl DockArea {
|
||||
bounds: Bounds::default(),
|
||||
center: dock_item,
|
||||
left_dock: None,
|
||||
right_dock: None,
|
||||
bottom_dock: None,
|
||||
zoom_view: None,
|
||||
toggle_button_panels: Edges::default(),
|
||||
toggle_button_visible: true,
|
||||
@@ -635,22 +523,6 @@ impl DockArea {
|
||||
self.bounds
|
||||
}
|
||||
|
||||
/// Subscribe to the tiles item drag item drop event
|
||||
fn subscribe_tiles_item_drop(
|
||||
&mut self,
|
||||
tile_panel: &Entity<Tiles>,
|
||||
_: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self._subscriptions
|
||||
.push(cx.subscribe(tile_panel, move |_, _, evt: &DragDrop, cx| {
|
||||
cx.emit(DockEvent::DragDrop {
|
||||
item: evt.0.clone(),
|
||||
target: DropTarget::Canvas,
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/// Set the panel style of the dock area.
|
||||
pub fn panel_style(mut self, style: PanelStyle) -> Self {
|
||||
self.panel_style = style;
|
||||
@@ -681,31 +553,11 @@ impl DockArea {
|
||||
self.left_dock.as_ref()
|
||||
}
|
||||
|
||||
/// Return the bottom dock item.
|
||||
pub fn bottom_dock(&self) -> Option<&Entity<Dock>> {
|
||||
self.bottom_dock.as_ref()
|
||||
}
|
||||
|
||||
/// Return the right dock item.
|
||||
pub fn right_dock(&self) -> Option<&Entity<Dock>> {
|
||||
self.right_dock.as_ref()
|
||||
}
|
||||
|
||||
/// Remove the left dock.
|
||||
pub fn remove_left_dock(&mut self, _: &mut Window, _: &mut Context<Self>) {
|
||||
self.left_dock = None;
|
||||
}
|
||||
|
||||
/// Remove the bottom dock.
|
||||
pub fn remove_bottom_dock(&mut self, _: &mut Window, _: &mut Context<Self>) {
|
||||
self.bottom_dock = None;
|
||||
}
|
||||
|
||||
/// Remove the right dock.
|
||||
pub fn remove_right_dock(&mut self, _: &mut Window, _: &mut Context<Self>) {
|
||||
self.right_dock = None;
|
||||
}
|
||||
|
||||
/// The the DockItem as the center of the dock area.
|
||||
///
|
||||
/// This is used to render at the Center of the DockArea.
|
||||
@@ -738,50 +590,6 @@ impl DockArea {
|
||||
self.update_toggle_button_tab_panels(window, cx);
|
||||
}
|
||||
|
||||
pub fn set_bottom_dock(
|
||||
&mut self,
|
||||
panel: DockItem,
|
||||
size: Option<Pixels>,
|
||||
open: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.subscribe_item(&panel, window, cx);
|
||||
let weak_self = cx.entity().downgrade();
|
||||
self.bottom_dock = Some(cx.new(|cx| {
|
||||
let mut dock = Dock::bottom(weak_self.clone(), window, cx);
|
||||
if let Some(size) = size {
|
||||
dock.set_size(size, window, cx);
|
||||
}
|
||||
dock.set_panel(panel, window, cx);
|
||||
dock.set_open(open, window, cx);
|
||||
dock
|
||||
}));
|
||||
self.update_toggle_button_tab_panels(window, cx);
|
||||
}
|
||||
|
||||
pub fn set_right_dock(
|
||||
&mut self,
|
||||
panel: DockItem,
|
||||
size: Option<Pixels>,
|
||||
open: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.subscribe_item(&panel, window, cx);
|
||||
let weak_self = cx.entity().downgrade();
|
||||
self.right_dock = Some(cx.new(|cx| {
|
||||
let mut dock = Dock::right(weak_self.clone(), window, cx);
|
||||
if let Some(size) = size {
|
||||
dock.set_size(size, window, cx);
|
||||
}
|
||||
dock.set_panel(panel, window, cx);
|
||||
dock.set_open(open, window, cx);
|
||||
dock
|
||||
}));
|
||||
self.update_toggle_button_tab_panels(window, cx);
|
||||
}
|
||||
|
||||
/// Set locked state of the dock area, if locked, the dock area cannot be split or move, but allows to resize panels.
|
||||
pub fn set_locked(&mut self, locked: bool, _window: &mut Window, _cx: &mut App) {
|
||||
self.locked = locked;
|
||||
@@ -797,9 +605,7 @@ impl DockArea {
|
||||
pub fn has_dock(&self, placement: DockPlacement) -> bool {
|
||||
match placement {
|
||||
DockPlacement::Left => self.left_dock.is_some(),
|
||||
DockPlacement::Bottom => self.bottom_dock.is_some(),
|
||||
DockPlacement::Right => self.right_dock.is_some(),
|
||||
DockPlacement::Center => false,
|
||||
DockPlacement::Center | DockPlacement::Right | DockPlacement::Bottom => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -811,23 +617,13 @@ impl DockArea {
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).is_open())
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Bottom => self
|
||||
.bottom_dock
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).is_open())
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Right => self
|
||||
.right_dock
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).is_open())
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Center => false,
|
||||
DockPlacement::Center | DockPlacement::Right | DockPlacement::Bottom => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the dock at the given placement to be open or closed.
|
||||
///
|
||||
/// Only the left, bottom, right dock can be toggled.
|
||||
/// Only the left dock can be toggled.
|
||||
pub fn set_dock_collapsible(
|
||||
&mut self,
|
||||
collapsible_edges: Edges<bool>,
|
||||
@@ -839,18 +635,6 @@ impl DockArea {
|
||||
dock.set_collapsible(collapsible_edges.left, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(bottom_dock) = self.bottom_dock.as_ref() {
|
||||
bottom_dock.update(cx, |dock, cx| {
|
||||
dock.set_collapsible(collapsible_edges.bottom, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(right_dock) = self.right_dock.as_ref() {
|
||||
right_dock.update(cx, |dock, cx| {
|
||||
dock.set_collapsible(collapsible_edges.right, window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine if the dock at the given placement is collapsible.
|
||||
@@ -861,17 +645,7 @@ impl DockArea {
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).collapsible)
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Bottom => self
|
||||
.bottom_dock
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).collapsible)
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Right => self
|
||||
.right_dock
|
||||
.as_ref()
|
||||
.map(|dock| dock.read(cx).collapsible)
|
||||
.unwrap_or(false),
|
||||
DockPlacement::Center => false,
|
||||
DockPlacement::Center | DockPlacement::Right | DockPlacement::Bottom => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -882,14 +656,9 @@ impl DockArea {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let dock = match placement {
|
||||
DockPlacement::Left => &self.left_dock,
|
||||
DockPlacement::Bottom => &self.bottom_dock,
|
||||
DockPlacement::Right => &self.right_dock,
|
||||
DockPlacement::Center => return,
|
||||
};
|
||||
|
||||
if let Some(dock) = dock {
|
||||
if let DockPlacement::Left = placement
|
||||
&& let Some(dock) = self.left_dock.as_ref()
|
||||
{
|
||||
dock.update(cx, |view, cx| {
|
||||
view.toggle_open(window, cx);
|
||||
})
|
||||
@@ -902,11 +671,17 @@ impl DockArea {
|
||||
}
|
||||
|
||||
/// Add a panel item to the dock area at the given placement.
|
||||
///
|
||||
/// - [`DockPlacement::Left`] adds the panel to the left dock (creating it
|
||||
/// if needed).
|
||||
/// - [`DockPlacement::Center`] adds the panel as a tab in the center.
|
||||
/// - [`DockPlacement::Right`] and [`DockPlacement::Bottom`] split the
|
||||
/// center so the panel lands on the given side of the existing center
|
||||
/// content.
|
||||
pub fn add_panel(
|
||||
&mut self,
|
||||
panel: Arc<dyn PanelView>,
|
||||
placement: DockPlacement,
|
||||
bounds: Option<Bounds<Pixels>>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
@@ -925,39 +700,69 @@ impl DockArea {
|
||||
);
|
||||
}
|
||||
}
|
||||
DockPlacement::Bottom => {
|
||||
if let Some(dock) = self.bottom_dock.as_ref() {
|
||||
dock.update(cx, |dock, cx| dock.add_panel(panel, window, cx))
|
||||
} else {
|
||||
self.set_bottom_dock(
|
||||
DockItem::tabs(vec![panel], &weak_self, window, cx),
|
||||
None,
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
DockPlacement::Right => {
|
||||
if let Some(dock) = self.right_dock.as_ref() {
|
||||
dock.update(cx, |dock, cx| dock.add_panel(panel, window, cx))
|
||||
} else {
|
||||
self.set_right_dock(
|
||||
DockItem::tabs(vec![panel], &weak_self, window, cx),
|
||||
None,
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
DockPlacement::Right | DockPlacement::Bottom => {
|
||||
self.add_panel_to_center_side(panel, placement, window, cx);
|
||||
}
|
||||
DockPlacement::Center => {
|
||||
self.center
|
||||
.add_panel(panel, &cx.entity().downgrade(), bounds, window, cx);
|
||||
.add_panel(panel, &cx.entity().downgrade(), window, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Split the center so the new panel lands on the given side of the
|
||||
/// existing center content (right of it for [`DockPlacement::Right`],
|
||||
/// below it for [`DockPlacement::Bottom`]).
|
||||
///
|
||||
/// The existing center keeps its place. Repeating adds with the same axis
|
||||
/// appends further tab groups to the split; a different axis wraps the
|
||||
/// current center in a new split. An empty center is added to directly.
|
||||
fn add_panel_to_center_side(
|
||||
&mut self,
|
||||
panel: Arc<dyn PanelView>,
|
||||
placement: DockPlacement,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
debug_assert!(matches!(
|
||||
placement,
|
||||
DockPlacement::Right | DockPlacement::Bottom
|
||||
));
|
||||
|
||||
let weak_self = cx.entity().downgrade();
|
||||
|
||||
if self.center.is_empty(cx) {
|
||||
self.center.add_panel(panel, &weak_self, window, cx);
|
||||
} else if let DockItem::Split {
|
||||
axis,
|
||||
size: _,
|
||||
items,
|
||||
sizes,
|
||||
view,
|
||||
} = &mut self.center
|
||||
&& *axis == placement.axis()
|
||||
{
|
||||
// The center is already split along this axis: append a new tab group.
|
||||
let new_item = DockItem::tabs(vec![panel], &weak_self, window, cx);
|
||||
items.push(new_item.clone());
|
||||
sizes.push(None);
|
||||
view.update(cx, |stack, cx| {
|
||||
stack.add_panel(new_item.view(), None, weak_self.clone(), window, cx);
|
||||
});
|
||||
} else {
|
||||
// Wrap the existing center in a new split.
|
||||
let existing = self.center.clone();
|
||||
let new_item = DockItem::tabs(vec![panel], &weak_self, window, cx);
|
||||
let items = vec![existing, new_item];
|
||||
let sizes = vec![None; items.len()];
|
||||
self.center =
|
||||
DockItem::split_with_sizes(placement.axis(), items, sizes, &weak_self, window, cx);
|
||||
}
|
||||
|
||||
self.update_toggle_button_tab_panels(window, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Remove panel from the DockArea at the given placement.
|
||||
pub fn remove_panel(
|
||||
&mut self,
|
||||
@@ -974,23 +779,13 @@ impl DockArea {
|
||||
});
|
||||
}
|
||||
}
|
||||
DockPlacement::Right => {
|
||||
if let Some(dock) = self.right_dock.as_mut() {
|
||||
dock.update(cx, |dock, cx| {
|
||||
dock.remove_panel(panel, window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
DockPlacement::Bottom => {
|
||||
if let Some(dock) = self.bottom_dock.as_mut() {
|
||||
dock.update(cx, |dock, cx| {
|
||||
dock.remove_panel(panel, window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
DockPlacement::Center => {
|
||||
self.center.remove_panel(panel, window, cx);
|
||||
}
|
||||
DockPlacement::Right | DockPlacement::Bottom => {
|
||||
// The panel lives in the center; splits are part of it.
|
||||
self.center.remove_panel(panel, window, cx);
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
@@ -1004,8 +799,6 @@ impl DockArea {
|
||||
) {
|
||||
self.remove_panel(panel.clone(), DockPlacement::Center, window, cx);
|
||||
self.remove_panel(panel.clone(), DockPlacement::Left, window, cx);
|
||||
self.remove_panel(panel.clone(), DockPlacement::Right, window, cx);
|
||||
self.remove_panel(panel.clone(), DockPlacement::Bottom, window, cx);
|
||||
}
|
||||
|
||||
/// Load the state of the DockArea from the DockAreaState.
|
||||
@@ -1024,14 +817,6 @@ impl DockArea {
|
||||
self.left_dock = Some(left_dock_state.to_dock(weak_self.clone(), window, cx));
|
||||
}
|
||||
|
||||
if let Some(right_dock_state) = state.right_dock {
|
||||
self.right_dock = Some(right_dock_state.to_dock(weak_self.clone(), window, cx));
|
||||
}
|
||||
|
||||
if let Some(bottom_dock_state) = state.bottom_dock {
|
||||
self.bottom_dock = Some(bottom_dock_state.to_dock(weak_self.clone(), window, cx));
|
||||
}
|
||||
|
||||
self.center = state.center.to_item(weak_self, window, cx);
|
||||
self.update_toggle_button_tab_panels(window, cx);
|
||||
Ok(())
|
||||
@@ -1048,21 +833,11 @@ impl DockArea {
|
||||
.left_dock
|
||||
.as_ref()
|
||||
.map(|dock| DockState::new(dock.clone(), cx));
|
||||
let right_dock = self
|
||||
.right_dock
|
||||
.as_ref()
|
||||
.map(|dock| DockState::new(dock.clone(), cx));
|
||||
let bottom_dock = self
|
||||
.bottom_dock
|
||||
.as_ref()
|
||||
.map(|dock| DockState::new(dock.clone(), cx));
|
||||
|
||||
DockAreaState {
|
||||
version: self.version,
|
||||
center,
|
||||
left_dock,
|
||||
right_dock,
|
||||
bottom_dock,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1094,9 +869,6 @@ impl DockArea {
|
||||
DockItem::Tabs { .. } => {
|
||||
// We subscribe to the tab panel event in StackPanel's insert_panel
|
||||
}
|
||||
DockItem::Tiles { .. } => {
|
||||
// We subscribe to the tab panel event in Tiles's [`add_item`](Tiles::add_item)
|
||||
}
|
||||
DockItem::Panel { .. } => {
|
||||
// Not supported
|
||||
}
|
||||
@@ -1171,30 +943,16 @@ impl DockArea {
|
||||
match &self.center {
|
||||
DockItem::Split { view, .. } => view.clone().into_any_element(),
|
||||
DockItem::Tabs { view, .. } => view.clone().into_any_element(),
|
||||
DockItem::Tiles { view, .. } => view.clone().into_any_element(),
|
||||
DockItem::Panel { view, .. } => view.clone().view().into_any_element(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_toggle_button_tab_panels(&mut self, _: &mut Window, cx: &mut Context<Self>) {
|
||||
// Left toggle button
|
||||
// The left dock's toggle button lives in the center's top-left tab panel.
|
||||
self.toggle_button_panels.left = self
|
||||
.center
|
||||
.left_top_tab_panel(cx)
|
||||
.map(|view| view.entity_id());
|
||||
|
||||
// Right toggle button
|
||||
self.toggle_button_panels.right = self
|
||||
.center
|
||||
.right_top_tab_panel(cx)
|
||||
.map(|view| view.entity_id());
|
||||
|
||||
// Bottom toggle button
|
||||
self.toggle_button_panels.bottom = self
|
||||
.bottom_dock
|
||||
.as_ref()
|
||||
.and_then(|dock| dock.read(cx).panel.left_top_tab_panel(cx))
|
||||
.map(|view| view.entity_id());
|
||||
}
|
||||
}
|
||||
impl EventEmitter<DockEvent> for DockArea {}
|
||||
@@ -1212,48 +970,24 @@ impl Render for DockArea {
|
||||
if let Some(zoom_view) = self.zoom_view.clone() {
|
||||
this.child(zoom_view)
|
||||
} else {
|
||||
match &self.center {
|
||||
DockItem::Tiles { view, .. } => {
|
||||
// render tiles
|
||||
this.child(view.clone())
|
||||
}
|
||||
_ => {
|
||||
// render dock
|
||||
this.child(
|
||||
// render dock
|
||||
this.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_row()
|
||||
.h_full()
|
||||
// Left dock
|
||||
.when_some(self.left_dock.clone(), |this, dock| {
|
||||
this.child(div().flex().flex_none().child(dock))
|
||||
})
|
||||
// Center
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_row()
|
||||
.h_full()
|
||||
// Left dock
|
||||
.when_some(self.left_dock.clone(), |this, dock| {
|
||||
this.child(div().flex().flex_none().child(dock))
|
||||
})
|
||||
// Center
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_1()
|
||||
.flex_col()
|
||||
.overflow_hidden()
|
||||
// Top center
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.overflow_hidden()
|
||||
.child(self.render_items(window, cx)),
|
||||
)
|
||||
// Bottom Dock
|
||||
.when_some(self.bottom_dock.clone(), |this, dock| {
|
||||
this.child(dock)
|
||||
}),
|
||||
)
|
||||
// Right Dock
|
||||
.when_some(self.right_dock.clone(), |this, dock| {
|
||||
this.child(div().flex().flex_none().child(dock))
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
.flex_1()
|
||||
.overflow_hidden()
|
||||
.child(self.render_items(window, cx)),
|
||||
),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ use gpui::{
|
||||
Window,
|
||||
};
|
||||
use gpui_component::{
|
||||
ActiveTheme, AxisExt as _, Placement, ResizablePanelEvent, ResizablePanelGroup, ResizableState,
|
||||
h_flex, resizable_panel,
|
||||
ActiveTheme, Placement, ResizablePanelEvent, ResizablePanelGroup, ResizableState, h_flex,
|
||||
resizable_panel,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
@@ -354,38 +354,6 @@ impl StackPanel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the first top right in the stack.
|
||||
pub(super) fn right_top_tab_panel(
|
||||
&self,
|
||||
check_parent: bool,
|
||||
cx: &App,
|
||||
) -> Option<Entity<TabPanel>> {
|
||||
if check_parent
|
||||
&& let Some(parent) = self.parent.as_ref().and_then(|parent| parent.upgrade())
|
||||
&& let Some(panel) = parent.read(cx).right_top_tab_panel(true, cx)
|
||||
{
|
||||
return Some(panel);
|
||||
}
|
||||
|
||||
let panel = if self.axis.is_vertical() {
|
||||
self.panels.first()
|
||||
} else {
|
||||
self.panels.last()
|
||||
};
|
||||
|
||||
if let Some(view) = panel {
|
||||
if let Ok(tab_panel) = view.view().downcast::<TabPanel>() {
|
||||
Some(tab_panel)
|
||||
} else if let Ok(stack_panel) = view.view().downcast::<StackPanel>() {
|
||||
stack_panel.read(cx).right_top_tab_panel(false, cx)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove all panels from the stack.
|
||||
pub(super) fn remove_all_panels(&mut self, _: &mut Window, cx: &mut Context<Self>) {
|
||||
self.panels.clear();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use gpui::{App, AppContext, Axis, Bounds, Entity, Pixels, WeakEntity, Window, point, px, size};
|
||||
use gpui::{App, AppContext, Axis, Entity, Pixels, WeakEntity, Window};
|
||||
use itertools::Itertools as _;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -15,10 +15,6 @@ pub struct DockAreaState {
|
||||
pub center: PanelState,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub left_dock: Option<DockState>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub right_dock: Option<DockState>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bottom_dock: Option<DockState>,
|
||||
}
|
||||
|
||||
/// Used to serialize and deserialize the Dock
|
||||
@@ -72,30 +68,6 @@ pub struct PanelState {
|
||||
pub info: PanelInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct TileMeta {
|
||||
pub bounds: Bounds<Pixels>,
|
||||
pub z_index: usize,
|
||||
}
|
||||
|
||||
impl Default for TileMeta {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
bounds: Bounds {
|
||||
origin: point(px(10.), px(10.)),
|
||||
size: size(px(200.), px(200.)),
|
||||
},
|
||||
z_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Bounds<Pixels>> for TileMeta {
|
||||
fn from(bounds: Bounds<Pixels>) -> Self {
|
||||
Self { bounds, z_index: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum PanelInfo {
|
||||
#[serde(rename = "stack")]
|
||||
@@ -107,8 +79,6 @@ pub enum PanelInfo {
|
||||
Tabs { active_index: usize },
|
||||
#[serde(rename = "panel")]
|
||||
Panel(serde_json::Value),
|
||||
#[serde(rename = "tiles")]
|
||||
Tiles { metas: Vec<TileMeta> },
|
||||
}
|
||||
|
||||
impl PanelInfo {
|
||||
@@ -127,10 +97,6 @@ impl PanelInfo {
|
||||
Self::Panel(info)
|
||||
}
|
||||
|
||||
pub fn tiles(metas: Vec<TileMeta>) -> Self {
|
||||
Self::Tiles { metas }
|
||||
}
|
||||
|
||||
pub fn axis(&self) -> Option<Axis> {
|
||||
match self {
|
||||
Self::Stack { axis, .. } => Some(if *axis == 0 {
|
||||
@@ -232,7 +198,6 @@ impl PanelState {
|
||||
);
|
||||
DockItem::tabs(vec![view.into()], &dock_area, window, cx)
|
||||
}
|
||||
PanelInfo::Tiles { metas } => DockItem::tiles(items, metas, &dock_area, window, cx),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,19 +229,5 @@ mod tests {
|
||||
assert_eq!(left_dock.panel.panel_name, "TabPanel");
|
||||
assert_eq!(left_dock.panel.children.len(), 1);
|
||||
assert_eq!(left_dock.panel.children[0].panel_name, "StoryContainer");
|
||||
|
||||
let bottom_dock = state.bottom_dock.unwrap();
|
||||
assert!(bottom_dock.open);
|
||||
assert_eq!(bottom_dock.size, px(200.0));
|
||||
assert_eq!(bottom_dock.panel.panel_name, "TabPanel");
|
||||
assert_eq!(bottom_dock.panel.children.len(), 2);
|
||||
assert_eq!(bottom_dock.panel.children[0].panel_name, "StoryContainer");
|
||||
|
||||
let right_dock = state.right_dock.unwrap();
|
||||
assert!(right_dock.open);
|
||||
assert_eq!(right_dock.size, px(320.0));
|
||||
assert_eq!(right_dock.panel.panel_name, "TabPanel");
|
||||
assert_eq!(right_dock.panel.children.len(), 1);
|
||||
assert_eq!(right_dock.panel.children[0].panel_name, "StoryContainer");
|
||||
}
|
||||
}
|
||||
|
||||
+99
-168
@@ -152,8 +152,6 @@ pub struct TabPanel {
|
||||
will_split_placement: Option<Placement>,
|
||||
drop_placeholder_animation: Option<DropPlaceholderAnimation>,
|
||||
drop_placeholder_animation_name: SharedString,
|
||||
/// Is TabPanel used in Tiles.
|
||||
in_tiles: bool,
|
||||
|
||||
/// Bounds of the title bar row (the wrapper around the tab bar), in
|
||||
/// window coordinates. Measured via `on_prepaint` to position the
|
||||
@@ -183,9 +181,8 @@ impl Panel for TabPanel {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. When is the final panel in the dock, it will not able to close.
|
||||
// 2. When is in the Tiles, it will always able to close (by active panel state).
|
||||
if !self.draggable(cx) && !self.in_tiles {
|
||||
// The final panel in the dock is not closable.
|
||||
if !self.draggable(cx) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -314,18 +311,12 @@ impl TabPanel {
|
||||
zoomed: false,
|
||||
collapsed: false,
|
||||
closable: true,
|
||||
in_tiles: false,
|
||||
title_bar_bounds: None,
|
||||
title_bar_strip_bounds: None,
|
||||
title_bar_suffix_bounds: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark the TabPanel as being used in Tiles.
|
||||
pub(super) fn set_in_tiles(&mut self, in_tiles: bool) {
|
||||
self.in_tiles = in_tiles;
|
||||
}
|
||||
|
||||
pub(super) fn set_parent(&mut self, view: WeakEntity<StackPanel>) {
|
||||
self.stack_panel = Some(view);
|
||||
}
|
||||
@@ -717,6 +708,11 @@ impl TabPanel {
|
||||
_: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Option<Button> {
|
||||
// Only the left dock can be toggled.
|
||||
if placement != DockPlacement::Left {
|
||||
return None;
|
||||
}
|
||||
|
||||
if self.zoomed {
|
||||
return None;
|
||||
}
|
||||
@@ -733,47 +729,15 @@ impl TabPanel {
|
||||
let toggle_button_panels = dock_area.toggle_button_panels;
|
||||
|
||||
// Check if current TabPanel's entity_id matches the one stored in DockArea for this placement
|
||||
if !match placement {
|
||||
DockPlacement::Left => {
|
||||
dock_area.left_dock.is_some() && toggle_button_panels.left == Some(view_entity_id)
|
||||
}
|
||||
DockPlacement::Right => {
|
||||
dock_area.right_dock.is_some() && toggle_button_panels.right == Some(view_entity_id)
|
||||
}
|
||||
DockPlacement::Bottom => {
|
||||
dock_area.bottom_dock.is_some()
|
||||
&& toggle_button_panels.bottom == Some(view_entity_id)
|
||||
}
|
||||
DockPlacement::Center => unreachable!(),
|
||||
} {
|
||||
if dock_area.left_dock.is_none() || toggle_button_panels.left != Some(view_entity_id) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let is_open = dock_area.is_dock_open(placement, cx);
|
||||
|
||||
let icon = match placement {
|
||||
DockPlacement::Left => {
|
||||
if is_open {
|
||||
IconName::PanelLeft
|
||||
} else {
|
||||
IconName::PanelLeftOpen
|
||||
}
|
||||
}
|
||||
DockPlacement::Right => {
|
||||
if is_open {
|
||||
IconName::PanelRight
|
||||
} else {
|
||||
IconName::PanelRightOpen
|
||||
}
|
||||
}
|
||||
DockPlacement::Bottom => {
|
||||
if is_open {
|
||||
IconName::PanelBottom
|
||||
} else {
|
||||
IconName::PanelBottomOpen
|
||||
}
|
||||
}
|
||||
DockPlacement::Center => unreachable!(),
|
||||
let icon = if is_open {
|
||||
IconName::PanelLeft
|
||||
} else {
|
||||
IconName::PanelLeftOpen
|
||||
};
|
||||
|
||||
Some(
|
||||
@@ -810,11 +774,7 @@ impl TabPanel {
|
||||
};
|
||||
|
||||
let left_dock_button = self.render_dock_toggle_button(DockPlacement::Left, window, cx);
|
||||
let bottom_dock_button = self.render_dock_toggle_button(DockPlacement::Bottom, window, cx);
|
||||
let right_dock_button = self.render_dock_toggle_button(DockPlacement::Right, window, cx);
|
||||
let has_extend_dock_button = left_dock_button.is_some() || bottom_dock_button.is_some();
|
||||
|
||||
let is_bottom_dock = bottom_dock_button.is_some();
|
||||
let has_extend_dock_button = left_dock_button.is_some();
|
||||
|
||||
// macOS: the traffic lights overlay the window's top-left corner. The
|
||||
// left dock (sidebar) normally clears them; when it is closed or
|
||||
@@ -849,7 +809,6 @@ impl TabPanel {
|
||||
.pr_2()
|
||||
.when(left_dock_button.is_some(), |this| this.pl_2())
|
||||
.when(needs_traffic_light_padding, |this| this.pl(px(80.)))
|
||||
.when(right_dock_button.is_some(), |this| this.pr_2())
|
||||
.when_some(title_style, |this, theme| {
|
||||
this.bg(theme.background).text_color(theme.foreground)
|
||||
})
|
||||
@@ -859,8 +818,7 @@ impl TabPanel {
|
||||
.flex_shrink_0()
|
||||
.mr_1()
|
||||
.gap_1()
|
||||
.children(left_dock_button)
|
||||
.children(bottom_dock_button),
|
||||
.children(left_dock_button),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
@@ -889,8 +847,7 @@ impl TabPanel {
|
||||
.flex_shrink_0()
|
||||
.ml_1()
|
||||
.gap_1()
|
||||
.child(self.render_toolbar(state, window, cx))
|
||||
.children(right_dock_button),
|
||||
.child(self.render_toolbar(state, window, cx)),
|
||||
),
|
||||
window,
|
||||
cx,
|
||||
@@ -973,17 +930,13 @@ impl TabPanel {
|
||||
.top_0()
|
||||
// Right -1 for avoid border overlap with the first tab
|
||||
.right(-px(1.))
|
||||
.border_r_1()
|
||||
.border_b_1()
|
||||
.h_full()
|
||||
.border_color(cx.theme().border)
|
||||
.bg(cx.theme().tokens.tab_bar)
|
||||
.px_2()
|
||||
.when(needs_traffic_light_padding, |this| {
|
||||
this.pl(px(80.))
|
||||
})
|
||||
.children(left_dock_button)
|
||||
.children(bottom_dock_button),
|
||||
.children(left_dock_button),
|
||||
)
|
||||
},
|
||||
)
|
||||
@@ -1017,21 +970,8 @@ impl TabPanel {
|
||||
})
|
||||
.selected(active)
|
||||
.on_click(cx.listener({
|
||||
let is_collapsed = self.collapsed;
|
||||
let dock_area = self.dock_area.clone();
|
||||
move |view, _, window, cx| {
|
||||
view.set_active_ix(ix, window, cx);
|
||||
|
||||
// Open dock if clicked on the collapsed bottom dock
|
||||
if is_bottom_dock && is_collapsed {
|
||||
_ = dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.toggle_dock(
|
||||
DockPlacement::Bottom,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}))
|
||||
.when(!droppable, |this| {
|
||||
@@ -1060,23 +1000,18 @@ impl TabPanel {
|
||||
this.on_drop(drag, Some(ix), true, window, cx)
|
||||
},
|
||||
))
|
||||
.when(
|
||||
!self.in_tiles,
|
||||
|this| {
|
||||
this.drag_over::<AnyDrag>(|this, _, _, cx| {
|
||||
this.rounded_l_none()
|
||||
.border_l_2()
|
||||
.border_r_0()
|
||||
.border_color(cx.theme().drag_border)
|
||||
})
|
||||
.on_drop(cx.listener(
|
||||
|this, item: &AnyDrag, _, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.emit_drag_drop(item, None, cx);
|
||||
},
|
||||
))
|
||||
.drag_over::<AnyDrag>(|this, _, _, cx| {
|
||||
this.rounded_l_none()
|
||||
.border_l_2()
|
||||
.border_r_0()
|
||||
.border_color(cx.theme().drag_border)
|
||||
})
|
||||
.on_drop(cx.listener(
|
||||
|this, item: &AnyDrag, _, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.emit_drag_drop(item, None, cx);
|
||||
},
|
||||
)
|
||||
))
|
||||
},
|
||||
)
|
||||
}),
|
||||
@@ -1120,19 +1055,14 @@ impl TabPanel {
|
||||
this.on_drop(drag, ix, false, window, cx)
|
||||
},
|
||||
))
|
||||
.when(
|
||||
!self.in_tiles,
|
||||
|this| {
|
||||
this.drag_over::<AnyDrag>(|this, _, _, cx| {
|
||||
this.bg(cx.theme().tokens.drop_target)
|
||||
})
|
||||
.on_drop(cx.listener(
|
||||
|this, item: &AnyDrag, _, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.emit_drag_drop(item, None, cx);
|
||||
},
|
||||
))
|
||||
},
|
||||
.drag_over::<AnyDrag>(|this, _, _, cx| {
|
||||
this.bg(cx.theme().tokens.drop_target)
|
||||
})
|
||||
.on_drop(
|
||||
cx.listener(|this, item: &AnyDrag, _, cx| {
|
||||
this.will_split_placement = None;
|
||||
this.emit_drag_drop(item, None, cx);
|
||||
}),
|
||||
)
|
||||
}),
|
||||
)
|
||||
@@ -1142,10 +1072,7 @@ impl TabPanel {
|
||||
.items_center()
|
||||
.top_0()
|
||||
.right_0()
|
||||
.border_l_1()
|
||||
.border_b_1()
|
||||
.h_full()
|
||||
.border_color(cx.theme().border)
|
||||
.bg(cx.theme().tokens.tab_bar)
|
||||
.px_2()
|
||||
.gap_1()
|
||||
@@ -1165,8 +1092,7 @@ impl TabPanel {
|
||||
self.active_panel(cx)
|
||||
.and_then(|panel| panel.title_suffix(window, cx)),
|
||||
)
|
||||
.child(self.render_toolbar(state, window, cx))
|
||||
.when_some(right_dock_button, |this, btn| this.child(btn)),
|
||||
.child(self.render_toolbar(state, window, cx)),
|
||||
)
|
||||
}),
|
||||
)
|
||||
@@ -1214,9 +1140,7 @@ impl TabPanel {
|
||||
)
|
||||
.when(state.droppable, |this| {
|
||||
this.on_drag_move(cx.listener(Self::on_panel_drag_move))
|
||||
.when(!self.in_tiles, |this| {
|
||||
this.on_drag_move(cx.listener(Self::on_item_drag_move))
|
||||
})
|
||||
.on_drag_move(cx.listener(Self::on_item_drag_move))
|
||||
.child(
|
||||
div()
|
||||
.invisible()
|
||||
@@ -1233,13 +1157,11 @@ impl TabPanel {
|
||||
.on_drop(cx.listener(|this, drag: &DragPanel, window, cx| {
|
||||
this.on_drop(drag, None, true, window, cx)
|
||||
}))
|
||||
.when(!self.in_tiles, |this| {
|
||||
this.group_drag_over::<AnyDrag>("", |this| this.visible())
|
||||
.on_drop(cx.listener(|this, item: &AnyDrag, _, cx| {
|
||||
let placement = this.will_split_placement.take();
|
||||
this.emit_drag_drop(item, placement, cx);
|
||||
}))
|
||||
})
|
||||
.group_drag_over::<AnyDrag>("", |this| this.visible())
|
||||
.on_drop(cx.listener(|this, item: &AnyDrag, _, cx| {
|
||||
let placement = this.will_split_placement.take();
|
||||
this.emit_drag_drop(item, placement, cx);
|
||||
}))
|
||||
.when_some(placeholder, |this, animation| {
|
||||
let from = animation.from.origin - animation.to.origin;
|
||||
this.child(
|
||||
@@ -1601,9 +1523,8 @@ impl TabPanel {
|
||||
self.remove_panel(panel, window, cx);
|
||||
}
|
||||
|
||||
// Remove self from the parent DockArea.
|
||||
// This is ensure to remove from Tiles
|
||||
if self.panels.is_empty() && self.in_tiles {
|
||||
// Remove self from the parent DockArea when the last panel is closed.
|
||||
if self.panels.is_empty() {
|
||||
let tab_panel = Arc::new(cx.entity());
|
||||
window.defer(cx, {
|
||||
let dock_area = self.dock_area.clone();
|
||||
@@ -1681,12 +1602,12 @@ mod tests {
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use gpui::{TestAppContext, VisualTestContext, WindowHandle, WindowOptions, size};
|
||||
use gpui::{Axis, TestAppContext, VisualTestContext, WindowHandle, WindowOptions, size};
|
||||
use gpui_component::tab::{Tab, TabBar};
|
||||
use gpui_component::{Root, Theme, v_flex};
|
||||
|
||||
use super::*;
|
||||
use crate::{DockItem, TileMeta};
|
||||
use crate::DockItem;
|
||||
|
||||
#[test]
|
||||
fn drop_placeholder_bounds_cover_each_target_placement() {
|
||||
@@ -2018,49 +1939,6 @@ mod tests {
|
||||
assert!(cx.read(|cx| fixture.dock_area.read(cx).is_center_empty(cx)));
|
||||
}
|
||||
|
||||
/// A `TabPanel` inside `Tiles` has no parent `StackPanel` to remove itself
|
||||
/// from, so emptying it leaves the tile behind and the walk has to recurse.
|
||||
#[gpui::test]
|
||||
fn center_holding_only_empty_tiles_is_empty(cx: &mut TestAppContext) {
|
||||
let fixture = setup(cx);
|
||||
let mut cx = VisualTestContext::from_window(fixture.window.into(), cx);
|
||||
|
||||
let (tabs, tab_panel, panels) = build_tabs(&fixture, &["A"], None, &mut cx);
|
||||
let weak_dock_area = fixture.dock_area.downgrade();
|
||||
cx.update(|window, cx| {
|
||||
let tiles = DockItem::tiles(
|
||||
vec![tabs],
|
||||
vec![TileMeta::default()],
|
||||
&weak_dock_area,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
fixture
|
||||
.dock_area
|
||||
.update(cx, |area, cx| area.set_center(tiles, window, cx))
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(!cx.read(|cx| fixture.dock_area.read(cx).is_center_empty(cx)));
|
||||
|
||||
for panel in panels {
|
||||
cx.update(|window, cx| {
|
||||
tab_panel.update(cx, |tab_panel, cx| {
|
||||
tab_panel.remove_panel(Arc::new(panel.clone()), window, cx)
|
||||
})
|
||||
});
|
||||
}
|
||||
cx.run_until_parked();
|
||||
|
||||
let tiles = cx.read(|cx| {
|
||||
let DockItem::Tiles { view, .. } = fixture.dock_area.read(cx).center() else {
|
||||
unreachable!("the centre is a Tiles item");
|
||||
};
|
||||
view.read(cx).panels().len()
|
||||
});
|
||||
assert_eq!(tiles, 1, "the emptied TabPanel is still listed as a tile");
|
||||
assert!(cx.read(|cx| fixture.dock_area.read(cx).is_center_empty(cx)));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn single_panel_group_receives_initial_active(cx: &mut TestAppContext) {
|
||||
let fixture = setup(cx);
|
||||
@@ -2287,6 +2165,59 @@ mod tests {
|
||||
assert_eq!(drain(&fixture.log), [("A", false)]);
|
||||
}
|
||||
|
||||
/// Adding a panel to the right of the center splits it horizontally;
|
||||
/// a later bottom add wraps the split in a vertical one.
|
||||
#[gpui::test]
|
||||
fn add_panel_splits_the_center(cx: &mut TestAppContext) {
|
||||
let fixture = setup(cx);
|
||||
let mut cx = VisualTestContext::from_window(fixture.window.into(), cx);
|
||||
|
||||
let (item_a, _, _) = build_tabs(&fixture, &["A"], None, &mut cx);
|
||||
let (_item_b, _, panels_b) = build_tabs(&fixture, &["B"], None, &mut cx);
|
||||
let (_item_c, _, panels_c) = build_tabs(&fixture, &["C"], None, &mut cx);
|
||||
|
||||
cx.update(|window, cx| {
|
||||
fixture.dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.set_center(item_a, window, cx);
|
||||
dock_area.add_panel(
|
||||
Arc::new(panels_b[0].clone()),
|
||||
DockPlacement::Right,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
let (axis, len) = cx.read(|cx| match fixture.dock_area.read(cx).center() {
|
||||
DockItem::Split { axis, items, .. } => (*axis, items.len()),
|
||||
other => panic!("center must be a horizontal split, got {other:?}"),
|
||||
});
|
||||
assert_eq!(axis, Axis::Horizontal);
|
||||
assert_eq!(len, 2, "the right panel must be appended to the split");
|
||||
|
||||
cx.update(|window, cx| {
|
||||
fixture.dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(
|
||||
Arc::new(panels_c[0].clone()),
|
||||
DockPlacement::Bottom,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
cx.read(|cx| match fixture.dock_area.read(cx).center() {
|
||||
DockItem::Split {
|
||||
axis, items, sizes, ..
|
||||
} => {
|
||||
assert_eq!(*axis, Axis::Vertical);
|
||||
assert_eq!(items.len(), 2);
|
||||
assert_eq!(sizes.len(), 2);
|
||||
}
|
||||
other => panic!("center must be wrapped in a vertical split, got {other:?}"),
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ProbeFlags {
|
||||
empty_down: AtomicBool,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -133,8 +133,6 @@ impl RenderOnce for ControlIcon {
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the window control buttons, or an empty placeholder on macOS (the
|
||||
/// traffic lights are drawn natively).
|
||||
pub(crate) fn window_controls(window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
if cfg!(target_os = "macos") || cfg!(target_family = "wasm") {
|
||||
return div().id("window-controls");
|
||||
|
||||
@@ -651,7 +651,7 @@ impl RepoDetailView {
|
||||
cx.new(|cx| CommitDiffView::new(worktree, repo_name, commit_id.into(), window, cx));
|
||||
|
||||
dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, None, window, cx);
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -665,7 +665,7 @@ impl RepoDetailView {
|
||||
cx.new(|cx| IssuesView::new(self.store.clone(), self.display_name(cx), window, cx));
|
||||
|
||||
dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, None, window, cx);
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -679,7 +679,7 @@ impl RepoDetailView {
|
||||
.new(|cx| PullRequestsView::new(self.store.clone(), self.display_name(cx), window, cx));
|
||||
|
||||
dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, None, window, cx);
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ impl RepoListView {
|
||||
|
||||
if let Some(dock_area) = dock_area.upgrade() {
|
||||
dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(Arc::new(detail), DockPlacement::Center, None, window, cx);
|
||||
dock_area.add_panel(Arc::new(detail), DockPlacement::Center, window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ impl SidebarPanel {
|
||||
self.explore = Some(panel.downgrade());
|
||||
|
||||
let _ = self.dock_area.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, None, window, cx);
|
||||
dock_area.add_panel(Arc::new(panel), DockPlacement::Center, window, cx);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user