diff --git a/.claude/skills/orca-wxwidgets/references/popups-menus.md b/.claude/skills/orca-wxwidgets/references/popups-menus.md index d165ceff0a..e96e00dfaa 100644 --- a/.claude/skills/orca-wxwidgets/references/popups-menus.md +++ b/.claude/skills/orca-wxwidgets/references/popups-menus.md @@ -81,6 +81,11 @@ Contents: [Rules](#rules) · [1. Choosing the window kind](#1-choosing-the-windo tooltip of a widget in that popup, or in a popup opened from it, is on screen: the tooltip has taken the keyboard while the user is still on the popup. `PopupWindow::topWindowActiavate` does it for every Orca popup; a popup outside `PopupWindow` with such a hook must do it itself. (§9) +31. When a dialog that hides on deactivation closes by itself, find the code that activated another window + instead of hardening the hook: on wxGTK, `SetFocus()` on a child of a visible, inactive toplevel presents + that toplevel. A handler bound on a book control acts only on events whose object is that book, because a + page-changed event from a book inside a page propagates up to it. A deferred, re-checked hide tolerates only + a focus bounce shorter than its delay. (§10) ## 1. Choosing the window kind @@ -827,8 +832,9 @@ created with `wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT | wxF - `wxFRAME_NO_TASKBAR` gives no taskbar entry on MSW/GTK, on GTK only when the WM supports `_NET_WM_STATE_SKIP_TASKBAR` (`interface/wx/frame.h:97-103`). - `wxFRAME_FLOAT_ON_PARENT` requires a non-null parent (`interface/wx/frame.h:104-106`). -- Its `wxEVT_ACTIVATE` handler hides on deactivation and `Skip()`s. On activation it focuses the web view, because - modeless focus is granted asynchronously and a focus request made right after `Show()` is dropped. +- Its `wxEVT_ACTIVATE` handler focuses the web view on activation, because modeless focus is granted + asynchronously and a focus request made right after `Show()` is dropped. It hides on deactivation after a + short settle delay and a re-check of `gtk_window_is_active()` (Pitfalls below), and it `Skip()`s. - On Linux `focus_webview` grabs focus on the WebKit native widget (`gtk_widget_grab_focus` on `GetNativeBackend()`). `browser()->SetFocus()` focuses only the container. - Display-only web content can stay in a transient popup (`MarkdownTip`). @@ -847,13 +853,39 @@ created with `wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT | wxF wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT | wxFRAME_SHAPED) { Bind(wxEVT_ACTIVATE, [this](wxActivateEvent& e) { - if (e.GetActive() && IsShown()) focus_webview(browser(), m_page_ready); - else if (!e.GetActive() && IsShown()) Hide(); + if (!IsShown()) { e.Skip(); return; } + if (e.GetActive()) { m_hide_timer.Stop(); focus_webview(browser(), m_page_ready); } + else schedule_hide(); // validated, see the next rule e.Skip(); }); } ``` Cite: f142716e51, a86202b45b (`SpeedDialPopup.cpp` replaced by `SpeedDialDialog.{hpp,cpp}`; `GUI_App.cpp`). +- **Rule:** When a dialog that hides on deactivation closes by itself, find the code that activated another + window; do not harden the dismissal hook. A handler bound on a book control checks that `e.GetEventObject()` + is that book before acting. + **Why:** On wxGTK, `wxWindow::SetFocus()` on a child of a visible but inactive toplevel calls + `gtk_window_present` on that toplevel (`src/gtk/window.cpp:5157-5162`), so an unexpected `SetFocus()` anywhere + in the main frame takes the keyboard from a modeless dialog that hides on deactivation. The Speed Dial closed + by itself a moment after the first Space on Linux (GNOME and KDE, X11 and Wayland): the idle builder + constructed the Calibration page, whose `Tabbook::InsertPage` emitted `wxEVT_BOOKCTRL_PAGE_CHANGED`; the event + propagated, as every `wxNotifyEvent` does, to the main frame's `m_tabpanel` handler + (`wxEVT_NOTEBOOK_PAGE_CHANGED` is the same type, `include/wx/bookctrl.h:434`), which treated it as a main tab + change and ended with `panel->SetFocus()`. Found with a 10 ms focus poller and a backtrace on + `gtk_window_present`. MSW and macOS `SetFocus()` do not activate the toplevel, so the bug was Linux-only. The + dial's deferred, re-checked hide (`kHideSettleMs`, `gtk_window_is_active()`, since `wxTopLevelWindowGTK::IsActive()` + answers from `g_activeFrame`, which the deactivation has already cleared) absorbs only a focus bounce shorter + than the delay; it did not stop this. + ```cpp + // Wrong: the main book's handler acts on every page-changed event that reaches it + m_tabpanel->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, [this](wxBookCtrlEvent& e) { /* ... */ panel->SetFocus(); }); + // Right: only the book's own events + m_tabpanel->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, [this](wxBookCtrlEvent& e) { + if (e.GetEventObject() != m_tabpanel) { e.Skip(); return; } + /* ... */ + }); + ``` + Cite: #17245 (`MainFrame::init_tabpanel`; `SpeedDialWebDialog::on_hide_timer`). - **Rule:** When restoring the main frame after hiding a popup or overlay frame, call `Show()` only if `!IsShown()`, and `Raise()` unconditionally (top-level `Show`/`Raise` are owned by `references/windows-dialogs.md`). diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 203af85de2..7feed2ca47 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -1510,6 +1510,14 @@ void MainFrame::init_tabpanel() { #else m_tabpanel->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, [this](wxBookCtrlEvent& e) { #endif + // A page's own book (the Calibration page's Tabbook) emits the same event, and it propagates + // up here. Treating it as a main tab change resets the selected tab and the undo/redo state, + // and the SetFocus() below re-activates the main window on wxGTK, which dismisses the Speed + // Dial when the page is built in the background while the dial is open. + if (e.GetEventObject() != m_tabpanel) { + e.Skip(); + return; + } //BBS wxWindow* panel = m_tabpanel->GetCurrentPage(); //wxString page_text = m_tabpanel->GetPageText(sel); diff --git a/src/slic3r/GUI/SpeedDialDialog.cpp b/src/slic3r/GUI/SpeedDialDialog.cpp index 17fe5ccfeb..e8086f71e5 100644 --- a/src/slic3r/GUI/SpeedDialDialog.cpp +++ b/src/slic3r/GUI/SpeedDialDialog.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,10 @@ namespace { constexpr int kPopupWidth = 540; constexpr int kPopupMinHeight = 60; // just above the bare search-bar height, so the dialog hugs content constexpr int kPopupMaxHeight = 282; +// Delay between a deactivation and the auto-hide decision, so that a focus bounce shorter than this (the +// keyboard leaving and coming back within the interval) does not dismiss the dial. A window that really +// takes the focus still hides it when the timer fires. +constexpr int kHideSettleMs = 150; int json_int_or(const nlohmann::json& j, const char* key, int fallback) { @@ -146,6 +151,8 @@ wxPoint speed_dial_origin(const wxRect& parent_screen_rect, const wxRect& work_a std::clamp(y, work_area.y, work_area.y + std::max(0, work_area.height - box_size.GetHeight()))}; } +bool speed_dial_should_hide(bool shown, bool active) { return shown && !active; } + SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent) : WebViewHostDialog(parent, wxID_ANY, @@ -160,14 +167,26 @@ SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent) // keeps that position for every later Show(). position_over_parent() supplies the origin instead. gtk_window_set_position(GTK_WINDOW(GetHandle()), GTK_WIN_POS_NONE); #endif + // The dial dismisses itself when the focus leaves it. The hide is deferred by kHideSettleMs and + // re-checked, so a focus bounce shorter than that is not a dismissal. It does not protect against + // another window being activated: the first-open close on Linux was the main frame's tab handler + // calling SetFocus() while the Calibration page was built in the background, which on wxGTK presents + // the main window (fixed in MainFrame::init_tabpanel). + m_hide_timer.SetOwner(this); + Bind(wxEVT_TIMER, &SpeedDialWebDialog::on_hide_timer, this); Bind(wxEVT_ACTIVATE, [this](wxActivateEvent& event) { // Focus the WebKit widget exactly when the WM makes the popup the active window // (modeless focus is granted asynchronously, so a focus request made right after // Show() is dropped). Also re-corrects focus on every re-open. - if (event.GetActive() && IsShown()) + if (!IsShown()) { + event.Skip(); + return; + } + if (event.GetActive()) { + m_hide_timer.Stop(); focus_webview(browser(), m_page_ready); - else if (!event.GetActive() && IsShown()) - Hide(); + } else + schedule_hide(); event.Skip(); }); if (!create_webview("web/dialog/SpeedDial/index.html", wxEmptyString, wxSize(kPopupWidth, kPopupMaxHeight), @@ -192,7 +211,11 @@ SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent) position_over_parent(); } -SpeedDialWebDialog::~SpeedDialWebDialog() { m_alive->store(false, std::memory_order_release); } +SpeedDialWebDialog::~SpeedDialWebDialog() +{ + m_hide_timer.Stop(); + m_alive->store(false, std::memory_order_release); +} nlohmann::json SpeedDialWebDialog::localized_strings() const { return speed_dial_ui_strings(); } @@ -217,8 +240,36 @@ void SpeedDialWebDialog::position_over_parent() #endif } +void SpeedDialWebDialog::schedule_hide() +{ + if (IsShown()) + m_hide_timer.StartOnce(kHideSettleMs); +} + +bool SpeedDialWebDialog::is_active() +{ +#ifdef __WXGTK__ + GtkWindow* const window = GTK_WINDOW(GetHandle()); + return window && gtk_window_is_active(window); +#else + return IsActive(); +#endif +} + +void SpeedDialWebDialog::on_hide_timer(wxTimerEvent&) +{ + if (!m_alive->load(std::memory_order_acquire)) + return; + if (!speed_dial_should_hide(IsShown(), is_active())) + return; + Hide(); +} + void SpeedDialWebDialog::request_show() { + // A pending auto-hide belongs to the previous open. + m_hide_timer.Stop(); + // Re-derived on every open: the dialog is a long-lived hidden window, so without this the // coordinates stay at the frame's position from the first open and the dial stops following the // frame when it is moved, resized, or dragged to another display. @@ -237,8 +288,11 @@ void SpeedDialWebDialog::request_show() if (m_page_ready) send_actions(); // Grab focus now and again on wxEVT_ACTIVATE; grabbing directly on the WebKit widget is - // what makes typing reach the search field immediately on open. - focus_webview(browser(), m_page_ready); + // what makes typing reach the search field immediately on open. Skipped while the page is still + // loading: on the first open the window is not mapped yet, so the grab would land on a window the + // WM has not activated - the ACTIVATE handler grabs it once it has. + if (m_page_ready) + focus_webview(browser(), true); repaint_webview(); // A position requested before the window is mapped can be dropped, so reapply once it is up. wxGetApp().CallAfter([this, alive = m_alive]() { diff --git a/src/slic3r/GUI/SpeedDialDialog.hpp b/src/slic3r/GUI/SpeedDialDialog.hpp index 851aaea7f5..9fe28d7510 100644 --- a/src/slic3r/GUI/SpeedDialDialog.hpp +++ b/src/slic3r/GUI/SpeedDialDialog.hpp @@ -9,6 +9,7 @@ #include #include +#include class wxWindow; @@ -20,6 +21,11 @@ namespace Slic3r { namespace GUI { // resize in resize_to_content() never moves the window. wxPoint speed_dial_origin(const wxRect& parent_screen_rect, const wxRect& work_area, const wxSize& box_size); +// Whether a deactivation really dismissed the dial: only when it is still shown and no longer holds the +// focus. Evaluated after the settle delay, never on the event itself, so a focus bounce shorter than the +// delay is ignored. +bool speed_dial_should_hide(bool shown, bool active); + class SpeedDialWebDialog : public WebViewHostDialog { public: @@ -35,6 +41,14 @@ private: // on every open, since the dialog is a long-lived hidden window: without this the coordinates // stay at the frame's position from the first open (frozen at the first map by the WM). void position_over_parent(); + // Queues the auto-hide check that runs kHideSettleMs later; cancelled by any re-activation or + // re-open. A check that fires after the dial was hidden on purpose is a no-op. + void schedule_hide(); + void on_hide_timer(wxTimerEvent& event); + // Whether the dial is the window that currently holds the keyboard focus. On wxGTK IsActive() answers + // from wx's own bookkeeping (g_activeFrame), which the deactivation that armed the timer has already + // cleared, so ask the window itself instead. + bool is_active(); void resize_to_content(int height); void run_action(const std::string& id, const std::string& title, const std::string& param = ""); void open_wiki(const std::string& id); @@ -56,6 +70,8 @@ private: // Guards the CallAfter in on_script_message across dialog destruction, same as // PluginsDialog::m_alive (PluginsDialog.hpp:249). std::shared_ptr> m_alive = std::make_shared>(true); + // Pending auto-hide, armed by a deactivation and validated when it fires (see schedule_hide). + wxTimer m_hide_timer; }; }} diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 044ad0ea99..f0337066c5 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -50,6 +50,7 @@ add_executable(${_TEST_NAME}_tests test_plugin_audit.cpp test_plugin_json_depth.cpp test_shortcuts.cpp + test_speed_dial_dismiss.cpp test_speed_dial_position.cpp test_texture_color_palette.cpp test_texture_island_view.cpp diff --git a/tests/slic3rutils/test_speed_dial_dismiss.cpp b/tests/slic3rutils/test_speed_dial_dismiss.cpp new file mode 100644 index 0000000000..561e203b45 --- /dev/null +++ b/tests/slic3rutils/test_speed_dial_dismiss.cpp @@ -0,0 +1,37 @@ +#include + +#include "slic3r/GUI/SpeedDialDialog.hpp" + +using namespace Slic3r::GUI; + +// The dial hides only after a settle delay, and only if it is still shown and really lost the focus, +// so a focus bounce shorter than the delay does not dismiss it. + +TEST_CASE("Speed dial stays shown while it still holds the focus", "[SpeedDialDismiss][SpeedDial]") +{ + // A focus bounce: a deactivation arrived, but the dial is active again when the timer fires. + REQUIRE_FALSE(speed_dial_should_hide(true, true)); +} + +TEST_CASE("Speed dial hides once the focus has really left it", "[SpeedDialDismiss][SpeedDial]") +{ + // The user clicked into the application behind the dial, or switched to another application. + REQUIRE(speed_dial_should_hide(true, false)); +} + +TEST_CASE("Hidden speed dial never hides again", "[SpeedDialDismiss][SpeedDial]") +{ + // Already hidden: a second hide would restart the delay and re-trigger any dismissal side effects. + REQUIRE_FALSE(speed_dial_should_hide(false, true)); + REQUIRE_FALSE(speed_dial_should_hide(false, false)); +} + +TEST_CASE("Speed dial dismiss decision follows the shown and active truth table", "[SpeedDialDismiss][SpeedDial]") +{ + // The rule in full, so a refactor cannot go back to hiding on the raw deactivation event. + // shown && !active is the only combination that hides. + REQUIRE_FALSE(speed_dial_should_hide(true, true)); + REQUIRE(speed_dial_should_hide(true, false)); + REQUIRE_FALSE(speed_dial_should_hide(false, true)); + REQUIRE_FALSE(speed_dial_should_hide(false, false)); +}