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

widget: click button only if key pressed and released #120

Closed
wants to merge 1 commit into from

Conversation

vsariola
Copy link
Contributor

This commit fixes the non-intuitive behaviour, where hitting return or space with a button focused, then tabbing to another button and releasing the key causes the second button to trigger. It feels wrong, as the "gesture" was never initiated on the second button. The fix makes widget.Clickable track which key was pressed, in a variable called pressedKey, and only considers a key release if the released key matches the pressed key. Finally, if the widget loses focus, pressedKey is cleared.

Fixes: https://todo.sr.ht/~eliasnaur/gio#525

@eliasnaur
Copy link
Contributor

This commit fixes the non-intuitive behaviour, where hitting return or space with a button focused, then tabbing to another button and releasing the key causes the second button to trigger. It feels wrong, as the "gesture" was never initiated on the second button. The fix makes widget.Clickable track which key was pressed, in a variable called pressedKey, and only considers a key release if the released key matches the pressed key. Finally, if the widget loses focus, pressedKey is cleared.

Can you add a test, say in widget/button_test.go, that demonstrates the issue and verifies the fix? There is an existing test for Bool in widget/widget_test.go.

Fixes: https://todo.sr.ht/~eliasnaur/gio#525

I believe this should be with a slash:

Fixes: https://todo.sr.ht/~eliasnaur/gio/525

@vsariola
Copy link
Contributor Author

Can you add a test, say in widget/button_test.go, that demonstrates the issue and verifies the fix? There is an existing test for Bool in widget/widget_test.go.

Done, to the best of my ability, but I have a feeling there's cleaner way to write the test.

Fixes: https://todo.sr.ht/~eliasnaur/gio#525

I believe this should be with a slash:

Fixes: https://todo.sr.ht/~eliasnaur/gio/525

Changed to a slash. I got the # syntax from here https://gioui.org/doc/contribute so assuming slash is correct, there's likely a typo in the instructions.

Copy link
Contributor

@eliasnaur eliasnaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, apart from nits. @whereswaldon do you have any objections to this behaviour change?

"gioui.org/widget"
)

func TestButtonKeypresses(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: rename to TestClickable.

Comment on lines 25 to 33
layout := func() {
layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return b1.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: image.Pt(100, 100)}
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return b2.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: image.Pt(100, 100)}
})
}),
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only need to offset b2, layout can be written without Flex, by using op.Offset just before laying out b2:

defer op.Offset(image.Pt(100, 100)).Push(gtx.Ops).Pop()

@whereswaldon
Copy link
Member

LGTM, apart from nits. @whereswaldon do you have any objections to this behaviour change?

No, I wholeheartedly agree with it.

This commit fixes the non-intuitive behaviour, where hitting return or
space with a button focused, then tabbing to another button and
releasing the key causes the second button to trigger. It feels wrong,
as the "gesture" was never initiated on the second button. The fix makes
widget.Clickable track which key was pressed, in a variable called
pressedKey, and only considers a key release if the released key matches
the pressed key. Finally, if the widget loses focus, pressedKey is
cleared.

Fixes: https://todo.sr.ht/~eliasnaur/gio/525
Signed-off-by: Veikko Sariola <5684185+vsariola@users.noreply.github.com>
@vsariola
Copy link
Contributor Author

vsariola commented Aug 21, 2023

Done the nits. I didn't even offset the buttons; just laid them out on top of each other as the test doesn't click anything, just uses Focus, Focused and keyboard presses, so it doesn't make a difference. Didn't want to add anything unnecessary to the test.

@eliasnaur
Copy link
Contributor

Merged, thank you!

Changed to a slash. I got the # syntax from here https://gioui.org/doc/contribute so assuming slash is correct, there's likely a typo in the instructions.

Fixed. Thanks for pointing that out.

@eliasnaur eliasnaur closed this Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants