Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Feb 26, 2025
1 parent 00e5385 commit c2fb04f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions crates/ui/src/dock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ impl DockArea {
self.bounds
}

/// Return the items of the dock area.
pub fn items(&self) -> &DockItem {
&self.items
}

/// Subscribe to the tiles item drag item drop event
fn subscribe_tiles_item_drop(
&mut self,
Expand Down
7 changes: 6 additions & 1 deletion crates/ui/src/dock/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use crate::{button::Button, popup_menu::PopupMenu};
use gpui::{
AnyElement, AnyView, App, Entity, EventEmitter, FocusHandle, Focusable, Global, Hsla,
AnyElement, AnyView, App, Entity, EntityId, EventEmitter, FocusHandle, Focusable, Global, Hsla,
IntoElement, Render, SharedString, WeakEntity, Window,
};

Expand Down Expand Up @@ -129,6 +129,7 @@ pub trait Panel: EventEmitter<PanelEvent> + Render + Focusable {
#[allow(unused_variables)]
pub trait PanelView: 'static + Send + Sync {
fn panel_name(&self, cx: &App) -> &'static str;
fn panel_id(&self, cx: &App) -> EntityId;
fn title(&self, window: &Window, cx: &App) -> AnyElement;
fn title_style(&self, cx: &App) -> Option<TitleStyle>;
fn closable(&self, cx: &App) -> bool;
Expand All @@ -149,6 +150,10 @@ impl<T: Panel> PanelView for Entity<T> {
self.read(cx).panel_name()
}

fn panel_id(&self, _: &App) -> EntityId {
self.entity_id()
}

fn title(&self, window: &Window, cx: &App) -> AnyElement {
self.read(cx).title(window, cx)
}
Expand Down
21 changes: 18 additions & 3 deletions crates/ui/src/dock/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::{
v_flex, ActiveTheme, Icon, IconName,
};

use super::{DockArea, Panel, PanelEvent, PanelInfo, PanelState, PanelView, TabPanel, TileMeta};
use super::{
DockArea, Panel, PanelEvent, PanelInfo, PanelState, PanelView, StackPanel, TabPanel, TileMeta,
};
use gpui::{
actions, canvas, div, point, px, size, AnyElement, App, AppContext, Bounds, Context,
DismissEvent, DragMoveEvent, Empty, EntityId, EventEmitter, FocusHandle, Focusable, Half,
Expand Down Expand Up @@ -359,6 +361,11 @@ impl Tiles {
window: &mut Window,
cx: &mut Context<Self>,
) {
assert!(
item.panel.view().downcast::<TabPanel>().is_ok(),
"only allows to add TabPanel type"
);

self.panels.push(item.clone());
window.defer(cx, {
let panel = item.panel.clone();
Expand Down Expand Up @@ -487,12 +494,20 @@ impl Tiles {
}

/// Returns the active panel, if any.
pub fn active_panel(&self) -> Option<Arc<dyn PanelView>> {
pub fn active_panel(&self, cx: &App) -> Option<Arc<dyn PanelView>> {
let Some(active_index) = self.active_index else {
return None;
};

self.panels.get(active_index).map(|item| item.panel.clone())
self.panels.get(active_index).and_then(|item| {
if let Ok(tab_panel) = item.panel.view().downcast::<TabPanel>() {
tab_panel.read(cx).active_panel(cx)
} else if let Ok(_) = item.panel.view().downcast::<StackPanel>() {
None
} else {
Some(item.panel.clone())
}
})
}

fn set_active_index(&mut self, index: Option<usize>, cx: &mut Context<Self>) {
Expand Down

0 comments on commit c2fb04f

Please sign in to comment.