OnlineLibrary

Форк
0
/
mainwindow.go 
412 строк · 13.2 Кб
1
package gui
2

3
import (
4
	"fmt"
5
	"time"
6

7
	"OnlineLibrary/internal/config"
8
	"OnlineLibrary/internal/gui/msg"
9
	"github.com/leonelquinteros/gotext"
10
	"gitverse.ru/kvark128/walk"
11
	. "gitverse.ru/kvark128/walk/declarative"
12
)
13

14
type Form interface {
15
	form() walk.Form
16
}
17

18
type MainWnd struct {
19
	mainWindow  *walk.MainWindow
20
	msgChan     chan msg.Message
21
	menuBar     *MenuBar
22
	mainListBox *MainListBox
23
	statusBar   *StatusBar
24
}
25

26
func NewMainWindow() (*MainWnd, error) {
27
	wnd := &MainWnd{
28
		msgChan:     make(chan msg.Message, config.MessageBufferSize),
29
		menuBar:     new(MenuBar),
30
		mainListBox: new(MainListBox),
31
		statusBar:   new(StatusBar),
32
	}
33

34
	wnd.menuBar.libraryLogon = walk.NewMutableCondition()
35
	MustRegisterCondition("libraryLogon", wnd.menuBar.libraryLogon)
36
	wnd.menuBar.bookMenuEnabled = walk.NewMutableCondition()
37
	MustRegisterCondition("bookMenuEnabled", wnd.menuBar.bookMenuEnabled)
38

39
	wndLayout := MainWindow{
40
		Title:    config.ProgramName,
41
		Layout:   VBox{},
42
		AssignTo: &wnd.mainWindow,
43

44
		MenuItems: []MenuItem{
45
			Menu{
46
				Text: gotext.Get("&Library"),
47
				Items: []MenuItem{
48
					Menu{
49
						Text:     gotext.Get("Accounts"),
50
						AssignTo: &wnd.menuBar.libraryMenu,
51
						Items: []MenuItem{
52
							Action{
53
								Text:        gotext.Get("New account"),
54
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyN},
55
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.LIBRARY_ADD} },
56
							},
57
						},
58
					},
59
					Action{
60
						Text:        gotext.Get("Bookshelf"),
61
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyE},
62
						Enabled:     Bind("libraryLogon"),
63
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.OPEN_BOOKSHELF} },
64
					},
65
					Action{
66
						Text:        gotext.Get("New books"),
67
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyK},
68
						Enabled:     Bind("libraryLogon"),
69
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.OPEN_NEWBOOKS} },
70
					},
71
					Action{
72
						Text:        gotext.Get("Find..."),
73
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyF},
74
						Enabled:     Bind("libraryLogon"),
75
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.SEARCH_BOOK} },
76
					},
77
					Action{
78
						Text:        gotext.Get("Main menu"),
79
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyM},
80
						Enabled:     Bind("libraryLogon"),
81
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.MAIN_MENU} },
82
					},
83
					Action{
84
						Text:        gotext.Get("Previous menu"),
85
						Shortcut:    Shortcut{Modifiers: 0, Key: walk.KeyBack},
86
						Enabled:     Bind("libraryLogon"),
87
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.MENU_BACK} },
88
					},
89
					Action{
90
						Text:        gotext.Get("Local books"),
91
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyL},
92
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.SET_PROVIDER, Data: config.LocalStorageID} },
93
					},
94
					Action{
95
						Text:        gotext.Get("Library information"),
96
						Enabled:     Bind("libraryLogon"),
97
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.LIBRARY_INFO} },
98
					},
99
					Action{
100
						Text:        gotext.Get("Delete account"),
101
						Enabled:     Bind("libraryLogon"),
102
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.LIBRARY_REMOVE} },
103
					},
104
					Action{
105
						Text:        gotext.Get("Exit"),
106
						Shortcut:    Shortcut{Modifiers: walk.ModAlt, Key: walk.KeyF4},
107
						OnTriggered: func() { wnd.mainWindow.Close() },
108
					},
109
				},
110
			},
111

112
			Menu{
113
				Text:     gotext.Get("&Book"),
114
				AssignTo: &wnd.menuBar.bookMenu,
115
				Enabled:  Bind("bookMenuEnabled"),
116
				Items: []MenuItem{
117
					Action{
118
						Text:        gotext.Get("Add book to bookshelf"),
119
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyA},
120
						Enabled:     Bind("libraryLogon"),
121
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.ISSUE_BOOK} },
122
					},
123
					Action{
124
						Text:        gotext.Get("Download book"),
125
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyD},
126
						Enabled:     Bind("libraryLogon"),
127
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.DOWNLOAD_BOOK} },
128
					},
129
					Action{
130
						Text:        gotext.Get("Remove book from bookshelf"),
131
						Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyDelete},
132
						Enabled:     Bind("libraryLogon"),
133
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.REMOVE_BOOK} },
134
					},
