mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
widget,widget/material: make Clickable widgets focusable
This change adds focus and keyboard control to Clickable widgets. They now consider a press of the enter or return key equivalent to a click. To keep the change simple, the focus indication is the same as the hover indication. References: https://todo.sr.ht/~eliasnaur/gio/195 References: https://github.com/tailscale/tailscale/issues/1611 Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+36
-3
@@ -9,6 +9,7 @@ import (
|
||||
"gioui.org/f32"
|
||||
"gioui.org/gesture"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/semantic"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
@@ -24,6 +25,9 @@ type Clickable struct {
|
||||
// clicks bounded.
|
||||
prevClicks int
|
||||
history []Press
|
||||
|
||||
keyTag struct{}
|
||||
focused bool
|
||||
}
|
||||
|
||||
// Click represents a click.
|
||||
@@ -67,16 +71,21 @@ func (b *Clickable) Clicked() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Hovered returns whether pointer is over the element.
|
||||
// Hovered reports whether a pointer is over the element.
|
||||
func (b *Clickable) Hovered() bool {
|
||||
return b.click.Hovered()
|
||||
}
|
||||
|
||||
// Pressed returns whether pointer is pressing the element.
|
||||
// Pressed reports whether a pointer is pressing the element.
|
||||
func (b *Clickable) Pressed() bool {
|
||||
return b.click.Pressed()
|
||||
}
|
||||
|
||||
// Focused reports whether b has focus.
|
||||
func (b *Clickable) Focused() bool {
|
||||
return b.focused
|
||||
}
|
||||
|
||||
// Clicks returns and clear the clicks since the last call to Clicks.
|
||||
func (b *Clickable) Clicks() []Click {
|
||||
clicks := b.clicks
|
||||
@@ -98,8 +107,12 @@ func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimension
|
||||
dims := w(gtx)
|
||||
c := m.Stop()
|
||||
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
|
||||
semantic.DisabledOp(gtx.Queue == nil).Add(gtx.Ops)
|
||||
disabled := gtx.Queue == nil
|
||||
semantic.DisabledOp(disabled).Add(gtx.Ops)
|
||||
b.click.Add(gtx.Ops)
|
||||
if !disabled {
|
||||
key.InputOp{Tag: &b.keyTag}.Add(gtx.Ops)
|
||||
}
|
||||
c.Add(gtx.Ops)
|
||||
for len(b.history) > 0 {
|
||||
c := b.history[0]
|
||||
@@ -137,10 +150,30 @@ func (b *Clickable) update(gtx layout.Context) {
|
||||
}
|
||||
}
|
||||
case gesture.TypePress:
|
||||
if e.Source == pointer.Mouse {
|
||||
key.FocusOp{Tag: &b.keyTag}.Add(gtx.Ops)
|
||||
}
|
||||
b.history = append(b.history, Press{
|
||||
Position: e.Position,
|
||||
Start: gtx.Now,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, e := range gtx.Events(&b.keyTag) {
|
||||
switch e := e.(type) {
|
||||
case key.FocusEvent:
|
||||
b.focused = e.Focus
|
||||
case key.Event:
|
||||
if e.State != key.Press {
|
||||
break
|
||||
}
|
||||
if e.Name != key.NameReturn && e.Name != key.NameSpace {
|
||||
break
|
||||
}
|
||||
b.clicks = append(b.clicks, Click{
|
||||
Modifiers: e.Modifiers,
|
||||
NumClicks: 1,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user