neofeels/ui/ui.go

86 lines
1.5 KiB
Go

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()
}
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() {
state.vx.Render()
}
func (state *State) HideCursor() {
state.vx.HideCursor()
}
func (state *State) Window() vaxis.Window {
return state.vx.Window()
}