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" "gioui.org/op"
) )
// Button represents a clickable area. // Clickable represents a clickable area.
type Button struct { type Clickable struct {
click gesture.Click click gesture.Click
// clicks tracks the number of unreported clicks. // clicks tracks the number of unreported clicks.
clicks int clicks int
@@ -30,7 +30,7 @@ type Click struct {
// Clicked calls Update and reports whether the button was // Clicked calls Update and reports whether the button was
// clicked since the last call. Multiple clicks result in Clicked // clicked since the last call. Multiple clicks result in Clicked
// returning true once per click. // returning true once per click.
func (b *Button) Clicked(gtx *layout.Context) bool { func (b *Clickable) Clicked(gtx *layout.Context) bool {
b.Update(gtx) b.Update(gtx)
if b.clicks > 0 { if b.clicks > 0 {
b.clicks-- b.clicks--
@@ -45,11 +45,11 @@ func (b *Button) Clicked(gtx *layout.Context) bool {
// History is the past clicks useful for drawing click markers. // History is the past clicks useful for drawing click markers.
// Clicks are retained for a short duration (about a second). // Clicks are retained for a short duration (about a second).
func (b *Button) History() []Click { func (b *Clickable) History() []Click {
return b.history return b.history
} }
func (b *Button) Layout(gtx *layout.Context) { func (b *Clickable) Layout(gtx *layout.Context) {
// Flush clicks from before the previous frame. // Flush clicks from before the previous frame.
b.Update(gtx) b.Update(gtx)
var st op.StackOp var st op.StackOp
@@ -69,7 +69,7 @@ func (b *Button) Layout(gtx *layout.Context) {
// Update the button state by processing events. The underlying // Update the button state by processing events. The underlying
// gesture events are returned for use beyond what Clicked offers. // 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) evts := b.click.Events(gtx)
for _, e := range evts { for _, e := range evts {
switch e.Type { switch e.Type {
+25 -3
View File
@@ -77,7 +77,29 @@ func IconButton(th *Theme, icon *widget.Icon) IconButtonStyle {
} }
} }
func (b ButtonStyle) Layout(gtx *layout.Context, button *widget.Button) { // Clickable lays out a rectangular clickable widget without further
// decoration.
func Clickable(gtx *layout.Context, button *widget.Clickable, w layout.Widget) {
layout.Stack{}.Layout(gtx,
layout.Expanded(func() {
button.Layout(gtx)
}),
layout.Expanded(func() {
clip.Rect{
Rect: f32.Rectangle{Max: f32.Point{
X: float32(gtx.Constraints.Width.Min),
Y: float32(gtx.Constraints.Height.Min),
}},
}.Op(gtx.Ops).Add(gtx.Ops)
for _, c := range button.History() {
drawInk(gtx, c)
}
}),
layout.Stacked(w),
)
}
func (b ButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
ButtonLayoutStyle{ ButtonLayoutStyle{
Background: b.Background, Background: b.Background,
CornerRadius: b.CornerRadius, CornerRadius: b.CornerRadius,
@@ -88,7 +110,7 @@ func (b ButtonStyle) Layout(gtx *layout.Context, button *widget.Button) {
}) })
} }
func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Button, w layout.Widget) { func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Clickable, w layout.Widget) {
hmin := gtx.Constraints.Width.Min hmin := gtx.Constraints.Width.Min
vmin := gtx.Constraints.Height.Min vmin := gtx.Constraints.Height.Min
layout.Stack{Alignment: layout.Center}.Layout(gtx, layout.Stack{Alignment: layout.Center}.Layout(gtx,
@@ -121,7 +143,7 @@ func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Button, w
) )
} }
func (b IconButtonStyle) Layout(gtx *layout.Context, button *widget.Button) { func (b IconButtonStyle) Layout(gtx *layout.Context, button *widget.Clickable) {
layout.Stack{Alignment: layout.Center}.Layout(gtx, layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Expanded(func() { layout.Expanded(func() {
size := gtx.Constraints.Width.Min size := gtx.Constraints.Width.Min
+5 -5
View File
@@ -5,14 +5,14 @@
// To maximize reusability and visual flexibility, user interface controls are // To maximize reusability and visual flexibility, user interface controls are
// split into two parts: the stateful widget and the stateless drawing of it. // split into two parts: the stateful widget and the stateless drawing of it.
// //
// For example, widget.Button encapsulates the state and event // For example, widget.Clickable encapsulates the state and event
// handling of all buttons, while the Theme can draw a single Button // handling of all clickable areas, while the Theme is responsible to
// in various styles. // draw a specific area, for example a button.
// //
// This snippet defines a button that prints a message when clicked: // This snippet defines a button that prints a message when clicked:
// //
// var gtx *layout.Context // var gtx *layout.Context
// button := new(widget.Button) // button := new(widget.Clickable)
// //
// for button.Clicked(gtx) { // for button.Clicked(gtx) {
// fmt.Println("Clicked!") // fmt.Println("Clicked!")
@@ -46,7 +46,7 @@
// btn.Layout(gtx) // btn.Layout(gtx)
// //
// Widget variants: A widget can have several distinct representations even // Widget variants: A widget can have several distinct representations even
// though the underlying state is the same. A widget.Button can be drawn as a // though the underlying state is the same. A widget.Clickable can be drawn as a
// round icon button: // round icon button:
// //
// icon := material.NewIcon(...) // icon := material.NewIcon(...)