forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapplication.go
More file actions
170 lines (151 loc) · 3.26 KB
/
application.go
File metadata and controls
170 lines (151 loc) · 3.26 KB
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
160
161
162
163
164
165
166
167
168
169
170
package gotui
import (
"sync"
)
// Application represents the application.
type Application struct {
root Widget
focus Widget
running bool
stop chan struct{}
Backend *Backend
sync.Mutex
}
// NewApp returns a new Application.
func NewApp() *Application {
return &Application{
Backend: DefaultBackend,
}
}
// SetRoot sets the root widget of the application.
// If focus is true, the root widget is also focused.
func (a *Application) SetRoot(root Widget, focus bool) {
a.Lock()
defer a.Unlock()
a.root = root
if focus {
a.focus = root
}
}
// SetFocus sets the focus to the given widget.
func (a *Application) SetFocus(p Widget) {
a.Lock()
defer a.Unlock()
a.focus = p
}
// Stop stops the application.
func (a *Application) Stop() {
a.Lock()
defer a.Unlock()
if a.running && a.stop != nil {
close(a.stop)
a.running = false
}
}
// getRoot returns the root widget under lock.
func (a *Application) getRoot() Widget {
a.Lock()
defer a.Unlock()
return a.root
}
// Run runs the application.
func (a *Application) Run() error {
if err := a.Backend.Init(); err != nil {
return err
}
defer a.Backend.Close()
a.Lock()
a.running = true
a.stop = make(chan struct{}) // Recreate for each Run
a.Unlock()
// Size the root widget to terminal size after init
root := a.getRoot()
if root != nil {
w, h := a.Backend.TerminalDimensions()
// Lock during SetRect to prevent race with Draw
root.Lock()
root.SetRect(0, 0, w, h)
root.Unlock()
a.Backend.Render(root)
}
uiEvents := a.Backend.PollEvents()
for {
select {
case <-a.stop:
return nil
case e := <-uiEvents:
if a.handleEvent(e) {
return nil
}
}
}
}
// handleEvent processes a single event. Returns true if the application should stop.
func (a *Application) handleEvent(e Event) bool {
if e.Type == ResizeEvent {
a.handleResize(e)
return false
}
handled := a.dispatchKeyOrMouse(e)
// Default handlers (like Quit)
if !handled {
if e.ID == "<C-c>" || e.ID == "q" {
return true
}
}
// Re-render with current terminal dimensions
root := a.getRoot()
if root != nil {
w, h := a.Backend.TerminalDimensions()
if w > 0 && h > 0 {
// Lock during SetRect to prevent race with Draw
root.Lock()
root.SetRect(0, 0, w, h)
root.Unlock()
}
// Clear before render to prevent stale content when widget content changes
a.Backend.Clear()
a.Backend.Render(root)
}
return false
}
func (a *Application) handleResize(e Event) {
payload := e.Payload.(Resize)
root := a.getRoot()
if root != nil {
// Ensure minimum dimensions to prevent rendering issues
w, h := payload.Width, payload.Height
if w < 1 {
w = 1
}
if h < 1 {
h = 1
}
// Lock during SetRect to prevent race with Draw
root.Lock()
root.SetRect(0, 0, w, h)
root.Unlock()
a.Backend.Clear() // Only clear on resize to prevent stale content at edges
a.Backend.Render(root)
}
}
func (a *Application) dispatchKeyOrMouse(e Event) bool {
handled := false
a.Lock()
focus := a.focus
root := a.root
a.Unlock()
// 1. Dispatch to Focus (Keyboard)
if e.Type == KeyboardEvent && focus != nil {
if focus.HandleEvent(e) {
handled = true
}
}
// 2. Dispatch to Root (Mouse? Bubble up?)
if !handled && root != nil {
if root.HandleEvent(e) {
handled = true
}
}
return handled
}