summaryrefslogtreecommitdiff
path: root/client/channel_list.go
blob: cfb1ec46c663c0c42aa2721aa2ca1488852aeb39 (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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main

import (
	"citrons.xyz/talk/proto"
	"citrons.xyz/talk/proto/validate"
	"citrons.xyz/talk/tui"
	"sort"
)

type channelList []channelListEntry

type channelListEntry struct {
	name string
	location channelLocation
	clicked bool
	unread bool
}

func (cl *channelList) Len() int {
	return len(*cl)
}

func (cl *channelList) Less(i, j int) bool {
	return (*cl)[i].name < (*cl)[j].name
}

func (cl *channelList) Swap(i, j int) {
	x := (*cl)[i]
	(*cl)[i] = (*cl)[j]
	(*cl)[j] = x
}

func (cl *channelList) setChannels(cs []proto.Object) {
	*cl = nil
	for _, c := range cs {
		*cl = append(*cl, channelListEntry {
			name: c.Fields[""], location: channelLocation {id: c.Id},
			unread: c.Fields["unread"] == "yes",
		})
	}
	sort.Sort(cl)
}

func (cl *channelList) contains(location channelLocation) bool {
	for i := 0; i < len(*cl); i++ {
		if (*cl)[i].location == location {
			return true
		}
	}
	return false
}

func (cl *channelList) setUnread(location channelLocation, unread bool) {
	for i := 0; i < len(*cl); i++ {
		if (*cl)[i].location == location {
			(*cl)[i].unread = unread
		}
	}
}

func (cl *channelList) getUnread(location channelLocation) bool {
	for i := 0; i < len(*cl); i++ {
		if (*cl)[i].location == location {
			return (*cl)[i].unread
		}
	}
	return false
}

func (cl *channelList) findName(name string) channelLocation {
	for i := 0; i < len(*cl); i++ {
		if validate.Fold((*cl)[i].name) == validate.Fold(name) {
			return (*cl)[i].location
		}
	}
	return channelLocation {}
}

func (cl *channelList) remove(location channelLocation) {
	for i := len(*cl) - 1; i >= 0; i-- {
		if (*cl)[i].location != location {
			continue
		}
		if i < len(*cl) - 1 {
			*cl = append((*cl)[:i], (*cl)[i + 1:]...)
		} else {
			*cl = (*cl)[:i]
		}
		break
	}
}

func (cl *channelList) traverse(direction int) int {
	var next int
	if direction >= 0 {
		next = 0
	} else {
		next = len(*cl) - 1
	}
	for i := 0; i < len(*cl); i++ {
		if (*cl)[i].location == globalApp.currentWindow {
			next = i + direction
			break
		}
	}
	if next >= 0 && next < len(*cl) {
		globalApp.goTo((*cl)[next].location)
	}
	return next
}

func (cl *channelList) add(name string, location channelLocation) {
	cl.remove(location)
	entry := channelListEntry {name: name, location: location}
	*cl = append([]channelListEntry {entry}, *cl...)
}

func (cl *channelList) show(
		scroll *tui.ScrollState, menu func(channelLocation)) {
	mouse := tui.Push("channel list", tui.Box {
		Width: 12, Height: tui.Fill, Overflow: true, Scroll: scroll,
	})
	scroll.ByMouse(mouse, false)
	for i, entry := range *cl {
		var style *tui.Style = &tui.Style {Fg: tui.White, Bg: tui.Black}
		if entry.location == globalApp.currentWindow {
			style = &tui.Style {Fg: tui.Black, Bg: tui.White}
		} else if entry.clicked {
			style = &tui.Style {Fg: tui.Black, Bg: tui.BrightBlack}
		}
		style.Bold = entry.unread

		mouse := tui.Push("channel list." + entry.location.id, tui.Box {
			Width: tui.Fill, Height: 1, NoWrap: true, Style: style,
		})

		if mouse.Button == 0 {
			if mouse.Pressed {
				globalApp.redraw = true
				entry.clicked = true
			}
			if mouse.Released {
				globalApp.redraw = true
				if entry.clicked {
					defer globalApp.goTo(entry.location)
				} else {
					for j, entry2 := range *cl {
						if entry2.clicked {
							entry2.clicked = false
							(*cl)[j] = entry
							entry = entry2
						}
					}
				}
			}
		}
		menu(entry.location)
	
		ch := globalApp.cache.Get(entry.location.id)
		if ch != nil {
			entry.name = ch.Fields[""]
		}

		tui.Text(entry.name, nil)
		tui.Pop()

		(*cl)[i] = entry
	}
	if mouse.Button == 0 && mouse.ReleasedAnywhere {
		for i, entry := range *cl {
			globalApp.redraw = globalApp.redraw || entry.clicked
			entry.clicked = false
			(*cl)[i] = entry
		}
	}
	tui.Pop()
}