BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / view_settings.rs

view_settings.rs

feat(calls): Hide meeting timer
metis Daniel Däschle · pushed by metis · 3 weeks ago
85506f7a History
view_settings.rs 5.8 KiB Download Raw
1use super::caption_style::{CaptionPosition, CaptionSize};
2
3const LAYOUT_META_KEY: &str = "call_layout";
4const HIDE_NO_VIDEO_META_KEY: &str = "call_hide_no_video";
5const CHAT_BUBBLES_META_KEY: &str = "call_chat_bubbles";
6const HIDE_TIMER_META_KEY: &str = "call_hide_timer";
7const CAPTION_SIZE_META_KEY: &str = "call_caption_size";
8const CAPTION_POSITION_META_KEY: &str = "call_caption_position";
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum CallLayout {
12 #[default]
13 Gallery,
14 Speaker,
15 Content,
16}
17
18impl CallLayout {
19 pub fn label(self) -> &'static str {
20 match self {
21 CallLayout::Gallery => "Gallery",
22 CallLayout::Speaker => "Speaker",
23 CallLayout::Content => "Focus on content",
24 }
25 }
26
27 fn meta(self) -> &'static str {
28 match self {
29 CallLayout::Gallery => "gallery",
30 CallLayout::Speaker => "speaker",
31 CallLayout::Content => "content",
32 }
33 }
34
35 fn from_meta(value: &str) -> CallLayout {
36 match value {
37 "speaker" => CallLayout::Speaker,
38 "content" => CallLayout::Content,
39 _ => CallLayout::Gallery,
40 }
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub struct ViewSettings {
46 pub layout: CallLayout,
47 pub layout_before_content: CallLayout,
48 pub hide_no_video: bool,
49 pub chat_bubbles: bool,
50 pub hide_me: bool,
51 pub hide_timer: bool,
52 pub caption_size: CaptionSize,
53 pub caption_position: CaptionPosition,
54}
55
56impl Default for ViewSettings {
57 fn default() -> Self {
58 ViewSettings {
59 layout: CallLayout::Gallery,
60 layout_before_content: CallLayout::Gallery,
61 hide_no_video: false,
62 chat_bubbles: true,
63 hide_me: false,
64 hide_timer: false,
65 caption_size: CaptionSize::Medium,
66 caption_position: CaptionPosition::Bottom,
67 }
68 }
69}
70
71impl ViewSettings {
72 pub fn load(store: &store::Store) -> ViewSettings {
73 let flag = |key: &str, default: bool| match store.meta(key).ok().flatten().as_deref() {
74 Some("1") => true,
75 Some("0") => false,
76 _ => default,
77 };
78 let layout = store
79 .meta(LAYOUT_META_KEY)
80 .ok()
81 .flatten()
82 .map_or(CallLayout::Gallery, |value| CallLayout::from_meta(&value));
83 ViewSettings {
84 layout,
85 hide_no_video: flag(HIDE_NO_VIDEO_META_KEY, false),
86 chat_bubbles: flag(CHAT_BUBBLES_META_KEY, true),
87 hide_timer: flag(HIDE_TIMER_META_KEY, false),
88 caption_size: store
89 .meta(CAPTION_SIZE_META_KEY)
90 .ok()
91 .flatten()
92 .map_or(CaptionSize::default(), |value| CaptionSize::from_meta(&value)),
93 caption_position: store
94 .meta(CAPTION_POSITION_META_KEY)
95 .ok()
96 .flatten()
97 .map_or(CaptionPosition::default(), |value| CaptionPosition::from_meta(&value)),
98 ..ViewSettings::default()
99 }
100 }
101
102 pub fn save(&self, store: &store::Store) {
103 let _ = store.set_meta(LAYOUT_META_KEY, self.layout.meta());
104 let _ = store.set_meta(
105 HIDE_NO_VIDEO_META_KEY,
106 if self.hide_no_video { "1" } else { "0" },
107 );
108 let _ = store.set_meta(
109 CHAT_BUBBLES_META_KEY,
110 if self.chat_bubbles { "1" } else { "0" },
111 );
112 let _ = store.set_meta(
113 HIDE_TIMER_META_KEY,
114 if self.hide_timer { "1" } else { "0" },
115 );
116 let _ = store.set_meta(CAPTION_SIZE_META_KEY, self.caption_size.meta());
117 let _ = store.set_meta(CAPTION_POSITION_META_KEY, self.caption_position.meta());
118 }
119
120 pub fn choose(&mut self, layout: CallLayout) {
121 if layout == CallLayout::Content && self.layout != CallLayout::Content {
122 self.layout_before_content = self.layout;
123 }
124 self.layout = layout;
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 #[test]
133 fn the_defaults_are_gallery_with_bubbles_on() {
134 let store = store::Store::open_in_memory().unwrap();
135 let settings = ViewSettings::load(&store);
136 assert_eq!(settings.layout, CallLayout::Gallery);
137 assert!(settings.chat_bubbles);
138 assert!(!settings.hide_no_video);
139 assert!(!settings.hide_me);
140 assert!(!settings.hide_timer);
141 assert_eq!(settings.caption_size, CaptionSize::Medium);
142 assert_eq!(settings.caption_position, CaptionPosition::Bottom);
143 }
144
145 #[test]
146 fn choices_survive_a_restart() {
147 let store = store::Store::open_in_memory().unwrap();
148 let mut settings = ViewSettings::load(&store);
149 settings.choose(CallLayout::Speaker);
150 settings.hide_no_video = true;
151 settings.chat_bubbles = false;
152 settings.hide_me = true;
153 settings.hide_timer = true;
154 settings.caption_size = CaptionSize::Large;
155 settings.caption_position = CaptionPosition::Top;
156 settings.save(&store);
157 let loaded = ViewSettings::load(&store);
158 assert_eq!(loaded.layout, CallLayout::Speaker);
159 assert!(loaded.hide_no_video);
160 assert!(!loaded.chat_bubbles);
161 assert!(!loaded.hide_me);
162 assert!(loaded.hide_timer);
163 assert_eq!(loaded.caption_size, CaptionSize::Large);
164 assert_eq!(loaded.caption_position, CaptionPosition::Top);
165 }
166
167 #[test]
168 fn content_remembers_the_layout_to_fall_back_to() {
169 let mut settings = ViewSettings::default();
170 settings.choose(CallLayout::Speaker);
171 settings.choose(CallLayout::Content);
172 assert_eq!(settings.layout_before_content, CallLayout::Speaker);
173 settings.choose(CallLayout::Content);
174 assert_eq!(settings.layout_before_content, CallLayout::Speaker);
175 }
176}