From 234ae3a2aed8aacc4837751bed8f0292b3f99ffc Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 19 Nov 2022 09:45:05 -0500 Subject: [PATCH] LibGUI: Allow buttons to control the behavior when their text changes Some buttons control how their text is set in unique ways. For example, GUI::ToolbarButton will set only its tooltip instead of its text if it has an icon. So when the text changes, ToolbarButton will want to change its tooltip instead. --- Userland/Libraries/LibGUI/AbstractButton.h | 2 +- Userland/Libraries/LibGUI/Toolbar.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGUI/AbstractButton.h b/Userland/Libraries/LibGUI/AbstractButton.h index 72ef9cfeeaa..2151a0b3b8a 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.h +++ b/Userland/Libraries/LibGUI/AbstractButton.h @@ -20,7 +20,7 @@ public: Function on_checked; - void set_text(String); + virtual void set_text(String); String const& text() const { return m_text; } bool is_exclusive() const { return m_exclusive; } diff --git a/Userland/Libraries/LibGUI/Toolbar.cpp b/Userland/Libraries/LibGUI/Toolbar.cpp index 62b16ee5817..81570302cdd 100644 --- a/Userland/Libraries/LibGUI/Toolbar.cpp +++ b/Userland/Libraries/LibGUI/Toolbar.cpp @@ -59,6 +59,17 @@ private: set_text(action.text()); set_button_style(Gfx::ButtonStyle::Coolbar); } + + virtual void set_text(String text) override + { + auto const* action = this->action(); + VERIFY(action); + + set_tooltip(tooltip(*action)); + if (!action->icon()) + Button::set_text(move(text)); + } + String tooltip(Action const& action) const { StringBuilder builder;