summary refs log tree commit diff stats
path: root/lib/ui/tab.go
blob: f18b0ac2ce1dd2a3c8ac37820b164e39d33879e6 (plain) (blame)
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
109
110
111
112pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td
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 {
				if tabs.onInvalidateContent != nil {
					tabs.onInvalidateContent(tabs.TabContent)
				}
			}
			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 = tcell.StyleDefault.
				Background(tcell.ColorDefault).
				Foreground(tcell.ColorDefault)
		}
		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)
	}
	tab := content.Tabs[content.Selected]
	tab.Content.Invalidate()
}

func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) {
	content.onInvalidateContent = onInvalidate
}
pan>="L12" class="LineNr">12 </span> syscall_clock_gettime <span id="L13" class="LineNr">13 </span> <span class="PreProc">var</span> t-secs-addr/<span class="Constant">ecx</span>: (addr int) <span class="SpecialChar">&lt;-</span> <a href='131table.subx.html#L26'>get</a> t-addr, tv_sec <span id="L14" class="LineNr">14 </span> <span class="PreProc">var</span> secs/<span class="Constant">eax</span>: int <span class="SpecialChar">&lt;-</span> copy *t-secs-addr <span id="L15" class="LineNr">15 </span> <span class="PreProc">return</span> secs <span id="L16" class="LineNr">16 </span><span class="Delimiter">}</span> <span id="L17" class="LineNr">17 </span> <span id="L18" class="LineNr">18 </span><span class="muComment"># return time in nanoseconds since epoch</span> <span id="L19" class="LineNr">19 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='402time.mu.html#L19'>ntime</a></span><span class="PreProc"> -&gt; </span>_/<span class="Constant">eax</span>: int <span class="Delimiter">{</span> <span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> t: <a href='402time.mu.html#L1'>timespec</a> <span id="L21" class="LineNr">21 </span> <span class="PreProc">var</span> clock/<span class="Constant">ebx</span>: int <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span> <span class="muComment"># CLOCK_MONOTONIC</span> <span id="L22" class="LineNr">22 </span> <span class="PreProc">var</span> t-addr/<span class="Constant">ecx</span>: (addr <a href='402time.mu.html#L1'>timespec</a>) <span class="SpecialChar">&lt;-</span> address t <span id="L23" class="LineNr">23 </span> syscall_clock_gettime <span id="L24" class="LineNr">24 </span> <span class="PreProc">var</span> t-nsecs-addr/<span class="Constant">ecx</span>: (addr int) <span class="SpecialChar">&lt;-</span> <a href='131table.subx.html#L26'>get</a> t-addr, tv_nsec <span id="L25" class="LineNr">25 </span> <span class="PreProc">var</span> nsecs/<span class="Constant">eax</span>: int <span class="SpecialChar">&lt;-</span> copy *t-nsecs-addr <span id="L26" class="LineNr">26 </span> <span class="PreProc">return</span> nsecs <span id="L27" class="LineNr">27 </span><span class="Delimiter">}</span> <span id="L28" class="LineNr">28 </span> <span id="L29" class="LineNr">29 </span><span class="muComment"># nsecs must be less than 999999999 or 0x3b9ac9ff nanoseconds</span> <span id="L30" class="LineNr">30 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='402time.mu.html#L30'>sleep</a></span> secs: int, nsecs: int <span class="Delimiter">{</span> <span id="L31" class="LineNr">31 </span> <span class="PreProc">var</span> t: <a href='402time.mu.html#L1'>timespec</a> <span id="L32" class="LineNr">32 </span> <span class="muComment"># initialize t</span> <span id="L33" class="LineNr">33 </span> <span class="PreProc">var</span> tmp/<span class="Constant">eax</span>: (addr int) <span class="SpecialChar">&lt;-</span> <a href='131table.subx.html#L26'>get</a> t, tv_sec <span id="L34" class="LineNr">34 </span> <span class="PreProc">var</span> tmp2/<span class="Constant">ecx</span>: int <span class="SpecialChar">&lt;-</span> copy secs <span id="L35" class="LineNr">35 </span> copy-to *tmp, tmp2 <span id="L36" class="LineNr">36 </span> tmp <span class="SpecialChar">&lt;-</span> <a href='131table.subx.html#L26'>get</a> t, tv_nsec <span id="L37" class="LineNr">37 </span> tmp2 <span class="SpecialChar">&lt;-</span> copy nsecs <span id="L38" class="LineNr">38 </span> copy-to *tmp, tmp2 <span id="L39" class="LineNr">39 </span> <span class="muComment"># perform the syscall</span> <span id="L40" class="LineNr">40 </span> <span class="PreProc">var</span> t-addr/<span class="Constant">ebx</span>: (addr <a href='402time.mu.html#L1'>timespec</a>) <span class="SpecialChar">&lt;-</span> address t <span id="L41" class="LineNr">41 </span> <span class="PreProc">var</span> rem-addr/<span class="Constant">ecx</span>: (addr <a href='402time.mu.html#L1'>timespec</a>) <span class="SpecialChar">&lt;-</span> copy <span class="Constant">0</span> <span id="L42" class="LineNr">42 </span> syscall_nanosleep <span id="L43" class="LineNr">43 </span><span class="Delimiter">}</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->