OnlineLibrary

Форк
0
309 строк · 6.4 Кб
1
package gui
2

3
import (
4
	"OnlineLibrary/internal/config"
5

6
	"github.com/leonelquinteros/gotext"
7
	"gitverse.ru/kvark128/walk"
8
	. "gitverse.ru/kvark128/walk/declarative"
9
	"gitverse.ru/kvark128/win"
10
)
11

12
type MsgBoxStyle uint
13

14
const (
15
	MsgBoxIconError       MsgBoxStyle = win.MB_ICONERROR
16
	MsgBoxIconInformation MsgBoxStyle = win.MB_ICONINFORMATION
17
	MsgBoxIconWarning     MsgBoxStyle = win.MB_ICONWARNING
18
	MsgBoxIconQuestion    MsgBoxStyle = win.MB_ICONQUESTION
19
	MsgBoxOK              MsgBoxStyle = win.MB_OK
20
	MsgBoxYesNo           MsgBoxStyle = win.MB_YESNO
21
)
22

23
const (
24
	DlgCmdOK     = win.IDOK
25
	DlgCmdCancel = win.IDCANCEL
26
	DlgCmdClose  = win.IDCLOSE
27
	DlgCmdYes    = win.IDYES
28
	DlgCmdNo     = win.IDNO
29
)
30

31
func MessageBox(owner Form, title, message string, style MsgBoxStyle) int {
32
	res := make(chan int)
33
	parent := owner.form()
34
	parent.Synchronize(func() {
35
		res <- walk.MsgBox(parent, title, message, walk.MsgBoxStyle(style))
36
	})
37
	return <-res
38
}
39

40
func BookInfoDialog(owner Form, title, description string) int {
41
	parent := owner.form()
42
	var (
43
		dlg              *walk.Dialog
44
		ClosePB          *walk.PushButton
45
		parentSize       = parent.Size()
46
		descriptionLabel = gotext.Get("Description:")
47
	)
48

49
	layout := Dialog{
50
		Title:        title,
51
		AssignTo:     &dlg,
52
		Layout:       VBox{},
53
		CancelButton: &ClosePB,
54
		MinSize:      Size{Width: parentSize.Width / 2, Height: parentSize.Height / 2},
55
		Children: []Widget{
56

57
			TextLabel{Text: descriptionLabel},
58
			TextEdit{
59
				Accessibility: Accessibility{Name: descriptionLabel},
60
				Text:          description,
61
				ReadOnly:      true,
62
				CompactHeight: true,
63
			},
64

65
			Composite{
66
				Layout: HBox{},
67
				Children: []Widget{
68
					HSpacer{},
69
					PushButton{
70
						AssignTo: &ClosePB,
71
						Text:     gotext.Get("Close"),
72
						OnClicked: func() {
73
							dlg.Close(walk.DlgCmdClose)
74
						},
75
					},
76
				},
77
			},
78
		},
79
	}
80

81
	res := make(chan int)
82
	parent.Synchronize(func() {
83
		layout.Create(parent)
84
		dlg.Run()
85
		res <- dlg.Result()
86
	})
87
	return <-res
88
}
89

90
func TextEntryDialog(owner Form, title, msg, value string, text *string) int {
91
	parent := owner.form()
92
	var (
93
		dlg            *walk.Dialog
94
		textLE         *walk.LineEdit
95
		OkPB, CancelPB *walk.PushButton
96
	)
97

98
	layout := Dialog{
99
		Title:         title,
100
		AssignTo:      &dlg,
101
		Layout:        VBox{},
102
		CancelButton:  &CancelPB,
103
		DefaultButton: &OkPB,
104
		Children: []Widget{
105

106
			TextLabel{Text: msg},
107
			LineEdit{
108
				Accessibility: Accessibility{Name: msg},
109
				Text:          value,
110
				AssignTo:      &textLE,
111
			},
112

113
			Composite{
114
				Layout: HBox{},
115
				Children: []Widget{
116
					HSpacer{},
117
					PushButton{
118
						AssignTo: &OkPB,
119
						Text:     gotext.Get("OK"),
120
						OnClicked: func() {
121
							*text = textLE.Text()
122
							dlg.Close(walk.DlgCmdOK)
123
						},
124
					},
125
					PushButton{
126
						AssignTo: &CancelPB,
127
						Text:     gotext.Get("Cancel"),
128
						OnClicked: func() {
129
							dlg.Close(walk.DlgCmdCancel)
130
						},
131
					},
132
				},
133
			},
134
		},
135
	}
136

137
	res := make(chan int)
138
	parent.Synchronize(func() {
139
		layout.Create(parent)
140
		NewFixedPushButton(OkPB)
141
		NewFixedPushButton(CancelPB)
142
		dlg.Run()
143
		res <- dlg.Result()
144
	})
145
	return <-res
146
}
147

