Merge pull request #97397 from timothyqiu/shortcut-in-tooltips

Make `_make_custom_tooltip` receive raw tooltip for buttons with shortcut enabled
This commit is contained in:
Rémi Verschelde 2024-09-26 18:45:31 +02:00
commit 9a8dcc1eeb
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 27 additions and 10 deletions

View File

@ -75,6 +75,7 @@
</member> </member>
<member name="shortcut_in_tooltip" type="bool" setter="set_shortcut_in_tooltip" getter="is_shortcut_in_tooltip_enabled" default="true"> <member name="shortcut_in_tooltip" type="bool" setter="set_shortcut_in_tooltip" getter="is_shortcut_in_tooltip_enabled" default="true">
If [code]true[/code], the button will add information about its shortcut in the tooltip. If [code]true[/code], the button will add information about its shortcut in the tooltip.
[b]Note:[/b] This property does nothing when the tooltip control is customized using [method Control._make_custom_tooltip].
</member> </member>
<member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" default="false"> <member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" default="false">
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. 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.

View File

@ -32,6 +32,7 @@
#include "core/config/project_settings.h" #include "core/config/project_settings.h"
#include "core/os/keyboard.h" #include "core/os/keyboard.h"
#include "scene/gui/label.h"
#include "scene/main/window.h" #include "scene/main/window.h"
void BaseButton::_unpress_group() { void BaseButton::_unpress_group() {
@ -390,16 +391,31 @@ void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
} }
} }
String BaseButton::get_tooltip(const Point2 &p_pos) const { Control *BaseButton::make_custom_tooltip(const String &p_text) const {
String tooltip = Control::get_tooltip(p_pos); Control *control = Control::make_custom_tooltip(p_text);
if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) { if (control) {
String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")"; return control;
if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
text += "\n" + atr(tooltip);
}
tooltip = text;
} }
return tooltip; if (!shortcut_in_tooltip || shortcut.is_null() || !shortcut->has_valid_event()) {
return nullptr; // Use the default tooltip label.
}
String text = atr(shortcut->get_name()) + " (" + shortcut->get_as_text() + ")";
if (!p_text.is_empty() && shortcut->get_name().nocasecmp_to(p_text) != 0) {
text += "\n" + atr(p_text);
}
// Make a label similar to the default tooltip label.
// Auto translation is disabled because we already did that manually above.
//
// We can't customize the tooltip text by overriding `get_tooltip()`
// because otherwise user-defined `_make_custom_tooltip()` would receive
// the translated and annotated text.
Label *label = memnew(Label(text));
label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
label->set_theme_type_variation(SNAME("TooltipLabel"));
return label;
} }
void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) { void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {

View File

@ -134,7 +134,7 @@ public:
void set_shortcut(const Ref<Shortcut> &p_shortcut); void set_shortcut(const Ref<Shortcut> &p_shortcut);
Ref<Shortcut> get_shortcut() const; Ref<Shortcut> get_shortcut() const;
virtual String get_tooltip(const Point2 &p_pos) const override; virtual Control *make_custom_tooltip(const String &p_text) const override;
void set_button_group(const Ref<ButtonGroup> &p_group); void set_button_group(const Ref<ButtonGroup> &p_group);
Ref<ButtonGroup> get_button_group() const; Ref<ButtonGroup> get_button_group() const;