A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
refactor(desktop): pass recorded call windows as a CallHandles value
Marcelito82b4c10aa6fe
diff --git a/apps/desktop/src-tauri/src/computer/mod.rs b/apps/desktop/src-tauri/src/computer/mod.rsindex ae5e01b..d5f0bfd 100644--- a/apps/desktop/src-tauri/src/computer/mod.rs+++ b/apps/desktop/src-tauri/src/computer/mod.rs@@ -54,12 +54,28 @@ mod capture_order {restore();}+ /// The process-lifetime handles recorded at window creation, as plain values.+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]+ pub(super) struct CallHandles {+ pub(super) dot: Option<usize>,+ pub(super) panel: Option<usize>,+ }++ impl CallHandles {+ pub(super) fn get(&self, part: super::working::Part) -> Option<usize> {+ match part {+ super::working::Part::Dot => self.dot,+ super::working::Part::Panel => self.panel,+ }+ }+ }+/// 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.+ /// `handles` is a value answered by an exhaustive match, so a new part cannot go unmatched.pub(super) fn current_window(saved: &[(Option<u64>, (super::working::Part, u32))], hwnd: usize,- handle: impl Fn(super::working::Part) -> Option<usize>) -> Option<usize> {- saved.iter().position(|(_, (window, _))| handle(*window) == Some(hwnd))+ handles: CallHandles) -> Option<usize> {+ saved.iter().position(|(_, (window, _))| handles.get(*window) == Some(hwnd))}/// End call ownership, retaining an ownerless recovery value if native restoration fails.@@ -81,14 +97,11 @@ 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();- // 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),- };+ // The API accepts only recorded values, so querying Tauri is not possible here.+ let recorded = CallHandles { dot: Some(101), 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);+ assert_eq!(current_window(&guard, 101, CallHandles { dot: None, panel: None }), None);}#[test]diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rsindex cfd5ca8..f5a6b76 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_window) {+ if let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_handles()) {saved[index].1.1 = previous;continue;}@@ -1650,6 +1650,13 @@ fn call_window(part: super::working::Part) -> Option<usize> {call_slot(part).get().copied()}+fn call_handles() -> super::capture_order::CallHandles {+ super::capture_order::CallHandles {+ dot: call_window(super::working::Part::Dot),+ panel: 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.@@ -1685,7 +1692,7 @@ pub fn hide_for_call(part: super::working::Part, operation: u64) -> Result<(), Sfn 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_window) else { return };+ let Some(index) = super::capture_order::current_window(&saved, hwnd as usize, call_handles()) 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.rsindex 7ce84dd..02aebf5 100644--- a/apps/desktop/src-tauri/src/computer/working.rs+++ b/apps/desktop/src-tauri/src/computer/working.rs@@ -394,17 +394,14 @@ mod tests {}impl FakeWindows {- fn handle(&self, part: Part) -> Option<usize> {+ fn handles(&self) -> super::super::capture_order::CallHandles {let [dot, panel] = *self.handles.lock().unwrap();- match part {- Part::Dot => dot,- Part::Panel => panel,- }+ super::super::capture_order::CallHandles { dot, 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, |part| self.handle(part)) {+ if let Some(index) = super::super::capture_order::current_window(&saved, hwnd, self.handles()) {saved[index].1.1 = previous;return;}