~eliasnaur/gio

b39d1bdac724f2c454a90931856462ef3cf12932 — Elias Naur 7 months ago de7a598
widget: handle hovering of the empty Enum key

Before this change, a radio button with the empty key ("") would be
displayed as hovering if no other button were.

It's still not possible to have no radio buttons selected when one of
them is the empty key. If that's becomes necessary, Enum.Value can be
converted to a *string.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2 files changed, 14 insertions(+), 6 deletions(-)

M widget/enum.go
M widget/material/radiobutton.go
M widget/enum.go => widget/enum.go +12 -5
@@ 10,8 10,9 @@ import (
)

type Enum struct {
	Value   string
	Hovered string
	Value    string
	hovered  string
	hovering bool

	changed bool



@@ 36,6 37,11 @@ func (e *Enum) Changed() bool {
	return changed
}

// Hovered returns the key that is highlighted, or false if none are.
func (e *Enum) Hovered() (string, bool) {
	return e.hovered, e.hovering
}

// Layout adds the event handler for key.
func (e *Enum) Layout(gtx layout.Context, key string) layout.Dimensions {
	defer op.Save(gtx.Ops).Load()


@@ 57,11 63,12 @@ func (e *Enum) Layout(gtx layout.Context, key string) layout.Dimensions {
				}
			}
		}
		if e.Hovered == key {
			e.Hovered = ""
		if e.hovering && e.hovered == key {
			e.hovering = false
		}
		if clk.Hovered() {
			e.Hovered = key
			e.hovered = key
			e.hovering = true
		}
		clk.Add(gtx.Ops)
	}

M widget/material/radiobutton.go => widget/material/radiobutton.go +2 -1
@@ 36,7 36,8 @@ func RadioButton(th *Theme, group *widget.Enum, key, label string) RadioButtonSt

// Layout updates enum and displays the radio button.
func (r RadioButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
	dims := r.layout(gtx, r.Group.Value == r.Key, r.Group.Hovered == r.Key)
	hovered, hovering := r.Group.Hovered()
	dims := r.layout(gtx, r.Group.Value == r.Key, hovering && hovered == r.Key)
	gtx.Constraints.Min = dims.Size
	r.Group.Layout(gtx, r.Key)
	return dims