-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanmodel.go
132 lines (105 loc) · 2.51 KB
/
scanmodel.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
package main
import (
"sort"
"strings"
"sync"
"github.com/lxn/walk"
)
// ScanModel is a view model for the scan results.
//
// ScanModel is not threadsafe. Its operation should be managed by a single
// goroutine, such as the Sync function.
type ScanModel struct {
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
mutex sync.RWMutex
files []File
}
// RowCount returns the number of rows in the model.
func (m *ScanModel) RowCount() int {
m.mutex.RLock()
defer m.mutex.RUnlock()
return len(m.files)
}
// Value returns the value for the cell at the given row and column.
func (m *ScanModel) Value(row, col int) interface{} {
m.mutex.RLock()
defer m.mutex.RUnlock()
if row >= len(m.files) {
return nil
}
file := m.files[row]
switch col {
case 0:
return file.Path
case 1:
return file.Version.String()
case 2:
return file.Version.ReleaseName()
default:
return nil
}
}
// Checked is called by the TableView to retrieve if a given row is checked.
func (m *ScanModel) Checked(row int) bool {
return false
}
// SetChecked is called by the TableView when the user toggled the check box
// of a given row.
func (m *ScanModel) SetChecked(row int, checked bool) error {
return nil
}
// Sort is called by the TableView to sort the model.
func (m *ScanModel) Sort(col int, order walk.SortOrder) error {
m.mutex.Lock()
defer m.mutex.Unlock()
m.sortColumn, m.sortOrder = col, order
sort.SliceStable(m.files, func(i, j int) bool {
a, b := m.files[i], m.files[j]
c := func(ls bool) bool {
if m.sortOrder == walk.SortAscending {
return ls
}
return !ls
}
switch m.sortColumn {
case 0:
return c(strings.Compare(a.Path, b.Path) < 0)
case 1:
return c(a.Version.Release() < b.Version.Release())
case 2:
return c(a.Version.Release() < b.Version.Release())
}
panic("unexpected table sort column number")
})
return m.SorterBase.Sort(col, order)
}
// Clear removes all results from the model.
func (m *ScanModel) Clear() {
m.mutex.Lock()
m.files = nil
m.mutex.Unlock()
m.PublishRowsReset()
}
// Append adds the given results to the model.
func (m *ScanModel) Append(results ...File) {
if len(results) == 0 {
return
}
m.mutex.Lock()
start := len(m.files)
m.files = append(m.files, results...)
end := len(m.files) - 1
m.mutex.Unlock()
if start <= end {
m.PublishRowsInserted(start, end)
}
}
// Results returns the current set of results in the model.
func (m *ScanModel) Results() []File {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.files
}