-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscreenobject.go
More file actions
337 lines (294 loc) · 7.73 KB
/
Copy pathscreenobject.go
File metadata and controls
337 lines (294 loc) · 7.73 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package vtui
import "github.com/unxed/vtinput"
type ColorStyleProvider interface {
GetPaletteIndex(baseIdx int) int
}
// CommandHandler defines an object that can process or route commands.
type CommandHandler interface {
HandleCommand(cmd int, args any) bool
IsLocked() bool
GetHelp() string
}
// ScreenObject is the base class for all visible UI elements,
// analog of ScreenObject from scrobj.hpp.
type ScreenObject struct {
X1, Y1, X2, Y2 int
owner CommandHandler
visible bool
focused bool
canFocus bool
lockCount int
helpTopic string
growMode GrowMode
hotkey rune
Id string
disabled bool
Command int
text string
cleanText string
hotkeyPos int
align string
stretch int
minW int
minH int
maxW int
maxH int
sizeSpecH *SizeSpec
sizeSpecV *SizeSpec
}
// SetID sets the stable identifier for the element.
func (so *ScreenObject) SetID(id string) {
so.Id = id
}
// ID returns the stable identifier for the element.
func (so *ScreenObject) ID() string {
return so.Id
}
// GetHotkey returns the assigned hotkey rune for the object.
func (so *ScreenObject) GetHotkey() rune {
return so.hotkey
}
func (so *ScreenObject) SetText(s string) {
so.text = s
clean, hk, pos := ParseAmpersandString(s)
so.cleanText = clean
so.hotkey = hk
so.hotkeyPos = pos
}
func (so *ScreenObject) GetText() string {
return so.text
}
func (so *ScreenObject) GetId() string {
return so.Id
}
func (so *ScreenObject) SetId(id string) {
so.Id = id
}
func (so *ScreenObject) SetOwner(owner CommandHandler) {
so.owner = owner
}
func (so *ScreenObject) GetOwner() CommandHandler {
return so.owner
}
// NotifyChange informs the owner that the element's data or state has changed.
func (so *ScreenObject) NotifyChange() {
if so.owner != nil {
if g, ok := so.owner.(interface{ OnElementChange(UIElement) }); ok {
g.OnElementChange(so)
}
}
}
func (so *ScreenObject) SetGrowMode(gm GrowMode) {
so.growMode = gm
}
func (so *ScreenObject) GetGrowMode() GrowMode {
return so.growMode
}
// HitTest returns true if the coordinates fall within the object's bounding box.
func (so *ScreenObject) HitTest(x, y int) bool {
return x >= so.X1 && x <= so.X2 && y >= so.Y1 && y <= so.Y2
}
func (so *ScreenObject) WantsChars() bool {
return false
}
func (so *ScreenObject) GetFocusLink() UIElement {
return nil
}
func (so *ScreenObject) MoveRelative(dx, dy int) {
so.X1 += dx
so.X2 += dx
so.Y1 += dy
so.Y2 += dy
}
// SetPosition sets the object's coordinates.
// Important: this does not trigger a redraw.
func (so *ScreenObject) SetPosition(x1, y1, x2, y2 int) {
if so.X1 == x1 && so.Y1 == y1 && so.X2 == x2 && so.Y2 == y2 {
return
}
so.X1, so.Y1, so.X2, so.Y2 = x1, y1, x2, y2
}
// GetPosition returns current object coordinates.
func (so *ScreenObject) GetPosition() (int, int, int, int) {
return so.X1, so.Y1, so.X2, so.Y2
}
// Show makes the object visible.
func (so *ScreenObject) Show(scr *ScreenBuf) {
if so.IsLocked() {
return
}
so.visible = true
}
// Hide hides the object.
func (so *ScreenObject) Hide(scr *ScreenBuf) {
so.visible = false
}
// IsVisible returns true if the object is visible.
func (so *ScreenObject) IsVisible() bool {
return so.visible
}
// SetVisible manually sets the visibility flag.
func (so *ScreenObject) SetVisible(v bool) {
so.visible = v
}
// SetFocus sets or removes focus from the object.
func (so *ScreenObject) SetFocus(f bool) {
if so.focused != f {
so.focused = f
}
}
// IsFocused returns the focus state of the object.
func (so *ScreenObject) IsFocused() bool {
return so.focused
}
// SetCanFocus sets whether the object can accept focus.
func (so *ScreenObject) SetCanFocus(c bool) {
so.canFocus = c
}
// CanFocus returns true if the object can be focused.
func (so *ScreenObject) CanFocus() bool {
return so.canFocus
}
// IsDisabled returns true if the object is explicitly disabled.
func (so *ScreenObject) IsDisabled() bool {
return so.disabled
}
// SetDisabled enables or disables the object.
func (so *ScreenObject) SetDisabled(d bool) {
so.disabled = d
if d {
so.focused = false
}
}
// Lock increases the lock counter. A locked object is not redrawn.
func (so *ScreenObject) Lock() {
so.lockCount++
}
// Unlock decreases the lock counter.
func (so *ScreenObject) Unlock() {
if so.lockCount > 0 {
so.lockCount--
}
}
// IsLocked returns true if the object or its owner is locked.
func (so *ScreenObject) IsLocked() bool {
if so.lockCount > 0 {
return true
}
if so.owner != nil {
return so.owner.IsLocked()
}
return false
}
func (so *ScreenObject) HandleBroadcast(cmd int, args any) bool {
return false
}
func (so *ScreenObject) Valid(cmd int) bool {
return true
}
// ProcessKey (stub) will be overridden in child classes.
func (so *ScreenObject) ProcessKey(key *vtinput.InputEvent) bool {
return false
}
// ProcessMouse is a default empty implementation.
func (so *ScreenObject) ProcessMouse(mouse *vtinput.InputEvent) bool {
return false
}
// ResizeConsole (stub) will be overridden to react to resizing.
func (so *ScreenObject) ResizeConsole() {
// Default empty implementation.
}
// SetHelp sets the help topic for this object.
func (so *ScreenObject) SetHelp(topic string) {
so.helpTopic = topic
}
// GetHelp returns the help topic for this object.
// If the topic is empty, it searches in the owner object.
func (so *ScreenObject) GetHelp() string {
if so.helpTopic != "" {
return so.helpTopic
}
if so.owner != nil {
return so.owner.GetHelp()
}
return ""
}
// ShowHelp triggers the help system for this object.
func (so *ScreenObject) ShowHelp() {
topic := so.GetHelp()
if topic == "" {
topic = UIStrings.DefaultHelp
}
DebugLog("HELP SYSTEM: Requested topic '%s'", topic)
if GlobalHelpEngine != nil {
hv := NewHelpView(GlobalHelpEngine, topic)
FrameManager.Push(hv)
}
}
func (so *ScreenObject) HasShadow() bool {
return false
}
func (so *ScreenObject) GetKeyLabels() *KeySet {
return nil
}
func (so *ScreenObject) GetMenuBar() *MenuBar {
return nil
}
// GetStateAttr returns the appropriate color attribute based on focus and disabled states.
func (so *ScreenObject) GetStateAttr(normIdx, focIdx int) uint64 {
idx := normIdx
if so.IsFocused() {
idx = focIdx
}
var owner CommandHandler = so.owner
for owner != nil {
if csp, ok := owner.(ColorStyleProvider); ok {
idx = csp.GetPaletteIndex(idx)
break
}
if s, ok := owner.(interface{ GetOwner() CommandHandler }); ok {
owner = s.GetOwner()
} else {
break
}
}
attr := Palette[idx]
if so.IsDisabled() {
return DimColor(attr)
}
return attr
}
// GetStateAttrs returns a pair of attributes (normal and highlight) based on states.
func (so *ScreenObject) GetStateAttrs(normIdx, focIdx, highIdx, focHighIdx int) (uint64, uint64) {
return so.GetStateAttr(normIdx, focIdx), so.GetStateAttr(highIdx, focHighIdx)
}
// FireAction centralizes the logic for executing an optional callback or emitting the internal Command.
// It gives priority to the callback.
func (so *ScreenObject) FireAction(callback func(), args any) bool {
if callback != nil {
callback()
return true
}
if so.Command != 0 {
if args == nil && so.ID() != "" {
args = so.ID()
}
if so.HandleCommand(so.Command, args) {
return true
}
if FrameManager != nil {
return FrameManager.EmitCommand(so.Command, args)
}
}
return false
}
// HandleCommand is the default implementation for command routing.
// It bubbles the command up to the owner.
func (so *ScreenObject) HandleCommand(cmd int, args any) bool {
if so.owner != nil {
DebugLog("CMD: [%p] Bubbling ID %d to owner [%p]", so, cmd, so.owner)
return so.owner.HandleCommand(cmd, args)
}
DebugLog("CMD: [%p] ID %d dropped (no owner)", so, cmd)
return false
}