Checkbox updates, SelectBox images

This commit is contained in:
Noah 2021-06-13 19:59:22 -07:00
parent 9a25ec3782
commit b87b4825af
4 changed files with 82 additions and 14 deletions

View File

@ -21,6 +21,10 @@ func NewRadiobox(name string, stringVar *string, value string, child Widget) *Ch
func makeCheckbox(name string, boolVar *bool, stringVar *string, value string, child Widget) *Checkbox { func makeCheckbox(name string, boolVar *bool, stringVar *string, value string, child Widget) *Checkbox {
// Our custom checkbutton widget. // Our custom checkbutton widget.
mark := NewFrame(name + "_mark") mark := NewFrame(name + "_mark")
mark.Configure(Config{
Width: 6,
Height: 6,
})
w := &Checkbox{ w := &Checkbox{
child: child, child: child,
@ -33,7 +37,7 @@ func makeCheckbox(name string, boolVar *bool, stringVar *string, value string, c
w.Frame.Setup() w.Frame.Setup()
// Forward clicks on the child widget to the CheckButton. // Forward clicks on the child widget to the CheckButton.
for _, e := range []Event{MouseOver, MouseOut, MouseUp, MouseDown} { for _, e := range []Event{MouseOver, MouseOut, MouseUp, MouseDown, Click} {
func(e Event) { func(e Event) {
w.child.Handle(e, func(ed EventData) error { w.child.Handle(e, func(ed EventData) error {
return w.button.Event(e, ed) return w.button.Event(e, ed)
@ -56,6 +60,11 @@ func (w *Checkbox) Child() Widget {
return w.child return w.child
} }
// Pass event handlers on to descendents.
func (w *Checkbox) Handle(e Event, fn func(EventData) error) {
w.button.Handle(e, fn)
}
// Supervise the checkbutton inside the widget. // Supervise the checkbutton inside the widget.
func (w *Checkbox) Supervise(s *Supervisor) { func (w *Checkbox) Supervise(s *Supervisor) {
s.Add(w.button) s.Add(w.button)

View File

@ -1,6 +1,8 @@
package ui package ui
import ( import (
"fmt"
"git.kirsle.net/go/render" "git.kirsle.net/go/render"
) )
@ -56,6 +58,34 @@ func (w *Frame) Pack(child Widget, config ...Pack) {
w.Add(child) w.Add(child)
} }
// Unpack removes the widget from the packed lists.
func (w *Frame) Unpack(child Widget) bool {
var any = false
for side, widgets := range w.packs {
var (
replace = []packedWidget{}
found = false
)
fmt.Printf("unpack:%s side:%s\n", child, side)
for _, widget := range widgets {
if widget.widget == child {
fmt.Printf("found!\n")
found = true
any = true
continue
}
replace = append(replace, widget)
}
if found {
w.packs[side] = replace
}
}
return any
}
// computePacked processes all the Pack layout widgets in the Frame. // computePacked processes all the Pack layout widgets in the Frame.
func (w *Frame) computePacked(e render.Engine) { func (w *Frame) computePacked(e render.Engine) {
var ( var (

View File

@ -64,6 +64,7 @@ func ImageFromImage(e render.Engine, im image.Image) (*Image, error) {
func ImageFromTexture(tex render.Texturer) *Image { func ImageFromTexture(tex render.Texturer) *Image {
return &Image{ return &Image{
texture: tex, texture: tex,
Image: tex.Image(),
} }
} }

View File

@ -20,6 +20,7 @@ type SelectBox struct {
frame *Frame frame *Frame
label *Label label *Label
arrow *Image arrow *Image
showImage *Image // User override show image
// Data storage. // Data storage.
textVariable string textVariable string
@ -82,6 +83,34 @@ func NewSelectBox(name string, withLabel Label) *SelectBox {
return w return w
} }
// SetImage sets the selectbox button to show the image instead of its
// normal text label. If the image corresponds with an option in the selectbox,
// it is up to the caller to call SetImage on change and set the right image here.
//
// Provide a nil value to remove the image and show the text labels instead.
func (w *SelectBox) SetImage(img *Image) {
// Get rid of the current image.
if w.showImage != nil {
w.showImage.Hide()
w.frame.Unpack(w.showImage)
}
// Are we getting a new one?
if img != nil {
w.label.Hide()
w.frame.Pack(img, Pack{
Side: W,
})
} else {
w.label.Show()
}
w.showImage = img
if w.showImage != nil {
w.showImage.Show()
}
}
// AddItem adds a new option to the SelectBox's menu. // AddItem adds a new option to the SelectBox's menu.
// The label is the text value to display. // The label is the text value to display.
// The value is the underlying value (string or int) for the TextVariable or IntVariable. // The value is the underlying value (string or int) for the TextVariable or IntVariable.
@ -197,4 +226,3 @@ func (w *SelectBox) setup() {
return nil return nil
}) })
} }