...
1 package cview
2
3 import (
4 "fmt"
5
6 "github.com/gdamore/tcell/v2"
7 )
8
9
10 func ExampleNewApplication() {
11
12 app := NewApplication()
13
14 defer app.HandlePanic()
15
16
17 sharedTextView := NewTextView()
18 sharedTextView.SetTextAlign(AlignCenter)
19 sharedTextView.SetText("Widgets may be re-used between multiple layouts.")
20
21
22 mainTextView := NewTextView()
23 mainTextView.SetTextAlign(AlignCenter)
24 mainTextView.SetText("This is mainLayout.\n\nPress <Tab> to view aboutLayout.")
25
26 mainLayout := NewGrid()
27 mainLayout.AddItem(mainTextView, 0, 0, 1, 1, 0, 0, false)
28 mainLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)
29
30
31 aboutTextView := NewTextView()
32 aboutTextView.SetTextAlign(AlignCenter)
33 aboutTextView.SetText("cview muti-layout application example\n\nhttps://code.rocketnine.space/tslocum/cview")
34
35 aboutLayout := NewGrid()
36 aboutLayout.AddItem(aboutTextView, 0, 0, 1, 1, 0, 0, false)
37 aboutLayout.AddItem(sharedTextView, 1, 0, 1, 1, 0, 0, false)
38
39
40 currentLayout := 0
41
42
43
44 app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
45 if event.Key() == tcell.KeyTab {
46 if currentLayout == 0 {
47 currentLayout = 1
48
49 app.SetRoot(aboutLayout, true)
50 } else {
51 currentLayout = 0
52
53 app.SetRoot(mainLayout, true)
54 }
55
56
57
58 return nil
59 }
60
61
62 return event
63 })
64
65
66 app.SetRoot(mainLayout, true)
67 if err := app.Run(); err != nil {
68 panic(err)
69 }
70 }
71
72
73 func ExampleApplication_EnableMouse() {
74
75 app := NewApplication()
76
77 defer app.HandlePanic()
78
79
80 app.EnableMouse(true)
81
82
83 app.SetDoubleClickInterval(StandardDoubleClick)
84
85
86 tv := NewTextView()
87 tv.SetText("Click somewhere!")
88
89
90 app.SetMouseCapture(func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction) {
91 if action == MouseLeftClick || action == MouseLeftDoubleClick {
92 actionLabel := "click"
93 if action == MouseLeftDoubleClick {
94 actionLabel = "double-click"
95 }
96
97 x, y := event.Position()
98
99 fmt.Fprintf(tv, "\nYou %sed at %d,%d! Amazing!", actionLabel, x, y)
100
101
102 return nil, 0
103 }
104
105
106 return event, action
107 })
108
109
110 app.SetRoot(tv, true)
111 if err := app.Run(); err != nil {
112 panic(err)
113 }
114 }
115
View as plain text