/
options.go
242 lines (193 loc) 路 5.26 KB
/
options.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
// +build windows
package gui
import (
"context"
res "github.com/gabyx/githooks/githooks/apps/dialog/result"
sets "github.com/gabyx/githooks/githooks/apps/dialog/settings"
strs "github.com/gabyx/githooks/githooks/strings"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type ListModel struct {
walk.ListModelBase
options []string
}
type OptionsApp struct {
*walk.Dialog
icon *walk.Icon
listBox *walk.ListBox
list *ListModel
multiple bool
acceptPB *walk.PushButton
cancelPB *walk.PushButton
}
func defineList(app *OptionsApp, opts *sets.Options) Widget {
app.multiple = opts.MultipleSelection
return ListBox{
AssignTo: &app.listBox,
Model: app.list,
MultiSelection: app.multiple,
}
}
func defineListButtons(app *OptionsApp, opts *sets.Options, r *res.Options) []Widget {
ok := "OK"
if strs.IsNotEmpty(opts.OkLabel) {
ok = opts.OkLabel
}
cancel := "Cancel"
if strs.IsNotEmpty(opts.CancelLabel) {
cancel = opts.CancelLabel
}
okCallback := func() {
*r = res.Options{
General: res.OkResult(),
Options: app.getCurrentSelectedIndices()}
app.Accept()
}
cancelCallback := func() {
*r = res.Options{General: res.CancelResult()}
app.Cancel()
}
extraButtonCallback := func(index uint) func() {
return func() {
*r = res.Options{
General: res.ExtraButtonResult(index)}
app.Accept()
}
}
return defineOkCancelButtons(
ok, cancel, opts.ExtraButtons,
&app.acceptPB, &app.cancelPB,
okCallback, cancelCallback, extraButtonCallback)
}
// nolint: gomnd
func defineListText(app *OptionsApp, opts *sets.Options, addTextIcon bool) (w []Widget) {
switch opts.WindowIcon {
case sets.InfoIcon:
app.icon = walk.IconInformation()
case sets.WarningIcon:
app.icon = walk.IconWarning()
case sets.ErrorIcon:
app.icon = walk.IconError()
case sets.QuestionIcon:
app.icon = walk.IconQuestion()
}
if app.icon != nil && addTextIcon {
bitmap, err := walk.NewBitmapFromIconForDPI(app.icon, walk.Size{Width: 48, Height: 48}, 96)
if err == nil {
w = append(w, ImageView{
Background: TransparentBrush{},
Image: bitmap,
Mode: ImageViewModeCenter,
MinSize: Size{Width: 48, Height: 48},
MaxSize: Size{Width: 48, Height: 48},
})
}
}
w = append(w, TextLabel{
RightToLeftReading: true,
MinSize: Size{Width: 10, Height: 0},
Text: opts.Text})
return
}
// Shows an options dialog.
func ShowOptions(ctx context.Context, opts *sets.Options) (r res.Options, err error) {
app := &OptionsApp{list: &ListModel{options: opts.Options}}
minSize := Size{Width: 240, Height: 240} // nolint: gomnd
size := walk.Size{Width: minSize.Width, Height: minSize.Height} // nolint: gomnd
if opts.Width != 0 {
size.Width = int(opts.Width)
}
if opts.Height != 0 {
size.Height = int(opts.Height)
}
defaultButton := &app.acceptPB
cancelButton := &app.cancelPB
if opts.DefaultCancel {
defaultButton, cancelButton = cancelButton, defaultButton
}
// nolint: gomnd
m := Dialog{
AssignTo: &app.Dialog,
Title: opts.Title,
MinSize: minSize,
DefaultButton: defaultButton,
CancelButton: cancelButton,
Layout: VBox{
Spacing: 8,
Margins: Margins{
Left: 12,
Top: 12,
Bottom: 12,
Right: 12}},
Children: []Widget{
Composite{
Layout: HBox{Spacing: 10, MarginsZero: true},
Children: defineListText(app, opts, false),
},
defineList(app, opts),
Composite{
Layout: HBox{SpacingZero: true, MarginsZero: true},
Children: defineListButtons(app, opts, &r),
},
},
}
if err = m.Create(nil); err != nil {
return
}
// Select default items
setDefaultSelection(app, opts)
app.Disposing().Once(func() {
// The dialog was closed -> Make it canceled if
// not yet set (ok or cancel pressed).
if r.IsUnset() {
r = res.Options{General: res.CancelResult()}
}
})
centerAndSetSize(app.Dialog, size)
if ctx != nil {
watchTimeout(ctx, app.Dialog)
}
ret := app.Run()
if ret == walk.DlgCmdCancel || ret == walk.DlgCmdClose {
r = res.Options{General: res.CancelResult()}
return
}
if ctx != nil && ctx.Err() != nil {
err = ctx.Err()
}
return
}
func setDefaultSelection(app *OptionsApp, opts *sets.Options) {
dO := len(opts.DefaultOptions)
indices := make([]int, 0, dO)
for i := range opts.DefaultOptions {
if i >= len(opts.Options) {
continue
}
indices = append(indices, int(opts.DefaultOptions[i]))
}
if opts.MultipleSelection {
app.listBox.SetSelectedIndexes(indices)
} else if len(indices) > 0 {
_ = app.listBox.SetCurrentIndex(indices[len(indices)-1])
}
}
func (app *OptionsApp) getCurrentSelectedIndices() (indices []uint) {
if app.multiple {
s := app.listBox.SelectedIndexes()
indices = make([]uint, 0, len(s))
for _, idx := range s {
indices = append(indices, uint(idx))
}
} else {
indices = []uint{uint(app.listBox.CurrentIndex())}
}
return
}
func (m *ListModel) ItemCount() int {
return len(m.options)
}
func (m *ListModel) Value(index int) interface{} {
return m.options[index]
}