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:
parent
00e5385878
commit
98625edf01
3 changed files with 29 additions and 4 deletions
|
|
@ -442,6 +442,11 @@ impl DockArea {
|
||||||
self.bounds
|
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
|
/// Subscribe to the tiles item drag item drop event
|
||||||
fn subscribe_tiles_item_drop(
|
fn subscribe_tiles_item_drop(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
use crate::{button::Button, popup_menu::PopupMenu};
|
use crate::{button::Button, popup_menu::PopupMenu};
|
||||||
use gpui::{
|
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,
|
IntoElement, Render, SharedString, WeakEntity, Window,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -129,6 +129,7 @@ pub trait Panel: EventEmitter<PanelEvent> + Render + Focusable {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
pub trait PanelView: 'static + Send + Sync {
|
pub trait PanelView: 'static + Send + Sync {
|
||||||
fn panel_name(&self, cx: &App) -> &'static str;
|
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(&self, window: &Window, cx: &App) -> AnyElement;
|
||||||
fn title_style(&self, cx: &App) -> Option<TitleStyle>;
|
fn title_style(&self, cx: &App) -> Option<TitleStyle>;
|
||||||
fn closable(&self, cx: &App) -> bool;
|
fn closable(&self, cx: &App) -> bool;
|
||||||
|
|
@ -149,6 +150,10 @@ impl<T: Panel> PanelView for Entity<T> {
|
||||||
self.read(cx).panel_name()
|
self.read(cx).panel_name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn panel_id(&self, _: &App) -> EntityId {
|
||||||
|
self.entity_id()
|
||||||
|
}
|
||||||
|
|
||||||
fn title(&self, window: &Window, cx: &App) -> AnyElement {
|
fn title(&self, window: &Window, cx: &App) -> AnyElement {
|
||||||
self.read(cx).title(window, cx)
|
self.read(cx).title(window, cx)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,9 @@ use crate::{
|
||||||
v_flex, ActiveTheme, Icon, IconName,
|
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::{
|
use gpui::{
|
||||||
actions, canvas, div, point, px, size, AnyElement, App, AppContext, Bounds, Context,
|
actions, canvas, div, point, px, size, AnyElement, App, AppContext, Bounds, Context,
|
||||||
DismissEvent, DragMoveEvent, Empty, EntityId, EventEmitter, FocusHandle, Focusable, Half,
|
DismissEvent, DragMoveEvent, Empty, EntityId, EventEmitter, FocusHandle, Focusable, Half,
|
||||||
|
|
@ -359,6 +361,11 @@ impl Tiles {
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
cx: &mut Context<Self>,
|
cx: &mut Context<Self>,
|
||||||
) {
|
) {
|
||||||
|
assert!(
|
||||||
|
item.panel.view().downcast::<TabPanel>().is_ok(),
|
||||||
|
"only allows to add TabPanel type"
|
||||||
|
);
|
||||||
|
|
||||||
self.panels.push(item.clone());
|
self.panels.push(item.clone());
|
||||||
window.defer(cx, {
|
window.defer(cx, {
|
||||||
let panel = item.panel.clone();
|
let panel = item.panel.clone();
|
||||||
|
|
@ -487,12 +494,20 @@ impl Tiles {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the active panel, if any.
|
/// 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 {
|
let Some(active_index) = self.active_index else {
|
||||||
return None;
|
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>) {
|
fn set_active_index(&mut self, index: Option<usize>, cx: &mut Context<Self>) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue