Skip to content

Commit 9a8dcc1

Browse files
committed
Merge pull request #97397 from timothyqiu/shortcut-in-tooltips
Make `_make_custom_tooltip` receive raw tooltip for buttons with shortcut enabled
2 parents 775edd5 + 6a71831 commit 9a8dcc1

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

doc/classes/BaseButton.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
</member>
7676
<member name="shortcut_in_tooltip" type="bool" setter="set_shortcut_in_tooltip" getter="is_shortcut_in_tooltip_enabled" default="true">
7777
If [code]true[/code], the button will add information about its shortcut in the tooltip.
78+
[b]Note:[/b] This property does nothing when the tooltip control is customized using [method Control._make_custom_tooltip].
7879
</member>
7980
<member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" default="false">
8081
If [code]true[/code], the button is in toggle mode. Makes the button flip state between pressed and unpressed each time its area is clicked.

scene/gui/base_button.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/config/project_settings.h"
3434
#include "core/os/keyboard.h"
35+
#include "scene/gui/label.h"
3536
#include "scene/main/window.h"
3637

3738
void BaseButton::_unpress_group() {
@@ -390,16 +391,31 @@ void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
390391
}
391392
}
392393

393-
String BaseButton::get_tooltip(const Point2 &p_pos) const {
394-
String tooltip = Control::get_tooltip(p_pos);
395-
if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
396-
String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
397-
if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
398-
text += "\n" + atr(tooltip);
399-
}
400-
tooltip = text;
394+
Control *BaseButton::make_custom_tooltip(const String &p_text) const {
395+
Control *control = Control::make_custom_tooltip(p_text);
396+
if (control) {
397+
return control;
398+
}
399+
if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) {
400+
return nullptr; // Use the default tooltip label.
401401
}
402-
return tooltip;
402+
403+
String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")";
404+
if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) {
405+
text += "\n" + atr(p_text);
406+
}
407+
408+
// Make a label similar to the default tooltip label.
409+
// Auto translation is disabled because we already did that manually above.
410+
//
411+
// We can't customize the tooltip text by overriding `get_tooltip()`
412+
// because otherwise user-defined `_make_custom_tooltip()` would receive
413+
// the translated and annotated text.
414+
Label *label = memnew(Label(text));
415+
label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
416+
label->set_theme_type_variation(SNAME("TooltipLabel"));
417+
418+
return label;
403419
}
404420

405421
void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {

scene/gui/base_button.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class BaseButton : public Control {
134134
void set_shortcut(const Ref<Shortcut> &p_shortcut);
135135
Ref<Shortcut> get_shortcut() const;
136136

137-
virtual String get_tooltip(const Point2 &p_pos) const override;
137+
virtual Control *make_custom_tooltip(const String &p_text) const override;
138138

139139
void set_button_group(const Ref<ButtonGroup> &p_group);
140140
Ref<ButtonGroup> get_button_group() const;

0 commit comments

Comments
 (0)