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

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