mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
widget,widget/material: remove disabled drawing modes
Determining the enabled state of a widget from whether its Clicked method has been called only works for button-like widgets. For example, it's not clear a Clicked method is appropriate for a CheckBox. Remove the feature for now, and let's find a better design in the future. As a nice side effect, we can now process events in Layout methods, so that buttons react to user input even when Clicked is not called. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+10
-16
@@ -62,13 +62,13 @@ var (
|
|||||||
SingleLine: true,
|
SingleLine: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}
|
}
|
||||||
button = new(widget.Button)
|
button = new(widget.Button)
|
||||||
disabledButton = new(widget.Button)
|
greenButton = new(widget.Button)
|
||||||
iconButton = new(widget.Button)
|
iconButton = new(widget.Button)
|
||||||
list = &layout.List{
|
list = &layout.List{
|
||||||
Axis: layout.Vertical,
|
Axis: layout.Vertical,
|
||||||
}
|
}
|
||||||
enabled bool
|
green = true
|
||||||
topLabel = "Hello, Gio"
|
topLabel = "Hello, Gio"
|
||||||
icon *material.Icon
|
icon *material.Icon
|
||||||
)
|
)
|
||||||
@@ -98,15 +98,13 @@ func kitchen(gtx *layout.Context, th *material.Theme) {
|
|||||||
in := layout.UniformInset(unit.Dp(8))
|
in := layout.UniformInset(unit.Dp(8))
|
||||||
b1 := buttons.Rigid(gtx, func() {
|
b1 := buttons.Rigid(gtx, func() {
|
||||||
in.Layout(gtx, func() {
|
in.Layout(gtx, func() {
|
||||||
for iconButton.Clicked(gtx) {
|
|
||||||
}
|
|
||||||
th.IconButton(icon).Layout(gtx, iconButton)
|
th.IconButton(icon).Layout(gtx, iconButton)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
b2 := buttons.Rigid(gtx, func() {
|
b2 := buttons.Rigid(gtx, func() {
|
||||||
in.Layout(gtx, func() {
|
in.Layout(gtx, func() {
|
||||||
for button.Clicked(gtx) {
|
for button.Clicked(gtx) {
|
||||||
enabled = !enabled
|
green = !green
|
||||||
}
|
}
|
||||||
th.Button("Click me!").Layout(gtx, button)
|
th.Button("Click me!").Layout(gtx, button)
|
||||||
})
|
})
|
||||||
@@ -114,15 +112,11 @@ func kitchen(gtx *layout.Context, th *material.Theme) {
|
|||||||
b3 := buttons.Rigid(gtx, func() {
|
b3 := buttons.Rigid(gtx, func() {
|
||||||
in.Layout(gtx, func() {
|
in.Layout(gtx, func() {
|
||||||
var btn material.Button
|
var btn material.Button
|
||||||
if enabled {
|
btn = th.Button("Green button")
|
||||||
for disabledButton.Clicked(gtx) {
|
if green {
|
||||||
}
|
btn.Background = color.RGBA{A: 0xff, R: 0x9e, G: 0x9d, B: 0x24}
|
||||||
btn = th.Button("Enabled")
|
|
||||||
} else {
|
|
||||||
btn = th.Button("Disabled")
|
|
||||||
}
|
}
|
||||||
btn.Background = color.RGBA{A: 0xff, R: 0x9e, G: 0x9d, B: 0x24}
|
btn.Layout(gtx, greenButton)
|
||||||
btn.Layout(gtx, disabledButton)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
buttons.Layout(gtx, b1, b2, b3)
|
buttons.Layout(gtx, b1, b2, b3)
|
||||||
|
|||||||
+43
-35
@@ -12,9 +12,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
click gesture.Click
|
click gesture.Click
|
||||||
clicks int
|
// clicks tracks the number of unreported clicks.
|
||||||
history []Click
|
clicks int
|
||||||
|
// prevClicks tracks the number of unreported clicks
|
||||||
|
// that belong to the previous frame.
|
||||||
|
prevClicks int
|
||||||
|
history []Click
|
||||||
}
|
}
|
||||||
|
|
||||||
// Click represents a historic click.
|
// Click represents a historic click.
|
||||||
@@ -24,6 +28,42 @@ type Click struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) Clicked(gtx *layout.Context) bool {
|
func (b *Button) Clicked(gtx *layout.Context) bool {
|
||||||
|
b.processEvents(gtx)
|
||||||
|
if b.clicks > 0 {
|
||||||
|
b.clicks--
|
||||||
|
if b.prevClicks > 0 {
|
||||||
|
b.prevClicks--
|
||||||
|
}
|
||||||
|
if b.clicks > 0 {
|
||||||
|
// Ensure timely delivery of remaining clicks.
|
||||||
|
op.InvalidateOp{}.Add(gtx.Ops)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) History() []Click {
|
||||||
|
return b.history
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) Layout(gtx *layout.Context) {
|
||||||
|
// Flush clicks from before the previous frame.
|
||||||
|
b.clicks -= b.prevClicks
|
||||||
|
b.prevClicks = 0
|
||||||
|
b.processEvents(gtx)
|
||||||
|
b.click.Add(gtx.Ops)
|
||||||
|
for len(b.history) > 0 {
|
||||||
|
c := b.history[0]
|
||||||
|
if gtx.Now().Sub(c.Time) < 1*time.Second {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
copy(b.history, b.history[1:])
|
||||||
|
b.history = b.history[:len(b.history)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Button) processEvents(gtx *layout.Context) {
|
||||||
for _, e := range b.click.Events(gtx) {
|
for _, e := range b.click.Events(gtx) {
|
||||||
switch e.Type {
|
switch e.Type {
|
||||||
case gesture.TypeClick:
|
case gesture.TypeClick:
|
||||||
@@ -35,36 +75,4 @@ func (b *Button) Clicked(gtx *layout.Context) bool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if b.clicks > 0 {
|
|
||||||
b.clicks--
|
|
||||||
if b.clicks > 0 {
|
|
||||||
// Ensure timely delivery of remaining clicks.
|
|
||||||
op.InvalidateOp{}.Add(gtx.Ops)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Button) Active() bool {
|
|
||||||
return b.click.Active()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Button) History() []Click {
|
|
||||||
return b.history
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Button) Layout(gtx *layout.Context) {
|
|
||||||
b.click.Add(gtx.Ops)
|
|
||||||
if !b.Active() {
|
|
||||||
b.clicks = 0
|
|
||||||
}
|
|
||||||
for len(b.history) > 0 {
|
|
||||||
c := b.history[0]
|
|
||||||
if gtx.Now().Sub(c.Time) < 1*time.Second {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
copy(b.history, b.history[1:])
|
|
||||||
b.history = b.history[:len(b.history)-1]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-3
@@ -60,6 +60,8 @@ type Editor struct {
|
|||||||
|
|
||||||
// events is the list of events not yet processed.
|
// events is the list of events not yet processed.
|
||||||
events []EditorEvent
|
events []EditorEvent
|
||||||
|
// prevEvents is the number of events from the previous frame.
|
||||||
|
prevEvents int
|
||||||
}
|
}
|
||||||
|
|
||||||
type EditorEvent interface {
|
type EditorEvent interface {
|
||||||
@@ -90,6 +92,7 @@ func (e *Editor) Events(gtx *layout.Context) []EditorEvent {
|
|||||||
e.processEvents(gtx)
|
e.processEvents(gtx)
|
||||||
events := e.events
|
events := e.events
|
||||||
e.events = nil
|
e.events = nil
|
||||||
|
e.prevEvents = 0
|
||||||
return events
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,15 +180,16 @@ func (e *Editor) Focus() {
|
|||||||
|
|
||||||
// Layout lays out the editor.
|
// Layout lays out the editor.
|
||||||
func (e *Editor) Layout(gtx *layout.Context, sh *text.Shaper, font text.Font) {
|
func (e *Editor) Layout(gtx *layout.Context, sh *text.Shaper, font text.Font) {
|
||||||
|
// Flush events from before the previous frame.
|
||||||
|
copy(e.events, e.events[e.prevEvents:])
|
||||||
|
e.events = e.events[:len(e.events)-e.prevEvents]
|
||||||
|
e.prevEvents = len(e.events)
|
||||||
if e.font != font {
|
if e.font != font {
|
||||||
e.invalidate()
|
e.invalidate()
|
||||||
e.font = font
|
e.font = font
|
||||||
}
|
}
|
||||||
e.processEvents(gtx)
|
e.processEvents(gtx)
|
||||||
e.layout(gtx, sh)
|
e.layout(gtx, sh)
|
||||||
if !e.clicker.Active() {
|
|
||||||
e.events = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) layout(gtx *layout.Context, sh *text.Shaper) {
|
func (e *Editor) layout(gtx *layout.Context, sh *text.Shaper) {
|
||||||
|
|||||||
@@ -78,10 +78,6 @@ func (t *Theme) IconButton(icon *Icon) IconButton {
|
|||||||
func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
|
func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
|
||||||
col := b.Color
|
col := b.Color
|
||||||
bgcol := b.Background
|
bgcol := b.Background
|
||||||
if !button.Active() {
|
|
||||||
col = rgb(0x888888)
|
|
||||||
bgcol = rgb(0xcccccc)
|
|
||||||
}
|
|
||||||
st := layout.Stack{Alignment: layout.Center}
|
st := layout.Stack{Alignment: layout.Center}
|
||||||
hmin := gtx.Constraints.Width.Min
|
hmin := gtx.Constraints.Width.Min
|
||||||
vmin := gtx.Constraints.Height.Min
|
vmin := gtx.Constraints.Height.Min
|
||||||
@@ -128,9 +124,6 @@ func (b IconButton) Layout(gtx *layout.Context, button *widget.Button) {
|
|||||||
button.Layout(gtx)
|
button.Layout(gtx)
|
||||||
})
|
})
|
||||||
bgcol := b.Background
|
bgcol := b.Background
|
||||||
if !button.Active() {
|
|
||||||
bgcol = rgb(0xcccccc)
|
|
||||||
}
|
|
||||||
bg := st.Expand(gtx, func() {
|
bg := st.Expand(gtx, func() {
|
||||||
size := float32(gtx.Constraints.Width.Min)
|
size := float32(gtx.Constraints.Width.Min)
|
||||||
rr := float32(size) * .5
|
rr := float32(size) * .5
|
||||||
|
|||||||
Reference in New Issue
Block a user