From c04cb0286add5d6ad0cfd9c972325b4ae131b823 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 28 Apr 2023 15:56:41 +0200 Subject: [PATCH] Remove `Dismiss` and `RespondToContactRequest` internal actions --- crates/collab_ui/src/collab_ui.rs | 1 - crates/collab_ui/src/contact_notification.rs | 35 ++++++-------------- crates/collab_ui/src/notifications.rs | 24 +++++++------- 3 files changed, 22 insertions(+), 38 deletions(-) diff --git a/crates/collab_ui/src/collab_ui.rs b/crates/collab_ui/src/collab_ui.rs index 093392dd96..3f3998fb6d 100644 --- a/crates/collab_ui/src/collab_ui.rs +++ b/crates/collab_ui/src/collab_ui.rs @@ -20,7 +20,6 @@ actions!(collab, [ToggleScreenSharing]); pub fn init(app_state: &Arc, cx: &mut AppContext) { collab_titlebar_item::init(cx); - contact_notification::init(cx); contact_list::init(cx); contact_finder::init(cx); contacts_popover::init(cx); diff --git a/crates/collab_ui/src/contact_notification.rs b/crates/collab_ui/src/contact_notification.rs index 9ad8a22485..a998be8efd 100644 --- a/crates/collab_ui/src/contact_notification.rs +++ b/crates/collab_ui/src/contact_notification.rs @@ -2,18 +2,9 @@ use std::sync::Arc; use crate::notifications::render_user_notification; use client::{ContactEventKind, User, UserStore}; -use gpui::{ - elements::*, impl_internal_actions, AppContext, Entity, ModelHandle, View, ViewContext, -}; +use gpui::{elements::*, Entity, ModelHandle, View, ViewContext}; use workspace::notifications::Notification; -impl_internal_actions!(contact_notifications, [Dismiss, RespondToContactRequest]); - -pub fn init(cx: &mut AppContext) { - cx.add_action(ContactNotification::dismiss); - cx.add_action(ContactNotification::respond_to_contact_request); -} - pub struct ContactNotification { user_store: ModelHandle, user: Arc, @@ -48,20 +39,18 @@ impl View for ContactNotification { self.user.clone(), "wants to add you as a contact", Some("They won't be alerted if you decline."), - Dismiss(self.user.id), + |notification, cx| notification.dismiss(cx), vec![ ( "Decline", - Box::new(RespondToContactRequest { - user_id: self.user.id, - accept: false, + Box::new(|notification, cx| { + notification.respond_to_contact_request(false, cx) }), ), ( "Accept", - Box::new(RespondToContactRequest { - user_id: self.user.id, - accept: true, + Box::new(|notification, cx| { + notification.respond_to_contact_request(true, cx) }), ), ], @@ -71,7 +60,7 @@ impl View for ContactNotification { self.user.clone(), "accepted your contact request", None, - Dismiss(self.user.id), + |notification, cx| notification.dismiss(cx), vec![], cx, ), @@ -113,7 +102,7 @@ impl ContactNotification { } } - fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext) { + fn dismiss(&mut self, cx: &mut ViewContext) { self.user_store.update(cx, |store, cx| { store .dismiss_contact_request(self.user.id, cx) @@ -122,14 +111,10 @@ impl ContactNotification { cx.emit(Event::Dismiss); } - fn respond_to_contact_request( - &mut self, - action: &RespondToContactRequest, - cx: &mut ViewContext, - ) { + fn respond_to_contact_request(&mut self, accept: bool, cx: &mut ViewContext) { self.user_store .update(cx, |store, cx| { - store.respond_to_contact_request(action.user_id, action.accept, cx) + store.respond_to_contact_request(self.user.id, accept, cx) }) .detach(); } diff --git a/crates/collab_ui/src/notifications.rs b/crates/collab_ui/src/notifications.rs index 66afea34c1..1dec5a8411 100644 --- a/crates/collab_ui/src/notifications.rs +++ b/crates/collab_ui/src/notifications.rs @@ -2,7 +2,7 @@ use client::User; use gpui::{ elements::*, platform::{CursorStyle, MouseButton}, - Action, AnyElement, Element, View, ViewContext, + AnyElement, Element, View, ViewContext, }; use settings::Settings; use std::sync::Arc; @@ -10,14 +10,18 @@ use std::sync::Arc; enum Dismiss {} enum Button {} -pub fn render_user_notification( +pub fn render_user_notification( user: Arc, title: &'static str, body: Option<&'static str>, - dismiss_action: A, - buttons: Vec<(&'static str, Box)>, + on_dismiss: F, + buttons: Vec<(&'static str, Box)>)>, cx: &mut ViewContext, -) -> AnyElement { +) -> AnyElement +where + F: 'static + Fn(&mut V, &mut ViewContext), + V: View, +{ let theme = cx.global::().theme.clone(); let theme = &theme.contact_notification; @@ -64,9 +68,7 @@ pub fn render_user_notification( }) .with_cursor_style(CursorStyle::PointingHand) .with_padding(Padding::uniform(5.)) - .on_click(MouseButton::Left, move |_, _, cx| { - cx.dispatch_any_action(dismiss_action.boxed_clone()) - }) + .on_click(MouseButton::Left, move |_, view, cx| on_dismiss(view, cx)) .aligned() .constrained() .with_height( @@ -90,7 +92,7 @@ pub fn render_user_notification( Some( Flex::row() .with_children(buttons.into_iter().enumerate().map( - |(ix, (message, action))| { + |(ix, (message, handler))| { MouseEventHandler::::new(ix, cx, |state, _| { let button = theme.button.style_for(state, false); Label::new(message, button.text.clone()) @@ -98,9 +100,7 @@ pub fn render_user_notification( .with_style(button.container) }) .with_cursor_style(CursorStyle::PointingHand) - .on_click(MouseButton::Left, move |_, _, cx| { - cx.dispatch_any_action(action.boxed_clone()) - }) + .on_click(MouseButton::Left, move |_, view, cx| handler(view, cx)) }, )) .aligned()