wip: refactor
This commit is contained in:
@@ -21,8 +21,9 @@ impl InvalidPanel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for InvalidPanel {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"InvalidPanel".into()
|
||||
}
|
||||
|
||||
@@ -30,12 +31,15 @@ impl Panel for InvalidPanel {
|
||||
self.old_state.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for InvalidPanel {}
|
||||
|
||||
impl FocusableView for InvalidPanel {
|
||||
fn focus_handle(&self, _: &AppContext) -> FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for InvalidPanel {
|
||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
|
||||
gpui::div()
|
||||
|
||||
@@ -2,6 +2,7 @@ use gpui::{
|
||||
AnyElement, AnyView, AppContext, EventEmitter, FocusHandle, FocusableView, Global, Hsla,
|
||||
IntoElement, SharedString, View, WeakView, WindowContext,
|
||||
};
|
||||
use nostr_sdk::prelude::Metadata;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use super::{DockArea, PanelInfo, PanelState};
|
||||
@@ -31,8 +32,13 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
|
||||
/// The name of the panel used to serialize, deserialize and identify the panel.
|
||||
///
|
||||
/// 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) -> SharedString;
|
||||
/// Once you have defined a panel id, this must not be changed.
|
||||
fn panel_id(&self) -> SharedString;
|
||||
|
||||
/// The optional metadata of the panel
|
||||
fn panel_metadata(&self) -> Option<Metadata> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The title of the panel
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
@@ -66,7 +72,8 @@ pub trait Panel: EventEmitter<PanelEvent> + FocusableView {
|
||||
}
|
||||
|
||||
pub trait PanelView: 'static + Send + Sync {
|
||||
fn panel_name(&self, cx: &WindowContext) -> SharedString;
|
||||
fn panel_id(&self, cx: &WindowContext) -> SharedString;
|
||||
fn panel_metadata(&self, cx: &WindowContext) -> Option<Metadata>;
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement;
|
||||
fn closeable(&self, cx: &WindowContext) -> bool;
|
||||
fn zoomable(&self, cx: &WindowContext) -> bool;
|
||||
@@ -78,8 +85,12 @@ pub trait PanelView: 'static + Send + Sync {
|
||||
}
|
||||
|
||||
impl<T: Panel> PanelView for View<T> {
|
||||
fn panel_name(&self, cx: &WindowContext) -> SharedString {
|
||||
self.read(cx).panel_name()
|
||||
fn panel_id(&self, cx: &WindowContext) -> SharedString {
|
||||
self.read(cx).panel_id()
|
||||
}
|
||||
|
||||
fn panel_metadata(&self, cx: &WindowContext) -> Option<Metadata> {
|
||||
self.read(cx).panel_metadata()
|
||||
}
|
||||
|
||||
fn title(&self, cx: &WindowContext) -> AnyElement {
|
||||
@@ -166,7 +177,7 @@ impl Default for PanelRegistry {
|
||||
impl Global for PanelRegistry {}
|
||||
|
||||
/// Register the Panel init by panel_name to global registry.
|
||||
pub fn register_panel<F>(cx: &mut AppContext, panel_name: &str, deserialize: F)
|
||||
pub fn register_panel<F>(cx: &mut AppContext, panel_id: &str, deserialize: F)
|
||||
where
|
||||
F: Fn(WeakView<DockArea>, &PanelState, &PanelInfo, &mut WindowContext) -> Box<dyn PanelView>
|
||||
+ 'static,
|
||||
@@ -177,5 +188,5 @@ where
|
||||
|
||||
cx.global_mut::<PanelRegistry>()
|
||||
.items
|
||||
.insert(panel_name.to_string(), Arc::new(deserialize));
|
||||
.insert(panel_id.to_string(), Arc::new(deserialize));
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ pub struct StackPanel {
|
||||
}
|
||||
|
||||
impl Panel for StackPanel {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"StackPanel".into()
|
||||
}
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ impl Default for PanelState {
|
||||
impl PanelState {
|
||||
pub fn new<P: Panel>(panel: &P) -> Self {
|
||||
Self {
|
||||
panel_name: panel.panel_name().to_string(),
|
||||
panel_name: panel.panel_id().to_string(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,14 +42,18 @@ impl Render for DragPanel {
|
||||
.id("drag-panel")
|
||||
.cursor_grab()
|
||||
.py_1()
|
||||
.px_3()
|
||||
.px_2()
|
||||
.w_24()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.overflow_hidden()
|
||||
.whitespace_nowrap()
|
||||
.border_1()
|
||||
.border_color(cx.theme().border)
|
||||
.rounded_md()
|
||||
.text_color(cx.theme().tab_foreground)
|
||||
.text_xs()
|
||||
.bg(cx.theme().tab_active)
|
||||
.opacity(0.75)
|
||||
.child(self.panel.title(cx))
|
||||
@@ -74,7 +78,7 @@ pub struct TabPanel {
|
||||
}
|
||||
|
||||
impl Panel for TabPanel {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"TabPanel".into()
|
||||
}
|
||||
|
||||
@@ -177,14 +181,14 @@ impl TabPanel {
|
||||
if self
|
||||
.panels
|
||||
.iter()
|
||||
.any(|p| p.panel_name(cx) == panel.panel_name(cx))
|
||||
.any(|p| p.panel_id(cx) == panel.panel_id(cx))
|
||||
{
|
||||
// set the active panel to the matched panel
|
||||
// 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))
|
||||
.position(|p| p.panel_id(cx) == panel.panel_id(cx))
|
||||
{
|
||||
self.set_active_ix(ix, cx);
|
||||
}
|
||||
@@ -195,7 +199,7 @@ impl TabPanel {
|
||||
|
||||
self.panels.push(panel);
|
||||
|
||||
// set the active panel to the new panel
|
||||
// Set the active panel to the new panel
|
||||
if active {
|
||||
self.set_active_ix(self.panels.len() - 1, cx);
|
||||
}
|
||||
@@ -459,6 +463,7 @@ impl TabPanel {
|
||||
let Some(dock_area) = self.dock_area.upgrade() else {
|
||||
return div().into_any_element();
|
||||
};
|
||||
|
||||
let panel_style = dock_area.read(cx).panel_style;
|
||||
|
||||
let left_dock_button = self.render_dock_toggle_button(DockPlacement::Left, cx);
|
||||
@@ -496,9 +501,37 @@ impl TabPanel {
|
||||
.flex_1()
|
||||
.min_w_16()
|
||||
.overflow_hidden()
|
||||
.text_ellipsis()
|
||||
.whitespace_nowrap()
|
||||
.child(panel.title(cx))
|
||||
.child(
|
||||
div()
|
||||
.w_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_1()
|
||||
.text_ellipsis()
|
||||
.text_xs()
|
||||
.child(div().when_some(
|
||||
panel.panel_metadata(cx),
|
||||
|this, metadata| {
|
||||
if let Some(picture) = metadata.picture {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!(
|
||||
"https://wsrv.nl/?url={}&w=100&h=100&n=-1",
|
||||
picture
|
||||
))
|
||||
.size_4()
|
||||
.rounded_full()
|
||||
.object_fit(ObjectFit::Cover),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0().child(
|
||||
img("brand/avatar.png").size_4().rounded_full(),
|
||||
)
|
||||
}
|
||||
},
|
||||
))
|
||||
.child(panel.title(cx)),
|
||||
)
|
||||
.when(state.draggable, |this| {
|
||||
this.on_drag(
|
||||
DragPanel {
|
||||
@@ -556,7 +589,7 @@ impl TabPanel {
|
||||
active = false;
|
||||
}
|
||||
|
||||
Tab::new(("tab", ix), panel.title(cx))
|
||||
Tab::new(("tab", ix), panel.title(cx), panel.panel_metadata(cx))
|
||||
.py_2()
|
||||
.selected(active)
|
||||
.disabled(disabled)
|
||||
|
||||
@@ -86,7 +86,7 @@ pub struct Tiles {
|
||||
}
|
||||
|
||||
impl Panel for Tiles {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"Tiles".into()
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ impl Panel for Tiles {
|
||||
.collect();
|
||||
|
||||
let mut state = PanelState::new(self);
|
||||
state.panel_name = self.panel_name().to_string();
|
||||
state.panel_name = self.panel_id().to_string();
|
||||
state.children = panels;
|
||||
state.info = PanelInfo::Tiles { metas };
|
||||
state
|
||||
|
||||
Reference in New Issue
Block a user