OnlineLibrary
181 строка · 4.6 Кб
1package gui
2
3import (
4"OnlineLibrary/internal/config"
5"OnlineLibrary/internal/gui/msg"
6"OnlineLibrary/internal/lang"
7"OnlineLibrary/internal/log"
8"github.com/leonelquinteros/gotext"
9"gitverse.ru/kvark128/walk"
10)
11
12type MenuBar struct {
13wnd *walk.MainWindow
14libraryMenu, outputDeviceMenu *walk.Menu
15libraryLogon *walk.MutableCondition
16bookMenu, bookmarkMenu, logLevelMenu *walk.Menu
17bookMenuEnabled *walk.MutableCondition
18languageMenu *walk.Menu
19pauseTimerItem *walk.Action
20msgCH chan msg.Message
21}
22
23func (mb *MenuBar) SetProvidersMenu(services []*config.Service, currentID string) {
24mb.wnd.Synchronize(func() {
25mb.libraryLogon.SetSatisfied(false)
26actions := mb.libraryMenu.Actions()
27for i := actions.Len(); i > 1; i-- {
28actions.RemoveAt(0)
29}
30
31for i, service := range services {
32a := walk.NewAction()
33id := service.ID
34if id == currentID {
35a.SetChecked(true)
36mb.libraryLogon.SetSatisfied(true)
37}
38a.SetText(service.Name)
39a.Triggered().Attach(func() {
40mb.msgCH <- msg.Message{Code: msg.SET_PROVIDER, Data: id}
41})
42actions.Insert(i, a)
43}
44})
45}
46
47func (mb *MenuBar) SetBookmarksMenu(bookmarks map[string]string) {
48mb.wnd.Synchronize(func() {
49actions := mb.bookmarkMenu.Actions()
50for i := actions.Len(); i > 1; i-- {
51actions.RemoveAt(0)
52}
53
54for id, name := range bookmarks {
55if name == "" {
56continue
57}
58id := id
59subMenu, err := walk.NewMenu()
60if err != nil {
61panic(err)
62}
63a, err := actions.InsertMenu(0, subMenu)
64if err != nil {
65panic(err)
66}
67a.SetText(name)
68bookmarkActions := subMenu.Actions()
69moveAction := walk.NewAction()
70moveAction.SetText(gotext.Get("Move"))
71moveAction.Triggered().Attach(func() {
72mb.msgCH <- msg.Message{Code: msg.BOOKMARK_FETCH, Data: id}
73})
74bookmarkActions.Add(moveAction)
75removeAction := walk.NewAction()
76removeAction.SetText(gotext.Get("Remove..."))
77removeAction.Triggered().Attach(func() {
78mb.msgCH <- msg.Message{Code: msg.BOOKMARK_REMOVE, Data: id}
79})
80bookmarkActions.Add(removeAction)
81}
82})
83}
84
85func (mb *MenuBar) SetBookMenuEnabled(enabled bool) {
86mb.wnd.Synchronize(func() {
87mb.bookMenuEnabled.SetSatisfied(enabled)
88})
89}
90
91func (mb *MenuBar) SetOutputDeviceMenu(deviceNames []string, current string) {
92mb.wnd.Synchronize(func() {
93actions := mb.outputDeviceMenu.Actions()
94actions.Clear()
95
96for i, name := range deviceNames {
97a := walk.NewAction()
98a.SetText(name)
99if name == current || (current == "" && i == 0) {
100a.SetChecked(true)
101}
102a.Triggered().Attach(func() {
103actions := mb.outputDeviceMenu.Actions()
104for k := 0; k < actions.Len(); k++ {
105actions.At(k).SetChecked(false)
106}
107a.SetChecked(true)
108mb.msgCH <- msg.Message{Code: msg.PLAYER_OUTPUT_DEVICE, Data: a.Text()}
109})
110actions.Add(a)
111}
112})
113}
114
115func (mb *MenuBar) SetLanguageMenu(langs []lang.Language, current string) {
116mb.wnd.Synchronize(func() {
117actions := mb.languageMenu.Actions()
118actions.Clear()
119
120langsWithDefaultLang := make([]lang.Language, len(langs)+1)
121langsWithDefaultLang[0] = lang.Language{Description: gotext.Get("Default")}
122copy(langsWithDefaultLang[1:], langs)
123
124for _, lang := range langsWithDefaultLang {
125a := walk.NewAction()
126a.SetText(lang.Description)
127id := lang.ID
128if id == current {
129a.SetChecked(true)
130}
131a.Triggered().Attach(func() {
132actions := mb.languageMenu.Actions()
133for k := 0; k < actions.Len(); k++ {
134actions.At(k).SetChecked(false)
135}
136a.SetChecked(true)
137mb.msgCH <- msg.Message{Code: msg.SET_LANGUAGE, Data: id}
138})
139actions.Add(a)
140}
141})
142}
143
144func (mb *MenuBar) SetPauseTimerLabel(minutes int) {
145label := gotext.Get("Pause timer (no)")
146if minutes > 0 {
147label = gotext.Get("Pause timer (%d min.)", minutes)
148}
149mb.wnd.Synchronize(func() {
150mb.pauseTimerItem.SetText(label)
151})
152}
153
154func (mb *MenuBar) SetLogLevelMenu(levels []log.Level, current log.Level) {
155mb.wnd.Synchronize(func() {
156actions := mb.logLevelMenu.Actions()
157actions.Clear()
158
159for _, level := range levels {
160level := level // Avoid capturing the iteration variable
161a := walk.NewAction()
162a.SetText(level.String())
163if level == current {
164a.SetChecked(true)
165}
166a.Triggered().Attach(func() {
167actions := mb.logLevelMenu.Actions()
168for k := 0; k < actions.Len(); k++ {
169actions.At(k).SetChecked(false)
170}
171a.SetChecked(true)
172mb.msgCH <- msg.Message{Code: msg.LOG_SET_LEVEL, Data: level}
173})
174actions.Add(a)
175}
176})
177}
178
179func (mb *MenuBar) BookMenu() *walk.Menu {
180return mb.bookMenu
181}
182