BlueGit

ict rusty-teams

Files

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

push_to_talk.rs

8629da58 History
push_to_talk.rs 1.6 KiB Download Raw
1use calling::MuteCommand;
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
4pub struct PushToTalk {
5 held: bool,
6}
7
8impl PushToTalk {
9 pub fn is_held(self) -> bool {
10 self.held
11 }
12
13 pub fn press(&mut self, muted: bool, can_unmute: bool) -> Option<MuteCommand> {
14 if self.held || !muted || !can_unmute {
15 return None;
16 }
17 self.held = true;
18 Some(MuteCommand::Unmute)
19 }
20
21 pub fn release(&mut self) -> Option<MuteCommand> {
22 std::mem::take(&mut self.held).then_some(MuteCommand::Mute)
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn pressing_while_muted_unmutes_and_releasing_mutes_again() {
32 let mut talk = PushToTalk::default();
33 assert_eq!(talk.press(true, true), Some(MuteCommand::Unmute));
34 assert!(talk.is_held());
35 assert_eq!(talk.release(), Some(MuteCommand::Mute));
36 assert!(!talk.is_held());
37 }
38
39 #[test]
40 fn key_repeat_sends_one_unmute() {
41 let mut talk = PushToTalk::default();
42 assert!(talk.press(true, true).is_some());
43 assert_eq!(talk.press(false, true), None);
44 assert_eq!(talk.press(true, true), None);
45 }
46
47 #[test]
48 fn pressing_while_unmuted_does_nothing_and_release_stays_quiet() {
49 let mut talk = PushToTalk::default();
50 assert_eq!(talk.press(false, true), None);
51 assert_eq!(talk.release(), None);
52 }
53
54 #[test]
55 fn listen_only_never_unmutes() {
56 let mut talk = PushToTalk::default();
57 assert_eq!(talk.press(true, false), None);
58 assert_eq!(talk.release(), None);
59 }
60}