BlueGit

ict rusty-teams

Files

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

view_settings.rs

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