From 4e11be7f3fe07aacc8a1090c2833e7ccc6081909 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Sat, 7 Nov 2026 22:24:11 +0800 Subject: [PATCH] Show popups in Windows and macOS recordings and screenshots --- docs/HLSD/mcp-server.md | 21 ++-- src/slic3r/GUI/Mcp/McpRecorder.cpp | 5 +- src/slic3r/GUI/Mcp/McpToolsView.cpp | 5 +- src/slic3r/GUI/Mcp/ScreenCapture.cpp | 23 ++++- src/slic3r/GUI/Mcp/ScreenCapture.hpp | 13 ++- src/slic3r/GUI/Mcp/ScreenCapture_mac.mm | 35 +++++-- src/slic3r/GUI/Mcp/ScreenCapture_win.cpp | 123 +++++++++++++++++++---- 7 files changed, 182 insertions(+), 43 deletions(-) diff --git a/docs/HLSD/mcp-server.md b/docs/HLSD/mcp-server.md index 649d4ab660..592ea19b27 100644 --- a/docs/HLSD/mcp-server.md +++ b/docs/HLSD/mcp-server.md @@ -1620,7 +1620,7 @@ are children of the window they opened from. `ui_snapshot` lists each shown one after its window (`popups_of()`), and `root: "popup"` lists only them. A widget in a popup belongs to that window (`owning_window()`), so it is inside the topmost modal for the input guards when its popup opened from there, and its locator's `within` names that window. A screenshot of a widget in -a popup is refused, since window captures leave popups out. +a popup is refused: a screenshot of its window shows the popup. ### Plate icons @@ -2159,8 +2159,15 @@ GTK 3's `gdkwindow-wayland.c`): GDK puts a popup's origin at its offset from the shows over, plus that content's origin, less where the window GTK placed it relative to is in that window. wx's menus are placed relative to the window `popup_menu()` was given, a submenu or a drop-down menu relative to the widget it is attached to, a tooltip relative to the window it shows over, and wx's -own popups, which wx places with `gtk_window_move()`, at their offset from the content directly. macOS and -Windows window captures leave popups out. The full-size PNG goes to `save_path` or `/mcp/out/OrcaSlicer-screenshot-.png`. The image +own popups, which wx places with `gtk_window_move()`, at their offset from the content directly. Windows +and macOS draw them in too, each where its screen position puts it on the window it shows over. wx's +popups (`popups_of()`) show over the window they opened from: Windows reads one from the screen, where +it shows topmost, and macOS draws its view as it draws a window's (`capture_native_popup()`). Windows +also reads the system's popups from the screen (`capture_native_popups()`): menus, tooltips and the +lists of native drop-downs are top-level windows of the thread that showed them, and each shows over the +topmost of our windows below it that it overlaps (`EnumWindows` lists windows topmost first). AppKit +draws macOS's menus and tooltips itself, and only reading the screen, which needs the Screen Recording +permission, would capture them, so they are left out. The full-size PNG goes to `save_path` or `/mcp/out/OrcaSlicer-screenshot-.png`. The image block is scaled to `max_dim` on the long edge (default 1280, clamped to 256–2000). `include_image: false` returns only the path. @@ -2178,10 +2185,10 @@ size (fit to `max_dim`, rounded down to even numbers for 4:2:0), and a failure f any file exists. The `window` target is the main window as `screenshot` captures it, with the modal stack (`ModalTracker::stack()`, outermost first, so in stacking order) composited at each dialog's client offset, resampled to the main window's content scale off MSW. Modeless windows and native -dialogs are left out: `wxTopLevelWindows` is creation order, not stacking order. Popups, menus and -tooltips are left out on macOS and Windows; on Wayland the recording draws them last, over the -dialogs, rather than into the window each shows over (`CaptureOptions::popups`), so a drop-down list -reaching past its dialog is not cut off, and the pointer follows a wx popup to its window. On X11 the +dialogs are left out: `wxTopLevelWindows` is creation order, not stacking order. Off X11, popups, +menus and tooltips (on macOS, wx's popups only) are drawn last, over the dialogs, rather than into the window +each shows over (`CaptureOptions::popups`), so a drop-down list reaching past its dialog is not cut +off, and the pointer follows a wx popup to its window. On X11 the capture reads the screen, so it already shows our windows over the main window as they are, the script runner included when it covers it, and nothing more is composited (a reparenting window manager's frame is searched for our window inside it); the capture records the diff --git a/src/slic3r/GUI/Mcp/McpRecorder.cpp b/src/slic3r/GUI/Mcp/McpRecorder.cpp index a046c7352e..7cace84fcc 100644 --- a/src/slic3r/GUI/Mcp/McpRecorder.cpp +++ b/src/slic3r/GUI/Mcp/McpRecorder.cpp @@ -902,11 +902,8 @@ bool McpRecorder::shows(wxWindow* window) const return true; if (capture_reads_screen()) return top->IsShownOnScreen(); - // A popup shows where the recording draws it, over the window it belongs to: only where capture_popups() - // draws popups at all (Wayland). + // A popup shows where the recording draws it (capture_popups()), over the window it belongs to. if (dynamic_cast(top) != nullptr) { - if (window_positions_known()) - return false; top = owning_window(top); if (top == wxGetApp().mainframe) return true; diff --git a/src/slic3r/GUI/Mcp/McpToolsView.cpp b/src/slic3r/GUI/Mcp/McpToolsView.cpp index 4c81410e6b..c6320a57a6 100644 --- a/src/slic3r/GUI/Mcp/McpToolsView.cpp +++ b/src/slic3r/GUI/Mcp/McpToolsView.cpp @@ -424,9 +424,10 @@ void register_view_tools(McpService& service) cap = capture_canvas(*canvas, err); } else { wxWindow* top = wxGetTopLevelParent(w); - // A popup is a native window of its own, which window captures leave out. + // A popup is a native window of its own, which its window's capture draws in. if (dynamic_cast(top) != nullptr) - throw ToolFailure("The widget is in a popup, which screenshots don't capture; ui_snapshot {root: \"popup\"} lists it"); + throw ToolFailure("The widget is in a popup; a screenshot of its window shows the popup, and ui_snapshot " + "{root: \"popup\"} lists it"); auto* tlw = dynamic_cast(top); if (tlw == nullptr) throw ToolFailure("The widget has no window"); diff --git a/src/slic3r/GUI/Mcp/ScreenCapture.cpp b/src/slic3r/GUI/Mcp/ScreenCapture.cpp index b1743ada6b..5771b5584d 100644 --- a/src/slic3r/GUI/Mcp/ScreenCapture.cpp +++ b/src/slic3r/GUI/Mcp/ScreenCapture.cpp @@ -306,7 +306,28 @@ std::optional capture_window(wxTopLevelWindow& window, std::string& err } #if !defined(__WXGTK__) -std::vector capture_popups() { return {}; } +std::vector capture_popups() +{ + std::vector out; + // wx's popups, each over the window it opened from, where its screen position puts it. + for (wxWindow* top : wxTopLevelWindows) { + auto* window = dynamic_cast(top); + if (window == nullptr || !window->IsShownOnScreen() || hidden_from_automation(window)) + continue; + for (wxWindow* popup : popups_of(window)) { + PopupCapture p; + p.owner = window; + p.offset = popup->GetScreenPosition() - window->ClientToScreen(wxPoint(0, 0)); + if (capture_native_popup(*popup, p.image)) + out.push_back(std::move(p)); + } + } + // The system's over them: menus and tooltips show topmost. + for (PopupCapture& p : capture_native_popups()) + if (p.owner->IsShownOnScreen() && !hidden_from_automation(p.owner)) + out.push_back(std::move(p)); + return out; +} #endif #if defined(__WXGTK__) diff --git a/src/slic3r/GUI/Mcp/ScreenCapture.hpp b/src/slic3r/GUI/Mcp/ScreenCapture.hpp index 424834ec3e..59d7b6848b 100644 --- a/src/slic3r/GUI/Mcp/ScreenCapture.hpp +++ b/src/slic3r/GUI/Mcp/ScreenCapture.hpp @@ -70,11 +70,18 @@ struct PopupCapture wxImage image; }; -// Our popups shown now, bottom to top, where window captures leave them out but their place on their window is -// known (Wayland). Empty elsewhere: X11 captures read them from the screen, and macOS and Windows captures -// leave them out. +// Our popups shown now, bottom to top, which window captures leave out. Empty on X11, whose captures read them +// from the screen. macOS has wx's popups only: AppKit draws its menus and tooltips itself, and only reading the +// screen, which needs the Screen Recording permission, would capture them. std::vector capture_popups(); +// The OS parts of capture_popups() on Windows and macOS. capture_native_popup() draws one of wx's popups from its +// GetScreenPosition() on, at its scale: Windows reads it from the screen, where it shows topmost, and macOS +// draws its view. capture_native_popups() gives the system's popups over our windows, bottom to top: menus, +// tooltips and the lists of native drop-downs on Windows, none on macOS. +bool capture_native_popup(wxWindow& popup, wxImage& out); +std::vector capture_native_popups(); + // Whether a window capture reads the screen, and so shows our own windows over the target (X11). bool capture_reads_screen(); // Whether the app knows where its top-level windows are on the screen (not on Wayland). diff --git a/src/slic3r/GUI/Mcp/ScreenCapture_mac.mm b/src/slic3r/GUI/Mcp/ScreenCapture_mac.mm index 54f49aefcc..5ba5cd2f4d 100644 --- a/src/slic3r/GUI/Mcp/ScreenCapture_mac.mm +++ b/src/slic3r/GUI/Mcp/ScreenCapture_mac.mm @@ -2,10 +2,13 @@ #include "ScreenCapture.hpp" +#include #include +#include #include #include +#include #import @@ -13,17 +16,16 @@ namespace Slic3r { namespace GUI { namespace Mcp { std::shared_ptr open_capture_sessions() { return nullptr; } -bool capture_native_window(wxTopLevelWindow& window, CaptureSessions* /*sessions*/, wxImage& out, bool& approximate, std::string& note, - std::vector& /*covered*/, std::string& err) +namespace { + +// The view drawn into a bitmap, which needs no screen-recording permission and works for covered windows. +// Layer-backed OpenGL and web views don't draw this way. +bool draw_view(NSView* view, wxImage& out, std::string& err) { - NSView* view = (NSView*) window.GetHandle(); if (view == nil) { err = "The window has no native view"; return false; } - // The content view draws itself into a bitmap, so this needs no screen-recording permission - // and works for covered windows. Layer-backed OpenGL and web views don't draw this way; the 3D - // canvases are composited from their own frames afterwards, web views may stay blank. NSRect bounds = [view bounds]; NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:bounds]; if (rep == nil) { @@ -49,9 +51,30 @@ bool capture_native_window(wxTopLevelWindow& window, CaptureSessions* /*sessions rgb[2] = row[x * spp + 2]; } } + return true; +} + +} // namespace + +bool capture_native_window(wxTopLevelWindow& window, CaptureSessions* /*sessions*/, wxImage& out, bool& approximate, std::string& note, + std::vector& /*covered*/, std::string& err) +{ + // The 3D canvases are composited from their own frames afterwards; web views may stay blank. + if (!draw_view((NSView*) window.GetHandle(), out, err)) + return false; approximate = true; note = "drawn from the view hierarchy; web views may be blank"; return true; } +// A wx popup's content view is all of it: wx makes it a borderless window. +bool capture_native_popup(wxWindow& popup, wxImage& out) +{ + std::string err; + return draw_view((NSView*) popup.GetHandle(), out, err); +} + +// AppKit draws its menus and tooltips itself; only reading the screen would capture them. +std::vector capture_native_popups() { return {}; } + }}} // namespace Slic3r::GUI::Mcp diff --git a/src/slic3r/GUI/Mcp/ScreenCapture_win.cpp b/src/slic3r/GUI/Mcp/ScreenCapture_win.cpp index 36f64567b1..c251ac84d1 100644 --- a/src/slic3r/GUI/Mcp/ScreenCapture_win.cpp +++ b/src/slic3r/GUI/Mcp/ScreenCapture_win.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +62,48 @@ namespace { constexpr auto kFormat = ABI::Windows::Graphics::DirectX::DirectXPixelFormat_B8G8R8A8UIntNormalized; +// The pixels of `bmp`, which must not be selected into a device context. +bool read_bitmap(HDC dc, HBITMAP bmp, int w, int h, wxImage& out) +{ + BITMAPINFO bi{}; + bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bi.bmiHeader.biWidth = w; + bi.bmiHeader.biHeight = -h; // top-down rows + bi.bmiHeader.biPlanes = 1; + bi.bmiHeader.biBitCount = 32; + bi.bmiHeader.biCompression = BI_RGB; + std::vector bgra(size_t(w) * size_t(h) * 4); + if (GetDIBits(dc, bmp, 0, UINT(h), bgra.data(), &bi, DIB_RGB_COLORS) != h) + return false; + out.Create(w, h, false); + unsigned char* rgb = out.GetData(); + for (size_t i = 0; i < size_t(w) * size_t(h); ++i, rgb += 3) { + rgb[0] = bgra[i * 4 + 2]; + rgb[1] = bgra[i * 4 + 1]; + rgb[2] = bgra[i * 4 + 0]; + } + return true; +} + +// What the screen shows in `r`, layered windows such as a fading menu or tooltip included. +bool read_screen(const RECT& r, wxImage& out) +{ + const int w = r.right - r.left, h = r.bottom - r.top; + if (w <= 0 || h <= 0) + return false; + HDC screen = GetDC(nullptr); + HDC mem = CreateCompatibleDC(screen); + HBITMAP bmp = CreateCompatibleBitmap(screen, w, h); + HGDIOBJ old = SelectObject(mem, bmp); + const BOOL copied = BitBlt(mem, 0, 0, w, h, screen, r.left, r.top, SRCCOPY | CAPTUREBLT); + SelectObject(mem, old); + const bool read = copied && read_bitmap(mem, bmp, w, h, out); + DeleteObject(bmp); + DeleteDC(mem); + ReleaseDC(nullptr, screen); + return read; +} + // PrintWindow makes the window and its children repaint into the bitmap, a repaint the screen can show as a // flash of the half-drawn window; it is what is left without Windows Graphics Capture. bool print_window(HWND hwnd, wxImage& out, bool& approximate, std::string& note, std::string& err) @@ -87,35 +131,42 @@ bool print_window(HWND hwnd, wxImage& out, bool& approximate, std::string& note, BitBlt(mem, 0, 0, w, h, screen, 0, 0, SRCCOPY); } SelectObject(mem, old); - - BITMAPINFO bi{}; - bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bi.bmiHeader.biWidth = w; - bi.bmiHeader.biHeight = -h; // top-down rows - bi.bmiHeader.biPlanes = 1; - bi.bmiHeader.biBitCount = 32; - bi.bmiHeader.biCompression = BI_RGB; - std::vector bgra(size_t(w) * size_t(h) * 4); - const int lines = GetDIBits(mem, bmp, 0, UINT(h), bgra.data(), &bi, DIB_RGB_COLORS); - + const bool read = read_bitmap(mem, bmp, w, h, out); DeleteObject(bmp); DeleteDC(mem); ReleaseDC(hwnd, screen); - if (lines != h) { + if (!read) { err = "Reading the window bitmap failed"; return false; } - - out.Create(w, h, false); - unsigned char* rgb = out.GetData(); - for (size_t i = 0; i < size_t(w) * size_t(h); ++i, rgb += 3) { - rgb[0] = bgra[i * 4 + 2]; - rgb[1] = bgra[i * 4 + 1]; - rgb[2] = bgra[i * 4 + 0]; - } return true; } +BOOL CALLBACK collect_own_window(HWND hwnd, LPARAM windows) +{ + if (IsWindowVisible(hwnd) && GetWindowThreadProcessId(hwnd, nullptr) == GetCurrentThreadId()) + reinterpret_cast*>(windows)->push_back(hwnd); + return TRUE; +} + +// A menu or submenu, a tooltip, or the list of a native combo box or choice: the system shows each in a +// top-level window of the thread that asked for it. +bool is_system_popup(HWND hwnd) +{ + wchar_t name[32] = {}; + if (GetClassNameW(hwnd, name, int(std::size(name))) == 0) + return false; + return wcscmp(name, L"#32768") == 0 || wcscmp(name, L"tooltips_class32") == 0 || wcscmp(name, L"ComboLBox") == 0; +} + +wxTopLevelWindow* top_level_window_of(HWND hwnd) +{ + for (wxWindow* w : wxTopLevelWindows) + if (w->GetHWND() == hwnd) + return dynamic_cast(w); + return nullptr; +} + void check(HRESULT hr, const char* what) { if (FAILED(hr)) { @@ -426,4 +477,36 @@ bool capture_native_window(wxTopLevelWindow& window, CaptureSessions* sessions, return print_window(hwnd, out, approximate, note, err); } +bool capture_native_popup(wxWindow& popup, wxImage& out) +{ + RECT r{}; + return GetWindowRect(static_cast(popup.GetHWND()), &r) && read_screen(r, out); +} + +std::vector capture_native_popups() +{ + // The UI thread's shown top-level windows, topmost first, as EnumWindows lists them. + std::vector windows; + EnumWindows(collect_own_window, reinterpret_cast(&windows)); + std::vector out; + for (std::size_t i = windows.size(); i-- > 0;) { + RECT r{}; + if (!is_system_popup(windows[i]) || !GetWindowRect(windows[i], &r)) + continue; + // It shows over the topmost of our windows below it that it overlaps, the one it opened over. + PopupCapture p; + for (std::size_t j = i + 1; j < windows.size() && p.owner == nullptr; ++j) { + RECT below{}, both{}; + if (wxTopLevelWindow* top = top_level_window_of(windows[j]); + top != nullptr && GetWindowRect(windows[j], &below) && IntersectRect(&both, &r, &below)) + p.owner = top; + } + if (p.owner == nullptr || !read_screen(r, p.image)) + continue; + p.offset = wxPoint(r.left, r.top) - p.owner->ClientToScreen(wxPoint(0, 0)); + out.push_back(std::move(p)); + } + return out; +} + }}} // namespace Slic3r::GUI::Mcp