A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(desktop): keep the panel out of standalone Windows screenshots
Marcelito36a831376ef4
diff --git a/apps/desktop/src-tauri/src/computer/app.rs b/apps/desktop/src-tauri/src/computer/app.rsindex 80b8e7e..860931f 100644--- a/apps/desktop/src-tauri/src/computer/app.rs+++ b/apps/desktop/src-tauri/src/computer/app.rs@@ -145,8 +145,7 @@ pub fn show_without_focus(window: &tauri::WebviewWindow) -> Result<(), String> {// Both sides bypass tao so its visibility flag continues to represent the user's choice.#[cfg(windows)]pub fn hide_for_call(window: &tauri::WebviewWindow) -> Result<(), String> {- super::win::hide_window(window.hwnd().map_err(|e| e.to_string())?.0);- Ok(())+ super::win::hide_for_call(window.hwnd().map_err(|e| e.to_string())?.0)}/// A window of Gentle Dot is shown or hidden through tao (the panel, the Dot) while the returneddiff --git a/apps/desktop/src-tauri/src/computer/mod.rs b/apps/desktop/src-tauri/src/computer/mod.rsindex 7f45a12..48b2d01 100644--- a/apps/desktop/src-tauri/src/computer/mod.rs+++ b/apps/desktop/src-tauri/src/computer/mod.rs@@ -35,6 +35,61 @@ pub mod winapps;pub mod winput;pub mod working;+// Windows capture ordering, kept platform-independent so CI exercises the contract.+#[cfg(any(windows, test))]+mod capture_order {+ pub(super) fn capture<T>(flush: impl FnOnce() -> Result<(), String>, grab: impl FnOnce() -> Result<T, String>) -> Result<T, String> {+ flush()?;+ grab()+ }++ pub(super) fn hide<T>(exclude: impl FnOnce() -> Result<T, String>, hide: impl FnOnce()) -> Result<T, String> {+ let excluded = exclude();+ hide();+ excluded+ }++ pub(super) fn show(show: impl FnOnce(), restore: impl FnOnce()) {+ show();+ restore();+ }++ #[cfg(test)]+ mod tests {+ use super::*;+ use std::cell::RefCell;++ #[test]+ fn capture_waits_even_without_new_exclusions() {+ let events = RefCell::new(Vec::new());+ capture(|| { events.borrow_mut().push("flush"); Ok(()) }, || { events.borrow_mut().push("grab"); Ok(()) }).unwrap();+ assert_eq!(*events.borrow(), ["flush", "grab"]);+ }++ #[test]+ fn failed_composition_never_grabs() {+ let result = capture(|| Err("composition failed".into()), || panic!("must not grab"));+ assert_eq!(result, Err::<(), _>("composition failed".into()));+ }++ #[test]+ fn exclusion_spans_hide_through_show() {+ let events = RefCell::new(Vec::new());+ let previous = hide(|| { events.borrow_mut().push("exclude"); Ok(7) }, || events.borrow_mut().push("hide")).unwrap();+ show(|| events.borrow_mut().push("show"), || { assert_eq!(previous, 7); events.borrow_mut().push("restore"); });+ assert_eq!(*events.borrow(), ["exclude", "hide", "show", "restore"]);+ }++ #[test]+ fn failed_exclusion_still_hides_and_remains_an_error() {+ let hidden = std::cell::Cell::new(false);+ let result = hide(|| Err::<(), _>("exclusion failed".into()), || hidden.set(true));+ assert!(hidden.get());+ assert_eq!(result, Err("exclusion failed".into()));+ }+ }+}+use blocklist::AppIdentity;use coords::{Point, ScreenshotGeometry};use keys::KeyCombo;diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rsindex 3cd3f08..842fea0 100644--- a/apps/desktop/src-tauri/src/computer/win.rs+++ b/apps/desktop/src-tauri/src/computer/win.rs@@ -416,7 +416,7 @@ fn draw_pointer(canvas: &Canvas, frame: MonitorFrame, width: u32, height: u32) {pub fn capture() -> Result<Capture, String> {let baseline = capture_baseline()?;let changed = exclude_own_windows()?;- let shot = grab();+ let shot = super::capture_order::capture(wait_for_composition, grab);// A window of ours can show during the copy and be gone by now (the user opens the panel and// closes it again, Stop gives the Dot back, a dialog opens and closes). Every change of our// windows runs inside a `WindowChange`, and an image taken while one overlapped it is not@@ -519,22 +519,37 @@ fn is_excluded(hwnd: HWND) -> bool {(unsafe { GetWindowDisplayAffinity(hwnd, &mut affinity) }) != 0 && affinity == WDA_EXCLUDEFROMCAPTURE}+/// Wait even when no visible windows needed exclusion: a just-hidden panel may still be composed.+fn wait_for_composition() -> Result<(), String> {+ // SAFETY: a plain call; it waits for the next composition.+ let result = unsafe { DwmFlush() };+ if result < 0 {+ Err(format!("Cannot wait for Windows composition (DwmFlush HRESULT 0x{:08X}), so no screenshot was taken.", result as u32))+ } else {+ Ok(())+ }+}+/// Makes sure no visible window of this process shows in the capture about to be taken, and-/// returns the ones it had to exclude, for `restore_affinity` once the capture is taken. A window-/// newly excluded is out of the picture after the next composition (`DwmFlush`).-fn exclude_own_windows() -> Result<Vec<HWND>, String> {+/// returns the ones it had to exclude, for `restore_affinity` once the capture is taken.+fn exclude_own_windows() -> Result<Vec<(HWND, u32)>, String> {let mut changed = Vec::new();let windows = own_visible_windows().map_err(|error| format!("Gentle Dot's window would show in the screenshot: {error}"))?;for hwnd in windows.into_iter().filter(|&hwnd| !is_excluded(hwnd)) {+ let mut previous = 0;+ // SAFETY: a query on our window with a valid out-pointer.+ if unsafe { GetWindowDisplayAffinity(hwnd, &mut previous) } == 0 {+ restore_affinity(&changed);+ return Err(format!("{OWN_WINDOW_SHOWN} Cannot read its capture affinity."));+ }+ if previous == WDA_EXCLUDEFROMCAPTURE {+ continue;+ }if let Err(error) = set_capture_excluded(hwnd, true) {restore_affinity(&changed);return Err(format!("Gentle Dot's window would show in the screenshot: {error}"));}- changed.push(hwnd);- }- if !changed.is_empty() {- // SAFETY: a plain call; it waits for the next composition.- unsafe { DwmFlush() };+ changed.push((hwnd, previous));}Ok(changed)}@@ -598,10 +613,19 @@ fn unchanged_since(baseline: u64) -> bool {/// Lets the windows a capture excluded show in screenshots again, as they did before it. A window/// that vanished meanwhile just fails; any failure is logged, since a window left out would be/// missing from the user's own screenshots.-fn restore_affinity(changed: &[HWND]) {- for &hwnd in changed {- if let Err(error) = set_capture_excluded(hwnd, false) {- eprintln!("gentle-dot: a window may stay out of screenshots: {error}");+fn restore_affinity(changed: &[(HWND, u32)]) {+ let mut saved = CALL_AFFINITIES.lock().unwrap_or_else(|error| error.into_inner());+ for &(hwnd, previous) in changed {+ // A hide may have taken ownership since enumeration. Hand it the original affinity+ // instead of undoing its exclusion; it will restore only after showing the window.+ if let Some((_, original)) = saved.iter_mut().find(|(window, _)| *window == hwnd as usize) {+ *original = previous;+ continue;+ }+ let _change = WindowChange::begin();+ // SAFETY: restore the value read from this window before excluding it.+ if unsafe { SetWindowDisplayAffinity(hwnd, previous) } == 0 {+ eprintln!("gentle-dot: a window may stay out of screenshots: {}", std::io::Error::last_os_error());}}}@@ -1517,10 +1541,61 @@ pub fn show_overlay(hwnd: *mut c_void, visible: bool) {}}+// Main-thread show/hide callers serialize these entries. Keep the original value, including+// WDA_MONITOR, rather than assuming every window started at WDA_NONE. A user-hidden Dot retains+// its entry only until it is next shown; no visible window stays excluded for the session.+static CALL_AFFINITIES: std::sync::Mutex<Vec<(usize, u32)>> = std::sync::Mutex::new(Vec::new());++/// 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.+pub fn hide_for_call(hwnd: *mut c_void) -> Result<(), String> {+ let _change = WindowChange::begin();+ let mut saved = CALL_AFFINITIES.lock().unwrap_or_else(|error| error.into_inner());+ let result = super::capture_order::hide(|| {+ if saved.iter().any(|&(window, _)| window == hwnd as usize) {+ return set_capture_excluded(hwnd, true);+ }+ let mut previous = 0;+ // SAFETY: a query on our window with a valid out-pointer.+ if unsafe { GetWindowDisplayAffinity(hwnd, &mut previous) } == 0 {+ return Err(format!("cannot read its capture affinity: {}", std::io::Error::last_os_error()));+ }+ set_capture_excluded(hwnd, true)?;+ saved.push((hwnd as usize, previous));+ Ok(())+ }, || {+ // SAFETY: a call on this process's window.+ unsafe { ShowWindow(hwnd, SW_HIDE) };+ });+ drop(saved);+ if let Err(error) = result {+ show_without_activating(hwnd);+ return Err(format!("{OWN_WINDOW_SHOWN} {error}"));+ }+ Ok(())+}++fn restore_call_affinity(hwnd: HWND) {+ let mut saved = CALL_AFFINITIES.lock().unwrap_or_else(|error| error.into_inner());+ let Some(index) = saved.iter().position(|&(window, _)| window == hwnd as usize) else { return };+ let previous = saved[index].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.+ // SAFETY: a call on this process's window, restoring the value read before the call.+ if unsafe { SetWindowDisplayAffinity(hwnd, previous) } != 0 {+ saved.remove(index);+ } else {+ eprintln!("gentle-dot: a window may stay out of screenshots: {}", std::io::Error::last_os_error());+ }+}+pub fn show_without_activating(hwnd: *mut c_void) {let _change = WindowChange::begin();- // SAFETY: a call on this process's window.- unsafe { ShowWindow(hwnd, SW_SHOWNOACTIVATE) };+ super::capture_order::show(|| {+ // SAFETY: a call on this process's window.+ unsafe { ShowWindow(hwnd, SW_SHOWNOACTIVATE) };+ }, || restore_call_affinity(hwnd));}pub fn hide_window(hwnd: *mut c_void) {@@ -1531,8 +1606,10 @@ pub fn hide_window(hwnd: *mut c_void) {pub fn show_window(hwnd: *mut c_void) {let _change = WindowChange::begin();- // SAFETY: a call on this process's window.- unsafe { ShowWindow(hwnd, SW_SHOW) };+ super::capture_order::show(|| {+ // SAFETY: a call on this process's window.+ unsafe { ShowWindow(hwnd, SW_SHOW) };+ }, || restore_call_affinity(hwnd));}/// Move the glow over a display without activating it.@@ -1847,7 +1924,7 @@ mod tests {// Every route is a whole change.let routes: [(&str, &dyn Fn()); 5] = [("let show again", &|| { let _ = set_capture_excluded(null_mut(), false); }),- ("restored after a capture", &|| restore_affinity(&[null_mut()])),+ ("restored after a capture", &|| restore_affinity(&[(null_mut(), WDA_NONE)])),("shown", &|| show_window(null_mut())),("shown without activation", &|| show_without_activating(null_mut())),("hidden", &|| hide_window(null_mut())),