Skip to content

Commit 8a0123c

Browse files
committed
Hide Control focus when given via mouse input
1 parent 6c9aa4c commit 8a0123c

27 files changed

+133
-51
lines changed

doc/classes/Control.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,15 +618,19 @@
618618
</method>
619619
<method name="grab_focus">
620620
<return type="void" />
621+
<param index="0" name="hide_focus" type="bool" default="false" />
621622
<description>
622623
Steal the focus from another control and become the focused control (see [member focus_mode]).
624+
If [param hide_focus] is [code]true[/code], the control will not visually show its focused state.
623625
[b]Note:[/b] Using this method together with [method Callable.call_deferred] makes it more reliable, especially when called inside [method Node._ready].
624626
</description>
625627
</method>
626628
<method name="has_focus" qualifiers="const">
627629
<return type="bool" />
630+
<param index="0" name="ignore_hidden_focus" type="bool" default="false" />
628631
<description>
629632
Returns [code]true[/code] if this is the current focused control. See [member focus_mode].
633+
If [param ignore_hidden_focus] is [code]true[/code], controls that have its focus hidden will always return [code]false[/code]. Hidden focus happens automatically when controls gain focus via mouse input, or manually with [method grab_focus] with [code]hide_focus[/code] set to [code]true[/code].
630634
</description>
631635
</method>
632636
<method name="has_theme_color" qualifiers="const">

editor/gui/editor_spin_slider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ void EditorSpinSlider::_draw_spin_slider() {
340340
}
341341
}
342342

343-
if (has_focus()) {
343+
if (has_focus(true)) {
344344
Ref<StyleBox> focus = get_theme_stylebox(SNAME("focus"), SNAME("LineEdit"));
345345
draw_style_box(focus, Rect2(Vector2(), size));
346346
}

editor/scene/canvas_item_editor_plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,7 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
27972797

27982798
// Grab focus
27992799
if (!viewport->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
2800-
callable_mp((Control *)viewport, &Control::grab_focus).call_deferred();
2800+
callable_mp((Control *)viewport, &Control::grab_focus).call_deferred(false);
28012801
}
28022802
}
28032803

scene/gui/button.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void Button::_notification(int p_what) {
251251
style->draw(ci, Rect2(Point2(), size));
252252
}
253253

