| 1 |
|
| 2 |
n |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package mauview |
| 9 |
|
| 10 |
import ( |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"os" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"time" |
| 17 |
|
| •18 |
"github.com/gdamore/tcell/v2" |
| 19 |
) |
| 20 |
|
| 21 |
type Component interface { |
| 22 |
Draw(screen Screen) |
| 23 |
OnKeyEvent(event KeyEvent) bool |
| 24 |
OnPasteEvent(event PasteEvent) bool |
| 25 |
OnMouseEvent(event MouseEvent) bool |
| 26 |
} |
| 27 |
|
| 28 |
type Focusable interface { |
| 29 |
Focus() |
| 30 |
Blur() |
| 31 |
} |
| 32 |
|
| 33 |
type FocusableComponent interface { |
| 34 |
Component |
| 35 |
Focusable |
| 36 |
} |
| 37 |
|
| 38 |
type Application struct { |
| 39 |
screenLock sync.RWMutex |
| 40 |
screen tcell.Screen |
| 41 |
prevMouseEvt *tcell.EventMouse |
| 42 |
root Component |
| 43 |
updates chan interface{} |
| 44 |
redrawTicker *time.Ticker |
| 45 |
stop chan struct{} |
| 46 |
waitForStop chan struct{} |
| 47 |
alwaysClear bool |
| 48 |
} |
| 49 |
|
| 50 |
const queueSize = 255 |
| 51 |
|
| 52 |
func NewApplication() *Application { |
| 53 |
return &Application{ |
| 54 |
prevMouseEvt: &tcell.EventMouse{}, |
| 55 |
updates: make(chan interface{}, queueSize), |
| 56 |
redrawTicker: time.NewTicker(1 * time.Minute), |
| 57 |
stop: make(chan struct{}, 1), |
| 58 |
alwaysClear: true, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func newScreen(events chan tcell.Event) (tcell.Screen, error) { |
| 63 |
if screen, err := tcell.NewScreen(); err != nil { |
| 64 |
return nil, fmt.Errorf("failed to create screen: %w", err) |
| 65 |
} else if err = screen.Init(); err != nil { |
| 66 |
return nil, fmt.Errorf("failed to initialize screen: %w", err) |
| 67 |
} else { |
| 68 |
screen.EnableMouse() |
| 69 |
screen.EnablePaste() |
| 70 |
go screen.ChannelEvents(events, nil) |
| 71 |
return screen, nil |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func (app *Application) SetRedrawTicker(tick time.Duration) { |
| 76 |
app.redrawTicker.Stop() |
| 77 |
app.redrawTicker = time.NewTicker(tick) |
| 78 |
} |
| 79 |
|
| 80 |
func (app *Application) Start() error { |
| 81 |
if app.root == nil { |
| 82 |
return errors.New("root component not set") |
| 83 |
} |
| 84 |
|
| 85 |
events := make(chan tcell.Event, queueSize) |
| 86 |
screen, err := newScreen(events) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
app.screenLock.Lock() |
| 92 |
app.screen = screen |
| 93 |
app.screenLock.Unlock() |
| 94 |
app.waitForStop = make(chan struct{}) |
| 95 |
|
| 96 |
defer func() { |
| 97 |
app.screenLock.Lock() |
| 98 |
app.screen = nil |
| 99 |
app.screenLock.Unlock() |
| 100 |
if screen != nil { |
| 101 |
screen.Fini() |
| 102 |
} |
| 103 |
close(app.waitForStop) |
| 104 |
}() |
| 105 |
|
| 106 |
var pasteBuffer strings.Builder |
| 107 |
var isPasting bool |
| 108 |
|
| 109 |
for { |
| 110 |
var redraw bool |
| 111 |
var clear bool |
| 112 |
select { |
| 113 |
case eventInterface := <-events: |
| 114 |
switch event := eventInterface.(type) { |
| 115 |
case *tcell.EventKey: |
| 116 |
if isPasting { |
| 117 |
switch event.Key() { |
| 118 |
case tcell.KeyRune: |
| 119 |
pasteBuffer.WriteRune(event.Rune()) |
| 120 |
case tcell.KeyEnter: |
| 121 |
pasteBuffer.WriteByte('\n') |
| 122 |
} |
| 123 |
} else { |
| 124 |
redraw = app.root.OnKeyEvent(event) |
| 125 |
} |
| 126 |
case *tcell.EventPaste: |
| 127 |
if event.Start() { |
| 128 |
isPasting = true |
| 129 |
pasteBuffer.Reset() |
| 130 |
} else { |
| 131 |
customEvt := customPasteEvent{event, pasteBuffer.String()} |
| 132 |
isPasting = false |
| 133 |
pasteBuffer.Reset() |
| 134 |
redraw = app.root.OnPasteEvent(customEvt) |
| 135 |
} |
| 136 |
case *tcell.EventMouse: |
| 137 |
onlyButtons := event.Buttons() < tcell.WheelUp |
| 138 |
hasMotion := onlyButtons && app.prevMouseEvt.Buttons() == event.Buttons() |
| 139 |
customEvt := customMouseEvent{event, hasMotion} |
| 140 |
app.prevMouseEvt = event |
| 141 |
redraw = app.root.OnMouseEvent(customEvt) |
| 142 |
case *tcell.EventResize: |
| 143 |
clear = true |
| 144 |
redraw = true |
| 145 |
} |
| 146 |
case <-app.redrawTicker.C: |
| 147 |
redraw = true |
| 148 |
case updaterInterface := <-app.updates: |
| 149 |
switch updater := updaterInterface.(type) { |
| 150 |
case redrawUpdate: |
| 151 |
redraw = true |
| 152 |
case setRootUpdate: |
| 153 |
app.root = updater.newRoot |
| 154 |
focusable, ok := app.root.(Focusable) |
| 155 |
if ok { |
| 156 |
focusable.Focus() |
| 157 |
} |
| 158 |
redraw = true |
| 159 |
clear = true |
| 160 |
case suspendUpdate: |
| 161 |
err = screen.Suspend() |
| 162 |
if err != nil { |
| 163 |
|
| 164 |
panic(err) |
| 165 |
} |
| 166 |
updater.wait() |
| 167 |
err = screen.Resume() |
| 168 |
if err != nil { |
| 169 |
screen.Fini() |
| 170 |
fmt.Println("Failed to resume screen:", err) |
| 171 |
os.Exit(40) |
| 172 |
} |
| 173 |
redraw = true |
| 174 |
clear = true |
| 175 |
} |
| 176 |
case <-app.stop: |
| 177 |
return nil |
| 178 |
} |
| 179 |
select { |
| 180 |
case <-app.stop: |
| 181 |
return nil |
| 182 |
default: |
| 183 |
} |
| 184 |
if redraw { |
| 185 |
if clear || app.alwaysClear { |
| 186 |
screen.Clear() |
| 187 |
} |
| 188 |
screen.HideCursor() |
| 189 |
app.root.Draw(screen) |
| 190 |
screen.Show() |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func (app *Application) Stop() { |
| 196 |
select { |
| 197 |
case app.stop <- struct{}{}: |
| 198 |
default: |
| 199 |
} |
| 200 |
<-app.waitForStop |
| 201 |
} |
| 202 |
|
| 203 |
func (app *Application) ForceStop() { |
| 204 |
if screen := app.screen; screen != nil { |
| 205 |
screen.Fini() |
| 206 |
} |
| 207 |
select { |
| 208 |
case app.stop <- struct{}{}: |
| 209 |
default: |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
type suspendUpdate struct { |
| 214 |
wait func() |
| 215 |
} |
| 216 |
|
| 217 |
type redrawUpdate struct{} |
| 218 |
|
| 219 |
type setRootUpdate struct { |
| 220 |
newRoot Component |
| 221 |
} |
| 222 |
|
| 223 |
func (app *Application) Suspend(wait func()) { |
| 224 |
app.updates <- suspendUpdate{wait} |
| 225 |
} |
| 226 |
|
| 227 |
func (app *Application) Redraw() { |
| 228 |
app.updates <- redrawUpdate{} |
| 229 |
} |
| 230 |
|
| 231 |
func (app *Application) SetRoot(view Component) { |
| 232 |
app.screenLock.RLock() |
| 233 |
defer app.screenLock.RUnlock() |
| 234 |
if app.screen != nil { |
| 235 |
app.updates <- setRootUpdate{view} |
| 236 |
} else { |
| 237 |
app.root = view |
| 238 |
focusable, ok := app.root.(Focusable) |
| 239 |
if ok { |
| 240 |
focusable.Focus() |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
func (app *Application) Screen() tcell.Screen { |
| 247 |
app.screenLock.RLock() |
| 248 |
screen := app.screen |
| 249 |
app.screenLock.RUnlock() |
| 250 |
return screen |
| 251 |
} |
| 252 |
|
| 253 |
func (app *Application) SetAlwaysClear(always bool) { |
| 254 |
app.alwaysClear = always |
| 255 |
} |