aboutsummaryrefslogtreecommitdiff
path: root/widgets/aerc.go
blob: e7f458314c6db11e632bac14d240084bc88e3930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package widgets

import (
	"log"

	"github.com/gdamore/tcell"

	"git.sr.ht/~sircmpwn/aerc2/config"
	"git.sr.ht/~sircmpwn/aerc2/lib/ui"
	libui "git.sr.ht/~sircmpwn/aerc2/lib/ui"
)

type Aerc struct {
	accounts map[string]*AccountView
	cmd      func(cmd string) error
	grid     *libui.Grid
	tabs     *libui.Tabs
}

func NewAerc(conf *config.AercConfig, logger *log.Logger,
	cmd func(cmd string) error) *Aerc {

	tabs := libui.NewTabs()

	mainGrid := libui.NewGrid().Rows([]libui.GridSpec{
		{libui.SIZE_EXACT, 1},
		{libui.SIZE_WEIGHT, 1},
	}).Columns([]libui.GridSpec{
		{libui.SIZE_EXACT, 20},
		{libui.SIZE_WEIGHT, 1},
	})

	// TODO: Grab sidebar size from config and via :set command
	mainGrid.AddChild(libui.NewText("aerc").
		Strategy(libui.TEXT_CENTER).
		Color(tcell.ColorBlack, tcell.ColorWhite))
	mainGrid.AddChild(tabs.TabStrip).At(0, 1)
	mainGrid.AddChild(tabs.TabContent).At(1, 0).Span(1, 2)

	aerc := &Aerc{
		accounts: make(map[string]*AccountView),
		cmd:      cmd,
		grid:     mainGrid,
		tabs:     tabs,
	}

	for _, acct := range conf.Accounts {
		view := NewAccountView(conf, &acct, logger, cmd)
		aerc.accounts[acct.Name] = view
		tabs.Add(view, acct.Name)
	}

	return aerc
}

func (aerc *Aerc) Children() []ui.Drawable {
	return aerc.grid.Children()
}

func (aerc *Aerc) OnInvalidate(onInvalidate func(d libui.Drawable)) {
	aerc.grid.OnInvalidate(func(_ libui.Drawable) {
		onInvalidate(aerc)
	})
}

func (aerc *Aerc) Invalidate() {
	aerc.grid.Invalidate()
}

func (aerc *Aerc) Draw(ctx *libui.Context) {
	aerc.grid.Draw(ctx)
}

func (aerc *Aerc) Event(event tcell.Event) bool {
	acct, _ := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(*AccountView)
	return acct.Event(event)
}

func (aerc *Aerc) SelectedAccount() *AccountView {
	return aerc.accounts[aerc.tabs.Tabs[aerc.tabs.Selected].Name]
}