135
					Action{
136
						Text:        gotext.Get("Book information"),
137
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyI},
138
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.BOOK_DESCRIPTION} },
139
					},
140
				},
141
			},
142

143
			Menu{
144
				Text: gotext.Get("&Playback"),
145
				Items: []MenuItem{
146
					Menu{
147
						Text:     gotext.Get("Bookmarks"),
148
						AssignTo: &wnd.menuBar.bookmarkMenu,
149
						Items: []MenuItem{
150
							Action{
151
								Text:        gotext.Get("New bookmark"),
152
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyB},
153
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.BOOKMARK_SET} },
154
							},
155
						},
156
					},
157
					Action{
158
						Text:        gotext.Get("Play / Pause"),
159
						Shortcut:    Shortcut{Key: walk.KeySpace},
160
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_PLAY_PAUSE} },
161
					},
162
					Action{
163
						Text:        gotext.Get("Stop"),
164
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeySpace},
165
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_STOP} },
166
					},
167

168
					Menu{
169
						Text: gotext.Get("Book navigation"),
170
						Items: []MenuItem{
171
							Action{
172
								Text:        gotext.Get("Beginning of the book"),
173
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyBack},
174
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_GOTO_FRAGMENT, Data: 0} },
175
							},
176
							Action{
177
								Text:        gotext.Get("Go to fragment..."),
178
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyG},
179
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_GOTO_FRAGMENT} },
180
							},
181
							Action{
182
								Text:        gotext.Get("Next fragment"),
183
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyNext},
184
								OnTriggered: func() { wnd.msgChan <- next_fragment },
185
							},
186
							Action{
187
								Text:        gotext.Get("Previous fragment"),
188
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyPrior},
189
								OnTriggered: func() { wnd.msgChan <- previous_fragment },
190
							},
191
						},
192
					},
193

194
					Menu{
195
						Text: gotext.Get("Fragment navigation"),
196
						Items: []MenuItem{
197
							Action{
198
								Text:        gotext.Get("Beginning of the fragment"),
199
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyBack},
200
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_GOTO_POSITION, Data: time.Duration(0)} },
201
							},
202
							Action{
203
								Text:        gotext.Get("Go to position..."),
204
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyG},
205
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_GOTO_POSITION} },
206
							},
207
							Action{
208
								Text:        gotext.Get("5 sec. forward"),
209
								Shortcut:    Shortcut{Modifiers: 0, Key: walk.KeyRight},
210
								OnTriggered: func() { wnd.msgChan <- rewind_5sec_forward },
211
							},
212
							Action{
213
								Text:        gotext.Get("5 sec. backward"),
214
								Shortcut:    Shortcut{Modifiers: 0, Key: walk.KeyLeft},
215
								OnTriggered: func() { wnd.msgChan <- rewind_5sec_back },
216
							},
217
							Action{
218
								Text:        gotext.Get("30 sec. forward"),
219
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyRight},
220
								OnTriggered: func() { wnd.msgChan <- rewind_30sec_forward },
221
							},
222
							Action{
223
								Text:        gotext.Get("30 sec. backward"),
224
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyLeft},
225
								OnTriggered: func() { wnd.msgChan <- rewind_30sec_back },
226
							},
227
							Action{
228
								Text:        gotext.Get("1 min. forward"),
229
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyRight},
230
								OnTriggered: func() { wnd.msgChan <- rewind_1min_forward },
231
							},
232
							Action{
233
								Text:        gotext.Get("1 min. backward"),
234
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyLeft},
235
								OnTriggered: func() { wnd.msgChan <- rewind_1min_back },
236
							},
237
							Action{
238
								Text:        gotext.Get("5 min. forward"),
239
								Shortcut:    Shortcut{Modifiers: walk.ModControl | walk.ModShift, Key: walk.KeyRight},
240
								OnTriggered: func() { wnd.msgChan <- rewind_5min_forward },
241
							},
242
							Action{
243
								Text:        gotext.Get("5 min. backward"),
244
								Shortcut:    Shortcut{Modifiers: walk.ModControl | walk.ModShift, Key: walk.KeyLeft},
245
								OnTriggered: func() { wnd.msgChan <- rewind_5min_back },
246
							},
247
						},
248
					},
249

250
					Menu{
251
						Text: gotext.Get("Volume"),
252
						Items: []MenuItem{
253
							Action{
254
								Text:        gotext.Get("Increase volume"),
255
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyUp},
256
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_VOLUME_UP} },
257
							},
258
							Action{
259
								Text:        gotext.Get("Decrease volume"),
260
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyDown},
261
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_VOLUME_DOWN} },
262
							},
263
							Action{
264
								Text:        gotext.Get("Reset volume"),
265
								Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyR},
266
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_VOLUME_RESET} },
267
							},
268
						},
269
					},
270

271
					Menu{
272
						Text: gotext.Get("Speed"),
273
						Items: []MenuItem{
274
							Action{
275
								Text:        gotext.Get("Increase speed"),
276
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyUp},
277
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_SPEED_UP} },
278
							},
