Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

refactor(desktop): look up call windows by part, not by array index

Marcelitofa5193605d1c
diff --git a/apps/desktop/src-tauri/src/computer/mod.rs b/apps/desktop/src-tauri/src/computer/mod.rs
index fd409b6..ae5e01b 100644
--- a/apps/desktop/src-tauri/src/computer/mod.rs
+++ b/apps/desktop/src-tauri/src/computer/mod.rs
@@ -56,9 +56,10 @@ mod capture_order {
/// Match only the process-lifetime handles recorded at window creation. No resolver or
/// main-thread dispatch is permitted here: callers may hold the affinity lock on a worker.
+ /// `handle` answers each part through an exhaustive match, so a new part cannot go unmatched.
pub(super) fn current_window(saved: &[(Option<u64>, (super::working::Part, u32))], hwnd: usize,
- handles: [Option<usize>; 2]) -> Option<usize> {
- saved.iter().position(|(_, (window, _))| handles[*window as usize] == Some(hwnd))
+ handle: impl Fn(super::working::Part) -> Option<usize>) -> Option<usize> {
+ saved.iter().position(|(_, (window, _))| handle(*window) == Some(hwnd))
}
/// End call ownership, retaining an ownerless recovery value if native restoration fails.
@@ -80,10 +81,14 @@ mod capture_order {
fn matching_under_the_affinity_lock_never_dispatches_a_window_query() {
let saved = std::sync::Mutex::new(vec![(Some(1), (super::super::working::Part::Dot, 7))]);
let guard = saved.lock().unwrap();
- // The API accepts only recorded values, so querying Tauri is not an option.
- assert_eq!(current_window(&guard, 101, [Some(101), Some(102)]), Some(0));
- assert_eq!(current_window(&guard, 201, [Some(101), Some(102)]), None);
- assert_eq!(current_window(&guard, 101, [None, None]), None);
+ // Callers pass the handles recorded at creation; querying Tauri is not an option.
+ let recorded = |part| match part {
+ super::super::working::Part::Dot => Some(101),
+ super::super::working::Part::Panel => Some(102),
+ };
+ assert_eq!(current_window(&guard, 101, recorded), Some(0));
+ assert_eq!(current_window(&guard, 201, recorded), None);
+ assert_eq!(current_window(&guard, 101, |_| None), None);
}
#[test]
diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rs
index 51a5818..cfd5ca8 100644
--- a/apps/desktop/src-tauri/src/computer/win.rs
+++ b/apps/desktop/src-tauri/src/computer/win.rs
@@ -633,7 +633,7 @@ fn restore_affinity(changed: &[(HWND, u32)]) {
for &(hwnd, previous) in changed {
// A hide may have taken ownership since enumeration. Hand it the original affinity
// instead of undoing its exclusion; it restores after showing, or at call end if left hidden.
- if let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_handles()) {
+ if let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_window) {
saved[index].1.1 = previous;
continue;
}
@@ -1650,10 +1650,6 @@ fn call_window(part: super::working::Part) -> Option<usize> {
call_slot(part).get().copied()
}
-fn call_handles() -> [Option<usize>; 2] {
- [call_window(super::working::Part::Dot), call_window(super::working::Part::Panel)]
-}
-
/// Exclude before hiding. If exclusion fails, still hide, then put this window back and fail
/// preparation: Working has not yet recorded it as hidden and cannot restore it on that error.
/// The failed call therefore never reaches capture, and no persistent failure poisons later calls.
@@ -1689,7 +1685,7 @@ pub fn hide_for_call(part: super::working::Part, operation: u64) -> Result<(), S
fn restore_call_affinity(hwnd: HWND) {
let mut saved = CALL_AFFINITIES.lock().unwrap_or_else(|error| error.into_inner());
- let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_handles()) else { return };
+ let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_window) else { return };
let previous = saved[index].1.1;
// This function is called only inside the showing WindowChange, after ShowWindow. Restoring
// even a nonzero affinity must count as a change, so an overlapping capture cannot be accepted.
diff --git a/apps/desktop/src-tauri/src/computer/working.rs b/apps/desktop/src-tauri/src/computer/working.rs
index 79e213c..7ce84dd 100644
--- a/apps/desktop/src-tauri/src/computer/working.rs
+++ b/apps/desktop/src-tauri/src/computer/working.rs
@@ -394,9 +394,17 @@ mod tests {
}
impl FakeWindows {
+ fn handle(&self, part: Part) -> Option<usize> {
+ let [dot, panel] = *self.handles.lock().unwrap();
+ match part {
+ Part::Dot => dot,
+ Part::Panel => panel,
+ }
+ }
+
fn restore_unrelated_capture(&self, hwnd: usize, previous: u32) {
let mut saved = self.call_affinities.lock().unwrap();
- if let Some(index) = super::super::capture_order::current_window(&saved, hwnd, *self.handles.lock().unwrap()) {
+ if let Some(index) = super::super::capture_order::current_window(&saved, hwnd, |part| self.handle(part)) {
saved[index].1.1 = previous;
return;
}