use super::account_list::account_list; use crate::gui::glyph::Fill as _; use crate::lang::{Label as _, w}; use common::model::Id; use gpui_kit::prelude::FluentBuilder as _; use std::{ collections::{HashMap, VecDeque}, sync::Arc, }; use common::{ call::{BanMember, DeleteUser, Kick, SetRank, UpdatePresence}, model::{Presence, Rank, User}, permission, }; use gpui_kit::{ Anchor, AnyElement, App, Context, Div, ElementId, Entity, FontWeight, Hsla, Image, IntoElement, ParentElement, SharedString, Stateful, StatefulInteractiveElement, StyleRefinement, Styled, Window, assets::IconName, component::{ WindowExt, input::InputState, popover::{Popover, PopoverState}, slider::SliderState, }, div, px, }; use super::chat::{Armed, CardMenu, ReportTarget, View}; use super::message_actions::report_bar; use super::parts::{ Ink, Scale, Text, Tone, Voice, band, icon_button, small_icon, stack, voice_line, words, }; use super::social::Relation; use super::style::{Tokens, radius, size, space, stroke, text}; use super::widget::spawn_done; use super::{ Tooltip as _, Trigger, Yumush, avatar, clickable, entity_action, input_dialog, millis_time, notify_error, text, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Removal { Kick, Ban, } impl Removal { fn title(self) -> &'static str { let w = &w().profile; match self { Removal::Kick => w.kick, Removal::Ban => w.ban, } } } pub(super) fn user_link( id: impl Into, user: Option<&User>, entity: &Entity, trigger: impl IntoElement, ) -> AnyElement { match user { Some(user) => profile_popover( id, Anchor::LeftCenter, user, entity, div().min_w_0().cursor_pointer().child(trigger), ) .trigger_style(StyleRefinement::default().min_w_0()) .into_any_element(), None => trigger.into_any_element(), } } pub(super) fn profile_popover( id: impl Into, anchor: Anchor, user: &User, entity: &Entity, trigger: impl IntoElement, ) -> Popover { let user = user.clone(); let entity = entity.clone(); let notify_entity = entity.clone(); let volume_user_id = user.id; Popover::new(id) .anchor(anchor) .appearance(false) .on_open_change(move |_, _, cx| { notify_entity.update(cx, |yumush, cx| { yumush.chat.session.card_menu = CardMenu::Main; yumush.chat.session.armed = None; yumush.open_user_volume(volume_user_id, cx); cx.notify() }) }) .trigger(Trigger::new(trigger)) .content(move |_, _, cx| { let yumush = entity.read(cx); let card = Card { is_self: yumush .user .as_ref() .is_some_and(|viewer| viewer.id == user.id), deletable: yumush.user.as_ref().is_some_and(|viewer| { permission::can_delete_user(viewer.role, false, user.role) }), actor: yumush.actor_rank(), target: yumush.chat.rank_of(&user), relation: yumush.relation(user.id), card_menu: yumush.chat.session.card_menu, presence: yumush.presence_of(user.id), armed: yumush.chat.session.armed.clone(), reporting: yumush.chat.session.report == Some(ReportTarget::User(user.id)), reason: yumush.chat.report_reason.clone(), status: yumush.chat.status_input.clone(), volume: yumush .chat .session .volume_popover .as_ref() .filter(|volume| volume.user_id == user.id) .map(|volume| (volume.slider.clone(), yumush.user_volume(user.id))), call: yumush.call_of(user.id).map(|community| { if community.kind.is_direct() { w().profile.a_call.to_string() } else { community.name.clone() } }), wave: yumush .chat .session .waves .voice(user.id) .map(|bars| bars.iter().copied().collect()), speaking: yumush.chat.session.waves.speaking.contains(&user.id), user: user.clone(), }; card.render( &entity, cx.entity(), Tokens::of(cx), &yumush.chat.session.avatars, ) }) } struct Card { user: User, call: Option, wave: Option>, speaking: bool, is_self: bool, deletable: bool, actor: Option, target: Option, relation: Relation, card_menu: CardMenu, presence: Presence, armed: Option, reporting: bool, reason: Entity, status: Entity, volume: Option<(Entity, f32)>, } impl Card { fn social_rows( &self, entity: &Entity, popover: &Entity, t: Tokens, ) -> Vec { let user_id = self.user.id; let message_id = user_id; let mut rows = vec![ menu_row( "profile_message", w().profile.message, small_icon(IconName::MessageSquare), t.paper(), t, yumush_action(entity, popover, move |yumush, window, cx| { yumush.open_direct(message_id, window, cx) }), ) .into_any_element(), menu_row( "profile_call", w().profile.call, small_icon(IconName::Phone), t.paper(), t, yumush_action(entity, popover, move |yumush, window, cx| { yumush.call_direct(message_id, window, cx) }), ) .into_any_element(), ]; let report_user_id = self.user.id; rows.push( menu_row( "profile_report", w().profile.report, small_icon(IconName::TriangleAlert), t.bad(), t, entity_action(entity, move |yumush, window, cx| { yumush.start_report(ReportTarget::User(report_user_id), window, cx) }), ) .into_any_element(), ); if let Some((label, icon)) = self.relation.action() { let relation = self.relation; rows.push( menu_row( "profile_friend", label, small_icon(icon), t.paper(), t, yumush_action(entity, popover, move |yumush, window, cx| { yumush.friend_action(relation, user_id, window, cx) }), ) .into_any_element(), ); } rows } fn render( self, entity: &Entity, popover: Entity, t: Tokens, avatars: &HashMap>, ) -> Div { let is_self = self.is_self; let actions = if is_self { div() .flex() .flex_col() .gap(px(space::XS)) .children(main_self_rows(self.armed, entity, &popover, t)) .into_any_element() } else { let mut rows = self.social_rows(entity, &popover, t); rows.extend(self.moderation_rows(entity, &popover, t)); if self.card_menu == CardMenu::More { div() .flex() .flex_col() .gap(px(space::XS)) .children(rows) .into_any_element() } else { let first = rows.remove(0); let second = rows.remove(0); band(space::XS) .child(first) .child(second) .child(div().flex_1()) .child(icon_button( "profile_more", IconName::Ellipsis, w().profile.more, Tone::Idle, Scale::Normal, t, card_menu_action(entity, CardMenu::More), )) .into_any_element() } }; let location = match &self.call { Some(room) => w().profile.in_room(room), None => self.presence.label().to_lowercase(), }; let place = match self.target { Some(Rank::Builder) => { let badge = band(space::XXS) .child(small_icon(IconName::Shield).text_color(t.paper())) .child( words(Rank::Builder.label(), Text::Caption, Ink::Faint, t) .text_color(t.paper()), ); if is_self { clickable( "profile_builder", t, yumush_action(entity, &popover, |yumush, window, cx| { yumush.show_view(View::Builder, window, cx) }), ) .rounded(px(radius::SM)) .child(badge) .into_any_element() } else { badge.into_any_element() } } Some(rank @ (Rank::Moderator | Rank::Admin | Rank::Owner)) => { words(rank.label(), Text::Caption, Ink::Faint, t).into_any_element() } _ => words(location, Text::Caption, Ink::Faint, t).into_any_element(), }; let tint = match self.presence { Presence::Offline => t.off(), _ => t.person(self.user.id), }; let line = if self.call.is_some() { div() .h(px(space::LG)) .child(voice_line( self.wave.as_ref(), if self.speaking { Voice::Live(t.person(self.user.id)) } else { Voice::Quiet }, t, )) .into_any_element() } else { div() .w_full() .h(px(stroke::LINE)) .bg(tint) .into_any_element() }; let caption = band(space::SM).justify_between().child(place).child(words( millis_time(self.user.created_at, crate::lang::w().common.day_year), Text::Caption, Ink::Faint, t, )); super::parts::popover(t) .w(px(size::POPOVER)) .gap(px(space::LG)) .child( band(space::LG) .child( super::zoomable( entity, "profile_picture", avatar( t, &self.user, px(size::AVATAR_LG), text(text::BODY), avatars, ), super::Zoom::new( self.user.avatar_id.and_then(|id| avatars.get(&id)).cloned(), true, self.user.username.clone(), ), ) .on_click({ let popover = popover.clone(); move |_, window, cx| { popover.update(cx, |state, cx| state.dismiss(window, cx)); } }) .map(|picture| { if is_self { div().relative().flex_shrink_0().child(picture).child( div() .absolute() .right(px(-space::XXS)) .bottom(px(-space::XXS)) .child(presence_menu(self.presence, entity, t)), ) } else { super::presence_avatar( picture, self.presence, px(size::AVATAR_LG), t, ) } }), ) .child( stack(space::XXS) .flex_1() .min_w_0() .child( band(space::XS) .child( words( self.user.username.clone(), Text::Body, Ink::Paper, t, ) .font_weight(FontWeight::MEDIUM) .min_w_0() .truncate(), ) .when(is_self, |name| { name.child(account_menu(entity, &popover, t)) }), ) .map(|names| { if !is_self { return names.children((!self.user.status.is_empty()).then( || { words( self.user.status.clone(), Text::Caption, Ink::Dim, t, ) }, )); } if self.card_menu == CardMenu::Status { let status = self.status.clone(); return names.child( div() .flex() .items_center() .border_b(px(stroke::LINE)) .border_color(t.paper()) .child( super::widget::bare_input( t, gpui_kit::component::input::Input::new( &self.status, ) .token(super::glyph::token), ) .text_size(text(text::CAPTION)) .px_0() .flex_1(), ) .child(super::glyph::picker_button( "status_emoji", entity, t, super::glyph::insert_into(status), )), ); } names.child( band(space::XS) .children((!self.user.status.is_empty()).then(|| { words( self.user.status.clone(), Text::Caption, Ink::Dim, t, ) .min_w_0() .truncate() })) .child(edit_status(entity, &self.user.status, t)), ) }), ), ) .child(stack(space::XS).child(line).child(caption)) .children(self.volume.clone().map(|(slider, volume)| { let user_id = self.user.id; band(space::SM) .child(icon_button( "profile_mute", if volume == 0. { IconName::VolumeX } else { IconName::Volume2 }, w().profile.mute, Tone::Idle, Scale::Normal, t, entity_action(entity, move |yumush, window, cx| { yumush.toggle_user_mute(user_id, window, cx) }), )) .child(super::parts::slider(&slider, t)) .child( words(format!("{volume:.0}%"), Text::Caption, Ink::Dim, t) .w(px(size::CONTROL)) .text_right(), ) })) .children(self.reporting.then(|| report_bar(&self.reason, entity, t))) .child(actions) } fn moderation_rows( &self, entity: &Entity, popover: &Entity, t: Tokens, ) -> Vec { let actor = self.actor; let target = self.target; let user_id = self.user.id; let mut rows = Vec::new(); for rank in [Rank::Member, Rank::Moderator, Rank::Admin] { if target != Some(rank) && permission::can_set_rank(actor, target, rank) { let label = w().profile.make_rank(rank.label().to_lowercase()); rows.push( menu_row( label.clone(), label, small_icon(IconName::Shield), t.paper(), t, yumush_action(entity, popover, move |yumush, window, cx| { yumush.set_member_rank(user_id, rank, window, cx) }), ) .into_any_element(), ); } } if permission::can_moderate(actor, target) { for (id, removal, icon) in [ ("kick", Removal::Kick, IconName::Close), ("ban", Removal::Ban, IconName::Ban), ] { rows.push( menu_row( id, removal.title(), small_icon(icon), t.bad(), t, yumush_action(entity, popover, move |yumush, window, cx| { yumush.open_removal(user_id, removal, window, cx) }), ) .into_any_element(), ); } } if self.deletable { let armed = Armed::DeleteUser(user_id); let label = if self.armed == Some(armed.clone()) { w().common.confirm } else { w().profile.delete_user }; rows.push( menu_row( "delete_user", label, small_icon(IconName::Trash), t.bad(), t, armed_action(armed, entity, popover, move |yumush, window, cx| { yumush.delete_member_account(user_id, window, cx) }), ) .into_any_element(), ); } rows } } fn main_self_rows( armed: Option, entity: &Entity, popover: &Entity, t: Tokens, ) -> Vec { vec![ menu_row( "profile_settings", w().profile.settings, small_icon(IconName::Settings), t.paper(), t, yumush_action(entity, popover, |yumush, window, cx| { yumush.show_view(View::Settings, window, cx) }), ) .into_any_element(), menu_row( "profile_logout", if armed == Some(Armed::LogOut) { w().common.confirm } else { w().profile.log_out }, small_icon(IconName::LogOut), t.bad(), t, armed_action(Armed::LogOut, entity, popover, |yumush, window, cx| { yumush.logout(window, cx) }), ) .into_any_element(), ] } fn account_menu(entity: &Entity, card: &Entity, t: Tokens) -> Popover { let entity = entity.clone(); let card = card.clone(); Popover::new("account_menu") .anchor(Anchor::TopLeft) .appearance(false) .trigger(Trigger::new( clickable("profile_accounts", t, |_, _| {}) .tip(w().profile.accounts) .rounded(px(radius::SM)) .flex_shrink_0() .child(small_icon(IconName::ChevronDown).text_color(t.dim())), )) .content(move |_, _, cx| { let yumush = entity.read(cx); super::parts::popover(t) .p(px(space::XS)) .w(px(size::SIDEBAR)) .child(account_list(yumush, &entity, Some(card.clone()), t, cx)) .child(menu_row( "profile_add_account", w().profile.add_account, small_icon(IconName::UserPlus), t.paper(), t, yumush_action(&entity, &card, |yumush, window, cx| { yumush.add_account(window, cx) }), )) }) } fn edit_status(entity: &Entity, status: &str, t: Tokens) -> impl IntoElement { let status = status.to_string(); clickable( "edit_status", t, entity_action(entity, move |yumush, window, cx| { yumush.chat.session.card_menu = CardMenu::Status; yumush.chat.status_input.update(cx, |input, cx| { input.fill(status.clone(), window, cx); input.focus(window, cx); }); cx.notify(); }), ) .tip(w().profile.edit_status) .flex_none() .p(px(space::XXS)) .rounded(px(radius::SM)) .child(small_icon(IconName::Pencil).text_color(t.faint())) } fn card_menu_action( entity: &Entity, menu: CardMenu, ) -> impl Fn(&mut Window, &mut App) + 'static { entity_action(entity, move |yumush, _, cx| { yumush.chat.session.card_menu = menu; cx.notify(); }) } pub(super) fn yumush_action( entity: &Entity, popover: &Entity, run: impl Fn(&mut Yumush, &mut Window, &mut Context) + 'static, ) -> impl Fn(&mut Window, &mut App) + 'static { let entity = entity.clone(); let popover = popover.clone(); move |window, cx| { popover.update(cx, |state, cx| state.dismiss(window, cx)); entity.update(cx, |yumush, cx| run(yumush, window, cx)); } } pub(super) fn armed_action( armed: Armed, entity: &Entity, popover: &Entity, run: impl Fn(&mut Yumush, &mut Window, &mut Context) + 'static, ) -> impl Fn(&mut Window, &mut App) + 'static { let entity = entity.clone(); let popover = popover.clone(); move |window, cx| { if entity.update(cx, |yumush, cx| yumush.arm(armed.clone(), cx)) { popover.update(cx, |state, cx| state.dismiss(window, cx)); entity.update(cx, |yumush, cx| run(yumush, window, cx)); } } } fn presence_dot(presence: Presence, t: Tokens) -> Div { div() .size(px(space::LG)) .rounded_full() .border_2() .border_color(t.raise()) .bg(super::presence_color(presence, t)) } fn presence_menu(current: Presence, entity: &Entity, t: Tokens) -> Popover { let entity = entity.clone(); Popover::new("presence_menu") .anchor(Anchor::TopLeft) .appearance(false) .trigger(Trigger::new( clickable("presence_dot", t, |_, _| {}) .rounded_full() .child(presence_dot(current, t)), )) .content(move |_, _, cx| { let popover = cx.entity(); super::parts::popover(t) .p(px(space::XS)) .children(Presence::ALL.into_iter().map(|presence| { let popover = popover.clone(); menu_row( ("presence", presence as usize), presence.label(), div() .size(px(size::ICON_SM)) .flex() .items_center() .justify_center() .child( div() .size(px(space::SM)) .rounded_full() .bg(super::presence_color(presence, t)), ), if presence == current { t.paper() } else { t.dim() }, t, { let entity = entity.clone(); move |window, cx| { popover.update(cx, |state, cx| state.dismiss(window, cx)); entity.update(cx, |yumush, cx| { yumush.set_presence(presence, window, cx) }); } }, ) })) }) } pub(super) fn menu_row( id: impl Into, label: impl Into, leading: impl IntoElement, color: Hsla, t: Tokens, on_click: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ clickable(id, t, on_click) .h(px(size::CONTROL)) .px(px(space::SM)) .flex() .items_center() .gap(px(space::SM)) .rounded(px(radius::MD)) .text_color(color) .child(leading) .child(label.into()) } impl Yumush { fn open_removal( &self, user_id: Id, removal: Removal, window: &mut Window, cx: &mut Context, ) { let Some(community_id) = self.chat.session.selected_community_id else { return; }; input_dialog( removal.title(), removal.title(), w().profile.reason_optional, window, cx, move |this, text, window, cx| { let reason = text.trim().to_string(); let reason = (!reason.is_empty()).then_some(reason); this.remove_member(community_id, user_id, removal, reason, window, cx); }, ); } fn remove_member( &self, community_id: Id, user_id: Id, removal: Removal, reason: Option, window: &mut Window, cx: &mut Context, ) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { let result = match removal { Removal::Kick => { network .call(Kick { community_id, user_id, reason, }) .await } Removal::Ban => { network .call(BanMember { community_id, user_id, reason, }) .await } }; match result { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| { window.close_dialog(cx); if removal == Removal::Ban { yumush.load_bans(window, cx); } }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn set_member_rank( &self, user_id: Id, rank: Rank, window: &mut Window, cx: &mut Context, ) { let Some(community_id) = self.chat.session.selected_community_id else { return; }; let network = self.network.clone(); spawn_done(window, cx, async move { network .call(SetRank { community_id, user_id, rank, }) .await }); } pub(super) fn set_presence( &mut self, presence: Presence, window: &mut Window, cx: &mut Context, ) { let me = self.user.as_ref().map(|me| me.id); let before = me.map(|me| self.presence_of(me)); if let Some(me) = me { self.chat.session.presences.insert(me, presence); } self.chat.session.card_menu = CardMenu::Main; cx.notify(); let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { if let Err(error) = network.call(UpdatePresence { presence }).await { let _ = this.update(cx, |yumush, cx| { if let (Some(me), Some(before)) = (me, before) { yumush.chat.session.presences.insert(me, before); } cx.notify(); }); notify_error(&this, cx, error); } }) .detach(); } pub(super) fn delete_member_account( &mut self, user_id: Id, window: &mut Window, cx: &mut Context, ) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(DeleteUser { user_id }).await { Ok(()) => { let _ = this.update(cx, |yumush, cx| { yumush.chat.remove_member(user_id); cx.notify(); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } }