chore: Upgrade to GPUI3 (#6)
* wip: gpui3 * wip: gpui3 * chore: fix clippy
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use common::utils::{random_name, room_hash};
|
||||
use gpui::{
|
||||
div, img, impl_internal_actions, px, uniform_list, Context, FocusHandle, InteractiveElement,
|
||||
IntoElement, Model, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
|
||||
View, ViewContext, VisualContext, WindowContext,
|
||||
div, img, impl_internal_actions, px, uniform_list, App, AppContext, Context, Entity,
|
||||
FocusHandle, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
||||
StatefulInteractiveElement, Styled, Window,
|
||||
};
|
||||
use nostr_sdk::prelude::*;
|
||||
use registry::{app::AppRegistry, contact::Contact, room::Room};
|
||||
@@ -24,48 +24,54 @@ struct SelectContact(PublicKey);
|
||||
impl_internal_actions!(contacts, [SelectContact]);
|
||||
|
||||
pub struct Compose {
|
||||
title_input: View<TextInput>,
|
||||
message_input: View<TextInput>,
|
||||
user_input: View<TextInput>,
|
||||
contacts: Model<Option<Vec<Contact>>>,
|
||||
selected: Model<HashSet<PublicKey>>,
|
||||
title_input: Entity<TextInput>,
|
||||
message_input: Entity<TextInput>,
|
||||
user_input: Entity<TextInput>,
|
||||
contacts: Entity<Option<Vec<Contact>>>,
|
||||
selected: Entity<HashSet<PublicKey>>,
|
||||
focus_handle: FocusHandle,
|
||||
is_loading: bool,
|
||||
}
|
||||
|
||||
impl Compose {
|
||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let contacts = cx.new_model(|_| None);
|
||||
let selected = cx.new_model(|_| HashSet::new());
|
||||
pub fn new(window: &mut Window, cx: &mut Context<'_, Self>) -> Self {
|
||||
let contacts = cx.new(|_| None);
|
||||
let selected = cx.new(|_| HashSet::new());
|
||||
|
||||
let user_input = cx.new_view(|cx| {
|
||||
TextInput::new(cx)
|
||||
let user_input = cx.new(|cx| {
|
||||
TextInput::new(window, cx)
|
||||
.text_size(ui::Size::Small)
|
||||
.small()
|
||||
.placeholder("npub1...")
|
||||
});
|
||||
|
||||
let title_input = cx.new_view(|cx| {
|
||||
let title_input = cx.new(|cx| {
|
||||
let name = random_name(2);
|
||||
let mut input = TextInput::new(cx).appearance(false).text_size(Size::XSmall);
|
||||
let mut input = TextInput::new(window, cx)
|
||||
.appearance(false)
|
||||
.text_size(Size::XSmall);
|
||||
|
||||
input.set_placeholder("Family... . (Optional)");
|
||||
input.set_text(name, cx);
|
||||
input.set_text(name, window, cx);
|
||||
input
|
||||
});
|
||||
|
||||
let message_input = cx.new_view(|cx| {
|
||||
TextInput::new(cx)
|
||||
let message_input = cx.new(|cx| {
|
||||
TextInput::new(window, cx)
|
||||
.appearance(false)
|
||||
.text_size(Size::XSmall)
|
||||
.placeholder("Hello... (Optional)")
|
||||
});
|
||||
|
||||
cx.subscribe(&user_input, move |this, _, input_event, cx| {
|
||||
if let InputEvent::PressEnter = input_event {
|
||||
this.add(cx);
|
||||
}
|
||||
})
|
||||
cx.subscribe_in(
|
||||
&user_input,
|
||||
window,
|
||||
move |this, _, input_event, window, cx| {
|
||||
if let InputEvent::PressEnter = input_event {
|
||||
this.add(window, cx);
|
||||
}
|
||||
},
|
||||
)
|
||||
.detach();
|
||||
|
||||
cx.spawn(|this, mut async_cx| {
|
||||
@@ -89,7 +95,7 @@ impl Compose {
|
||||
|
||||
if let Ok(contacts) = query {
|
||||
if let Some(view) = this.upgrade() {
|
||||
_ = async_cx.update_view(&view, |this, cx| {
|
||||
_ = async_cx.update_entity(&view, |this, cx| {
|
||||
this.contacts.update(cx, |this, cx| {
|
||||
*this = Some(contacts);
|
||||
cx.notify();
|
||||
@@ -114,8 +120,8 @@ impl Compose {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn room(&self, cx: &WindowContext) -> Option<Room> {
|
||||
let current_user = cx.global::<AppRegistry>().current_user(cx);
|
||||
pub fn room(&self, window: &Window, cx: &App) -> Option<Room> {
|
||||
let current_user = cx.global::<AppRegistry>().current_user(window, cx);
|
||||
|
||||
if let Some(current_user) = current_user {
|
||||
// Convert selected pubkeys into nostr tags
|
||||
@@ -151,7 +157,7 @@ impl Compose {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(&self, cx: &WindowContext) -> SharedString {
|
||||
pub fn label(&self, window: &Window, cx: &App) -> SharedString {
|
||||
if self.selected.read(cx).len() > 1 {
|
||||
"Create Group DM".into()
|
||||
} else {
|
||||
@@ -159,7 +165,7 @@ impl Compose {
|
||||
}
|
||||
}
|
||||
|
||||
fn add(&mut self, cx: &mut ViewContext<Self>) {
|
||||
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let content = self.user_input.read(cx).text().to_string();
|
||||
let input = self.user_input.downgrade();
|
||||
|
||||
@@ -183,7 +189,7 @@ impl Compose {
|
||||
|
||||
if let Ok(metadata) = query {
|
||||
if let Some(view) = this.upgrade() {
|
||||
_ = async_cx.update_view(&view, |this, cx| {
|
||||
_ = async_cx.update_entity(&view, |this, cx| {
|
||||
this.contacts.update(cx, |this, cx| {
|
||||
if let Some(members) = this {
|
||||
members.insert(0, Contact::new(public_key, metadata));
|
||||
@@ -202,8 +208,8 @@ impl Compose {
|
||||
}
|
||||
|
||||
if let Some(input) = input.upgrade() {
|
||||
_ = async_cx.update_view(&input, |input, cx| {
|
||||
input.set_text("", cx);
|
||||
_ = async_cx.update_entity(&input, |input, cx| {
|
||||
// input.set_text("", window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -214,7 +220,12 @@ impl Compose {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_action_select(&mut self, action: &SelectContact, cx: &mut ViewContext<Self>) {
|
||||
fn on_action_select(
|
||||
&mut self,
|
||||
action: &SelectContact,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.selected.update(cx, |this, cx| {
|
||||
if this.contains(&action.0) {
|
||||
this.remove(&action.0);
|
||||
@@ -227,7 +238,7 @@ impl Compose {
|
||||
}
|
||||
|
||||
impl Render for Compose {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let msg =
|
||||
"Start a conversation with someone using their npub or NIP-05 (like foo@bar.com).";
|
||||
|
||||
@@ -291,7 +302,9 @@ impl Render for Compose {
|
||||
.small()
|
||||
.rounded(ButtonRounded::Size(px(9999.)))
|
||||
.loading(self.is_loading)
|
||||
.on_click(cx.listener(|this, _, cx| this.add(cx))),
|
||||
.on_click(
|
||||
cx.listener(|this, _, window, cx| this.add(window, cx)),
|
||||
),
|
||||
)
|
||||
.child(self.user_input.clone()),
|
||||
)
|
||||
@@ -299,10 +312,10 @@ impl Render for Compose {
|
||||
if let Some(contacts) = self.contacts.read(cx).clone() {
|
||||
this.child(
|
||||
uniform_list(
|
||||
cx.view().clone(),
|
||||
cx.model().clone(),
|
||||
"contacts",
|
||||
contacts.len(),
|
||||
move |this, range, cx| {
|
||||
move |this, range, window, cx| {
|
||||
let selected = this.selected.read(cx);
|
||||
let mut items = Vec::new();
|
||||
|
||||
@@ -348,9 +361,9 @@ impl Render for Compose {
|
||||
.base
|
||||
.step(cx, ColorScaleStep::THREE))
|
||||
})
|
||||
.on_click(move |_, cx| {
|
||||
cx.dispatch_action(Box::new(
|
||||
SelectContact(item.public_key()),
|
||||
.on_click(move |_, window, cx| {
|
||||
cx.dispatch_action(&SelectContact(
|
||||
item.public_key(),
|
||||
));
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::views::app::{AddPanel, PanelKind};
|
||||
use common::utils::message_ago;
|
||||
use gpui::{
|
||||
div, img, percentage, prelude::FluentBuilder, px, InteractiveElement, IntoElement,
|
||||
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, ViewContext,
|
||||
div, img, percentage, prelude::FluentBuilder, px, Context, InteractiveElement, IntoElement,
|
||||
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window,
|
||||
};
|
||||
use registry::chat::ChatRegistry;
|
||||
use ui::{
|
||||
@@ -18,7 +18,7 @@ pub struct Inbox {
|
||||
}
|
||||
|
||||
impl Inbox {
|
||||
pub fn new(_cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
pub fn new(_window: &mut Window, _cx: &mut Context<'_, Self>) -> Self {
|
||||
Self {
|
||||
label: "Inbox".into(),
|
||||
is_collapsed: false,
|
||||
@@ -38,7 +38,7 @@ impl Inbox {
|
||||
})
|
||||
}
|
||||
|
||||
fn render_item(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render_item(&self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let weak_model = cx.global::<ChatRegistry>().inbox();
|
||||
|
||||
if let Some(model) = weak_model.upgrade() {
|
||||
@@ -91,8 +91,8 @@ impl Inbox {
|
||||
.text_color(cx.theme().base.step(cx, ColorScaleStep::ELEVEN))
|
||||
.child(ago),
|
||||
)
|
||||
.on_click(cx.listener(move |this, _, cx| {
|
||||
this.action(id, cx);
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.action(id, window, cx);
|
||||
}))
|
||||
}))
|
||||
}
|
||||
@@ -102,11 +102,13 @@ impl Inbox {
|
||||
}
|
||||
}
|
||||
|
||||
fn action(&self, id: u64, cx: &mut ViewContext<Self>) {
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
fn action(&self, id: u64, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
let action = AddPanel {
|
||||
panel: PanelKind::Room(id),
|
||||
position: DockPlacement::Center,
|
||||
}))
|
||||
};
|
||||
|
||||
cx.dispatch_action(&action)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +124,7 @@ impl Collapsible for Inbox {
|
||||
}
|
||||
|
||||
impl Render for Inbox {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.px_2()
|
||||
.gap_1()
|
||||
@@ -145,11 +147,13 @@ impl Render for Inbox {
|
||||
)
|
||||
.child(self.label.clone())
|
||||
.hover(|this| this.bg(cx.theme().base.step(cx, ColorScaleStep::THREE)))
|
||||
.on_click(cx.listener(move |view, _event, cx| {
|
||||
.on_click(cx.listener(move |view, _event, _window, cx| {
|
||||
view.is_collapsed = !view.is_collapsed;
|
||||
cx.notify();
|
||||
})),
|
||||
)
|
||||
.when(!self.is_collapsed, |this| this.child(self.render_item(cx)))
|
||||
.when(!self.is_collapsed, |this| {
|
||||
this.child(self.render_item(window, cx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
use crate::views::sidebar::inbox::Inbox;
|
||||
use compose::Compose;
|
||||
use gpui::{
|
||||
div, px, AnyElement, AppContext, BorrowAppContext, Entity, EntityId, EventEmitter, FocusHandle,
|
||||
FocusableView, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
||||
StatefulInteractiveElement, Styled, View, ViewContext, VisualContext, WindowContext,
|
||||
div, px, AnyElement, App, AppContext, BorrowAppContext, Context, Entity, EntityId,
|
||||
EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
|
||||
SharedString, StatefulInteractiveElement, Styled, Window,
|
||||
};
|
||||
use registry::chat::ChatRegistry;
|
||||
use ui::{
|
||||
button::{Button, ButtonRounded, ButtonVariants},
|
||||
dock_area::{
|
||||
panel::{Panel, PanelEvent},
|
||||
state::PanelState,
|
||||
},
|
||||
dock_area::panel::{Panel, PanelEvent},
|
||||
popup_menu::PopupMenu,
|
||||
scroll::ScrollbarAxis,
|
||||
theme::{scale::ColorScaleStep, ActiveTheme},
|
||||
@@ -28,33 +25,33 @@ pub struct Sidebar {
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Dock
|
||||
inbox: View<Inbox>,
|
||||
inbox: Entity<Inbox>,
|
||||
view_id: EntityId,
|
||||
}
|
||||
|
||||
impl Sidebar {
|
||||
pub fn new(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::view)
|
||||
pub fn new(window: &mut Window, cx: &mut App) -> Entity<Self> {
|
||||
cx.new(|cx| Self::view(window, cx))
|
||||
}
|
||||
|
||||
fn view(cx: &mut ViewContext<Self>) -> Self {
|
||||
let inbox = cx.new_view(Inbox::new);
|
||||
fn view(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let inbox = cx.new(|cx| Inbox::new(window, cx));
|
||||
|
||||
Self {
|
||||
name: "Sidebar".into(),
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
view_id: cx.view().entity_id(),
|
||||
view_id: cx.model().entity_id(),
|
||||
inbox,
|
||||
}
|
||||
}
|
||||
|
||||
fn show_compose(&mut self, cx: &mut ViewContext<Self>) {
|
||||
let compose = cx.new_view(Compose::new);
|
||||
fn show_compose(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let compose = cx.new(|cx| Compose::new(window, cx));
|
||||
|
||||
cx.open_modal(move |modal, cx| {
|
||||
let label = compose.read(cx).label(cx);
|
||||
window.open_modal(cx, move |modal, window, cx| {
|
||||
let label = compose.read(cx).label(window, cx);
|
||||
|
||||
modal
|
||||
.title("Direct Messages")
|
||||
@@ -72,13 +69,13 @@ impl Sidebar {
|
||||
.bold()
|
||||
.rounded(ButtonRounded::Large)
|
||||
.w_full()
|
||||
.on_click(cx.listener_for(&compose, |this, _, cx| {
|
||||
if let Some(room) = this.room(cx) {
|
||||
.on_click(window.listener_for(&compose, |this, _, window, cx| {
|
||||
if let Some(room) = this.room(window, cx) {
|
||||
cx.update_global::<ChatRegistry, _>(|this, cx| {
|
||||
this.new_room(room, cx);
|
||||
});
|
||||
|
||||
cx.close_modal();
|
||||
window.close_modal(cx);
|
||||
}
|
||||
})),
|
||||
),
|
||||
@@ -92,41 +89,37 @@ impl Panel for Sidebar {
|
||||
"Sidebar".into()
|
||||
}
|
||||
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
fn title(&self, _cx: &App) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
fn closeable(&self, _cx: &WindowContext) -> bool {
|
||||
fn closeable(&self, _cx: &App) -> bool {
|
||||
self.closeable
|
||||
}
|
||||
|
||||
fn zoomable(&self, _cx: &WindowContext) -> bool {
|
||||
fn zoomable(&self, _cx: &App) -> bool {
|
||||
self.zoomable
|
||||
}
|
||||
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &WindowContext) -> PopupMenu {
|
||||
fn popup_menu(&self, menu: PopupMenu, _cx: &App) -> PopupMenu {
|
||||
menu.track_focus(&self.focus_handle)
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _cx: &WindowContext) -> Vec<Button> {
|
||||
fn toolbar_buttons(&self, _window: &Window, _cx: &App) -> Vec<Button> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn dump(&self, _cx: &AppContext) -> PanelState {
|
||||
PanelState::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<PanelEvent> for Sidebar {}
|
||||
|
||||
impl FocusableView for Sidebar {
|
||||
fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
|
||||
impl Focusable for Sidebar {
|
||||
fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Sidebar {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.scrollable(self.view_id, ScrollbarAxis::Vertical)
|
||||
.py_3()
|
||||
@@ -159,7 +152,7 @@ impl Render for Sidebar {
|
||||
)
|
||||
.child("New Message")
|
||||
.hover(|this| this.bg(cx.theme().base.step(cx, ColorScaleStep::THREE)))
|
||||
.on_click(cx.listener(|this, _, cx| this.show_compose(cx))),
|
||||
.on_click(cx.listener(|this, _, window, cx| this.show_compose(window, cx))),
|
||||
),
|
||||
)
|
||||
.child(self.inbox.clone())
|
||||
|
||||
Reference in New Issue
Block a user