Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpinBox focus mode does not work as expected #11711

Open
BaconEggsRL opened this issue Feb 6, 2025 · 0 comments
Open

SpinBox focus mode does not work as expected #11711

BaconEggsRL opened this issue Feb 6, 2025 · 0 comments

Comments

@BaconEggsRL
Copy link

BaconEggsRL commented Feb 6, 2025

Describe the project you are working on

A synthesizer tool

Describe the problem or limitation you are having in your project

SpinBox focus mode does not work as expected.
Setting the focus mode to None does not disable the focus mode of the internal LineEdit used by the node.
Some hacky script workaround on the SpinBox is required to prevent this.

(The reason this is an issue for my project is that the user needs full use of the keyboard to press buttons on the synthesizer keyboard. If the spinbox button is pressed with the mouse, it auto-focuses and causes keyboard to type in the built-in LineEdit which is not desired behavior.)

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add an option to the inspector to disable the line edit focus.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

TBD

If this enhancement will not be used often, can it be worked around with a few lines of script?

It can be done but I haven't found an elegant solution yet.

The grab_focus() method can't be overriden.
Setting the line focus mode to None results in an warning.
So there is something internal to the SpinBox node that is doing this.

I used a combination of changing the focus style and releasing focus after text or value change.
But it's not perfect. Clicking the arrows even when they are disabled pulses the line edit.

Perhaps instead of a LineEdit, I'd be better off with a Label and two buttons.
I tried setting Editable to false, but that just makes it so it can't be changed at all (even via the buttons.)
I just don't want to be able to focus the text field. But the spin box auto-focuses the text field even if focus mode is disabled on the spin box.

extends SpinBox

const NORMAL_LINEEDIT_STYLE = preload("res://normal_lineedit_style.tres")
@onready var line = get_line_edit()

func _ready():
	# Ensure SpinBox itself can't grab focus
	focus_mode = Control.FOCUS_NONE
	
	# line.focus_mode = Control.FOCUS_NONE
	# line.mouse_filter = Control.MOUSE_FILTER_IGNORE  # Prevent mouse clicks from selecting it
	# Remove focus border from internal line edit
	line.add_theme_stylebox_override("focus", NORMAL_LINEEDIT_STYLE)
	
	# Remove focus from LineEdit after it grabs it
	line.text_changed.connect(_on_text_changed)
	self.value_changed.connect(_on_value_changed)

func _on_text_changed(_new_text):        
	line.release_focus()

func _on_value_changed(_value):        
	line.release_focus()

Is there a reason why this should be core and not an add-on in the asset library?

Core function of SpinBox / LineEdit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants