fix: stop the Speed Dial dismissing itself on the first open (#17245)

* fix: stop the Speed Dial dismissing itself on the first open

Opening the dial with Space and not touching anything closed it again a
moment later, on GNOME and KDE, X11 and Wayland, and only on the first open.

The dial hides on wxEVT_ACTIVATE(false), but wxGTK does not deliver that
event to the window that lost the focus: gtk_frame_focus_out_callback sends
it to g_activeFrame, the toplevel that was activated last (wx
src/gtk/toplevel.cpp:192-205). A deactivation that belongs to another
window, or the same one arriving while the dial is still taking the focus,
therefore read as "the user dismissed the dial". The first open is the only
one that maps the window, moves it, resizes it to its content and grabs
focus into a web view that has never held it, so it is the only open
exposed to that ordering.

Validate the dismissal instead of trusting the event: arm a 150 ms one-shot,
cancelled by any activation, and hide only if the dial is still shown and
its window is no longer active. On GTK ask the window with
gtk_window_is_active(), because wxTopLevelWindow::IsActive() resolves
through wx focus tracking, which does not follow the focus that WebKit's
native widget takes. Also skip the pre-map focus grab on the first open: at
that point wxGTK has not mapped the window yet, so the grab cannot land.

speed_dial_should_hide() is a free function, so the rule is covered by
tests/slic3rutils/test_speed_dial_dismiss.cpp without a GUI.

* docs(wxwidgets-skill): document the validated Speed Dial deactivation

Section 10 described the dial's old handler, which hid on every
wxEVT_ACTIVATE(false). The skill is the review ground truth, so it has to
match the code: the deactivation is now validated after a settle interval
against the window's real active state, because wxGTK sends that event to
the last activated toplevel whatever window lost the focus.

* Ignore Page Changes of Inner Books in the Main Tab Handler

The main frame's tab-panel handler receives the page-changed event of
any book inside a page, because wxBookCtrlEvent propagates and the
Calibration page's Tabbook has no handler of its own. It then treated
the inner page switch as a main tab change: it reset the selected tab,
toggled the undo/redo buttons and called SetFocus() on the current page.

On wxGTK, SetFocus() on a child of a visible but inactive top-level
window presents that window. While the Calibration page is built in the
background after start-up, this re-activated the main window and the
Speed Dial, which hides when it loses the focus, closed by itself a
moment after its first open on Linux.

Only act on events whose object is the main tab panel.

* Include wx/timer.h Where the Speed Dial Binds Its Timer

* Describe the Real Cause of the Speed Dial's First-Open Close

The deferred, re-checked hide absorbs a focus bounce shorter than its
delay. It did not stop the first-open close, which was the main frame
activating itself from its tab handler; say so in the comments, the
test and the skill reference instead of describing a misdelivered
deactivation.

---------

Co-authored-by: Hanif Koh <hanifkoh@outlook.com>
This commit is contained in:
Lam Wei LunandHanif Koh authored and GitHub committed 2026-11-05 11:34:33 +08:00
1 parent 834c6987e0
commit bd3e9aeb96
6 files changed
+158 -10

No files matched your search

+60 -6
View File
@@ -31,6 +31,7 @@
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/string.h>
#include <wx/timer.h>
#include <wx/utils.h>
#include <wx/webview.h>
@@ -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]() {