widget/button,widget/material: introduce Clickable for generic click areas

material.Clickable is useful for adding a click response to any widget
or area.

Rename widget.Button to widget.Clickable to reflect the wider use
spectrum.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-11 13:17:14 +02:00
parent 47ce4b8cb8
commit 23baeff18d
3 changed files with 36 additions and 14 deletions
+6 -6
View File
@@ -13,8 +13,8 @@ import (
"gioui.org/op"
)
// Button represents a clickable area.
type Button struct {
// Clickable represents a clickable area.
type Clickable struct {
click gesture.Click
// clicks tracks the number of unreported clicks.
clicks int
@@ -30,7 +30,7 @@ type Click struct {
// Clicked calls Update and reports whether the button was
// clicked since the last call. Multiple clicks result in Clicked
// returning true once per click.
func (b *Button) Clicked(gtx *layout.Context) bool {
func (b *Clickable) Clicked(gtx *layout.Context) bool {
b.Update(gtx)
if b.clicks > 0 {
b.clicks--
@@ -45,11 +45,11 @@ func (b *Button) Clicked(gtx *layout.Context) bool {
// History is the past clicks useful for drawing click markers.
// Clicks are retained for a short duration (about a second).
func (b *Button) History() []Click {
func (b *Clickable) History() []Click {
return b.history
}
func (b *Button) Layout(gtx *layout.Context) {
func (b *Clickable) Layout(gtx *layout.Context) {
// Flush clicks from before the previous frame.
b.Update(gtx)
var st op.StackOp
@@ -69,7 +69,7 @@ func (b *Button) Layout(gtx *layout.Context) {
// Update the button state by processing events. The underlying
// gesture events are returned for use beyond what Clicked offers.
func (b *Button) Update(gtx *layout.Context) []gesture.ClickEvent {
func (b *Clickable) Update(gtx *layout.Context) []gesture.ClickEvent {
evts := b.click.Events(gtx)
for _, e := range evts {
switch e.Type {