-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
315 lines (263 loc) · 6.07 KB
/
Copy pathruntime.go
File metadata and controls
315 lines (263 loc) · 6.07 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
package rego
import (
"fmt"
"runtime/debug"
"github.com/gdamore/tcell/v2"
)
// Runtime 是应用运行时
type Runtime struct {
screen tcell.Screen
root func(C) Node
rootContext *componentContext
focusManager *FocusManager
refreshChan chan struct{}
quitChan chan struct{}
// 光标位置(用于 IME 输入定位)
cursorX, cursorY int
showCursor bool
// 错误处理
lastPanic any
panicStack []byte
}
// newRuntime 创建运行时
func newRuntime(root func(C) Node) *Runtime {
return &Runtime{
root: root,
focusManager: newFocusManager(),
refreshChan: make(chan struct{}, 1),
quitChan: make(chan struct{}),
}
}
// Run 启动运行时
func (r *Runtime) Run() error {
// 初始化 tcell screen
screen, err := tcell.NewScreen()
if err != nil {
return err
}
if err := screen.Init(); err != nil {
return err
}
defer func() {
// 清理所有 effects
if r.rootContext != nil {
r.rootContext.cleanup()
}
screen.Fini()
}()
r.screen = screen
r.rootContext = newComponentContext("root", nil, r)
// 启用粘贴模式(改善 IME 支持)
screen.EnablePaste()
// 隐藏光标(避免 IME 定位问题)
screen.HideCursor()
// 启用鼠标支持(包含运动追踪以支持 Hover)
screen.EnableMouse(tcell.MouseButtonEvents | tcell.MouseMotionEvents)
// 初始渲染
r.render()
// 启动事件监听协程
eventChan := make(chan tcell.Event)
go func() {
for {
ev := screen.PollEvent()
if ev == nil {
return
}
eventChan <- ev
}
}()
// 主循环
for {
select {
case <-r.quitChan:
return nil
case <-r.refreshChan:
r.render()
case ev := <-eventChan:
r.handleEvent(ev)
}
}
}
// render 执行渲染
func (r *Runtime) render() {
r.screen.Clear()
// 如果之前发生了 panic,显示错误界面
if r.lastPanic != nil {
r.drawErrorScreen()
r.screen.Show()
return
}
defer func() {
if err := recover(); err != nil {
r.lastPanic = err
r.panicStack = debug.Stack()
r.screen.Clear()
r.drawErrorScreen()
r.screen.Show()
}
}()
r.rootContext.reset()
// 重置焦点管理器(每次渲染前)
r.focusManager.Reset()
// 重置光标状态(每次渲染前)
r.showCursor = false
// 调用根组件
node := r.root(r.rootContext)
// 准备渲染屏幕代理(拦截光标设置)
renderScreen := &renderScreenProxy{
Screen: r.screen,
runtime: r,
}
// 渲染到屏幕
width, height := r.screen.Size()
if node != nil {
node.render(renderScreen, 0, 0, width, height)
}
// 设置光标位置(用于 IME 输入定位)
if r.showCursor {
r.screen.ShowCursor(r.cursorX, r.cursorY)
} else {
r.screen.HideCursor()
}
r.screen.Show()
}
// renderScreenProxy 代理 tcell.Screen 以拦截光标设置
type renderScreenProxy struct {
tcell.Screen
runtime *Runtime
}
func (p *renderScreenProxy) ShowCursor(x, y int) {
if p.runtime != nil {
p.runtime.setCursor(x, y)
}
}
func (p *renderScreenProxy) HideCursor() {
if p.runtime != nil {
p.runtime.showCursor = false
}
}
// handleEvent 处理事件
func (r *Runtime) handleEvent(event tcell.Event) {
switch e := event.(type) {
case *tcell.EventKey:
// Ctrl+C 退出
if e.Key() == tcell.KeyCtrlC {
r.quit()
return
}
// Tab/Shift+Tab 焦点导航
if e.Key() == tcell.KeyTab {
if e.Modifiers()&tcell.ModShift != 0 {
r.focusManager.Prev()
} else {
r.focusManager.Next()
}
r.scheduleRefresh()
return
}
// 转换按键
key, ru, _ := convertTcellKey(e)
// 分发给组件树
r.rootContext.dispatchKeyEvent(key, ru)
case *tcell.EventMouse:
ev := convertTcellMouseEvent(e)
r.rootContext.dispatchMouseEvent(ev)
case *tcell.EventResize:
r.scheduleRefresh()
}
}
// convertTcellMouseEvent 将 tcell 鼠标事件转换为 rego 鼠标事件
func convertTcellMouseEvent(e *tcell.EventMouse) MouseEvent {
x, y := e.Position()
button := MouseButtonNone
eventType := MouseEventMove // 默认为移动
b := e.Buttons()
if b&tcell.Button1 != 0 {
button = MouseButtonLeft
eventType = MouseEventClick
} else if b&tcell.Button3 != 0 {
button = MouseButtonRight
eventType = MouseEventClick
} else if b&tcell.Button2 != 0 {
button = MouseButtonMiddle
eventType = MouseEventClick
}
// 处理滚轮
if b&tcell.WheelUp != 0 {
eventType = MouseEventScrollUp
} else if b&tcell.WheelDown != 0 {
eventType = MouseEventScrollDown
}
return MouseEvent{
X: x,
Y: y,
Button: button,
Type: eventType,
}
}
// scheduleRefresh 调度刷新
func (r *Runtime) scheduleRefresh() {
select {
case r.refreshChan <- struct{}{}:
default:
// 已有刷新请求,忽略
}
}
// quit 退出应用
func (r *Runtime) quit() {
close(r.quitChan)
}
// setCursor 设置光标位置
func (r *Runtime) setCursor(x, y int) {
r.cursorX = x
r.cursorY = y
r.showCursor = true
}
// drawErrorScreen 绘制错误界面
func (r *Runtime) drawErrorScreen() {
w, h := r.screen.Size()
style := tcell.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorWhite)
// 填充红色背景
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r.screen.SetContent(x, y, ' ', nil, style)
}
}
// 绘制标题
title := " REGO RUNTIME PANIC "
r.drawText((w-len(title))/2, 2, title, style.Bold(true))
// 绘制错误信息
msg := fmt.Sprintf("Error: %v", r.lastPanic)
r.drawText(2, 4, msg, style)
// 绘制堆栈(截取部分)
r.drawText(2, 6, "Stack Trace:", style.Underline(true))
lines := fmt.Sprintf("%s", r.panicStack)
for i, line := range splitLines(lines) {
if i > h-10 {
break
}
r.drawText(2, 7+i, line, style)
}
// 绘制退出提示
footer := "Press Ctrl+C to quit"
r.drawText((w-len(footer))/2, h-2, footer, style.Dim(true))
}
func (r *Runtime) drawText(x, y int, text string, style tcell.Style) {
for i, ru := range text {
r.screen.SetContent(x+i, y, ru, nil, style)
}
}
func splitLines(s string) []string {
var lines []string
start := 0
for i := 0; i < len(s); i++ {
if s[i] == '\n' {
lines = append(lines, s[start:i])
start = i + 1
}
}
if start < len(s) {
lines = append(lines, s[start:])
}
return lines
}