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

Focus selects when tapped #4803

Merged
merged 8 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,8 @@ func (c *Check) Tapped(pe *fyne.PointEvent) {
return
}

if !c.focused && !fyne.CurrentDevice().IsMobile() {
impl := c.super()

if c := fyne.CurrentApp().Driver().CanvasForObject(impl); c != nil {
c.Focus(impl.(fyne.Focusable))
}
if !c.focused {
focusIfNotMobile(c.super())
}
c.SetChecked(!c.Checked)
}
Expand Down Expand Up @@ -382,3 +378,12 @@ func (c *checkRenderer) updateFocusIndicator(th fyne.Theme, v fyne.ThemeVariant)
c.focusIndicator.FillColor = color.Transparent
}
}

func focusIfNotMobile(w fyne.Widget) {
if !fyne.CurrentDevice().IsMobile() {
if c := fyne.CurrentApp().Driver().CanvasForObject(w); c != nil {
c.Focus(w.(fyne.Focusable))
}
}

}
8 changes: 2 additions & 6 deletions widget/radio_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ func (i *radioItem) SetSelected(selected bool) {
//
// Implements: fyne.Tappable
func (i *radioItem) Tapped(_ *fyne.PointEvent) {
if !i.focused && !fyne.CurrentDevice().IsMobile() {
impl := i.super()

if c := fyne.CurrentApp().Driver().CanvasForObject(impl); c != nil {
c.Focus(impl.(fyne.Focusable))
}
if !i.focused {
focusIfNotMobile(i.super())
}
i.toggle()
}
Expand Down
4 changes: 4 additions & 0 deletions widget/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func (s *Select) Tapped(*fyne.PointEvent) {
return
}

if !s.focused {
focusIfNotMobile(s.super())
}

s.tapAnimation()
s.Refresh()

Expand Down
9 changes: 2 additions & 7 deletions widget/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,8 @@ func (s *Slider) Tapped(e *fyne.PointEvent) {
return
}

driver := fyne.CurrentApp().Driver()
if !s.focused && !driver.Device().IsMobile() {
impl := s.super()

if c := driver.CanvasForObject(impl); c != nil {
c.Focus(impl.(fyne.Focusable))
}
if !s.focused {
focusIfNotMobile(s.super())
}

ratio := s.getRatio(e)
Expand Down
13 changes: 12 additions & 1 deletion widget/slider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,20 @@ func TestSlider_SetValue(t *testing.T) {
assert.Equal(t, 2.0, slider.Value)
}

func TestSlider_FocusDesktop(t *testing.T) {
if fyne.CurrentDevice().IsMobile() {
return
}
slider := NewSlider(0, 10)
win := test.NewWindow(slider)
test.Tap(slider)

assert.Equal(t, win.Canvas().Focused(), slider)
assert.True(t, slider.focused)
}

func TestSlider_Focus(t *testing.T) {
slider := NewSlider(0, 5)

slider.FocusGained()
assert.True(t, slider.focused)

Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/center.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="192x142">
<widget pos="32,53" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text alignment="center" pos="10,4" size="75x19">(Select one)</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/layout_empty_expanded.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="7,78" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="21,78" size="98x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="98x35"/>
<rectangle fillColor="focus" radius="4" size="98x35"/>
<rectangle size="98x35"/>
<widget pos="4,4" size="66x27" type="*widget.RichText">
<text pos="4,4" size="46x19">(Pick 1)</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/layout_multiple_expanded.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="7,78" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="21,78" size="98x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="98x35"/>
<rectangle fillColor="focus" radius="4" size="98x35"/>
<rectangle size="98x35"/>
<widget pos="4,4" size="66x27" type="*widget.RichText">
<text pos="4,4" size="46x19">(Pick 1)</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="7,78" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="24x19">Foo</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="21,78" size="98x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="98x35"/>
<rectangle fillColor="focus" radius="4" size="98x35"/>
<rectangle size="98x35"/>
<widget pos="4,4" size="66x27" type="*widget.RichText">
<text pos="4,4" size="24x19">Foo</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/layout_single_expanded.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="7,78" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="21,78" size="98x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="98x35"/>
<rectangle fillColor="focus" radius="4" size="98x35"/>
<rectangle size="98x35"/>
<widget pos="4,4" size="66x27" type="*widget.RichText">
<text pos="4,4" size="46x19">(Pick 1)</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/layout_single_expanded_selected.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="7,78" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="26x19">Test</text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="142x192">
<widget pos="21,78" size="98x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="98x35"/>
<rectangle fillColor="focus" radius="4" size="98x35"/>
<rectangle size="98x35"/>
<widget pos="4,4" size="66x27" type="*widget.RichText">
<text pos="4,4" size="26x19">Test</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/move_moved.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<canvas padded size="200x150">
<content>
<widget pos="20,20" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/move_tapped.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<canvas padded size="200x150">
<content>
<widget pos="10,10" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
Binary file modified widget/testdata/select/tap_animation.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion widget/testdata/select/tapped.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<canvas padded size="200x150">
<content>
<widget pos="4,4" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
2 changes: 1 addition & 1 deletion widget/testdata/select/tapped_constrained.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<canvas padded size="200x150">
<content>
<widget pos="190,140" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text pos="4,4" size="75x19">(Select one)</text>
Expand Down
Binary file modified widget/testdata/select/theme_changed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified widget/testdata/select/theme_initial.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion widget/testdata/select/trailing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<content>
<container pos="4,4" size="192x142">
<widget pos="32,53" size="127x35" type="*widget.Select">
<rectangle fillColor="inputBackground" radius="4" size="127x35"/>
<rectangle fillColor="focus" radius="4" size="127x35"/>
<rectangle size="127x35"/>
<widget pos="4,4" size="95x27" type="*widget.RichText">
<text alignment="trailing" pos="16,4" size="75x19">(Select one)</text>
Expand Down