File: radio.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (159 lines) | stat: -rw-r--r-- 4,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package radio provides radio button widgets where one can be selected among many.
package radio

import (
	"fmt"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwutil"
	"github.com/gcla/gowid/widgets/button"
	"github.com/gcla/gowid/widgets/checkbox"
)

//======================================================================

type IWidget interface {
	gowid.IWidget
	gowid.ICallbacks
	IsChecked() bool
	Group() *[]IWidget
	SetStateInternal(selected bool)
}

type Widget struct {
	Selected bool
	group    *[]IWidget
	*gowid.Callbacks
	gowid.ClickCallbacks
	checkbox.Decoration
	gowid.AddressProvidesID
	gowid.IsSelectable
}

// If the group supplied is empty, this radio button will be marked as selected, regardless
// of the isChecked parameter.
func New(group *[]IWidget) *Widget {
	res := &Widget{
		Selected:   false,
		group:      group,
		Decoration: checkbox.Decoration{button.Decoration{"(", ")"}, "X"},
	}
	res.ClickCallbacks = gowid.ClickCallbacks{CB: &res.Callbacks}
	res.initRadioButton(group)

	var _ IWidget = res

	return res
}

func NewDecorated(group *[]IWidget, decoration checkbox.Decoration) *Widget {
	res := &Widget{
		Selected:   false,
		group:      group,
		Decoration: decoration,
	}
	res.ClickCallbacks = gowid.ClickCallbacks{CB: &res.Callbacks}
	res.initRadioButton(group)

	var _ gowid.IWidget = res

	return res
}

func (w *Widget) initRadioButton(group *[]IWidget) {
	*group = append(*group, w)
	if len(*group) == 1 {
		w.SetStateInternal(true)
	}
}

func (w *Widget) String() string {
	return fmt.Sprintf("radio[%s]", gwutil.If(w.IsChecked(), "X", " ").(string))
}

func (w *Widget) Select(app gowid.IApp) {
	Select(w, app)
}

func (w *Widget) Group() *[]IWidget {
	return w.group
}

// Don't ensure consistency of other widgets, but do issue callbacks for
// state change. TODO - need to do callbacks here to capture
// losing selection
func (w *Widget) SetStateInternal(selected bool) {
	w.Selected = selected
}

func (w *Widget) IsChecked() bool {
	return w.Selected
}

func (w *Widget) Click(app gowid.IApp) {
	if app.GetMouseState().NoButtonClicked() || app.GetMouseState().LeftIsClicked() {
		w.Select(app)
	}
}

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	if _, ok := size.(gowid.IRenderFixed); !ok {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFixed"})
	}
	return gowid.RenderBox{C: len(w.LeftDec()) + len(w.RightDec()) + len(w.MiddleDec()), R: 1}
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	if _, ok := size.(gowid.IRenderFixed); !ok {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFixed"})
	}

	res := checkbox.Render(w, size, focus, app)

	return res
}

func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	return button.UserInput(w, ev, size, focus, app)
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

func Select(w IWidget, app gowid.IApp) {
	cur := w.IsChecked()
	if !cur {
		for _, w2 := range *w.Group() {
			if w != w2 && w2.IsChecked() {
				w2.SetStateInternal(false)
				gowid.RunWidgetCallbacks(w2, gowid.ClickCB{}, app, w2)
				break
			}
		}
		w.SetStateInternal(true)
		gowid.RunWidgetCallbacks(w, gowid.ClickCB{}, app, w)
	}
}

//======================================================================

// This is here to avoid import cycles
type RadioButtonTester struct {
	State bool
}

func (f *RadioButtonTester) Changed(app gowid.IApp, w gowid.IWidget, data ...interface{}) {
	rb := w.(*Widget)
	f.State = rb.Selected
}

func (f *RadioButtonTester) ID() interface{} { return "bar" }

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: