File: doc_test.go

package info (click to toggle)
golang-code.rocketnine-tslocum-cview 1.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,396 kB
  • sloc: makefile: 40
file content (110 lines) | stat: -rw-r--r-- 2,850 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
package cview

import (
	"fmt"

	"github.com/gdamore/tcell/v2"
)

// Example of an application with multiple layouts.
func ExampleNewApplication() {
	// Initialize application.
	app := NewApplication()

	// Create shared TextView.
	sharedTextView := NewTextView()
	sharedTextView.SetTextAlign(AlignCenter)
	sharedTextView.SetText("Widgets may be re-used between multiple layouts.")

	// Create main layout using Grid.
	mainTextView := NewTextView()
	mainTextView.SetTextAlign(AlignCenter)
	mainTextView.SetText("This is mainLayout.\n\nPress <Tab> to view aboutLayout.")

	mainLayout := NewGrid()
	mainLayout.AddItem(mainTextView, 0, 0, 1, 1, 0, 0, false)
	mainLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)

	// Create about layout using Grid.
	aboutTextView := NewTextView()
	aboutTextView.SetTextAlign(AlignCenter)
	aboutTextView.SetText("cview muti-layout application example\n\nhttps://code.rocketnine.space/tslocum/cview")

	aboutLayout := NewGrid()
	aboutLayout.AddItem(aboutTextView, 0, 0, 1, 1, 0, 0, false)
	aboutLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)

	// Track the current layout.
	currentLayout := 0

	// Set an input capture function that switches between layouts when the tab
	// key is pressed.
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyTab {
			if currentLayout == 0 {
				currentLayout = 1

				app.SetRoot(aboutLayout, true)
			} else {
				currentLayout = 0

				app.SetRoot(mainLayout, true)
			}

			// Return nil to stop propagating the event to any remaining
			// handlers.
			return nil
		}

		// Return the event to continue propagating it.
		return event
	})

	// Run the application.
	app.SetRoot(mainLayout, true)
	if err := app.Run(); err != nil {
		panic(err)
	}
}

// Example of an application with mouse support.
func ExampleApplication_EnableMouse() {
	// Initialize application.
	app := NewApplication()

	// Enable mouse support.
	app.EnableMouse(true)

	// Enable double clicks.
	app.SetDoubleClickInterval(StandardDoubleClick)

	// Create a textview.
	tv := NewTextView()
	tv.SetText("Click somewhere!")

	// Set a mouse capture function which prints where the mouse was clicked.
	app.SetMouseCapture(func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction) {
		if action == MouseLeftClick || action == MouseLeftDoubleClick {
			actionLabel := "click"
			if action == MouseLeftDoubleClick {
				actionLabel = "double-click"
			}

			x, y := event.Position()

			fmt.Fprintf(tv, "\nYou %sed at %d,%d! Amazing!", actionLabel, x, y)

			// Return nil to stop propagating the event to any remaining handlers.
			return nil, 0
		}

		// Return the event to continue propagating it.
		return event, action
	})

	// Run the application.
	app.SetRoot(tv, true)
	if err := app.Run(); err != nil {
		panic(err)
	}
}