wip: refactor

This commit is contained in:
2024-12-27 09:56:56 +07:00
parent 7fd9f22b4a
commit 2ddd2d3b17
17 changed files with 294 additions and 46 deletions

View File

@@ -28,3 +28,4 @@ rust-embed.workspace = true
smol.workspace = true
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
random_name_generator = "0.3.6"

View File

@@ -1,8 +1,10 @@
use gpui::*;
use nostr_sdk::prelude::*;
use rnglib::{Language, RNG};
use serde::Deserialize;
use std::sync::{Arc, RwLock};
use super::metadata::MetadataRegistry;
use crate::utils::get_room_id;
#[derive(Clone, PartialEq, Eq, Deserialize)]
@@ -12,31 +14,40 @@ pub struct Room {
pub members: Vec<PublicKey>,
pub last_seen: Timestamp,
pub title: Option<SharedString>,
pub metadata: Option<Metadata>,
}
impl Room {
pub fn new(event: &Event) -> Self {
pub fn new(event: &Event, cx: &mut WindowContext<'_>) -> Self {
let owner = event.pubkey;
let last_seen = event.created_at;
// Get all members from event's tag
let members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
// Get title from event's tag
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
tag.content().map(|s| s.to_owned().into())
} else {
// TODO: create random name?
None
let rng = RNG::from(&Language::Roman);
let name = rng.generate_names(2, true).join("-").to_lowercase();
Some(name.into())
};
// Get unique id based on members
let id = get_room_id(&owner, &members).into();
// Get metadata for all members if exists
let metadata = cx.global::<MetadataRegistry>().get(&owner);
Self {
id,
title,
members,
last_seen,
owner,
metadata,
}
}
}

View File

