-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsoletab.go
288 lines (248 loc) · 5.04 KB
/
consoletab.go
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"fmt"
"sort"
"sync"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type LinkItem struct {
Index int
Bind string
Count int
Speed int64
Traffic int64
Status string
checked bool
}
type LinkModel struct {
sync.RWMutex
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
items []*LinkItem
}
func (n *LinkModel) RowCount() int {
return len(n.items)
}
func (n *LinkModel) Value(row, col int) interface{} {
item := n.items[row]
switch col {
case 0:
return item.Index
case 1:
return item.Bind
case 2:
return fmt.Sprintf("%d", item.Count)
case 3:
return fmt.Sprintf("%s/s", ByteView(item.Speed))
case 4:
return ByteView(item.Traffic)
case 5:
return item.Status
}
panic("unexpected col")
}
func (n *LinkModel) Checked(row int) bool {
return n.items[row].checked
}
func (n *LinkModel) SetChecked(row int, checked bool) error {
n.items[row].checked = checked
return nil
}
func (m *LinkModel) Sort(col int, order walk.SortOrder) error {
m.sortColumn, m.sortOrder = col, order
sort.SliceStable(m.items, func(i, j int) bool {
a, b := m.items[i], m.items[j]
c := func(ls bool) bool {
if m.sortOrder == walk.SortAscending {
return ls
}
return !ls
}
switch m.sortColumn {
case 0:
return c(a.Index < b.Index)
case 1:
return c(a.Bind < b.Bind)
case 2:
return c(a.Count < b.Count)
case 3:
return c(a.Speed < b.Speed)
case 4:
return c(a.Traffic < b.Traffic)
case 5:
return c(a.Status < b.Status)
}
panic("unreachable")
})
return m.SorterBase.Sort(col, order)
}
const (
STATUS_UNLINK = "unlink"
STATUS_LINK = "link"
)
func StatusToIcon(status string) walk.Image {
switch status {
case STATUS_LINK:
return ICON_STATUS_LINK
case STATUS_UNLINK:
return ICON_STATUS_UNLINK
default:
return ICON_STATUS_UNLINK
}
}
var consoleLinkTable *LinkModel
func init() {
consoleLinkTable = new(LinkModel)
consoleLinkTable.items = make([]*LinkItem, 0)
}
func LinkTalbeUpdate(items []*LinkItem) {
lt := consoleLinkTable
idx := tableView.CurrentIndex()
lt.Lock()
defer lt.Unlock()
oldItem := lt.items
if len(oldItem) == len(items) {
for i, v := range items {
v.checked = oldItem[i].checked
}
}
if idx < len(items) {
tableView.SetCurrentIndex(idx)
}
lt.items = items
lt.PublishRowsReset()
lt.Sort(lt.sortColumn, lt.sortOrder)
}
func (lk *LinkModel) LinkTableSelectClean() {
lk.Lock()
defer lk.Unlock()
for _, v := range lk.items {
v.checked = false
}
lk.PublishRowsReset()
lk.Sort(lk.sortColumn, lk.sortOrder)
}
func (lk *LinkModel) LinkTableSelectAll() {
lk.Lock()
defer lk.Unlock()
done := true
for _, v := range lk.items {
if !v.checked {
done = false
}
}
for _, v := range lk.items {
v.checked = !done
}
lk.PublishRowsReset()
lk.Sort(lk.sortColumn, lk.sortOrder)
}
func (lt *LinkModel) LinkTableSelectList() []string {
lt.RLock()
defer lt.RUnlock()
var output []string
for _, v := range lt.items {
if v.checked {
output = append(output, v.Bind)
}
}
return output
}
func (lt *LinkModel) LinkTableSelectStatus(status string) {
lt.Lock()
defer lt.Unlock()
for _, v := range lt.items {
v.checked = false
}
for _, v := range lt.items {
if v.Status == status {
v.checked = true
}
}
lt.PublishRowsReset()
lt.Sort(lt.sortColumn, lt.sortOrder)
}
func DetailItem() {
var bind string
consoleLinkTable.RLock()
idx := tableView.CurrentIndex()
if idx < len(consoleLinkTable.items) {
bind = consoleLinkTable.items[idx].Bind
}
consoleLinkTable.RUnlock()
cfg := LinkFind(bind)
if cfg != nil {
ShowToolBar(cfg)
}
}
var tableView *walk.TableView
func TableWight() []Widget {
return []Widget{
Label{
Text: "Link List:",
},
TableView{
AssignTo: &tableView,
AlternatingRowBG: true,
ColumnsOrderable: true,
CheckBoxes: true,
OnItemActivated: func() {
DetailItem()
},
Columns: []TableViewColumn{
{Title: "#", Width: 30},
{Title: "Bind", Width: 120},
{Title: "Connects", Width: 60},
{Title: "Speed", Width: 60},
{Title: "Traffic", Width: 60},
{Title: "Status", Width: 80},
},
StyleCell: func(style *walk.CellStyle) {
item := consoleLinkTable.items[style.Row()]
if style.Row()%2 == 0 {
style.BackgroundColor = walk.RGB(248, 248, 255)
} else {
style.BackgroundColor = walk.RGB(220, 220, 220)
}
switch style.Col() {
case 5:
style.Image = StatusToIcon(item.Status)
}
},
Model: consoleLinkTable,
},
Composite{
Layout: HBox{MarginsZero: true},
Children: []Widget{
PushButton{
Text: "All",
OnClicked: func() {
go func() {
consoleLinkTable.LinkTableSelectAll()
}()
},
},
PushButton{
Text: "Linked",
OnClicked: func() {
go func() {
consoleLinkTable.LinkTableSelectStatus(STATUS_LINK)
}()
},
},
PushButton{
Text: "Unlinked",
OnClicked: func() {
go func() {
consoleLinkTable.LinkTableSelectStatus(STATUS_UNLINK)
}()
},
},
HSpacer{},
},
},
}
}