Skip to content

Commit 01d567b

Browse files
committed
Merge pull request #97406 from timothyqiu/tooltip-atm
Add auto translate mode for tooltips
2 parents 9a8dcc1 + 78801f6 commit 01d567b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

doc/classes/Control.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,10 @@
10571057
[b]Note:[/b] To look up [Control]'s own items use various [code]get_theme_*[/code] methods without specifying [code]theme_type[/code].
10581058
[b]Note:[/b] Theme items are looked for in the tree order, from branch to root, where each [Control] node is checked for its [member theme] property. The earliest match against any type/class name is returned. The project-level Theme and the default Theme are checked last.
10591059
</member>
1060+
<member name="tooltip_auto_translate_mode" type="int" setter="set_tooltip_auto_translate_mode" getter="get_tooltip_auto_translate_mode" enum="Node.AutoTranslateMode" default="0">
1061+
Defines if tooltip text should automatically change to its translated version depending on the current locale. Uses the same auto translate mode as this control when set to [constant Node.AUTO_TRANSLATE_MODE_INHERIT].
1062+
[b]Note:[/b] When the tooltip is customized using [method _make_custom_tooltip], this auto translate mode is applied automatically to the returned control.
1063+
</member>
10601064
<member name="tooltip_text" type="String" setter="set_tooltip_text" getter="get_tooltip_text" default="&quot;&quot;">
10611065
The default tooltip text. The tooltip appears when the user's mouse cursor stays idle over this control for a few moments, provided that the [member mouse_filter] property is not [constant MOUSE_FILTER_IGNORE]. The time required for the tooltip to appear can be changed with the [member ProjectSettings.gui/timers/tooltip_delay_sec] option. See also [method get_tooltip].
10621066
The tooltip popup will use either a default implementation, or a custom one that you can provide by overriding [method _make_custom_tooltip]. The default tooltip includes a [PopupPanel] and [Label] whose theme properties can be customized using [Theme] methods with the [code]"TooltipPanel"[/code] and [code]"TooltipLabel"[/code] respectively. For example:

scene/gui/control.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@
3333
#include "container.h"
3434
#include "core/config/project_settings.h"
3535
#include "core/math/geometry_2d.h"
36-
#include "core/os/keyboard.h"
3736
#include "core/os/os.h"
38-
#include "core/string/print_string.h"
3937
#include "core/string/translation_server.h"
40-
#include "scene/gui/label.h"
41-
#include "scene/gui/panel.h"
4238
#include "scene/main/canvas_layer.h"
4339
#include "scene/main/window.h"
4440
#include "scene/theme/theme_db.h"
@@ -3161,6 +3157,16 @@ bool Control::is_auto_translating() const {
31613157
}
31623158
#endif
31633159

3160+
void Control::set_tooltip_auto_translate_mode(AutoTranslateMode p_mode) {
3161+
ERR_MAIN_THREAD_GUARD;
3162+
data.tooltip_auto_translate_mode = p_mode;
3163+
}
3164+
3165+
Node::AutoTranslateMode Control::get_tooltip_auto_translate_mode() const {
3166+
ERR_READ_THREAD_GUARD_V(AUTO_TRANSLATE_MODE_INHERIT);
3167+
return data.tooltip_auto_translate_mode;
3168+
}
3169+
31643170
// Extra properties.
31653171

31663172
void Control::set_tooltip_text(const String &p_hint) {
@@ -3510,6 +3516,8 @@ void Control::_bind_methods() {
35103516
ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
35113517
ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
35123518

3519+
ClassDB::bind_method(D_METHOD("set_tooltip_auto_translate_mode", "mode"), &Control::set_tooltip_auto_translate_mode);
3520+
ClassDB::bind_method(D_METHOD("get_tooltip_auto_translate_mode"), &Control::get_tooltip_auto_translate_mode);
35133521
ClassDB::bind_method(D_METHOD("set_tooltip_text", "hint"), &Control::set_tooltip_text);
35143522
ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text);
35153523
ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
@@ -3617,6 +3625,7 @@ void Control::_bind_methods() {
36173625

36183626
ADD_GROUP("Tooltip", "tooltip_");
36193627
ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text");
3628+
ADD_PROPERTY(PropertyInfo(Variant::INT, "tooltip_auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_tooltip_auto_translate_mode", "get_tooltip_auto_translate_mode");
36203629

36213630
ADD_GROUP("Focus", "focus_");
36223631
ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_LEFT);

scene/gui/control.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
#include "core/math/transform_2d.h"
3535
#include "core/object/gdvirtual.gen.inc"
36-
#include "core/templates/rid.h"
3736
#include "scene/main/canvas_item.h"
3837
#include "scene/main/timer.h"
3938
#include "scene/resources/theme.h"
@@ -262,6 +261,7 @@ class Control : public CanvasItem {
262261
// Extra properties.
263262

264263
String tooltip;
264+
AutoTranslateMode tooltip_auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
265265

266266
} data;
267267

@@ -634,6 +634,9 @@ class Control : public CanvasItem {
634634
bool is_auto_translating() const;
635635
#endif
636636

637+
void set_tooltip_auto_translate_mode(AutoTranslateMode p_mode);
638+
AutoTranslateMode get_tooltip_auto_translate_mode() const;
639+
637640
// Extra properties.
638641

639642
void set_tooltip_text(const String &text);

scene/main/viewport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Cont
14001400
String tooltip;
14011401

14021402
while (p_control) {
1403-
tooltip = p_control->atr(p_control->get_tooltip(pos));
1403+
tooltip = p_control->get_tooltip(pos);
14041404

14051405
// Temporary solution for PopupMenus.
14061406
PopupMenu *menu = Object::cast_to<PopupMenu>(this);
@@ -1483,6 +1483,7 @@ void Viewport::_gui_show_tooltip() {
14831483
}
14841484

14851485
base_tooltip->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
1486+
base_tooltip->set_auto_translate_mode(tooltip_owner->get_tooltip_auto_translate_mode());
14861487

14871488
panel->set_transient(true);
14881489
panel->set_flag(Window::FLAG_NO_FOCUS, true);

0 commit comments

Comments
 (0)