aerc/widgets/aerc.go

216 lines
4.7 KiB
Go
Raw Normal View History

2018-02-28 02:17:26 +00:00
package widgets
import (
"log"
"time"
2018-02-28 02:17:26 +00:00
"github.com/gdamore/tcell"
2018-02-28 02:17:26 +00:00
"git.sr.ht/~sircmpwn/aerc2/config"
2019-02-10 21:46:13 +00:00
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
2018-02-28 02:17:26 +00:00
libui "git.sr.ht/~sircmpwn/aerc2/lib/ui"
)
type Aerc struct {
accounts map[string]*AccountView
cmd func(cmd string) error
conf *config.AercConfig
focused libui.Interactive
grid *libui.Grid
logger *log.Logger
statusbar *libui.Stack
statusline *StatusLine
pendingKeys []config.KeyStroke
tabs *libui.Tabs
2018-02-28 02:17:26 +00:00
}
2019-03-11 01:15:24 +00:00
func NewAerc(conf *config.AercConfig, logger *log.Logger,
cmd func(cmd string) error) *Aerc {
2018-02-28 02:17:26 +00:00
tabs := libui.NewTabs()
statusbar := ui.NewStack()
statusline := NewStatusLine()
statusbar.Push(statusline)
grid := libui.NewGrid().Rows([]libui.GridSpec{
2018-06-12 13:50:46 +00:00
{libui.SIZE_EXACT, 1},
{libui.SIZE_WEIGHT, 1},
{libui.SIZE_EXACT, 1},
2018-02-28 02:17:26 +00:00
}).Columns([]libui.GridSpec{
2019-03-16 00:40:28 +00:00
{libui.SIZE_EXACT, conf.Ui.SidebarWidth},
2018-06-12 13:50:46 +00:00
{libui.SIZE_WEIGHT, 1},
2018-02-28 02:17:26 +00:00
})
grid.AddChild(statusbar).At(2, 1)
// Minor hack
grid.AddChild(libui.NewBordered(
libui.NewFill(' '), libui.BORDER_RIGHT)).At(2, 0)
2018-02-28 02:17:26 +00:00
grid.AddChild(libui.NewText("aerc").
2018-02-28 02:17:26 +00:00
Strategy(libui.TEXT_CENTER).
Color(tcell.ColorBlack, tcell.ColorWhite))
grid.AddChild(tabs.TabStrip).At(0, 1)
grid.AddChild(tabs.TabContent).At(1, 0).Span(1, 2)
2018-02-28 02:17:26 +00:00
aerc := &Aerc{
accounts: make(map[string]*AccountView),
conf: conf,
cmd: cmd,
grid: grid,
logger: logger,
statusbar: statusbar,
statusline: statusline,
tabs: tabs,
}
for _, acct := range conf.Accounts {
view := NewAccountView(conf, &acct, logger, aerc)
aerc.accounts[acct.Name] = view
2019-01-13 18:03:28 +00:00
tabs.Add(view, acct.Name)
}
2018-02-28 02:17:26 +00:00
return aerc
2018-02-28 02:17:26 +00:00
}
2019-02-10 21:46:13 +00:00
func (aerc *Aerc) Children() []ui.Drawable {
return aerc.grid.Children()
}
2018-02-28 02:17:26 +00:00
func (aerc *Aerc) OnInvalidate(onInvalidate func(d libui.Drawable)) {
aerc.grid.OnInvalidate(func(_ libui.Drawable) {
onInvalidate(aerc)
})
2018-02-28 02:17:26 +00:00
}
func (aerc *Aerc) Invalidate() {
aerc.grid.Invalidate()
}
2019-03-17 18:02:33 +00:00
func (aerc *Aerc) Focus(focus bool) {
// who cares
}
2018-02-28 02:17:26 +00:00
func (aerc *Aerc) Draw(ctx *libui.Context) {
aerc.grid.Draw(ctx)
}
func (aerc *Aerc) Event(event tcell.Event) bool {
if aerc.focused != nil {
return aerc.focused.Event(event)
}
switch event := event.(type) {
case *tcell.EventKey:
aerc.pendingKeys = append(aerc.pendingKeys, config.KeyStroke{
Key: event.Key(),
Rune: event.Rune(),
})
result, output := aerc.conf.Lbinds.GetBinding(aerc.pendingKeys)
switch result {
case config.BINDING_FOUND:
aerc.pendingKeys = []config.KeyStroke{}
for _, stroke := range output {
simulated := tcell.NewEventKey(
stroke.Key, stroke.Rune, tcell.ModNone)
aerc.Event(simulated)
}
case config.BINDING_INCOMPLETE:
return false
case config.BINDING_NOT_FOUND:
aerc.pendingKeys = []config.KeyStroke{}
if event.Rune() == ':' {
aerc.BeginExCommand()
return true
}
2019-03-17 21:39:22 +00:00
interactive, ok := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(ui.Interactive)
if ok {
return interactive.Event(event)
}
return false
}
}
return false
}
func (aerc *Aerc) Config() *config.AercConfig {
return aerc.conf
2018-02-28 02:17:26 +00:00
}
2019-03-11 01:15:24 +00:00
func (aerc *Aerc) SelectedAccount() *AccountView {
acct, ok := aerc.accounts[aerc.tabs.Tabs[aerc.tabs.Selected].Name]
if !ok {
return nil
}
return acct
}
2019-03-17 21:23:53 +00:00
func (aerc *Aerc) SelectedTab() ui.Drawable {
return aerc.tabs.Tabs[aerc.tabs.Selected].Content
}
func (aerc *Aerc) NewTab(drawable ui.Drawable, name string) *ui.Tab {
tab := aerc.tabs.Add(drawable, name)
aerc.tabs.Select(len(aerc.tabs.Tabs) - 1)
return tab
}
2019-03-17 21:23:53 +00:00
func (aerc *Aerc) RemoveTab(tab ui.Drawable) {
aerc.tabs.Remove(tab)
}
2019-03-17 20:24:17 +00:00
func (aerc *Aerc) NextTab() {
next := aerc.tabs.Selected + 1
if next >= len(aerc.tabs.Tabs) {
next = 0
}
aerc.tabs.Select(next)
}
func (aerc *Aerc) PrevTab() {
next := aerc.tabs.Selected - 1
if next < 0 {
next = len(aerc.tabs.Tabs) - 1
}
aerc.tabs.Select(next)
}
// TODO: Use per-account status lines, but a global ex line
func (aerc *Aerc) SetStatus(status string) *StatusMessage {
return aerc.statusline.Set(status)
}
func (aerc *Aerc) PushStatus(text string, expiry time.Duration) *StatusMessage {
return aerc.statusline.Push(text, expiry)
}
func (aerc *Aerc) focus(item libui.Interactive) {
if aerc.focused == item {
return
}
if aerc.focused != nil {
aerc.focused.Focus(false)
}
aerc.focused = item
if item != nil {
item.Focus(true)
}
}
func (aerc *Aerc) BeginExCommand() {
previous := aerc.focused
exline := NewExLine(func(cmd string) {
err := aerc.cmd(cmd)
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorRed, tcell.ColorWhite)
}
aerc.statusbar.Pop()
aerc.focus(previous)
}, func() {
aerc.statusbar.Pop()
aerc.focus(previous)
})
aerc.statusbar.Push(exline)
aerc.focus(exline)
}