BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / account.rs

account.rs

account.rs 4.7 KiB Download Raw
1use gpui_kit::*;
2use teams_core::{
3 ActivityAlert, ChatSettingChange, EnterKeyBehavior, MeetingChatNotifications, MessageDensity,
4 NotificationPreset, UpArrowBehavior,
5};
6
7use super::rows::{Choice, control_row, select};
8use crate::app_state::AppState;
9
10pub struct Labeled<T: 'static> {
11 pub label: &'static str,
12 pub value: T,
13}
14
15const fn option<T>(label: &'static str, value: T) -> Labeled<T> {
16 Labeled { label, value }
17}
18
19pub const DENSITY: [Labeled<MessageDensity>; 2] = [
20 option("Comfy", MessageDensity::Comfy),
21 option("Compact", MessageDensity::Compact),
22];
23
24pub const ENTER_KEY: [Labeled<EnterKeyBehavior>; 2] = [
25 option("Send the message", EnterKeyBehavior::Send),
26 option(
27 "Start a new line (to send, select Send or use Ctrl+Enter).",
28 EnterKeyBehavior::NewLine,
29 ),
30];
31
32pub const UP_ARROW: [Labeled<UpArrowBehavior>; 3] = [
33 option(
34 "Move focus to message list",
35 UpArrowBehavior::NavigateToMessages,
36 ),
37 option("Edit last message", UpArrowBehavior::EditLastMessage),
38 option("Stay in compose box", UpArrowBehavior::StayInCompose),
39];
40
41pub const PRESETS: [Labeled<NotificationPreset>; 3] = [
42 option("All activity", NotificationPreset::AllActivity),
43 option("Mentions & replies", NotificationPreset::MentionsAndReplies),
44 option("Customised", NotificationPreset::Customised),
45];
46
47pub const CHAT_MESSAGES: [Labeled<ActivityAlert>; 2] = [
48 option("Show in banner", ActivityAlert::OnlyBanner),
49 option("Off", ActivityAlert::Off),
50];
51
52pub const MENTIONS: [Labeled<ActivityAlert>; 2] = [
53 option("Show in Activity and banner", ActivityAlert::BannerAndFeed),
54 option("Show in Activity", ActivityAlert::OnlyFeed),
55];
56
57pub const ACTIVITY_OR_OFF: [Labeled<ActivityAlert>; 3] = [
58 option("Show in Activity and banner", ActivityAlert::BannerAndFeed),
59 option("Show in Activity", ActivityAlert::OnlyFeed),
60 option("Off", ActivityAlert::Off),
61];
62
63pub const MEETING_CHATS: [Labeled<MeetingChatNotifications>; 3] = [
64 option("Mute", MeetingChatNotifications::Mute),
65 option("Unmute", MeetingChatNotifications::Unmute),
66 option(
67 "Mute until I join or send a message",
68 MeetingChatNotifications::MuteUntilJoin,
69 ),
70];
71
72pub fn preset_detail(preset: NotificationPreset) -> &'static str {
73 match preset {
74 NotificationPreset::AllActivity => "New messages, reactions, and all mentions",
75 NotificationPreset::MentionsAndReplies => "Personal mentions and replies to your messages",
76 NotificationPreset::Customised => "Your customised settings are active.",
77 }
78}
79
80pub fn meeting_chats_detail(setting: MeetingChatNotifications) -> &'static str {
81 match setting {
82 MeetingChatNotifications::Mute => "Don't send notifications for any meetings.",
83 MeetingChatNotifications::Unmute => "Send notifications for all meetings.",
84 MeetingChatNotifications::MuteUntilJoin => {
85 "Only send notifications if I've joined a meeting or sent a message."
86 }
87 }
88}
89
90pub fn label_of<T: PartialEq + Copy>(options: &[Labeled<T>], current: T) -> &'static str {
91 options
92 .iter()
93 .find(|option| option.value == current)
94 .map(|option| option.label)
95 .unwrap_or_default()
96}
97
98pub fn account_select<T: PartialEq + Copy + 'static>(
99 app: &Entity<AppState>,
100 (id, title, detail): (
101 &'static str,
102 impl Into<SharedString>,
103 impl Into<SharedString>,
104 ),
105 options: &'static [Labeled<T>],
106 current: T,
107 change: fn(T) -> ChatSettingChange,
108) -> Div {
109 let choices = options
110 .iter()
111 .map(|option| {
112 let (app, value) = (app.clone(), option.value);
113 Choice::new(option.label, value == current, move |cx| {
114 app.update(cx, |state, cx| state.save_chat_setting(change(value), cx))
115 })
116 })
117 .collect();
118 control_row(
119 title,
120 detail,
121 select(id, label_of(options, current), false, choices),
122 )
123}
124
125#[cfg(test)]
126mod tests {
127 use teams_core::{ActivityAlert, MeetingChatNotifications, MessageDensity, UpArrowBehavior};
128
129 use super::{CHAT_MESSAGES, DENSITY, MEETING_CHATS, MENTIONS, UP_ARROW, label_of};
130
131 #[test]
132 fn every_value_has_a_teams_label() {
133 assert_eq!(label_of(&DENSITY, MessageDensity::Compact), "Compact");
134 assert_eq!(
135 label_of(&UP_ARROW, UpArrowBehavior::default()),
136 "Edit last message"
137 );
138 assert_eq!(
139 label_of(&MEETING_CHATS, MeetingChatNotifications::default()),
140 "Mute until I join or send a message"
141 );
142 assert_eq!(
143 label_of(&CHAT_MESSAGES, ActivityAlert::OnlyBanner),
144 "Show in banner"
145 );
146 assert_eq!(label_of(&MENTIONS, ActivityAlert::Off), "");
147 }
148}