From 1a5db7016cc458a79e2befb8f848ad58db56982d Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 11 Nov 2022 05:48:11 +0100 Subject: [PATCH] #4764: In cases the popup window is sticking around, allow ESC to close it immediately --- libs/wxutil/TransientPopupWindow.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/libs/wxutil/TransientPopupWindow.h b/libs/wxutil/TransientPopupWindow.h index 371d73d7b0..1983461702 100644 --- a/libs/wxutil/TransientPopupWindow.h +++ b/libs/wxutil/TransientPopupWindow.h @@ -10,13 +10,22 @@ namespace wxutil * A popup window that is destroying itself as soon as it loses focus */ class TransientPopupWindow : - public wxPopupTransientWindow + public wxPopupTransientWindow, + public wxEventFilter { public: TransientPopupWindow(wxWindow* parent) : wxPopupTransientWindow(parent, wxBORDER_DEFAULT) { SetSizer(new wxBoxSizer(wxVERTICAL)); + + // Register as global filter to catch events + wxEvtHandler::AddFilter(this); + } + + ~TransientPopupWindow() override + { + wxEvtHandler::RemoveFilter(this); } void Dismiss() override @@ -25,6 +34,18 @@ class TransientPopupWindow : Destroy(); } + int FilterEvent(wxEvent& ev) override + { + // Close the popup on any ESC keypress + if (ev.GetEventType() == wxEVT_KEY_DOWN && static_cast(ev).GetKeyCode() == WXK_ESCAPE) + { + Dismiss(); + return Event_Processed; + } + + return Event_Skip; + } + // Attempts to position the popup to the right or left of the given window // The vertical offset is added to the screen position of the given window void PositionNextTo(wxWindow* win, int verticalOffset, const wxSize& size)