package main import "strings" // Shell surfaces often have an empty title and are omitted by upstream's // ordinary application-window enumeration. They still require exact HWND/PID. func shellKind(class string) string { switch class { case "Shell_TrayWnd", "Shell_SecondaryTrayWnd": return "taskbar" case "NotifyIconOverflowWindow", "TopLevelWindowForOverflowXamlIsland": return "tray" case "#32768": return "menu" case "Progman", "WorkerW": return "desktop" } return "" } func shellMatches(window, query obj) bool { if query["pid"] != nil && num(query["pid"]) != num(window["pid"]) { return false } app := strings.TrimSuffix(strings.ToLower(str(query["app"])), ".exe") name := strings.TrimSuffix(strings.ToLower(str(window["app_name"])), ".exe") return (app == "" || strings.Contains(name, app)) && (query["on_screen_only"] != true || window["is_on_screen"] == true) } func supplementShellWindows(result, query obj, native []obj) obj { if result["isError"] == true { return result } data := object(result["structuredContent"]) windows, ok := data["windows"].([]any) if !ok { // Do not turn an upstream observation failure into a valid list. return result } known := map[int64]obj{} for _, value := range windows { w := object(value) known[num(w["window_id"])] = w } for _, w := range native { if shellKind(str(w["class_name"])) == "" || !shellMatches(w, query) { continue } if old, exists := known[num(w["window_id"])]; exists { old["class_name"] = w["class_name"] old["window_kind"] = shellKind(str(w["class_name"])) } else { w["window_kind"] = shellKind(str(w["class_name"])) windows = append(windows, w) known[num(w["window_id"])] = w } } data["windows"] = windows result["structuredContent"] = data return result } // Follow only native keyboard focus or a uniquely associated active menu. // A different foreground application never silently becomes screenshot scope. func postActionTarget(args, requested, focus obj, candidates []obj) (obj, string) { selected := object(focus["focus_root"]) if focus["probe_ok"] != true { return observationArgs(args), "keyboard_focus_unavailable" } if focus["menu_active"] == true { owner := object(focus["menu_owner"]) var menus []obj for _, w := range candidates { if shellKind(str(w["class_name"])) == "menu" && w["is_on_screen"] == true && num(w["pid"]) == num(args["pid"]) && num(owner["pid"]) == num(args["pid"]) && (num(w["owner_id"]) == num(owner["window_id"]) || (num(w["thread_id"]) != 0 && num(w["thread_id"]) == num(owner["thread_id"]))) { menus = append(menus, w) } } if len(menus) == 1 { selected = menus[0] } else if len(menus) > 1 { return observationArgs(args), "ambiguous_active_menu_observe_candidates_explicitly" } } if num(selected["window_id"]) == num(args["window_id"]) { return observationArgs(args), "keyboard_focus_remains_in_requested_window" } if num(selected["pid"]) != num(args["pid"]) || selected["is_on_screen"] != true { return observationArgs(args), "unrelated_or_unverified_focus_not_followed" } kind := shellKind(str(selected["class_name"])) relatedShell := shellKind(str(requested["class_name"])) != "" && kind != "" && kind != "desktop" menuOwner := object(focus["menu_owner"]) // Native #32768 menus can report GW_OWNER=0. GetGUIThreadInfo still // identifies their owning window; the candidate was also matched by PID // and UI thread above. Do not broaden this to arbitrary same-PID windows. relatedPopup := kind == "menu" && (num(selected["owner_id"]) == num(args["window_id"]) || (focus["menu_active"] == true && num(menuOwner["window_id"]) == num(args["window_id"]))) if !relatedShell && !relatedPopup { return observationArgs(args), "unrelated_focus_not_followed" } next := observationArgs(args) next["pid"], next["window_id"] = selected["pid"], selected["window_id"] return next, "associated_native_keyboard_focus" }