Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

refactor(capture-native): tell windows popups by their styles and system classes, and fullness by the monitor's work area

Luis Ortiz68a084c6d4ae
diff --git a/packages/capture-native/src/win.rs b/packages/capture-native/src/win.rs
index 36b2243..f568110 100644
--- a/packages/capture-native/src/win.rs
+++ b/packages/capture-native/src/win.rs
@@ -6,7 +6,9 @@ use napi::Result;
use windows::core::{Interface, BOOL, BSTR, PCWSTR, PWSTR};
use windows::Win32::Foundation::{CloseHandle, HWND, LPARAM, POINT, RECT};
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED, DWMWA_EXTENDED_FRAME_BOUNDS};
-use windows::Win32::Graphics::Gdi::{EnumDisplayMonitors, GetMonitorInfoW, HDC, HMONITOR, MONITORINFO};
+use windows::Win32::Graphics::Gdi::{
+ GetMonitorInfoW, MonitorFromWindow, MONITORINFO, MONITOR_DEFAULTTONEAREST,
+};
use windows::Win32::Storage::FileSystem::{GetFileVersionInfoSizeW, GetFileVersionInfoW, VerQueryValueW};
use windows::Win32::System::Com::{
CoCreateInstance, CoInitializeEx, CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED,
@@ -27,7 +29,7 @@ use windows::Win32::UI::WindowsAndMessaging::{
};
use crate::hit::smallest_under;
-use crate::window::{covers_most_of_the_screen, is_popup, Traits};
+use crate::window::{fills, is_popup, Edges, Traits};
use crate::{ActiveWindow, ElementNode, ElementRect, UiElement};
const ROLES: &[(UIA_CONTROLTYPE_ID, &str)] = &[
@@ -376,28 +378,22 @@ fn frame_rect(window: HWND) -> Option<RECT> {
}
}
-fn monitor_areas() -> Vec<f64> {
- unsafe extern "system" fn collect(monitor: HMONITOR, _: HDC, _: *mut RECT, data: LPARAM) -> BOOL {
- let areas = unsafe { &mut *(data.0 as *mut Vec<f64>) };
- let mut info = MONITORINFO {
- cbSize: size_of::<MONITORINFO>() as u32,
- ..Default::default()
- };
- if unsafe { GetMonitorInfoW(monitor, &mut info) }.as_bool() {
- areas.push(area(&info.rcMonitor));
- }
- true.into()
+fn edges(rect: &RECT) -> Edges {
+ Edges {
+ left: rect.left,
+ top: rect.top,
+ right: rect.right,
+ bottom: rect.bottom,
}
- let mut areas: Vec<f64> = Vec::new();
- let _ = unsafe {
- EnumDisplayMonitors(
- None,
- None,
- Some(collect),
- LPARAM(&mut areas as *mut Vec<f64> as isize),
- )
+}
+
+fn fills_its_monitor(window: HWND, bounds: &RECT) -> bool {
+ let monitor = unsafe { MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST) };
+ let mut info = MONITORINFO {
+ cbSize: size_of::<MONITORINFO>() as u32,
+ ..Default::default()
};
- areas
+ unsafe { GetMonitorInfoW(monitor, &mut info) }.as_bool() && fills(edges(bounds), edges(&info.rcWork))
}
fn text_of(read: impl FnOnce(&mut [u16]) -> i32) -> Option<String> {
@@ -550,10 +546,7 @@ fn framed(front: HWND) -> Option<ActiveWindow> {
}
let process = process_of(front);
let bounds = frame_rect(front)?;
- let monitors = monitor_areas();
- let largest = monitors.iter().copied().fold(0.0, f64::max);
- let desktop: f64 = monitors.iter().sum();
- let window = if !covers_most_of_the_screen(area(&bounds), largest, desktop) && popup(front) {
+ let window = if !fills_its_monitor(front, &bounds) && popup(front) {
main_window_of(process).unwrap_or(front)
} else {
front
diff --git a/packages/capture-native/src/window.rs b/packages/capture-native/src/window.rs
index 648edc9..938c143 100644
--- a/packages/capture-native/src/window.rs
+++ b/packages/capture-native/src/window.rs
@@ -1,6 +1,5 @@
-const POPUP_CLASSES: &[&str] = &["#32768", "#32770", "Auto-Suggest Dropdown", "tooltips_class32"];
-const POPUP_FRAGMENTS: &[&str] = &["Popup", "DropDown", "ComboLBox"];
-const MOST_OF_THE_SCREEN: f64 = 0.8;
+const SYSTEM_POPUP_CLASSES: &[&str] = &["#32768", "#32770", "ComboLBox", "tooltips_class32"];
+const EDGE_SLACK: i32 = 4;
pub struct Traits<'a> {
pub class: &'a str,
@@ -10,24 +9,29 @@ pub struct Traits<'a> {
pub captionless_popup: bool,
}
+#[derive(Clone, Copy)]
+pub struct Edges {
+ pub left: i32,
+ pub top: i32,
+ pub right: i32,
+ pub bottom: i32,
+}
+
pub fn is_popup(traits: &Traits) -> bool {
- POPUP_CLASSES.contains(&traits.class)
- || POPUP_FRAGMENTS
- .iter()
- .any(|fragment| traits.class.contains(fragment))
- || traits.owned
- || traits.no_activate
- || traits.tool_window
- || traits.captionless_popup
+ let styled = traits.owned || traits.no_activate || traits.tool_window || traits.captionless_popup;
+ styled || SYSTEM_POPUP_CLASSES.contains(&traits.class)
}
-pub fn covers_most_of_the_screen(window: f64, largest_monitor: f64, desktop: f64) -> bool {
- window > largest_monitor * MOST_OF_THE_SCREEN || window > desktop * MOST_OF_THE_SCREEN
+pub fn fills(window: Edges, work_area: Edges) -> bool {
+ window.left <= work_area.left + EDGE_SLACK
+ && window.top <= work_area.top + EDGE_SLACK
+ && window.right >= work_area.right - EDGE_SLACK
+ && window.bottom >= work_area.bottom - EDGE_SLACK
}
#[cfg(test)]
mod tests {
- use super::{covers_most_of_the_screen, is_popup, Traits};
+ use super::{fills, is_popup, Edges, Traits};
fn plain(class: &str) -> Traits<'_> {
Traits {
@@ -39,52 +43,69 @@ mod tests {
}
}
+ fn edges(left: i32, top: i32, right: i32, bottom: i32) -> Edges {
+ Edges {
+ left,
+ top,
+ right,
+ bottom,
+ }
+ }
+
#[test]
- fn menus_dropdowns_and_dialogs_are_popups() {
- for class in [
- "#32768",
- "#32770",
- "Auto-Suggest Dropdown",
- "tooltips_class32",
- "Net UI Popup",
- "ComboLBox",
- ] {
+ fn system_menus_dialogs_lists_and_tooltips_are_popups() {
+ for class in ["#32768", "#32770", "ComboLBox", "tooltips_class32"] {
assert!(is_popup(&plain(class)), "{class}");
}
}
#[test]
- fn an_ordinary_top_level_window_is_not_a_popup() {
- assert!(!is_popup(&plain("Chrome_WidgetWin_1")));
- assert!(!is_popup(&plain("CabinetWClass")));
+ fn a_class_name_alone_never_makes_an_unstyled_window_a_popup() {
+ for class in [
+ "Chrome_WidgetWin_1",
+ "CabinetWClass",
+ "Net UI Popup",
+ "SomeDropDownHost",
+ ] {
+ assert!(!is_popup(&plain(class)), "{class}");
+ }
}
#[test]
- fn owned_tool_and_captionless_windows_are_popups() {
+ fn owned_tool_no_activate_and_captionless_windows_are_popups() {
+ let base = || plain("Chrome_WidgetWin_1");
assert!(is_popup(&Traits {
owned: true,
- ..plain("Chrome_WidgetWin_1")
+ ..base()
}));
assert!(is_popup(&Traits {
tool_window: true,
- ..plain("Chrome_WidgetWin_1")
+ ..base()
}));
assert!(is_popup(&Traits {
no_activate: true,
- ..plain("Chrome_WidgetWin_1")
+ ..base()
}));
assert!(is_popup(&Traits {
captionless_popup: true,
- ..plain("Chrome_WidgetWin_1")
+ ..base()
}));
}
#[test]
- fn most_of_the_screen_is_measured_by_area() {
- let monitor = 1920.0 * 1080.0;
- assert!(covers_most_of_the_screen(1900.0 * 1000.0, monitor, monitor));
- assert!(!covers_most_of_the_screen(400.0 * 300.0, monitor, monitor));
- assert!(covers_most_of_the_screen(1800.0 * 1000.0, monitor, monitor * 2.0));
- assert!(covers_most_of_the_screen(10.0, 0.0, 0.0));
+ fn a_window_fills_its_monitor_only_when_it_reaches_every_edge_of_the_work_area() {
+ let work = edges(0, 0, 1920, 1040);
+ assert!(fills(edges(0, 0, 1920, 1040), work));
+ assert!(fills(edges(-8, -8, 1928, 1048), work));
+ assert!(fills(edges(2, 1, 1918, 1038), work));
+ assert!(!fills(edges(0, 0, 1700, 1040), work));
+ assert!(!fills(edges(400, 300, 800, 600), work));
+ }
+
+ #[test]
+ fn filling_is_judged_against_the_monitor_the_window_is_on() {
+ let second = edges(1920, 0, 3840, 1040);
+ assert!(fills(edges(1920, 0, 3840, 1040), second));
+ assert!(!fills(edges(0, 0, 1920, 1040), second));
}
}