From 5cd9c6d9a0ba22ccd6071f6bd08490547397c5f9 Mon Sep 17 00:00:00 2001 From: PeterN Date: Mon, 12 Jun 2023 08:42:02 +0100 Subject: [PATCH] Fix #10987: Double-close of dropdown stopped land-info tool working as default. (#11000) Clicking and releasing on the query toolbar icon is meant to select the land-info tool. This did not work as during closing a window, OnFocusLost() is called, which then closes the window again. These two calls toggled the land-info tool one and off in the same action. Resolve by not calling Window::Close in OnFocusLost() if the window is already closing. --- src/console_gui.cpp | 2 +- src/osk_gui.cpp | 4 ++-- src/widgets/dropdown.cpp | 4 ++-- src/window.cpp | 6 +++--- src/window_gui.h | 9 ++++++++- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 972ef23b24..d7ad5d75b8 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -341,7 +341,7 @@ struct IConsoleWindow : Window VideoDriver::GetInstance()->EditBoxGainedFocus(); } - void OnFocusLost() override + void OnFocusLost(bool closing) override { VideoDriver::GetInstance()->EditBoxLostFocus(); } diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 630cd18f2d..cd5cb53a49 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -202,10 +202,10 @@ struct OskWindow : public Window { this->parent->SetWidgetDirty(this->text_btn); } - void OnFocusLost() override + void OnFocusLost(bool closing) override { VideoDriver::GetInstance()->EditBoxLostFocus(); - this->Close(); + if (!closing) this->Close(); } }; diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 18118dc60d..ea4bad0fcc 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -201,9 +201,9 @@ struct DropdownWindow : Window { if (nwc != nullptr) SetBit(nwc->disp_flags, NDB_DROPDOWN_CLOSED); } - void OnFocusLost() override + void OnFocusLost(bool closing) override { - this->Close(); + if (!closing) this->Close(); } Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override diff --git a/src/window.cpp b/src/window.cpp index 5644364507..fa632c3e81 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -469,7 +469,7 @@ void SetFocusedWindow(Window *w) _focused_window = w; /* So we can inform it that it lost focus */ - if (old_focused != nullptr) old_focused->OnFocusLost(); + if (old_focused != nullptr) old_focused->OnFocusLost(false); if (_focused_window != nullptr) _focused_window->OnFocus(); } @@ -545,7 +545,7 @@ void Window::OnFocus() /** * Called when window loses focus */ -void Window::OnFocusLost() +void Window::OnFocusLost(bool closing) { if (this->nested_focus != nullptr && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus(); } @@ -1127,7 +1127,7 @@ void Window::Close() /* Make sure we don't try to access this window as the focused window when it doesn't exist anymore. */ if (_focused_window == this) { - this->OnFocusLost(); + this->OnFocusLost(true); _focused_window = nullptr; } diff --git a/src/window_gui.h b/src/window_gui.h index 00dfd9960a..798d6d32dc 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -521,9 +521,16 @@ public: */ virtual void SetStringParameters(int widget) const {} + /** + * The window has gained focus. + */ virtual void OnFocus(); - virtual void OnFocusLost(); + /** + * The window has lost focus. + * @param closing True iff the window has lost focus in the process of closing. + */ + virtual void OnFocusLost(bool closing); /** * A key has been pressed.