about summary refs log tree commit diff stats
path: root/linux/bootstrap
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-12 19:25:42 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-11-12 19:26:00 -0800
commita79de8773d56a7c7d003c973ac584eb9301e6861 (patch)
tree4aa142d6a864cba353629fd39d3162d8b311e370 /linux/bootstrap
parent8e9ddaee55650e87aa8cb3c851824229e41564d2 (diff)
downloadmu-a79de8773d56a7c7d003c973ac584eb9301e6861.tar.gz
I'm going to hell for this..
This typo has been around since July 2019 (commit 784e17d487). I swear
I've spelt the name correctly scores of times before that.
Diffstat (limited to 'linux/bootstrap')
0 files changed, 0 insertions, 0 deletions
itle='author Markus Ongyerth <aerc@ongy.net> 2018-06-01 09:58:00 +0200 committer Drew DeVault <sir@cmpwn.com> 2018-06-01 16:04:43 -0700 switch to tcell from termbox' href='/akspecs/aerc/commit/lib/ui/tab.go?h=0.2.1&id=80e891a8024ac10a60daa790131e04f0326b0c73'>80e891a ^
5b2e3a0 ^
80e891a ^

5b2e3a0 ^

























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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108


          
                                  





































































                                                                             
                                                                                                     
                                        
                                                   
                 
                                                              
         

                                                                                             

























                                                                        
package ui

import (
	"github.com/gdamore/tcell"
)

type Tabs struct {
	Tabs       []*Tab
	TabStrip   *TabStrip
	TabContent *TabContent
	Selected   int

	onInvalidateStrip   func(d Drawable)
	onInvalidateContent func(d Drawable)
}

type Tab struct {
	Content Drawable
	Name    string
	invalid bool
}

type TabStrip Tabs
type TabContent Tabs

func NewTabs() *Tabs {
	tabs := &Tabs{}
	tabs.TabStrip = (*TabStrip)(tabs)
	tabs.TabContent = (*TabContent)(tabs)
	return tabs
}

func (tabs *Tabs) Add(content Drawable, name string) {
	tabs.Tabs = append(tabs.Tabs, &Tab{
		Content: content,
		Name:    name,
	})
	tabs.TabStrip.Invalidate()
	content.OnInvalidate(tabs.invalidateChild)
}

func (tabs *Tabs) invalidateChild(d Drawable) {
	for i, tab := range tabs.Tabs {
		if tab.Content == d {
			if i == tabs.Selected {
				tabs.TabContent.Invalidate()
			}
			return
		}
	}
}

func (tabs *Tabs) Remove(content Drawable) {
	for i, tab := range tabs.Tabs {
		if tab.Content == content {
			tabs.Tabs = append(tabs.Tabs[:i], tabs.Tabs[i+1:]...)
			break
		}
	}
	tabs.TabStrip.Invalidate()
}

func (tabs *Tabs) Select(index int) {
	if tabs.Selected != index {
		tabs.Selected = index
		tabs.TabStrip.Invalidate()
		tabs.TabContent.Invalidate()
	}
}

// TODO: Color repository
func (strip *TabStrip) Draw(ctx *Context) {
	x := 0
	for i, tab := range strip.Tabs {
		style := tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack)
		if strip.Selected == i {
			style = style.Reverse(true)
		}
		x += ctx.Printf(x, 0, style, " %s ", tab.Name)
	}
	style := tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack)
	ctx.Fill(x, 0, ctx.Width()-x, 1, ' ', style)
}

func (strip *TabStrip) Invalidate() {
	if strip.onInvalidateStrip != nil {
		strip.onInvalidateStrip(strip)
	}
}

func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) {
	strip.onInvalidateStrip = onInvalidate
}

func (content *TabContent) Draw(ctx *Context) {
	tab := content.Tabs[content.Selected]
	tab.Content.Draw(ctx)
}

func (content *TabContent) Invalidate() {
	if content.onInvalidateContent != nil {
		content.onInvalidateContent(content)
	}
}

func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
	content.onInvalidateContent = onInvalidate
}