254-
if (has_focus()) {
254+
if (has_focus(true)) {
255255
theme_cache.focus->draw(ci, Rect2(Point2(), size));
256256
}
257257

@@ -315,7 +315,7 @@ void Button::_notification(int p_what) {
315315
switch (get_draw_mode()) {
316316
case DRAW_NORMAL: {
317317
// Focus colors only take precedence over normal state.
318-
if (has_focus()) {
318+
if (has_focus(true)) {
319319
font_color = theme_cache.font_focus_color;
320320
if (has_theme_color(SNAME("icon_focus_color"))) {
321321
icon_modulate_color = theme_cache.icon_focus_color;

scene/gui/color_picker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ void ColorPicker::_sample_draw() {
14911491

14921492
sample->draw_rect(rect_new, color);
14931493

1494-
if (display_old_color && !old_color.is_equal_approx(color) && sample->has_focus()) {
1494+
if (display_old_color && !old_color.is_equal_approx(color) && sample->has_focus(true)) {
14951495
RID ci = sample->get_canvas_item();
14961496
theme_cache.sample_focus->draw(ci, rect_old);
14971497
}
@@ -2663,7 +2663,7 @@ void ColorPresetButton::_notification(int p_what) {
26632663
WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead.");
26642664
}
26652665

2666-
if (has_focus()) {
2666+
if (has_focus(true)) {
26672667
RID ci = get_canvas_item();
26682668
theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
26692669
}

scene/gui/color_picker_shape.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void ColorPickerShape::cancel_event() {
8484
}
8585

8686
void ColorPickerShape::draw_focus_rect(Control *p_control, const Rect2 &p_rect) {
87-
if (!p_control->has_focus()) {
87+
if (!p_control->has_focus(true)) {
8888
return;
8989
}
9090

@@ -103,7 +103,7 @@ void ColorPickerShape::draw_focus_rect(Control *p_control, const Rect2 &p_rect)
103103
}
104104

105105
void ColorPickerShape::draw_focus_circle(Control *p_control) {
106-
if (!p_control->has_focus()) {
106+
if (!p_control->has_focus(true)) {
107107
return;
108108
}
109109

scene/gui/control.compat.inc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**************************************************************************/
2+
/* control.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
bool Control::_has_focus_bind_compat_110250() {
34+
return has_focus(false);
35+
}
36+
37+
void Control::_grab_focus_bind_compat_110250() {
38+
return grab_focus(false);
39+
}
40+
41+
void Control::_bind_compatibility_methods() {
42+
ClassDB::bind_compatibility_method(D_METHOD("has_focus"), &Control::_has_focus_bind_compat_110250);
43+
ClassDB::bind_compatibility_method(D_METHOD("grab_focus"), &Control::_grab_focus_bind_compat_110250);
44+
}
45+
46+
#endif

scene/gui/control.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,12 +2315,12 @@ void Control::_propagate_focus_behavior_recursive_recursively(bool p_enabled, bo
23152315
}
23162316
}
23172317

2318-
bool Control::has_focus() const {
2318+
bool Control::has_focus(bool p_ignore_hidden_focus) const {
23192319
ERR_READ_THREAD_GUARD_V(false);
2320-
return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
2320+
return is_inside_tree() && get_viewport()->_gui_control_has_focus(this, p_ignore_hidden_focus);
23212321
}
23222322

2323-
void Control::grab_focus() {
2323+
void Control::grab_focus(bool p_hide_focus) {
23242324
ERR_MAIN_THREAD_GUARD;
23252325
ERR_FAIL_COND(!is_inside_tree());
23262326

@@ -2329,7 +2329,7 @@ void Control::grab_focus() {
23292329
return;
23302330
}
23312331

2332-
get_viewport()->_gui_control_grab_focus(this);
2332+
get_viewport()->_gui_control_grab_focus(this, p_hide_focus);
23332333
}
23342334

23352335
void Control::grab_click_focus() {
@@ -4010,8 +4010,8 @@ void Control::_bind_methods() {
40104010
ClassDB::bind_method(D_METHOD("get_focus_mode_with_override"), &Control::get_focus_mode_with_override);
40114011
ClassDB::bind_method(D_METHOD("set_focus_behavior_recursive", "focus_behavior_recursive"), &Control::set_focus_behavior_recursive);
40124012
ClassDB::bind_method(D_METHOD("get_focus_behavior_recursive"), &Control::get_focus_behavior_recursive);
4013-
ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
4014-
ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
4013+
ClassDB::bind_method(D_METHOD("has_focus", "ignore_hidden_focus"), &Control::has_focus, DEFVAL(false));
4014+
ClassDB::bind_method(D_METHOD("grab_focus", "hide_focus"), &Control::grab_focus, DEFVAL(false));
40154015
ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
40164016
ClassDB::bind_method(D_METHOD("find_prev_valid_focus"), &Control::find_prev_valid_focus);
40174017
ClassDB::bind_method(D_METHOD("find_next_valid_focus"), &Control::find_next_valid_focus);

scene/gui/control.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ class Control : public CanvasItem {
592592
FocusMode get_focus_mode_with_override() const;
593593
void set_focus_behavior_recursive(FocusBehaviorRecursive p_focus_behavior_recursive);
594594
FocusBehaviorRecursive get_focus_behavior_recursive() const;
595-
bool has_focus() const;
596-
void grab_focus();
595+
bool has_focus(bool p_ignore_hidden_focus = false) const;
596+
void grab_focus(bool p_hide_focus = false);
597597
void grab_click_focus();
598598
void release_focus();
599599

scene/gui/foldable_container.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void FoldableContainer::_notification(int p_what) {
317317
_draw_flippable_stylebox(theme_cache.panel_style, panel_rect);
318318
}
319319

320-
if (has_focus()) {
320+
if (has_focus(true)) {
321321
Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size);
322322
_draw_flippable_stylebox(theme_cache.focus_style, focus_rect);
323323
}

0 commit comments

Comments
 (0)