Browse Source

Added Primitive demo to go along with the GitHub Wiki entry on how to create your own primitive.

pull/82/merge
Oliver 1 year ago
parent
commit
761e3d72da

+ 5
- 3
box.go View File

@@ -130,10 +130,12 @@ func (b *Box) GetDrawFunc() func(screen tcell.Screen, x, y, width, height int) (
130 130
 	return b.draw
131 131
 }
132 132
 
133
-// wrapInputHandler wraps an input handler (see InputHandler()) with the
133
+// WrapInputHandler wraps an input handler (see InputHandler()) with the
134 134
 // functionality to capture input (see SetInputCapture()) before passing it
135 135
 // on to the provided (default) input handler.
136
-func (b *Box) wrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primitive))) func(*tcell.EventKey, func(p Primitive)) {
136
+//
137
+// This is only meant to be used by subclassing primitives.
138
+func (b *Box) WrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primitive))) func(*tcell.EventKey, func(p Primitive)) {
137 139
 	return func(event *tcell.EventKey, setFocus func(p Primitive)) {
138 140
 		if b.inputCapture != nil {
139 141
 			event = b.inputCapture(event)
@@ -146,7 +148,7 @@ func (b *Box) wrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primiti
146 148
 
147 149
 // InputHandler returns nil.
148 150
 func (b *Box) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
149
-	return b.wrapInputHandler(nil)
151
+	return b.WrapInputHandler(nil)
150 152
 }
151 153
 
152 154
 // SetInputCapture installs a function which captures key events before they are

+ 1
- 1
button.go View File

@@ -121,7 +121,7 @@ func (b *Button) Draw(screen tcell.Screen) {
121 121
 
122 122
 // InputHandler returns the handler for this primitive.
123 123
 func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
124
-	return b.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
124
+	return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
125 125
 		// Process key event.
126 126
 		switch key := event.Key(); key {
127 127
 		case tcell.KeyEnter: // Selected.

+ 1
- 1
checkbox.go View File

@@ -155,7 +155,7 @@ func (c *Checkbox) Draw(screen tcell.Screen) {
155 155
 
156 156
 // InputHandler returns the handler for this primitive.
157 157
 func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
158
-	return c.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
158
+	return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
159 159
 		// Process key event.
160 160
 		switch key := event.Key(); key {
161 161
 		case tcell.KeyRune, tcell.KeyEnter: // Check.

+ 1
- 0
demos/primitive/README.md View File

@@ -0,0 +1 @@
1
+![Screenshot](screenshot.png)

BIN
demos/primitive/boxwithcenterline.png View File


+ 70
- 0
demos/primitive/main.go View File

@@ -0,0 +1,70 @@
1
+// Demo code which illustrates how to implement your own primitive.
2
+package main
3
+
4
+import (
5
+	"fmt"
6
+
7
+	"github.com/gdamore/tcell"
8
+	"github.com/rivo/tview"
9
+)
10
+
11
+// RadioButtons implements a simple primitive for radio button selections.
12
+type RadioButtons struct {
13
+	*tview.Box
14
+	options       []string
15
+	currentOption int
16
+}
17
+
18
+// NewRadioButtons returns a new radio button primitive.
19
+func NewRadioButtons(options []string) *RadioButtons {
20
+	return &RadioButtons{
21
+		Box:     tview.NewBox(),
22
+		options: options,
23
+	}
24
+}
25
+
26
+// Draw draws this primitive onto the screen.
27
+func (r *RadioButtons) Draw(screen tcell.Screen) {
28
+	r.Box.Draw(screen)
29
+	x, y, width, height := r.GetInnerRect()
30
+
31
+	for index, option := range r.options {
32
+		if index >= height {
33
+			break
34
+		}
35
+		radioButton := "\u25ef" // Unchecked.
36
+		if index == r.currentOption {
37
+			radioButton = "\u25c9" // Checked.
38
+		}
39
+		line := fmt.Sprintf(`%s[white]  %s`, radioButton, option)
40
+		tview.Print(screen, line, x, y+index, width, tview.AlignLeft, tcell.ColorYellow)
41
+	}
42
+}
43
+
44
+// InputHandler returns the handler for this primitive.
45
+func (r *RadioButtons) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
46
+	return r.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
47
+		switch event.Key() {
48
+		case tcell.KeyUp:
49
+			r.currentOption--
50
+			if r.currentOption < 0 {
51
+				r.currentOption = 0
52
+			}
53
+		case tcell.KeyDown:
54
+			r.currentOption++
55
+			if r.currentOption >= len(r.options) {
56
+				r.currentOption = len(r.options) - 1
57
+			}
58
+		}
59
+	})
60
+}
61
+
62
+func main() {
63
+	radioButtons := NewRadioButtons([]string{"Lions", "Elephants", "Giraffes"})
64
+	radioButtons.SetBorder(true).
65
+		SetTitle("Radio Button Demo").
66
+		SetRect(0, 0, 30, 5)
67
+	if err := tview.NewApplication().SetRoot(radioButtons, false).Run(); err != nil {
68
+		panic(err)
69
+	}
70
+}

BIN
demos/primitive/screenshot.png View File


BIN
demos/primitive/textviewwithcenterline.png View File


+ 1
- 1
dropdown.go View File

@@ -299,7 +299,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
299 299
 
300 300
 // InputHandler returns the handler for this primitive.
301 301
 func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
302
-	return d.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
302
+	return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
303 303
 		// A helper function which selects an item in the drop-down list based on
304 304
 		// the current prefix.
305 305
 		evalPrefix := func() {

+ 1
- 1
grid.go View File

@@ -263,7 +263,7 @@ func (g *Grid) HasFocus() bool {
263 263
 
264 264
 // InputHandler returns the handler for this primitive.
265 265
 func (g *Grid) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
266
-	return g.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
266
+	return g.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
267 267
 		switch event.Key() {
268 268
 		case tcell.KeyRune:
269 269
 			switch event.Rune() {

+ 1
- 1
inputfield.go View File

@@ -289,7 +289,7 @@ func (i *InputField) setCursor(screen tcell.Screen) {
289 289
 
290 290
 // InputHandler returns the handler for this primitive.
291 291
 func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
292
-	return i.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
292
+	return i.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
293 293
 		// Trigger changed events.
294 294
 		currentText := i.text
295 295
 		defer func() {

+ 1
- 1
list.go View File

@@ -259,7 +259,7 @@ func (l *List) Draw(screen tcell.Screen) {
259 259
 
260 260
 // InputHandler returns the handler for this primitive.
261 261
 func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
262
-	return l.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
262
+	return l.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
263 263
 		previousItem := l.currentItem
264 264
 
265 265
 		switch key := event.Key(); key {

+ 1
- 1
primitive.go View File

@@ -31,7 +31,7 @@ type Primitive interface {
31 31
 	//
32 32
 	// The Box class provides functionality to intercept keyboard input. If you
33 33
 	// subclass from Box, it is recommended that you wrap your handler using
34
-	// Box.wrapInputHandler() so you inherit that functionality.
34
+	// Box.WrapInputHandler() so you inherit that functionality.
35 35
 	InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive))
36 36
 
37 37
 	// Focus is called by the application when the primitive receives focus.

+ 1
- 1
table.go View File

@@ -818,7 +818,7 @@ ColumnLoop:
818 818
 
819 819
 // InputHandler returns the handler for this primitive.
820 820
 func (t *Table) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
821
-	return t.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
821
+	return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
822 822
 		key := event.Key()
823 823
 
824 824
 		if (!t.rowsSelectable && !t.columnsSelectable && key == tcell.KeyEnter) ||

+ 1
- 1
textview.go View File

@@ -839,7 +839,7 @@ func (t *TextView) Draw(screen tcell.Screen) {
839 839
 
840 840
 // InputHandler returns the handler for this primitive.
841 841
 func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
842
-	return t.wrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
842
+	return t.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
843 843
 		key := event.Key()
844 844
 
845 845
 		if key == tcell.KeyEscape || key == tcell.KeyEnter || key == tcell.KeyTab || key == tcell.KeyBacktab {

Loading…
Cancel
Save