github.com/Seikaijyu/gio@v0.0.1/widget/bool.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package widget 4 5 import ( 6 "github.com/Seikaijyu/gio/io/semantic" 7 "github.com/Seikaijyu/gio/layout" 8 ) 9 10 type Bool struct { 11 Value bool 12 _checked func(bool) 13 clk Clickable 14 } 15 16 // Update the widget state and report whether Value was changed. 17 func (b *Bool) Update(gtx layout.Context) bool { 18 changed := false 19 for b.clk.Clicked(gtx) { 20 b.Value = !b.Value 21 changed = true 22 if b._checked != nil { 23 b._checked(b.Value) 24 } 25 } 26 return changed 27 } 28 29 // Hovered reports whether pointer is over the element. 30 func (b *Bool) Hovered() bool { 31 return b.clk.Hovered() 32 } 33 34 // Pressed reports whether pointer is pressing the element. 35 func (b *Bool) OnChecked(fn func(bool)) { 36 b._checked = fn 37 } 38 39 // Focused reports whether b has focus. 40 func (b *Bool) Focused() bool { 41 return b.clk.Focused() 42 } 43 44 func (b *Bool) History() []Press { 45 return b.clk.History() 46 } 47 48 func (b *Bool) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions { 49 b.Update(gtx) 50 dims := b.clk.Layout(gtx, func(gtx layout.Context) layout.Dimensions { 51 semantic.SelectedOp(b.Value).Add(gtx.Ops) 52 semantic.EnabledOp(gtx.Queue != nil).Add(gtx.Ops) 53 return w(gtx) 54 }) 55 return dims 56 }