148
func CredentialsEntryDialog(owner Form, service *config.Service) int {
149
	parent := owner.form()
150
	var (
151
		dlg                                   *walk.Dialog
152
		nameLE, urlLE, usernameLE, passwordLE *walk.LineEdit
153
		nameLabel                             = gotext.Get("Displayed name:")
154
		urlLabel                              = gotext.Get("Server address:")
155
		usernameLabel                         = gotext.Get("User name:")
156
		passwordLabel                         = gotext.Get("Password:")
157
		OkPB, CancelPB                        *walk.PushButton
158
	)
159

160
	layout := Dialog{
161
		Title:         gotext.Get("Adding a new account"),
162
		AssignTo:      &dlg,
163
		Layout:        VBox{},
164
		CancelButton:  &CancelPB,
165
		DefaultButton: &OkPB,
166
		Children: []Widget{
167
			TextLabel{Text: nameLabel},
168
			LineEdit{
169
				Accessibility: Accessibility{Name: nameLabel},
170
				AssignTo:      &nameLE,
171
			},
172

173
			TextLabel{Text: urlLabel},
174
			LineEdit{
175
				Accessibility: Accessibility{Name: urlLabel},
176
				AssignTo:      &urlLE,
177
			},
178

179
			TextLabel{Text: usernameLabel},
180
			LineEdit{
181
				Accessibility: Accessibility{Name: usernameLabel},
182
				AssignTo:      &usernameLE,
183
			},
184

185
			TextLabel{Text: passwordLabel},
186
			LineEdit{
187
				Accessibility: Accessibility{Name: passwordLabel},
188
				AssignTo:      &passwordLE,
189
				PasswordMode:  true,
190
			},
191

192
			Composite{
193
				Layout: HBox{},
194
				Children: []Widget{
195
					HSpacer{},
196
					PushButton{
197
						AssignTo: &OkPB,
198
						Text:     gotext.Get("OK"),
199
						OnClicked: func() {
200
							service.Name = nameLE.Text()
201
							service.URL = urlLE.Text()
202
							service.Username = usernameLE.Text()
203
							service.Password = passwordLE.Text()
204
							dlg.Accept()
205
						},
206
					},
207
					PushButton{
208
						AssignTo: &CancelPB,
209
						Text:     gotext.Get("Cancel"),
210
						OnClicked: func() {
211
							dlg.Cancel()
212
						},
213
					},
214
				},
215
			},
216
		},
217
	}
218

219
	res := make(chan int)
220
	parent.Synchronize(func() {
221
		layout.Create(parent)
222
		NewFixedPushButton(OkPB)
223
		NewFixedPushButton(CancelPB)
224
		dlg.Run()
225
		res <- dlg.Result()
226
	})
227
	return <-res
228
}
229

230
type ProgressDialog struct {
231
	parent walk.Form
232
	dlg    *walk.Dialog
233
	label  *walk.TextLabel
234
	pb     *walk.ProgressBar
235
}
236

237
func NewProgressDialog(owner Form, title, label string, maxValue int, cancelFN func()) *ProgressDialog {
238
	pd := &ProgressDialog{parent: owner.form()}
239
	var CancelPB *walk.PushButton
240

241
	var layout = Dialog{
242
		Title:        title,
243
		AssignTo:     &pd.dlg,
244
		Layout:       VBox{},
245
		CancelButton: &CancelPB,
246
		Children: []Widget{
247

248
			TextLabel{
249
				Text:     label,
250
				AssignTo: &pd.label,
251
			},
252

253
			ProgressBar{
254
				MaxValue: maxValue,
255
				AssignTo: &pd.pb,
256
			},
257

258
			PushButton{
259
				AssignTo: &CancelPB,
260
				Text:     gotext.Get("Cancel"),
261
				OnClicked: func() {
262
					cancelFN()
263
					pd.dlg.Cancel()
264
				},
265
			},
266
		},
267
	}
268

269
	done := make(chan bool)
270
	pd.parent.Synchronize(func() {
271
		layout.Create(pd.parent)
272
		done <- true
273
	})
274
	<-done
275
	return pd
276
}
277

278
func (pd *ProgressDialog) Run() {
279
	pd.parent.Synchronize(func() {
280
		pd.dlg.Run()
281
	})
282
}
283

284
func (pd *ProgressDialog) SetLabel(label string) {
285
	doneCH := make(chan bool)
286
	pd.dlg.Synchronize(func() {
287
		pd.label.SetText(label)
288
		doneCH <- true
289
	})
290
	<-doneCH
291
}
292

293
func (pd *ProgressDialog) SetValue(value int) {
294
	doneCH := make(chan bool)
295
	pd.dlg.Synchronize(func() {
296
		pd.pb.SetValue(value)
297
		doneCH <- true
298
	})
299
	<-doneCH
300
}
301

302
func (pd *ProgressDialog) Cancel() {
303
	done := make(chan bool)
304
	pd.dlg.Synchronize(func() {
305
		pd.dlg.Cancel()
306
		done <- true
307
	})
308
	<-done
309
}
310

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.