BlueGit

ict rusty-teams

Files

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

view_settings.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 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| {
93 CaptionSize::from_meta(&value)
94 }),
95 caption_position: store
96 .meta(CAPTION_POSITION_META_KEY)
97 .ok()
98 .flatten()
99 .map_or(CaptionPosition::default(), |value| {
100 CaptionPosition::from_meta(&value)
101 }),
102 ..ViewSettings::default()
103 }
104 }
105
106 pub fn save(&self, store: &store::Store) {
107 let _ = store.set_meta(LAYOUT_META_KEY, self.layout.meta());
108 let _ = store.set_meta(
109 HIDE_NO_VIDEO_META_KEY,
110 if self.hide_no_video { "1" } else { "0" },
111 );
112 let _ = store.set_meta(
113 CHAT_BUBBLES_META_KEY,
114 if self.chat_bubbles { "1" } else { "0" },
115 );
116 let _ = store.set_meta(HIDE_TIMER_META_KEY, if self.hide_timer { "1" } else { "0" });
117 let _ = store.set_meta(CAPTION_SIZE_META_KEY, self.caption_size.meta());
118 let _ = store.set_meta(CAPTION_POSITION_META_KEY, self.caption_position.meta());
119 }
120
121 pub fn choose(&mut self, layout: CallLayout) {
122 if layout == CallLayout::Content && self.layout != CallLayout::Content {
123 self.layout_before_content = self.layout;
124 }
125 self.layout = layout;
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn the_defaults_are_gallery_with_bubbles_on() {
135 let store = store::Store::open_in_memory().unwrap();
136 let settings = ViewSettings::load(&store);
137 assert_eq!(settings.layout, CallLayout::Gallery);
138 assert!(settings.chat_bubbles);
139 assert!(!settings.hide_no_video);
140 assert!(!settings.hide_me);
141 assert!(!settings.hide_timer);
142 assert_eq!(settings.caption_size, CaptionSize::Medium);
143 assert_eq!(settings.caption_position, CaptionPosition::Bottom);
144 }
145
146 #[test]
147 fn choices_survive_a_restart() {
148 let store = store::Store::open_in_memory().unwrap();
149 let mut settings = ViewSettings::load(&store);
150 settings.choose(CallLayout::Speaker);
151 settings.hide_no_video = true;
152 settings.chat_bubbles = false;
153 settings.hide_me = true;
154 settings.hide_timer = true;
155 settings.caption_size = CaptionSize::Large;
156 settings.caption_position = CaptionPosition::Top;
157 settings.save(&store);
158 let loaded = ViewSettings::load(&store);
159 assert_eq!(loaded.layout, CallLayout::Speaker);
160 assert!(loaded.hide_no_video);
161 assert!(!loaded.chat_bubbles);
162 assert!(!loaded.hide_me);
163 assert!(loaded.hide_timer);
164 assert_eq!(loaded.caption_size, CaptionSize::Large);
165 assert_eq!(loaded.caption_position, CaptionPosition::Top);
166 }
167
168 #[test]
169 fn content_remembers_the_layout_to_fall_back_to() {
170 let mut settings = ViewSettings::default();
171 settings.choose(CallLayout::Speaker);
172 settings.choose(CallLayout::Content);
173 assert_eq!(settings.layout_before_content, CallLayout::Speaker);
174 settings.choose(CallLayout::Content);
175 assert_eq!(settings.layout_before_content, CallLayout::Speaker);
176 }
177}