Remove Dismiss and RespondToContactRequest internal actions

This commit is contained in:
Antonio Scandurra 2023-04-28 15:56:41 +02:00
parent 0469e25de6
commit c04cb0286a
3 changed files with 22 additions and 38 deletions

View file

@ -20,7 +20,6 @@ actions!(collab, [ToggleScreenSharing]);
pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) { pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
collab_titlebar_item::init(cx); collab_titlebar_item::init(cx);
contact_notification::init(cx);
contact_list::init(cx); contact_list::init(cx);
contact_finder::init(cx); contact_finder::init(cx);
contacts_popover::init(cx); contacts_popover::init(cx);

View file

@ -2,18 +2,9 @@ use std::sync::Arc;
use crate::notifications::render_user_notification; use crate::notifications::render_user_notification;
use client::{ContactEventKind, User, UserStore}; use client::{ContactEventKind, User, UserStore};
use gpui::{ use gpui::{elements::*, Entity, ModelHandle, View, ViewContext};
elements::*, impl_internal_actions, AppContext, Entity, ModelHandle, View, ViewContext,
};
use workspace::notifications::Notification; 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 { pub struct ContactNotification {
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
user: Arc<User>, user: Arc<User>,
@ -48,20 +39,18 @@ impl View for ContactNotification {
self.user.clone(), self.user.clone(),
"wants to add you as a contact", "wants to add you as a contact",
Some("They won't be alerted if you decline."), Some("They won't be alerted if you decline."),
Dismiss(self.user.id), |notification, cx| notification.dismiss(cx),
vec![ vec![
( (
"Decline", "Decline",
Box::new(RespondToContactRequest { Box::new(|notification, cx| {
user_id: self.user.id, notification.respond_to_contact_request(false, cx)
accept: false,
}), }),
), ),
( (
"Accept", "Accept",
Box::new(RespondToContactRequest { Box::new(|notification, cx| {
user_id: self.user.id, notification.respond_to_contact_request(true, cx)
accept: true,
}), }),
), ),
], ],
@ -71,7 +60,7 @@ impl View for ContactNotification {
self.user.clone(), self.user.clone(),
"accepted your contact request", "accepted your contact request",
None, None,
Dismiss(self.user.id), |notification, cx| notification.dismiss(cx),
vec![], vec![],
cx, cx,
), ),
@ -113,7 +102,7 @@ impl ContactNotification {
} }
} }
fn dismiss(&mut self, _: &Dismiss, cx: &mut ViewContext<Self>) { fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
self.user_store.update(cx, |store, cx| { self.user_store.update(cx, |store, cx| {
store store
.dismiss_contact_request(self.user.id, cx) .dismiss_contact_request(self.user.id, cx)
@ -122,14 +111,10 @@ impl ContactNotification {
cx.emit(Event::Dismiss); cx.emit(Event::Dismiss);
} }
fn respond_to_contact_request( fn respond_to_contact_request(&mut self, accept: bool, cx: &mut ViewContext<Self>) {
&mut self,
action: &RespondToContactRequest,
cx: &mut ViewContext<Self>,
) {
self.user_store self.user_store
.update(cx, |store, cx| { .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(); .detach();
} }

View file

@ -2,7 +2,7 @@ use client::User;
use gpui::{ use gpui::{
elements::*, elements::*,
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
Action, AnyElement, Element, View, ViewContext, AnyElement, Element, View, ViewContext,
}; };
use settings::Settings; use settings::Settings;
use std::sync::Arc; use std::sync::Arc;
@ -10,14 +10,18 @@ use std::sync::Arc;
enum Dismiss {} enum Dismiss {}
enum Button {} enum Button {}
pub fn render_user_notification<V: View, A: Action + Clone>( pub fn render_user_notification<F, V>(
user: Arc<User>, user: Arc<User>,
title: &'static str, title: &'static str,
body: Option<&'static str>, body: Option<&'static str>,
dismiss_action: A, on_dismiss: F,
buttons: Vec<(&'static str, Box<dyn Action>)>, buttons: Vec<(&'static str, Box<dyn Fn(&mut V, &mut ViewContext<V>)>)>,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
) -> AnyElement<V> { ) -> AnyElement<V>
where
F: 'static + Fn(&mut V, &mut ViewContext<V>),
V: View,
{
let theme = cx.global::<Settings>().theme.clone(); let theme = cx.global::<Settings>().theme.clone();
let theme = &theme.contact_notification; let theme = &theme.contact_notification;
@ -64,9 +68,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_padding(Padding::uniform(5.)) .with_padding(Padding::uniform(5.))
.on_click(MouseButton::Left, move |_, _, cx| { .on_click(MouseButton::Left, move |_, view, cx| on_dismiss(view, cx))
cx.dispatch_any_action(dismiss_action.boxed_clone())
})
.aligned() .aligned()
.constrained() .constrained()
.with_height( .with_height(
@ -90,7 +92,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
Some( Some(
Flex::row() Flex::row()
.with_children(buttons.into_iter().enumerate().map( .with_children(buttons.into_iter().enumerate().map(
|(ix, (message, action))| { |(ix, (message, handler))| {
MouseEventHandler::<Button, V>::new(ix, cx, |state, _| { MouseEventHandler::<Button, V>::new(ix, cx, |state, _| {
let button = theme.button.style_for(state, false); let button = theme.button.style_for(state, false);
Label::new(message, button.text.clone()) Label::new(message, button.text.clone())
@ -98,9 +100,7 @@ pub fn render_user_notification<V: View, A: Action + Clone>(
.with_style(button.container) .with_style(button.container)
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, move |_, _, cx| { .on_click(MouseButton::Left, move |_, view, cx| handler(view, cx))
cx.dispatch_any_action(action.boxed_clone())
})
}, },
)) ))
.aligned() .aligned()