tiles: Fix active_panel method to returns actually Panel view, avoid return TabPanel. (#665)

- Limit `add_item` only allows TabPanel type.
- Export `items` method in DockArea.
This commit is contained in:
Jason Lee 2025-02-26 11:33:58 +08:00 committed by GitHub
parent 00e5385878
commit 98625edf01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 4 deletions

View file

@ -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,

View file

@ -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,
};
@ -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;
@ -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)
}

View file

@ -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,
@ -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();
@ -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>) {