wip: refactor
This commit is contained in:
@@ -305,23 +305,30 @@ async fn main() {
|
||||
let bounds = Bounds::centered(None, size(px(900.0), px(680.0)), cx);
|
||||
|
||||
let opts = WindowOptions {
|
||||
window_bounds: Some(WindowBounds::Windowed(bounds)),
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
titlebar: Some(TitlebarOptions {
|
||||
title: Some(SharedString::new_static(APP_NAME)),
|
||||
traffic_light_position: Some(point(px(9.0), px(9.0))),
|
||||
appears_transparent: true,
|
||||
}),
|
||||
window_bounds: Some(WindowBounds::Windowed(bounds)),
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
#[cfg(target_os = "linux")]
|
||||
window_background: WindowBackgroundAppearance::Transparent,
|
||||
#[cfg(target_os = "linux")]
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
kind: WindowKind::Normal,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
cx.open_window(opts, |cx| {
|
||||
let app_view = cx.new_view(AppView::new);
|
||||
|
||||
cx.set_window_title("Coop");
|
||||
cx.activate(true);
|
||||
cx.new_view(|cx| Root::new(app_view.into(), cx))
|
||||
})
|
||||
.unwrap();
|
||||
.expect("System error");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,31 @@ pub struct Room {
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub fn new(event: &Event, cx: &mut WindowContext<'_>) -> Self {
|
||||
pub fn new(
|
||||
owner: PublicKey,
|
||||
members: Vec<PublicKey>,
|
||||
last_seen: Timestamp,
|
||||
title: Option<SharedString>,
|
||||
cx: &mut WindowContext<'_>,
|
||||
) -> Self {
|
||||
// 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,
|
||||
is_initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(event: &Event, cx: &mut WindowContext<'_>) -> Self {
|
||||
let owner = event.pubkey;
|
||||
let last_seen = event.created_at;
|
||||
|
||||
@@ -36,21 +60,7 @@ impl Room {
|
||||
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,
|
||||
is_initialized: false,
|
||||
}
|
||||
Self::new(owner, members, last_seen, title, cx)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ impl InboxListItem {
|
||||
}
|
||||
|
||||
pub fn action(&self, cx: &mut WindowContext<'_>) {
|
||||
let room = Arc::new(Room::new(&self.event, cx));
|
||||
let room = Arc::new(Room::parse(&self.event, cx));
|
||||
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
panel: PanelKind::Room(room),
|
||||
|
||||
253
crates/app/src/views/sidebar/contact_list.rs
Normal file
253
crates/app/src/views/sidebar/contact_list.rs
Normal file
@@ -0,0 +1,253 @@
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
use gpui::{
|
||||
div, img, impl_actions, list, px, Context, ElementId, FocusHandle, InteractiveElement,
|
||||
IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, RenderOnce,
|
||||
SharedString, StatefulInteractiveElement, Styled, ViewContext, WindowContext,
|
||||
};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Deserialize;
|
||||
use ui::{
|
||||
prelude::FluentBuilder,
|
||||
theme::{ActiveTheme, Colorize},
|
||||
Icon, IconName, Selectable, StyledExt,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
constants::IMAGE_SERVICE, get_client, states::account::AccountRegistry, utils::show_npub,
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
struct SelectContact(PublicKey);
|
||||
|
||||
impl_actions!(contacts, [SelectContact]);
|
||||
|
||||
#[derive(Clone, IntoElement)]
|
||||
struct ContactListItem {
|
||||
id: ElementId,
|
||||
public_key: PublicKey,
|
||||
metadata: Option<Metadata>,
|
||||
selected: bool,
|
||||
}
|
||||
|
||||
impl ContactListItem {
|
||||
pub fn new(public_key: PublicKey, metadata: Option<Metadata>) -> Self {
|
||||
let id = SharedString::from(public_key.to_hex()).into();
|
||||
|
||||
Self {
|
||||
id,
|
||||
public_key,
|
||||
metadata,
|
||||
selected: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Selectable for ContactListItem {
|
||||
fn selected(mut self, selected: bool) -> Self {
|
||||
self.selected = selected;
|
||||
self
|
||||
}
|
||||
|
||||
fn element_id(&self) -> &gpui::ElementId {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for ContactListItem {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
let fallback = show_npub(self.public_key, 16);
|
||||
let mut content = div().flex().items_center().gap_2().text_sm();
|
||||
|
||||
if let Some(metadata) = self.metadata {
|
||||
content = content
|
||||
.map(|this| {
|
||||
if let Some(picture) = metadata.picture {
|
||||
this.flex_shrink_0().child(
|
||||
img(format!(
|
||||
"{}/?url={}&w=72&h=72&fit=cover&mask=circle&n=-1",
|
||||
IMAGE_SERVICE, picture
|
||||
))
|
||||
.size_6(),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
}
|
||||
})
|
||||
.map(|this| {
|
||||
if let Some(display_name) = metadata.display_name {
|
||||
this.flex_1().child(display_name)
|
||||
} else {
|
||||
this.flex_1().child(fallback)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
content = content
|
||||
.child(img("brand/avatar.png").size_6().rounded_full())
|
||||
.child(fallback)
|
||||
}
|
||||
|
||||
div()
|
||||
.id(self.id)
|
||||
.w_full()
|
||||
.h_8()
|
||||
.px_1()
|
||||
.rounded_md()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_between()
|
||||
.child(content)
|
||||
.when(self.selected, |this| {
|
||||
this.child(
|
||||
Icon::new(IconName::CircleCheck)
|
||||
.size_4()
|
||||
.text_color(cx.theme().primary),
|
||||
)
|
||||
})
|
||||
.hover(|this| {
|
||||
this.bg(cx.theme().muted.darken(0.1))
|
||||
.text_color(cx.theme().muted_foreground.darken(0.1))
|
||||
})
|
||||
.on_click(move |_, cx| {
|
||||
cx.dispatch_action(Box::new(SelectContact(self.public_key)));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Contacts {
|
||||
#[allow(dead_code)]
|
||||
count: usize,
|
||||
items: Vec<ContactListItem>,
|
||||
}
|
||||
|
||||
pub struct ContactList {
|
||||
list: ListState,
|
||||
contacts: Model<BTreeSet<Profile>>,
|
||||
selected: HashSet<PublicKey>,
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl ContactList {
|
||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let list = ListState::new(0, ListAlignment::Top, Pixels(50.), move |_, _| {
|
||||
div().into_any_element()
|
||||
});
|
||||
|
||||
let contacts = cx.new_model(|_| BTreeSet::new());
|
||||
let async_contacts = contacts.clone();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn({
|
||||
let client = get_client();
|
||||
let current_user = cx.global::<AccountRegistry>().get();
|
||||
|
||||
async move {
|
||||
if let Some(public_key) = current_user {
|
||||
if let Ok(profiles) = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().contacts(public_key).await })
|
||||
.await
|
||||
{
|
||||
_ = async_cx.update_model(&async_contacts, |model, cx| {
|
||||
*model = profiles;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.observe(&contacts, |this, model, cx| {
|
||||
let profiles = model.read(cx).clone();
|
||||
let contacts = Contacts {
|
||||
count: profiles.len(),
|
||||
items: profiles
|
||||
.into_iter()
|
||||
.map(|contact| {
|
||||
ContactListItem::new(contact.public_key(), Some(contact.metadata()))
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
this.list = ListState::new(
|
||||
contacts.items.len(),
|
||||
ListAlignment::Top,
|
||||
Pixels(50.),
|
||||
move |idx, _cx| {
|
||||
let item = contacts.items.get(idx).unwrap().clone();
|
||||
div().child(item).into_any_element()
|
||||
},
|
||||
);
|
||||
|
||||
cx.notify();
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
list,
|
||||
contacts,
|
||||
selected: HashSet::new(),
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Vec<PublicKey> {
|
||||
self.selected.clone().into_iter().collect()
|
||||
}
|
||||
|
||||
fn on_action_select(&mut self, action: &SelectContact, cx: &mut ViewContext<Self>) {
|
||||
self.selected.insert(action.0);
|
||||
|
||||
let profiles = self.contacts.read(cx).clone();
|
||||
let contacts = Contacts {
|
||||
count: profiles.len(),
|
||||
items: profiles
|
||||
.into_iter()
|
||||
.map(|contact| {
|
||||
let public_key = contact.public_key();
|
||||
let metadata = contact.metadata();
|
||||
|
||||
ContactListItem::new(public_key, Some(metadata))
|
||||
.selected(self.selected.contains(&public_key))
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
self.list = ListState::new(
|
||||
contacts.items.len(),
|
||||
ListAlignment::Top,
|
||||
Pixels(50.),
|
||||
move |idx, _cx| {
|
||||
let item = contacts.items.get(idx).unwrap().clone();
|
||||
div().child(item).into_any_element()
|
||||
},
|
||||
);
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ContactList {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.track_focus(&self.focus_handle)
|
||||
.on_action(cx.listener(Self::on_action_select))
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_1()
|
||||
.child(div().font_semibold().child("Contacts"))
|
||||
.child(
|
||||
div()
|
||||
.p_1()
|
||||
.bg(cx.theme().muted)
|
||||
.text_color(cx.theme().muted_foreground)
|
||||
.rounded_lg()
|
||||
.child(list(self.list.clone()).h(px(300.))),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,25 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use contact_list::ContactList;
|
||||
use gpui::*;
|
||||
use nostr_sdk::Timestamp;
|
||||
use rnglib::{Language, RNG};
|
||||
use ui::{
|
||||
button::{Button, ButtonVariants},
|
||||
button::{Button, ButtonRounded, ButtonVariants},
|
||||
dock::{Panel, PanelEvent, PanelState},
|
||||
popup_menu::PopupMenu,
|
||||
scroll::ScrollbarAxis,
|
||||
v_flex, ContextModal, Icon, IconName, Sizable, StyledExt,
|
||||
};
|
||||
|
||||
use super::inbox::Inbox;
|
||||
use crate::views::app::{AddPanel, PanelKind};
|
||||
use crate::states::{account::AccountRegistry, chat::Room};
|
||||
|
||||
use super::{
|
||||
app::{AddPanel, PanelKind},
|
||||
inbox::Inbox,
|
||||
};
|
||||
|
||||
mod contact_list;
|
||||
|
||||
pub struct Sidebar {
|
||||
// Panel
|
||||
@@ -38,6 +49,44 @@ impl Sidebar {
|
||||
inbox,
|
||||
}
|
||||
}
|
||||
|
||||
fn show_compose(&mut self, cx: &mut ViewContext<Self>) {
|
||||
let contact_list = cx.new_view(ContactList::new);
|
||||
|
||||
cx.open_modal(move |modal, _cx| {
|
||||
modal.child(contact_list.clone()).footer(
|
||||
div().flex().gap_2().child(
|
||||
Button::new("create")
|
||||
.label("Create DM")
|
||||
.primary()
|
||||
.rounded(ButtonRounded::Large)
|
||||
.w_full()
|
||||
.on_click({
|
||||
let contact_list = contact_list.clone();
|
||||
move |_, cx| {
|
||||
let members = contact_list.model.read(cx).selected();
|
||||
let owner = cx.global::<AccountRegistry>().get().unwrap();
|
||||
let rng = RNG::from(&Language::Roman);
|
||||
let name = rng.generate_names(2, true).join("-").to_lowercase();
|
||||
|
||||
let room = Arc::new(Room::new(
|
||||
owner,
|
||||
members,
|
||||
Timestamp::now(),
|
||||
Some(name.into()),
|
||||
cx,
|
||||
));
|
||||
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
panel: PanelKind::Room(room),
|
||||
position: ui::dock::DockPlacement::Center,
|
||||
}))
|
||||
}
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for Sidebar {
|
||||
@@ -79,7 +128,7 @@ impl FocusableView for Sidebar {
|
||||
}
|
||||
|
||||
impl Render for Sidebar {
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.scrollable(self.view_id, ScrollbarAxis::Vertical)
|
||||
.py_3()
|
||||
@@ -89,15 +138,13 @@ impl Render for Sidebar {
|
||||
.px_2()
|
||||
.gap_1()
|
||||
.child(
|
||||
Button::new("new")
|
||||
Button::new("compose")
|
||||
.small()
|
||||
.ghost()
|
||||
.not_centered()
|
||||
.icon(Icon::new(IconName::ComposeFill))
|
||||
.label("New Message")
|
||||
.on_click(|_, cx| {
|
||||
cx.open_modal(move |modal, _| modal.child("TODO"));
|
||||
}),
|
||||
.on_click(cx.listener(|this, _, cx| this.show_compose(cx))),
|
||||
)
|
||||
.child(
|
||||
Button::new("contacts")
|
||||
@@ -105,13 +152,7 @@ impl Render for Sidebar {
|
||||
.ghost()
|
||||
.not_centered()
|
||||
.icon(Icon::new(IconName::GroupFill))
|
||||
.label("Contacts")
|
||||
.on_click(|_, cx| {
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
panel: PanelKind::Contact,
|
||||
position: ui::dock::DockPlacement::Center,
|
||||
}))
|
||||
}),
|
||||
.label("Contacts"),
|
||||
),
|
||||
)
|
||||
.child(self.inbox.clone())
|
||||
Reference in New Issue
Block a user