|
| 1 | +use tokio::task::JoinHandle; |
| 2 | + |
| 3 | +pub(crate) fn run_while_active<T, FExit, F>(is_exiting: FExit, operation: F) -> Option<T> |
| 4 | +where |
| 5 | + FExit: Fn() -> bool, |
| 6 | + F: FnOnce() -> T, |
| 7 | +{ |
| 8 | + if is_exiting() { |
| 9 | + None |
| 10 | + } else { |
| 11 | + Some(operation()) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +pub(crate) fn collect_device_inventory<TCamera, TMicrophone, FExit, FCamera, FMicrophone>( |
| 16 | + is_exiting: FExit, |
| 17 | + camera_permitted: bool, |
| 18 | + microphone_permitted: bool, |
| 19 | + list_cameras: FCamera, |
| 20 | + list_microphones: FMicrophone, |
| 21 | +) -> Option<(Vec<TCamera>, Vec<TMicrophone>)> |
| 22 | +where |
| 23 | + FExit: Fn() -> bool, |
| 24 | + FCamera: FnOnce() -> Vec<TCamera>, |
| 25 | + FMicrophone: FnOnce() -> Vec<TMicrophone>, |
| 26 | +{ |
| 27 | + if is_exiting() { |
| 28 | + return None; |
| 29 | + } |
| 30 | + |
| 31 | + let cameras = if camera_permitted { |
| 32 | + if is_exiting() { |
| 33 | + return None; |
| 34 | + } |
| 35 | + |
| 36 | + list_cameras() |
| 37 | + } else { |
| 38 | + Vec::new() |
| 39 | + }; |
| 40 | + |
| 41 | + if is_exiting() { |
| 42 | + return None; |
| 43 | + } |
| 44 | + |
| 45 | + let microphones = if microphone_permitted { |
| 46 | + if is_exiting() { |
| 47 | + return None; |
| 48 | + } |
| 49 | + |
| 50 | + list_microphones() |
| 51 | + } else { |
| 52 | + Vec::new() |
| 53 | + }; |
| 54 | + |
| 55 | + if is_exiting() { |
| 56 | + return None; |
| 57 | + } |
| 58 | + |
| 59 | + Some((cameras, microphones)) |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 63 | +pub(crate) enum AppExitAction { |
| 64 | + #[cfg(target_os = "macos")] |
| 65 | + Process(i32), |
| 66 | + #[cfg(not(target_os = "macos"))] |
| 67 | + Runtime(i32), |
| 68 | +} |
| 69 | + |
| 70 | +pub(crate) fn app_exit_action(exit_code: i32) -> AppExitAction { |
| 71 | + #[cfg(target_os = "macos")] |
| 72 | + { |
| 73 | + AppExitAction::Process(exit_code) |
| 74 | + } |
| 75 | + |
| 76 | + #[cfg(not(target_os = "macos"))] |
| 77 | + { |
| 78 | + AppExitAction::Runtime(exit_code) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +pub(crate) fn read_target_under_cursor<TDisplay, TWindow, FExit, FDisplay, FWindow>( |
| 83 | + is_exiting: FExit, |
| 84 | + display: FDisplay, |
| 85 | + window: FWindow, |
| 86 | +) -> Option<(Option<TDisplay>, Option<TWindow>)> |
| 87 | +where |
| 88 | + FExit: Fn() -> bool, |
| 89 | + FDisplay: FnOnce() -> Option<TDisplay>, |
| 90 | + FWindow: FnOnce() -> Option<TWindow>, |
| 91 | +{ |
| 92 | + if is_exiting() { |
| 93 | + return None; |
| 94 | + } |
| 95 | + |
| 96 | + let display = display(); |
| 97 | + |
| 98 | + if is_exiting() { |
| 99 | + return None; |
| 100 | + } |
| 101 | + |
| 102 | + let window = window(); |
| 103 | + |
| 104 | + if is_exiting() { |
| 105 | + return None; |
| 106 | + } |
| 107 | + |
| 108 | + Some((display, window)) |
| 109 | +} |
| 110 | + |
| 111 | +pub(crate) fn abort_join_handles<T>( |
| 112 | + tasks: impl IntoIterator<Item = JoinHandle<T>>, |
| 113 | + task: Option<JoinHandle<T>>, |
| 114 | +) { |
| 115 | + for task in tasks { |
| 116 | + task.abort(); |
| 117 | + } |
| 118 | + |
| 119 | + if let Some(task) = task { |
| 120 | + task.abort(); |
| 121 | + } |
| 122 | +} |
0 commit comments