-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanwindow.go
203 lines (178 loc) · 4.58 KB
/
scanwindow.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
package main
import (
"fmt"
"strings"
"github.com/lxn/walk"
ui "github.com/lxn/walk/declarative"
)
// ScanWindow is the main scanning window.
type ScanWindow struct {
scanner *Scanner
model *ScanModel
ui *ui.MainWindow
form *walk.MainWindow
tree *walk.TreeView
table *walk.TableView
splitter *walk.Splitter
selection *walk.LineEdit
cancel *walk.PushButton
actionCopy *walk.Action
actionSelectAll *walk.Action
}
// NewScanWindow returns a new scanning window.
func NewScanWindow(scanner *Scanner, treeModel *DirectoryTreeModel, scanModel *ScanModel) (window *ScanWindow, err error) {
window = &ScanWindow{
scanner: scanner,
model: scanModel,
}
icon, err := walk.NewIconFromResourceId(2)
if err != nil {
icon = walk.IconInformation()
}
window.ui = &ui.MainWindow{
Icon: icon,
Title: "CAD File Scanner",
MinSize: ui.Size{Width: 600, Height: 400},
Size: ui.Size{Width: 1024, Height: 640},
Layout: ui.VBox{},
AssignTo: &window.form,
MenuItems: []ui.MenuItem{
ui.Menu{
Text: "&Edit",
Items: []ui.MenuItem{
ui.Action{
AssignTo: &window.actionSelectAll,
Text: "Select &All",
Shortcut: ui.Shortcut{Key: walk.KeyA, Modifiers: walk.ModControl},
OnTriggered: window.onSelectAllResults,
},
ui.Action{
AssignTo: &window.actionCopy,
Text: "Copy",
Shortcut: ui.Shortcut{Key: walk.KeyC, Modifiers: walk.ModControl},
OnTriggered: window.onCopyResults,
},
},
},
},
Children: []ui.Widget{
ui.HSplitter{
AssignTo: &window.splitter,
HandleWidth: 12,
Children: []ui.Widget{
ui.TreeView{
AssignTo: &window.tree,
Model: treeModel,
OnCurrentItemChanged: window.onSelection,
},
ui.TableView{
Name: "table",
AssignTo: &window.table,
MultiSelection: true,
Columns: []ui.TableViewColumn{
{Title: "File", Width: 300},
{Title: "Header", Width: 200},
{Title: "Version", Width: 200},
},
ContextMenuItems: []ui.MenuItem{
ui.ActionRef{Action: &window.actionSelectAll},
ui.ActionRef{Action: &window.actionCopy},
},
Model: scanModel,
},
},
},
ui.HSplitter{
Children: []ui.Widget{
ui.Label{Text: "Directory:"},
ui.LineEdit{AssignTo: &window.selection},
},
},
ui.HSplitter{
Children: []ui.Widget{
ui.PushButton{
Text: "Scan",
OnClicked: window.onScan,
},
ui.PushButton{
AssignTo: &window.cancel,
Text: "Cancel",
OnClicked: window.onCancel,
Enabled: false,
},
},
},
},
}
err = window.ui.Create()
if err == nil {
window.splitter.SetFixed(window.tree, true)
window.splitter.SetFixed(window.table, false)
}
return
}
// Run will display the queued lease dialog.
//
// Run blocks until the dialog is closed. The dialog can be closed by the user.
//
// Run returns the result of the dialog.
func (window *ScanWindow) Run() int {
return window.form.Run()
}
func (window *ScanWindow) onSelection() {
dir := window.tree.CurrentItem().(*Directory)
window.selection.SetText(dir.Path())
}
func (window *ScanWindow) onScan() {
root := window.selection.Text()
go window.scanner.Scan(
root,
func() { window.form.Synchronize(window.onScanStarted) },
func() { window.form.Synchronize(window.onScanCompleted) },
)
}
func (window *ScanWindow) onScanStarted() {
window.cancel.SetEnabled(true)
}
func (window *ScanWindow) onScanCompleted() {
window.cancel.SetEnabled(false)
}
func (window *ScanWindow) onCancel() {
go window.scanner.Stop()
}
func (window *ScanWindow) onSelectAllResults() {
window.table.SetSelectedIndexes([]int{-1})
}
func (window *ScanWindow) onCopyResults() {
rows := window.table.SelectedIndexes()
if len(rows) == 0 {
return
}
results := window.model.Results()
files := make([]File, 0, len(rows))
for _, row := range rows {
if row >= len(results) {
continue
}
files = append(files, results[row])
}
if len(files) == 0 {
return
}
maxPath := 0
maxHeader := 0
for _, file := range files {
if plen := len(file.Path); plen > maxPath {
maxPath = plen
}
if hlen := len(file.Version.String()); hlen > maxHeader {
maxHeader = hlen
}
}
var lines []string
for _, file := range files {
lines = append(lines, fmt.Sprintf("%-*s %-*s %s", maxPath, file.Path, maxHeader, file.Version.String(), file.Version.ReleaseName()))
}
text := strings.Join(lines, "\n")
walk.Clipboard().SetText(text)
}