update dock

This commit is contained in:
2026-08-19 10:13:29 +07:00
parent 071cb9e714
commit c8bf7eb5d3
11 changed files with 236 additions and 2228 deletions
+106 -372
View File
@@ -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)),
),
)
}
})
}