~pierrec/giox

d5ee5597564bbcfdd966fcc234dc6a17f92f8ef7 — pierre 9 months ago ab8c69f
widgetx: better init of Clickable slices

Signed-off-by: pierre <pierre.curto@gmail.com>
3 files changed, 19 insertions(+), 16 deletions(-)

M widgetx/click.go
M widgetx/select.go
M widgetx/tabs.go
M widgetx/click.go => widgetx/click.go +17 -8
@@ 34,6 34,21 @@ func (h Hover) Layout(gtx layout.Context, hovered bool) layout.Dimensions {
	return layout.Dimensions{Size: gtx.Constraints.Min}
}

func initClicks(clicks []widget.Clickable, n int) []widget.Clickable {
	switch cn := len(clicks); {
	case cn < n:
		// Grow the clicks.
		clicks = append(clicks, make([]widget.Clickable, n-cn)...)
	case cn > n:
		// Avoid leaks.
		for i := range clicks[n:] {
			clicks[n+i] = widget.Clickable{}
		}
		clicks = clicks[:n]
	}
	return clicks
}

// ClickElement displays the ith element.
type ClickElement func(layout.Context, int, *widget.Clickable) layout.Dimensions



@@ 100,10 115,7 @@ func (cl *ClickIcon) Changed() bool {
}

func (cl *ClickList) init(n int) {
	if cn := len(cl.clicks); cn < n {
		cl.clicks = append(cl.clicks, make([]widget.Clickable, n-cn)...)
	}
	cl.clicks = cl.clicks[:n]
	cl.clicks = initClicks(cl.clicks, n)
}

func (cl *ClickList) update() {


@@ 148,10 160,7 @@ func (cl *ClickList) Hovered(idx int) bool {
}

func (cl *ClickListWrap) init(n int) {
	if cn := len(cl.clicks); cn < n {
		cl.clicks = append(cl.clicks, make([]widget.Clickable, n-cn)...)
	}
	cl.clicks = cl.clicks[:n]
	cl.clicks = initClicks(cl.clicks, n)
}

func (cl *ClickListWrap) update() {

M widgetx/select.go => widgetx/select.go +1 -4
@@ 21,10 21,7 @@ type Select struct {

func (s *Select) init(n int) {
	s.list.Axis = layout.Vertical
	if cn := len(s.clicks); cn < n {
		s.clicks = append(s.clicks, make([]widget.Clickable, n-cn)...)
	}
	s.clicks = s.clicks[:n]
	s.clicks = initClicks(s.clicks, n)
}

func (s *Select) update() {

M widgetx/tabs.go => widgetx/tabs.go +1 -4
@@ 18,10 18,7 @@ func (ts *Tabs) init(n int) {
		// Set the first tab as the current one if none set.
		ts.SetActive(0)
	}
	if cn := len(ts.clicks); cn < n {
		ts.clicks = append(ts.clicks, make([]widget.Clickable, n-cn)...)
	}
	ts.clicks = ts.clicks[:n]
	ts.clicks = initClicks(ts.clicks, n)
}

func (ts *Tabs) update() {