-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodfiletablemodel.go
More file actions
134 lines (109 loc) · 2.92 KB
/
Copy pathmodfiletablemodel.go
File metadata and controls
134 lines (109 loc) · 2.92 KB
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
package main
import (
"sort"
"github.com/lxn/walk"
)
type ModFileTableItem struct {
Name string
checked bool
}
type ModFileTableModel struct {
walk.TableModelBase
walk.SorterBase
ConfigIndex int
sortColumn int
sortOrder walk.SortOrder
items []*ModFileTableItem
}
func NewModFileTableModel(configIndex int) *ModFileTableModel {
m := new(ModFileTableModel)
m.ConfigIndex = configIndex
m.ResetRows()
return m
}
// Called by the TableView from SetModel and every time the model publishes a RowsReset event.
func (m *ModFileTableModel) RowCount() int {
return len(m.items)
}
// Called by the TableView when it needs the text to display for a given cell.
func (m *ModFileTableModel) Value(row, col int) interface{} {
item := m.items[row]
switch col {
case 0:
return item.Name
}
panic("unexpected col")
}
// Called by the TableView to retrieve if a given row is checked.
func (m *ModFileTableModel) Checked(row int) bool {
return m.items[row].checked
}
// Called by the TableView when the user toggled the check box of a given row.
func (m *ModFileTableModel) SetChecked(row int, checked bool) error {
m.items[row].checked = checked
return nil
}
func (m *ModFileTableModel) CheckAll() {
for i := 0; i < len(m.items); i++ {
m.items[i].checked = true
}
// m.ResetRows()
}
func (m *ModFileTableModel) UncheckAll() {
for i := 0; i < len(m.items); i++ {
m.items[i].checked = false
}
// m.ResetRows()
}
// Called by the TableView to sort the model.
func (m *ModFileTableModel) 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.Name < b.Name)
}
panic("unreachable")
})
return m.SorterBase.Sort(col, order)
}
func (m *ModFileTableModel) ResetRows() {
config.ModConfigSlice[m.ConfigIndex].RefreshModConfig()
m.DataToModel()
// Notify TableView and other interested parties about the reset.
m.PublishRowsReset()
m.Sort(m.sortColumn, m.sortOrder)
}
func (m *ModFileTableModel) ApplyChange() {
config.ModConfigSlice[m.ConfigIndex].RefreshModConfig()
m.ModelToData()
config.ModConfigSlice[m.ConfigIndex].ApplyModFileInfoToactualFile()
}
func (m *ModFileTableModel) DataToModel() {
m.items = make([]*ModFileTableItem, len(config.ModConfigSlice[m.ConfigIndex].ModFilesMap))
var i = 0
for name, disable := range config.ModConfigSlice[m.ConfigIndex].ModFilesMap {
m.items[i] = &ModFileTableItem{
Name: name,
checked: !disable,
}
i += 1
}
}
func (m *ModFileTableModel) ModelToData() {
FileInfo := make(map[string]bool)
for _, item := range m.items {
_, ok := config.ModConfigSlice[m.ConfigIndex].ModFilesMap[item.Name]
if ok {
FileInfo[item.Name] = !item.checked
}
}
config.ModConfigSlice[m.ConfigIndex].ModFilesMap = FileInfo
}