2018-01-11 03:03:56 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2019-05-04 14:31:16 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
"github.com/gdamore/tcell"
|
2018-01-11 03:03:56 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/config"
|
|
|
|
)
|
|
|
|
|
2018-02-17 21:35:36 +00:00
|
|
|
type UI struct {
|
2018-02-28 02:17:26 +00:00
|
|
|
Content DrawableInteractive
|
2019-05-04 14:31:16 +00:00
|
|
|
exit atomic.Value
|
2018-02-17 21:35:36 +00:00
|
|
|
ctx *Context
|
2018-06-01 07:58:00 +00:00
|
|
|
screen tcell.Screen
|
2018-01-11 03:41:15 +00:00
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
tcEvents chan tcell.Event
|
2018-02-17 21:35:36 +00:00
|
|
|
invalidations chan interface{}
|
|
|
|
}
|
|
|
|
|
2018-02-28 02:17:26 +00:00
|
|
|
func Initialize(conf *config.AercConfig,
|
|
|
|
content DrawableInteractive) (*UI, error) {
|
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
screen, err := tcell.NewScreen()
|
|
|
|
if err != nil {
|
2018-01-11 03:03:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-01 07:58:00 +00:00
|
|
|
|
|
|
|
if err = screen.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
screen.Clear()
|
|
|
|
screen.HideCursor()
|
|
|
|
|
|
|
|
width, height := screen.Size()
|
|
|
|
|
2018-02-17 21:35:36 +00:00
|
|
|
state := UI{
|
|
|
|
Content: content,
|
2018-06-01 07:58:00 +00:00
|
|
|
ctx: NewContext(width, height, screen),
|
|
|
|
screen: screen,
|
2018-02-17 21:35:36 +00:00
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
tcEvents: make(chan tcell.Event, 10),
|
2018-02-17 21:35:36 +00:00
|
|
|
invalidations: make(chan interface{}),
|
|
|
|
}
|
2019-05-04 14:31:16 +00:00
|
|
|
state.exit.Store(false)
|
2018-01-11 03:41:15 +00:00
|
|
|
go (func() {
|
2019-05-04 14:31:16 +00:00
|
|
|
for !state.ShouldExit() {
|
2018-06-01 07:58:00 +00:00
|
|
|
state.tcEvents <- screen.PollEvent()
|
2018-01-11 03:41:15 +00:00
|
|
|
}
|
|
|
|
})()
|
2018-06-11 23:52:21 +00:00
|
|
|
go (func() {
|
|
|
|
state.invalidations <- nil
|
|
|
|
})()
|
2018-02-17 21:35:36 +00:00
|
|
|
content.OnInvalidate(func(_ Drawable) {
|
2018-06-11 23:52:21 +00:00
|
|
|
go (func() {
|
|
|
|
state.invalidations <- nil
|
|
|
|
})()
|
2018-02-17 21:35:36 +00:00
|
|
|
})
|
2019-03-17 18:02:33 +00:00
|
|
|
content.Focus(true)
|
2018-01-11 03:03:56 +00:00
|
|
|
return &state, nil
|
|
|
|
}
|
|
|
|
|
2019-05-04 14:31:16 +00:00
|
|
|
func (state *UI) ShouldExit() bool {
|
|
|
|
return state.exit.Load().(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *UI) Exit() {
|
|
|
|
state.exit.Store(true)
|
|
|
|
}
|
|
|
|
|
2018-02-17 21:35:36 +00:00
|
|
|
func (state *UI) Close() {
|
2018-06-01 07:58:00 +00:00
|
|
|
state.screen.Fini()
|
2018-01-11 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 21:35:36 +00:00
|
|
|
func (state *UI) Tick() bool {
|
2018-01-11 03:41:15 +00:00
|
|
|
select {
|
2018-06-01 07:58:00 +00:00
|
|
|
case event := <-state.tcEvents:
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventResize:
|
|
|
|
state.screen.Clear()
|
|
|
|
width, height := event.Size()
|
|
|
|
state.ctx = NewContext(width, height, state.screen)
|
2018-02-17 21:35:36 +00:00
|
|
|
state.Content.Invalidate()
|
2018-01-11 03:41:15 +00:00
|
|
|
}
|
2018-02-28 02:17:26 +00:00
|
|
|
state.Content.Event(event)
|
2018-02-17 21:35:36 +00:00
|
|
|
case <-state.invalidations:
|
2019-01-15 02:11:33 +00:00
|
|
|
for {
|
|
|
|
// Flush any other pending invalidations
|
|
|
|
select {
|
|
|
|
case <-state.invalidations:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
goto done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2018-02-17 21:35:36 +00:00
|
|
|
state.Content.Draw(state.ctx)
|
2018-06-01 07:58:00 +00:00
|
|
|
state.screen.Show()
|
2018-02-17 21:35:36 +00:00
|
|
|
default:
|
|
|
|
return false
|
2018-01-11 03:03:56 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|