OnlineLibrary
309 строк · 6.4 Кб
1package gui
2
3import (
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
12type MsgBoxStyle uint
13
14const (
15MsgBoxIconError MsgBoxStyle = win.MB_ICONERROR
16MsgBoxIconInformation MsgBoxStyle = win.MB_ICONINFORMATION
17MsgBoxIconWarning MsgBoxStyle = win.MB_ICONWARNING
18MsgBoxIconQuestion MsgBoxStyle = win.MB_ICONQUESTION
19MsgBoxOK MsgBoxStyle = win.MB_OK
20MsgBoxYesNo MsgBoxStyle = win.MB_YESNO
21)
22
23const (
24DlgCmdOK = win.IDOK
25DlgCmdCancel = win.IDCANCEL
26DlgCmdClose = win.IDCLOSE
27DlgCmdYes = win.IDYES
28DlgCmdNo = win.IDNO
29)
30
31func MessageBox(owner Form, title, message string, style MsgBoxStyle) int {
32res := make(chan int)
33parent := owner.form()
34parent.Synchronize(func() {
35res <- walk.MsgBox(parent, title, message, walk.MsgBoxStyle(style))
36})
37return <-res
38}
39
40func BookInfoDialog(owner Form, title, description string) int {
41parent := owner.form()
42var (
43dlg *walk.Dialog
44ClosePB *walk.PushButton
45parentSize = parent.Size()
46descriptionLabel = gotext.Get("Description:")
47)
48
49layout := Dialog{
50Title: title,
51AssignTo: &dlg,
52Layout: VBox{},
53CancelButton: &ClosePB,
54MinSize: Size{Width: parentSize.Width / 2, Height: parentSize.Height / 2},
55Children: []Widget{
56
57TextLabel{Text: descriptionLabel},
58TextEdit{
59Accessibility: Accessibility{Name: descriptionLabel},
60Text: description,
61ReadOnly: true,
62CompactHeight: true,
63},
64
65Composite{
66Layout: HBox{},
67Children: []Widget{
68HSpacer{},
69PushButton{
70AssignTo: &ClosePB,
71Text: gotext.Get("Close"),
72OnClicked: func() {
73dlg.Close(walk.DlgCmdClose)
74},
75},
76},
77},
78},
79}
80
81res := make(chan int)
82parent.Synchronize(func() {
83layout.Create(parent)
84dlg.Run()
85res <- dlg.Result()
86})
87return <-res
88}
89
90func TextEntryDialog(owner Form, title, msg, value string, text *string) int {
91parent := owner.form()
92var (
93dlg *walk.Dialog
94textLE *walk.LineEdit
95OkPB, CancelPB *walk.PushButton
96)
97
98layout := Dialog{
99Title: title,
100AssignTo: &dlg,
101Layout: VBox{},
102CancelButton: &CancelPB,
103DefaultButton: &OkPB,
104Children: []Widget{
105
106TextLabel{Text: msg},
107LineEdit{
108Accessibility: Accessibility{Name: msg},
109Text: value,
110AssignTo: &textLE,
111},
112
113Composite{
114Layout: HBox{},
115Children: []Widget{
116HSpacer{},
117PushButton{
118AssignTo: &OkPB,
119Text: gotext.Get("OK"),
120OnClicked: func() {
121*text = textLE.Text()
122dlg.Close(walk.DlgCmdOK)
123},
124},
125PushButton{
126AssignTo: &CancelPB,
127Text: gotext.Get("Cancel"),
128OnClicked: func() {
129dlg.Close(walk.DlgCmdCancel)
130},
131},
132},
133},
134},
135}
136
137res := make(chan int)
138parent.Synchronize(func() {
139layout.Create(parent)
140NewFixedPushButton(OkPB)
141NewFixedPushButton(CancelPB)
142dlg.Run()
143res <- dlg.Result()
144})
145return <-res
146}
147
148func CredentialsEntryDialog(owner Form, service *config.Service) int {
149parent := owner.form()
150var (
151dlg *walk.Dialog
152nameLE, urlLE, usernameLE, passwordLE *walk.LineEdit
153nameLabel = gotext.Get("Displayed name:")
154urlLabel = gotext.Get("Server address:")
155usernameLabel = gotext.Get("User name:")
156passwordLabel = gotext.Get("Password:")
157OkPB, CancelPB *walk.PushButton
158)
159
160layout := Dialog{
161Title: gotext.Get("Adding a new account"),
162AssignTo: &dlg,
163Layout: VBox{},
164CancelButton: &CancelPB,
165DefaultButton: &OkPB,
166Children: []Widget{
167TextLabel{Text: nameLabel},
168LineEdit{
169Accessibility: Accessibility{Name: nameLabel},
170AssignTo: &nameLE,
171},
172
173TextLabel{Text: urlLabel},
174LineEdit{
175Accessibility: Accessibility{Name: urlLabel},
176AssignTo: &urlLE,
177},
178
179TextLabel{Text: usernameLabel},
180LineEdit{
181Accessibility: Accessibility{Name: usernameLabel},
182AssignTo: &usernameLE,
183},
184
185TextLabel{Text: passwordLabel},
186LineEdit{
187Accessibility: Accessibility{Name: passwordLabel},
188AssignTo: &passwordLE,
189PasswordMode: true,
190},
191
192Composite{
193Layout: HBox{},
194Children: []Widget{
195HSpacer{},
196PushButton{
197AssignTo: &OkPB,
198Text: gotext.Get("OK"),
199OnClicked: func() {
200service.Name = nameLE.Text()
201service.URL = urlLE.Text()
202service.Username = usernameLE.Text()
203service.Password = passwordLE.Text()
204dlg.Accept()
205},
206},
207PushButton{
208AssignTo: &CancelPB,
209Text: gotext.Get("Cancel"),
210OnClicked: func() {
211dlg.Cancel()
212},
213},
214},
215},
216},
217}
218
219res := make(chan int)
220parent.Synchronize(func() {
221layout.Create(parent)
222NewFixedPushButton(OkPB)
223NewFixedPushButton(CancelPB)
224dlg.Run()
225res <- dlg.Result()
226})
227return <-res
228}
229
230type ProgressDialog struct {
231parent walk.Form
232dlg *walk.Dialog
233label *walk.TextLabel
234pb *walk.ProgressBar
235}
236
237func NewProgressDialog(owner Form, title, label string, maxValue int, cancelFN func()) *ProgressDialog {
238pd := &ProgressDialog{parent: owner.form()}
239var CancelPB *walk.PushButton
240
241var layout = Dialog{
242Title: title,
243AssignTo: &pd.dlg,
244Layout: VBox{},
245CancelButton: &CancelPB,
246Children: []Widget{
247
248TextLabel{
249Text: label,
250AssignTo: &pd.label,
251},
252
253ProgressBar{
254MaxValue: maxValue,
255AssignTo: &pd.pb,
256},
257
258PushButton{
259AssignTo: &CancelPB,
260Text: gotext.Get("Cancel"),
261OnClicked: func() {
262cancelFN()
263pd.dlg.Cancel()
264},
265},
266},
267}
268
269done := make(chan bool)
270pd.parent.Synchronize(func() {
271layout.Create(pd.parent)
272done <- true
273})
274<-done
275return pd
276}
277
278func (pd *ProgressDialog) Run() {
279pd.parent.Synchronize(func() {
280pd.dlg.Run()
281})
282}
283
284func (pd *ProgressDialog) SetLabel(label string) {
285doneCH := make(chan bool)
286pd.dlg.Synchronize(func() {
287pd.label.SetText(label)
288doneCH <- true
289})
290<-doneCH
291}
292
293func (pd *ProgressDialog) SetValue(value int) {
294doneCH := make(chan bool)
295pd.dlg.Synchronize(func() {
296pd.pb.SetValue(value)
297doneCH <- true
298})
299<-doneCH
300}
301
302func (pd *ProgressDialog) Cancel() {
303done := make(chan bool)
304pd.dlg.Synchronize(func() {
305pd.dlg.Cancel()
306done <- true
307})
308<-done
309}
310