2025-01-05 16:40:26 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.sr.ht/~rockorager/vaxis"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
Window vaxis.Window
|
|
|
|
x, y int
|
|
|
|
}
|
|
|
|
|
|
|
|
type View interface {
|
|
|
|
Event(state *State, event vaxis.Event) bool
|
|
|
|
// MouseEvent(localX int, localY int, event vaxis.Event)
|
|
|
|
}
|
|
|
|
|
|
|
|
var Events = make(chan vaxis.Event)
|
|
|
|
var Quit = make(chan struct{})
|
|
|
|
var ViewChange = make(chan View, 1)
|
|
|
|
|
|
|
|
type State struct {
|
|
|
|
content View
|
|
|
|
vx *vaxis.Vaxis
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(view View) (state State, err error) {
|
|
|
|
vx, err := vaxis.New(vaxis.Options{
|
|
|
|
DisableMouse: false,
|
|
|
|
CSIuBitMask: vaxis.CSIuDisambiguate | vaxis.CSIuReportEvents | vaxis.CSIuAlternateKeys | vaxis.CSIuAllKeys | vaxis.CSIuAssociatedText,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = State{
|
|
|
|
content: view,
|
|
|
|
vx: vx,
|
|
|
|
}
|
|
|
|
state.vx.SetTitle("neofeels")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for event := range state.vx.Events() {
|
|
|
|
state.vx.Window().Clear()
|
|
|
|
Events <- event
|
|
|
|
}
|
|
|
|
close(Events)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *State) HandleEvent(event vaxis.Event) {
|
|
|
|
state.content.Event(state, event)
|
|
|
|
state.vx.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *State) HandleViewChange(view View) {
|
|
|
|
state.vx.Window().Clear()
|
|
|
|
state.content = view
|
|
|
|
state.content.Event(state, vaxis.Redraw{})
|
|
|
|
state.vx.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *State) Close() {
|
|
|
|
state.vx.Close()
|
|
|
|
}
|
|
|
|
|
2025-01-05 19:07:39 +00:00
|
|
|
func (state *State) PostEvent() func(ev vaxis.Event) {
|
|
|
|
return state.vx.PostEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *State) Events() chan vaxis.Event {
|
|
|
|
return state.vx.Events()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *State) Render() {
|
2025-01-05 16:40:26 +00:00
|
|
|
state.vx.Render()
|
|
|
|
}
|
|
|
|
|
2025-01-05 19:12:06 +00:00
|
|
|
func (state *State) HideCursor() {
|
|
|
|
state.vx.HideCursor()
|
|
|
|
}
|
|
|
|
|
2025-01-05 16:40:26 +00:00
|
|
|
func (state *State) Window() vaxis.Window {
|
|
|
|
return state.vx.Window()
|
|
|
|
}
|