diff --git a/crates/chat/src/lib.rs b/crates/chat/src/lib.rs index 001e0e6..43b6448 100644 --- a/crates/chat/src/lib.rs +++ b/crates/chat/src/lib.rs @@ -333,18 +333,17 @@ impl ChatRegistry { /// Emit an open room event. /// /// If the room is new, add it to the registry. - pub fn emit_room(&mut self, room: WeakEntity, cx: &mut Context) { - if let Some(room) = room.upgrade() { - let id = room.read(cx).id; + pub fn emit_room(&mut self, room: &Entity, cx: &mut Context) { + // Get the room's ID. + let id = room.read(cx).id; - // If the room is new, add it to the registry. - if !self.rooms.iter().any(|r| r.read(cx).id == id) { - self.rooms.insert(0, room); - } - - // Emit the open room event. - cx.emit(ChatEvent::OpenRoom(id)); + // If the room is new, add it to the registry. + if !self.rooms.iter().any(|r| r.read(cx).id == id) { + self.rooms.insert(0, room.to_owned()); } + + // Emit the open room event. + cx.emit(ChatEvent::OpenRoom(id)); } /// Close a room. diff --git a/crates/coop/src/sidebar/mod.rs b/crates/coop/src/sidebar/mod.rs index 22faa11..942ef6d 100644 --- a/crates/coop/src/sidebar/mod.rs +++ b/crates/coop/src/sidebar/mod.rs @@ -75,7 +75,7 @@ pub struct Sidebar { contact_list: Entity>>, /// Async tasks - tasks: SmallVec<[Task<()>; 1]>, + tasks: SmallVec<[Task>; 1]>, /// Event subscriptions _subscriptions: SmallVec<[Subscription; 1]>, @@ -118,11 +118,11 @@ impl Sidebar { } } InputEvent::Focus => { - this.set_input_focus(cx); + this.set_input_focus(window, cx); this.get_contact_list(window, cx); } InputEvent::Blur => { - this.set_input_focus(cx); + this.set_input_focus(window, cx); } }; }), @@ -168,16 +168,16 @@ impl Sidebar { Ok(contacts) => { this.update(cx, |this, cx| { this.set_contact_list(contacts, cx); - }) - .ok(); + })?; } Err(e) => { cx.update(|window, cx| { window.push_notification(Notification::error(e.to_string()), cx); - }) - .ok(); + })?; } }; + + Ok(()) })); } @@ -309,7 +309,7 @@ impl Sidebar { } /// Get all selected public keys in the sidebar. - fn selected(&self, cx: &Context) -> HashSet { + fn get_selected(&self, cx: &Context) -> HashSet { self.selected_pkeys.read(cx).clone() } @@ -322,26 +322,24 @@ impl Sidebar { let signer_pkey = nostr.read(cx).signer_pkey(cx); // Get all selected public keys - let receivers = self.selected(cx); + let receivers = self.get_selected(cx); - let task: Task> = cx.spawn_in(window, async move |this, cx| { + self.tasks.push(cx.spawn_in(window, async move |this, cx| { let public_key = signer_pkey.await?; + // Create a new room and emit it + async_chat.update_in(cx, |this, _window, cx| { + let room = cx.new(|_| Room::new(public_key, receivers).kind(RoomKind::Ongoing)); + this.emit_room(&room, cx); + })?; + // Reset the find panel this.update_in(cx, |this, window, cx| { this.reset(window, cx); })?; - // Create a new room and emit it - async_chat.update_in(cx, |this, _window, cx| { - let room = cx.new(|_| Room::new(public_key, receivers).kind(RoomKind::Ongoing)); - this.emit_room(room.downgrade(), cx); - })?; - Ok(()) - }); - - task.detach(); + })); } /// Get the active filter. @@ -369,11 +367,11 @@ impl Sidebar { .enumerate() .map(|(ix, item)| { let room = item.read(cx); - let weak_room = item.downgrade(); + let room_clone = item.clone(); let public_key = room.display_member(cx).public_key(); let handler = cx.listener(move |_this, _ev, _window, cx| { ChatRegistry::global(cx).update(cx, |s, cx| { - s.emit_room(weak_room.clone(), cx); + s.emit_room(&room_clone, cx); }); }); @@ -628,12 +626,14 @@ impl Render for Sidebar { .gap_1() .overflow_y_hidden() .when(show_find_panel, |this| { - this.gap_2() + this.gap_3() .when_some(self.find_results.read(cx).as_ref(), |this, results| { this.child( v_flex() - .gap_2() + .gap_1() .flex_1() + .border_b_1() + .border_color(cx.theme().border_variant) .child( div() .text_xs() @@ -657,7 +657,7 @@ impl Render for Sidebar { .when_some(self.contact_list.read(cx).as_ref(), |this, contacts| { this.child( v_flex() - .gap_2() + .gap_1() .flex_1() .child( div() @@ -708,6 +708,7 @@ impl Render for Sidebar { .label(button_label) .primary() .small() + .shadow_lg() .on_click(cx.listener(move |this, _ev, window, cx| { this.create_room(window, cx); })),