@@ -35,8 +35,8 @@ impl MetadataRegistry {
}
}
pub fn get(&self, public_key: PublicKey) -> Option<Metadata> {
self.profiles.read().unwrap().get(&public_key).cloned()
pub fn get(&self, public_key: &PublicKey) -> Option<Metadata> {
self.profiles.read().unwrap().get(public_key).cloned()
}
fn new() -> Self {

View File

@@ -1,11 +1,13 @@
use std::sync::Arc;
use coop_ui::{
button::Button,
dock::{Panel, PanelEvent, PanelState},
popup_menu::PopupMenu,
};
use gpui::*;
use room::ChatRoom;
use std::sync::Arc;
use nostr_sdk::prelude::*;
use room::RoomPanel;
use crate::states::chat::Room;
@@ -20,14 +22,18 @@ pub struct ChatPanel {
focus_handle: FocusHandle,
// Room
id: SharedString,
room: View<ChatRoom>,
room: View<RoomPanel>,
metadata: Option<Metadata>,
}
impl ChatPanel {
pub fn new(room: &Arc<Room>, cx: &mut WindowContext) -> View<Self> {
let id = room.id.clone();
let title = room.title.clone();
let metadata = room.metadata.clone();
let room = cx.new_view(|cx| {
let view = ChatRoom::new(room, cx);
let view = RoomPanel::new(room, cx);
// Load messages
view.load(cx);
// Subscribe for new messages
@@ -37,21 +43,26 @@ impl ChatPanel {
});
cx.new_view(|cx| Self {
name: "Chat".into(),
name: title.unwrap_or("Untitled".into()),
closeable: true,
zoomable: true,
focus_handle: cx.focus_handle(),
id,
room,
metadata,
})
}
}
impl Panel for ChatPanel {
fn panel_name(&self) -> SharedString {
fn panel_id(&self) -> SharedString {
self.id.clone()
}
fn panel_metadata(&self) -> Option<Metadata> {
self.metadata.clone()
}
fn title(&self, _cx: &WindowContext) -> AnyElement {
self.name.clone().into_any_element()
}

View File

@@ -24,7 +24,7 @@ pub struct Messages {
items: Vec<RoomMessage>,
}
pub struct ChatRoom {
pub struct RoomPanel {
owner: PublicKey,
members: Arc<[PublicKey]>,
// Form
@@ -34,7 +34,7 @@ pub struct ChatRoom {
messages: Model<Messages>,
}
impl ChatRoom {
impl RoomPanel {
pub fn new(room: &Arc<Room>, cx: &mut ViewContext<'_, Self>) -> Self {
let members: Arc<[PublicKey]> = room.members.clone().into();
let owner = room.owner;
@@ -125,7 +125,7 @@ impl ChatRoom {
// Get user's metadata
let metadata = async_cx
.read_global::<MetadataRegistry, _>(|state, _cx| {
state.get(ev.pubkey)
state.get(&ev.pubkey)
})
.unwrap();
@@ -231,7 +231,7 @@ impl ChatRoom {
}
}
impl Render for ChatRoom {
impl Render for RoomPanel {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
v_flex()
.size_full()

View File

@@ -70,7 +70,7 @@ impl InboxItem {
}
pub fn action(&self, cx: &mut WindowContext<'_>) {
let room = Arc::new(Room::new(&self.event));
let room = Arc::new(Room::new(&self.event, cx));
cx.dispatch_action(Box::new(AddPanel {
room,

View File

@@ -40,7 +40,7 @@ impl LeftDock {
}
impl Panel for LeftDock {
fn panel_name(&self) -> SharedString {
fn panel_id(&self) -> SharedString {
"LeftDock".into()
}

View File

@@ -30,7 +30,7 @@ impl WelcomePanel {
}
impl Panel for WelcomePanel {
fn panel_name(&self) -> SharedString {
fn panel_id(&self) -> SharedString {
"WelcomePanel".into()
}

View File

@@ -15,6 +15,7 @@ smallvec.workspace = true
anyhow.workspace = true
itertools.workspace = true
chrono.workspace = true
nostr-sdk.workspace = true
paste = "1"
regex = "1"

View File

@@ -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()

View File

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

View File

@@ -25,7 +25,7 @@ pub struct StackPanel {
}
impl Panel for StackPanel {
fn panel_name(&self) -> SharedString {
fn panel_id(&self) -> SharedString {
"StackPanel".into()
}

View File

@@ -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()
}
}

View File

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

View File

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

View File

@@ -1,16 +1,16 @@
use gpui::*;
use nostr_sdk::prelude::*;
use prelude::FluentBuilder as _;
use crate::theme::ActiveTheme;
use crate::Selectable;
use gpui::prelude::FluentBuilder as _;
use gpui::{
div, px, AnyElement, Div, ElementId, InteractiveElement, IntoElement, ParentElement as _,
RenderOnce, Stateful, StatefulInteractiveElement, Styled, WindowContext,
};
#[derive(IntoElement)]
pub struct Tab {
id: ElementId,
base: Stateful<Div>,
label: AnyElement,
metadata: Option<Metadata>,
prefix: Option<AnyElement>,
suffix: Option<AnyElement>,
disabled: bool,
@@ -18,12 +18,18 @@ pub struct Tab {
}
impl Tab {
pub fn new(id: impl Into<ElementId>, label: impl IntoElement) -> Self {
pub fn new(
id: impl Into<ElementId>,
label: impl IntoElement,
metadata: Option<Metadata>,
) -> Self {
let id: ElementId = id.into();
Self {
id: id.clone(),
base: div().id(id).gap_1().py_1p5().px_3().h(px(30.)),
label: label.into_any_element(),
metadata,
disabled: false,
selected: false,
prefix: None,
@@ -100,7 +106,28 @@ impl RenderOnce for Tab {
.when_some(self.prefix, |this, prefix| {
this.child(prefix).text_color(text_color)
})
.child(div().text_ellipsis().child(self.label))
.child(
div()
.flex()
.items_center()
.gap_1()
.text_ellipsis()
.text_xs()
.child(div().when_some(self.metadata, |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(self.label),
)
.when_some(self.suffix, |this, suffix| this.child(suffix))
}
}