279
							Action{
280
								Text:        gotext.Get("Decrease speed"),
281
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyDown},
282
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_SPEED_DOWN} },
283
							},
284
							Action{
285
								Text:        gotext.Get("Reset speed"),
286
								Shortcut:    Shortcut{Modifiers: walk.ModShift, Key: walk.KeyR},
287
								OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_SPEED_RESET} },
288
							},
289
						},
290
					},
291
				},
292
			},
293
			Menu{
294
				Text: gotext.Get("&Settings"),
295
				Items: []MenuItem{
296
					Menu{
297
						Text:     gotext.Get("Audio output device"),
298
						AssignTo: &wnd.menuBar.outputDeviceMenu,
299
					},
300
					Menu{
301
						Text:     gotext.Get("Language"),
302
						AssignTo: &wnd.menuBar.languageMenu,
303
					},
304
					Action{
305
						Text:        gotext.Get("Pause timer"),
306
						AssignTo:    &wnd.menuBar.pauseTimerItem,
307
						Shortcut:    Shortcut{Modifiers: walk.ModControl, Key: walk.KeyP},
308
						OnTriggered: func() { wnd.msgChan <- msg.Message{Code: msg.PLAYER_SET_TIMER} },
309
					},
310
					Menu{
311
						Text:     gotext.Get("Logging level"),
312
						AssignTo: &wnd.menuBar.logLevelMenu,
313
					},
314
				},
315
			},
316
			Menu{
317
				Text: gotext.Get("&Help"),
318
				Items: []MenuItem{
319
					Action{
320
						Text: gotext.Get("About"),
321
						OnTriggered: func() {
322
							msg := gotext.Get("%v version %v\nWorking directory: %v\nAuthor: %v", config.ProgramName, config.ProgramVersion, config.UserData(), config.CopyrightInfo)
323
							walk.MsgBox(wnd.mainWindow, gotext.Get("About"), msg, walk.MsgBoxOK|walk.MsgBoxIconInformation)
324
						},
325
					},
326
				},
327
			},
328
		},
329

330
		Children: []Widget{
331
			TextLabel{
332
				AssignTo: &wnd.mainListBox.label,
333
			},
334
			ListBox{
335
				AssignTo: &wnd.mainListBox.ListBox,
336
				OnItemActivated: func() {
337
					wnd.msgChan <- msg.Message{Code: msg.ACTIVATE_MENU}
338
				},
339
			},
340
		},
341

342
		StatusBarItems: []StatusBarItem{
343
			StatusBarItem{
344
				AssignTo: &wnd.statusBar.elapseTime,
345
				Text:     "00:00:00",
346
			},
347
			StatusBarItem{
348
				Text: "/",
349
			},
350
			StatusBarItem{
351
				AssignTo: &wnd.statusBar.totalTime,
352
				Text:     "00:00:00",
353
			},
354
			StatusBarItem{
355
				AssignTo: &wnd.statusBar.fragments,
356
			},
357
			StatusBarItem{
358
				AssignTo: &wnd.statusBar.bookPercent,
359
			},
360
		},
361
	}
362

363
	if err := wndLayout.Create(); err != nil {
364
		return nil, err
365
	}
366

367
	wnd.menuBar.wnd = wnd.mainWindow
368
	wnd.menuBar.msgCH = wnd.msgChan
369
	wnd.mainListBox.msgCH = wnd.msgChan
370
	wnd.statusBar.StatusBar = wnd.mainWindow.StatusBar()
371

372
	if err := walk.InitWrapperWindow(wnd.mainListBox); err != nil {
373
		return nil, err
374
	}
375
	return wnd, nil
376
}
377

378
func (mw *MainWnd) MsgChan() chan msg.Message {
379
	return mw.msgChan
380
}
381

382
func (mw *MainWnd) form() walk.Form {
383
	return mw.mainWindow
384
}
385

386
func (mw *MainWnd) Run() {
387
	mw.msgChan <- msg.Message{Code: msg.SET_PROVIDER}
388
	mw.mainWindow.Run()
389
	close(mw.msgChan)
390
}
391

392
func (mw *MainWnd) MenuBar() *MenuBar {
393
	return mw.menuBar
394
}
395

396
func (mw *MainWnd) MainListBox() *MainListBox {
397
	return mw.mainListBox
398
}
399

400
func (mw *MainWnd) StatusBar() *StatusBar {
401
	return mw.statusBar
402
}
403

404
func (mw *MainWnd) SetTitle(title string) {
405
	mw.mainWindow.Synchronize(func() {
406
		var windowTitle = config.ProgramName
407
		if title != "" {
408
			windowTitle = fmt.Sprintf("%v \u2014 %v", title, windowTitle)
409
		}
410
		mw.mainWindow.SetTitle(windowTitle)
411
	})
412
}
413

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

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

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

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