Beispiel #1
0
func (r *Radio) makeButtons(options []string) {
	// see if the options are actually different
	changed := len(r.options) != len(options)
	if !changed {
		for i := range r.options {
			if r.options[i] != options[i] {
				changed = true
			}
		}
	}
	if !changed {
		return
	}
	r.options = options

	// remove old buttons
	for _, b := range r.buttons {
		r.RemoveBlock(&b.Block)
	}
	for _, d := range r.buttonsDone {
		d <- true
	}

	r.buttons = make([]*Button, len(r.options))
	r.buttonsDone = make([]chan bool, len(r.options))
	for i, option := range r.options {
		ob := NewButton(option)
		r.buttons[i] = ob
		r.buttonsDone[i] = make(chan bool, 1)

		pb := layouts.NewPadBox(layouts.PadConfig{
			Left: 2, Right: 2,
			Top: 2, Bottom: 2,
		}, &ob.Block)

		r.radioGrid.Add(&pb.Block, layouts.GridComponent{
			GridX: 0, GridY: i,
			AnchorLeft:   true,
			AnchorRight:  true,
			AnchorTop:    true,
			AnchorBottom: true,
		})

		clicker := make(chan wde.Button, 1)
		go func(clicker chan wde.Button, index int, done chan bool) {
			for {
				select {
				case <-clicker:
					r.SetSelection <- index
				case <-done:
					return
				}
			}
		}(clicker, i, r.buttonsDone[i])
		ob.AddClicker <- clicker
	}
	r.updateButtons()
}
Beispiel #2
0
func uikplay() {

	w, err := uik.NewWindow(nil, 480, 320)
	if err != nil {
		fmt.Println(err)
		return
	}
	w.W.SetTitle("go.uik")

	gcfg, err := layouts.ParseGridConfig(`
{
	"Components": {
		"radio": {
			"GridX": 0,
			"GridY": 0,
			"AnchorLeft": true,
			"AnchorTop": true
		},
		"label": {
			"GridX": 1,
			"GridY": 0,
			"AnchorRight": true,
			"AnchorTop": true
		}
	}
}
		`)
	if err != nil {
		fmt.Println(err)
		return
	}

	ge := layouts.NewGridEngine(gcfg)
	g := layouts.NewLayouter(ge)

	rg := widgets.NewRadio([]string{"bread", "cake", "beheadings"})
	ge.AddName("radio", &rg.Block)

	l := widgets.NewLabel(geom.Coord{100, 30}, widgets.LabelConfig{"text", 14, color.Black})
	ge.AddName("label", &layouts.NewPadBox(layouts.PadConfig{Right: 10}, &l.Block).Block)

	selLis := make(widgets.SelectionListener, 1)
	rg.AddSelectionListener <- selLis

	w.SetPane(&g.Block)

	w.Show()

	done := make(chan interface{}, 1)
	isDone := func(e interface{}) (accept, done bool) {
		_, accept = e.(uik.CloseEvent)
		done = accept
		return
	}
	w.Block.Subscribe <- uik.Subscription{isDone, done}

loop:
	for {
		select {
		case sel := <-selLis:
			l.SetConfig(widgets.LabelConfig{
				Text:     fmt.Sprintf("Clicked option %d, %q", sel.Index, sel.Option),
				FontSize: 14,
				Color:    color.Black,
			})
		case <-done:
			break loop
		}
	}

	w.W.Close()

	wde.Stop()
}