mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 10:25:37 +00:00
widget: [API] change Clickable.Update to report one click at a time
Similar to how events are processed one at a time, change Clickable to report clicks one at a time. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+20
-28
@@ -18,9 +18,7 @@ import (
|
|||||||
|
|
||||||
// Clickable represents a clickable area.
|
// Clickable represents a clickable area.
|
||||||
type Clickable struct {
|
type Clickable struct {
|
||||||
click gesture.Click
|
click gesture.Click
|
||||||
// clicks is for saved clicks to support Clicked.
|
|
||||||
clicks []Click
|
|
||||||
history []Press
|
history []Press
|
||||||
|
|
||||||
keyTag struct{}
|
keyTag struct{}
|
||||||
@@ -53,19 +51,10 @@ func (b *Clickable) Click() {
|
|||||||
b.requestClicks++
|
b.requestClicks++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clicked reports whether there are pending clicks. If so, Clicked
|
// Clicked calls Update and reports whether a click was registered.
|
||||||
// removes the earliest click.
|
|
||||||
func (b *Clickable) Clicked(gtx layout.Context) bool {
|
func (b *Clickable) Clicked(gtx layout.Context) bool {
|
||||||
if len(b.clicks) > 0 {
|
_, clicked := b.Update(gtx)
|
||||||
b.clicks = b.clicks[1:]
|
return clicked
|
||||||
return true
|
|
||||||
}
|
|
||||||
b.clicks = b.Update(gtx)
|
|
||||||
if len(b.clicks) > 0 {
|
|
||||||
b.clicks = b.clicks[1:]
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hovered reports whether a pointer is over the element.
|
// Hovered reports whether a pointer is over the element.
|
||||||
@@ -96,7 +85,12 @@ func (b *Clickable) History() []Press {
|
|||||||
|
|
||||||
// Layout and update the button state.
|
// Layout and update the button state.
|
||||||
func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
|
func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
|
||||||
b.Update(gtx)
|
for {
|
||||||
|
_, ok := b.Update(gtx)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
m := op.Record(gtx.Ops)
|
m := op.Record(gtx.Ops)
|
||||||
dims := w(gtx)
|
dims := w(gtx)
|
||||||
c := m.Stop()
|
c := m.Stop()
|
||||||
@@ -108,10 +102,9 @@ func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimension
|
|||||||
return dims
|
return dims
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the button state by processing events, and return the resulting
|
// Update the button state by processing events, and return the next
|
||||||
// clicks, if any.
|
// click, if any.
|
||||||
func (b *Clickable) Update(gtx layout.Context) []Click {
|
func (b *Clickable) Update(gtx layout.Context) (Click, bool) {
|
||||||
b.clicks = nil
|
|
||||||
if !gtx.Enabled() {
|
if !gtx.Enabled() {
|
||||||
b.focused = false
|
b.focused = false
|
||||||
}
|
}
|
||||||
@@ -123,12 +116,11 @@ func (b *Clickable) Update(gtx layout.Context) []Click {
|
|||||||
n := copy(b.history, b.history[1:])
|
n := copy(b.history, b.history[1:])
|
||||||
b.history = b.history[:n]
|
b.history = b.history[:n]
|
||||||
}
|
}
|
||||||
var clicks []Click
|
|
||||||
if c := b.requestClicks; c > 0 {
|
if c := b.requestClicks; c > 0 {
|
||||||
b.requestClicks = 0
|
b.requestClicks = 0
|
||||||
clicks = append(clicks, Click{
|
return Click{
|
||||||
NumClicks: c,
|
NumClicks: c,
|
||||||
})
|
}, true
|
||||||
}
|
}
|
||||||
for _, e := range b.click.Update(gtx.Source) {
|
for _, e := range b.click.Update(gtx.Source) {
|
||||||
switch e.Kind {
|
switch e.Kind {
|
||||||
@@ -136,10 +128,10 @@ func (b *Clickable) Update(gtx layout.Context) []Click {
|
|||||||
if l := len(b.history); l > 0 {
|
if l := len(b.history); l > 0 {
|
||||||
b.history[l-1].End = gtx.Now
|
b.history[l-1].End = gtx.Now
|
||||||
}
|
}
|
||||||
clicks = append(clicks, Click{
|
return Click{
|
||||||
Modifiers: e.Modifiers,
|
Modifiers: e.Modifiers,
|
||||||
NumClicks: e.NumClicks,
|
NumClicks: e.NumClicks,
|
||||||
})
|
}, true
|
||||||
case gesture.KindCancel:
|
case gesture.KindCancel:
|
||||||
for i := range b.history {
|
for i := range b.history {
|
||||||
b.history[i].Cancelled = true
|
b.history[i].Cancelled = true
|
||||||
@@ -190,12 +182,12 @@ func (b *Clickable) Update(gtx layout.Context) []Click {
|
|||||||
}
|
}
|
||||||
// only register a key as a click if the key was pressed and released while this button was focused
|
// only register a key as a click if the key was pressed and released while this button was focused
|
||||||
b.pressedKey = ""
|
b.pressedKey = ""
|
||||||
clicks = append(clicks, Click{
|
return Click{
|
||||||
Modifiers: e.Modifiers,
|
Modifiers: e.Modifiers,
|
||||||
NumClicks: 1,
|
NumClicks: 1,
|
||||||
})
|
}, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return clicks
|
return Click{}, false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user