wip: refactor

This commit is contained in:
2024-12-25 13:44:16 +07:00
parent 37d810d9e5
commit 0745b497f0
13 changed files with 117 additions and 120 deletions

View File

@@ -22,8 +22,8 @@ impl InvalidPanel {
}
}
impl Panel for InvalidPanel {
fn panel_name(&self) -> &'static str {
"InvalidPanel"
fn panel_name(&self) -> SharedString {
"InvalidPanel".into()
}
fn dump(&self, _cx: &AppContext) -> super::PanelState {

View File

@@ -32,18 +32,13 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
///
/// This is used to identify the panel when deserializing the panel.
/// Once you have defined a panel name, this must not be changed.
fn panel_name(&self) -> &'static str;
fn panel_name(&self) -> SharedString;
/// The title of the panel
fn title(&self, _cx: &WindowContext) -> AnyElement {
SharedString::from("Untitled").into_any_element()
}
/// The theme of the panel title, default is `None`.
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
None
}
/// Whether the panel can be closed, default is `true`.
fn closeable(&self, _cx: &WindowContext) -> bool {
true
@@ -71,9 +66,8 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
}
pub trait PanelView: 'static + Send + Sync {
fn panel_name(&self, _cx: &AppContext) -> &'static str;
fn panel_name(&self, cx: &WindowContext) -> SharedString;
fn title(&self, _cx: &WindowContext) -> AnyElement;
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle>;
fn closeable(&self, cx: &WindowContext) -> bool;
fn zoomable(&self, cx: &WindowContext) -> bool;
fn popup_menu(&self, menu: PopupMenu, cx: &WindowContext) -> PopupMenu;
@@ -84,7 +78,7 @@ pub trait PanelView: 'static + Send + Sync {
}
impl<T: Panel> PanelView for View<T> {
fn panel_name(&self, cx: &AppContext) -> &'static str {
fn panel_name(&self, cx: &WindowContext) -> SharedString {
self.read(cx).panel_name()
}
@@ -92,10 +86,6 @@ impl<T: Panel> PanelView for View<T> {
self.read(cx).title(cx)
}
fn title_style(&self, cx: &WindowContext) -> Option<TitleStyle> {
self.read(cx).title_style(cx)
}
fn closeable(&self, cx: &WindowContext) -> bool {
self.read(cx).closeable(cx)
}

View File

@@ -1,8 +1,5 @@
use gpui::{
prelude::FluentBuilder as _, AppContext, Axis, DismissEvent, EventEmitter, FocusHandle,
FocusableView, IntoElement, ParentElement, Pixels, Render, Styled, Subscription, View,
ViewContext, VisualContext, WeakView,
};
use gpui::*;
use prelude::FluentBuilder;
use smallvec::SmallVec;
use std::sync::Arc;
@@ -28,8 +25,8 @@ pub struct StackPanel {
}
impl Panel for StackPanel {
fn panel_name(&self) -> &'static str {
"StackPanel"
fn panel_name(&self) -> SharedString {
"StackPanel".into()
}
fn title(&self, _cx: &gpui::WindowContext) -> gpui::AnyElement {

View File

@@ -71,7 +71,6 @@ pub struct TabPanel {
/// If this is true, the Panel closeable will follow the active panel's closeable,
/// otherwise this TabPanel will not able to close
pub(crate) closeable: bool,
tab_bar_scroll_handle: ScrollHandle,
is_zoomed: bool,
is_collapsed: bool,
@@ -80,8 +79,8 @@ pub struct TabPanel {
}
impl Panel for TabPanel {
fn panel_name(&self) -> &'static str {
"TabPanel"
fn panel_name(&self) -> SharedString {
"TabPanel".into()
}
fn title(&self, cx: &WindowContext) -> gpui::AnyElement {
@@ -180,25 +179,32 @@ impl TabPanel {
active: bool,
cx: &mut ViewContext<Self>,
) {
assert_ne!(
panel.panel_name(cx),
"StackPanel",
"can not allows add `StackPanel` to `TabPanel`"
);
if self
.panels
.iter()
.any(|p| p.view().entity_id() == panel.view().entity_id())
.any(|p| p.panel_name(cx) == panel.panel_name(cx))
{
// set the active panel to the matched panel
if active {
if let Some(ix) = self
.panels
.iter()
.position(|p| p.panel_name(cx) == panel.panel_name(cx))
{
self.set_active_ix(ix, cx);
}
}
return;
}
self.panels.push(panel);
// set the active panel to the new panel
if active {
self.set_active_ix(self.panels.len() - 1, cx);
}
cx.emit(PanelEvent::LayoutChanged);
cx.notify();
}
@@ -466,7 +472,6 @@ impl TabPanel {
if self.panels.len() == 1 && panel_style == PanelStyle::Default {
let panel = self.panels.first().unwrap();
let title_style = panel.title_style(cx);
return h_flex()
.justify_between()
@@ -477,9 +482,6 @@ impl TabPanel {
.px_3()
.when(left_dock_button.is_some(), |this| this.pl_2())
.when(right_dock_button.is_some(), |this| this.pr_2())
.when_some(title_style, |this, theme| {
this.bg(theme.background).text_color(theme.foreground)
})
.when(
left_dock_button.is_some() || bottom_dock_button.is_some(),
|this| {

View File

@@ -1,10 +1,4 @@
use gpui::{
canvas, div, point, px, size, AnyElement, AppContext, Bounds, DismissEvent, DragMoveEvent,
Entity, EntityId, EventEmitter, FocusHandle, FocusableView, Half, InteractiveElement,
IntoElement, MouseButton, MouseDownEvent, MouseUpEvent, ParentElement, Pixels, Point, Render,
ScrollHandle, Size, StatefulInteractiveElement, Styled, ViewContext, VisualContext, WeakView,
WindowContext,
};
use gpui::*;
use std::{
cell::Cell,
fmt::{Debug, Formatter},
@@ -92,8 +86,8 @@ pub struct Tiles {
}
impl Panel for Tiles {
fn panel_name(&self) -> &'static str {
"Tiles"
fn panel_name(&self) -> SharedString {
"Tiles".into()
}
fn title(&self, _cx: &WindowContext) -> AnyElement {