OnlineLibrary

Форк
0
181 строка · 4.6 Кб
1
package gui
2

3
import (
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

12
type MenuBar struct {
13
	wnd                                  *walk.MainWindow
14
	libraryMenu, outputDeviceMenu        *walk.Menu
15
	libraryLogon                         *walk.MutableCondition
16
	bookMenu, bookmarkMenu, logLevelMenu *walk.Menu
17
	bookMenuEnabled                      *walk.MutableCondition
18
	languageMenu                         *walk.Menu
19
	pauseTimerItem                       *walk.Action
20
	msgCH                                chan msg.Message
21
}
22

23
func (mb *MenuBar) SetProvidersMenu(services []*config.Service, currentID string) {
24
	mb.wnd.Synchronize(func() {
25
		mb.libraryLogon.SetSatisfied(false)
26
		actions := mb.libraryMenu.Actions()
27
		for i := actions.Len(); i > 1; i-- {
28
			actions.RemoveAt(0)
29
		}
30

31
		for i, service := range services {
32
			a := walk.NewAction()
33
			id := service.ID
34
			if id == currentID {
35
				a.SetChecked(true)
36
				mb.libraryLogon.SetSatisfied(true)
37
			}
38
			a.SetText(service.Name)
39
			a.Triggered().Attach(func() {
40
				mb.msgCH <- msg.Message{Code: msg.SET_PROVIDER, Data: id}
41
			})
42
			actions.Insert(i, a)
43
		}
44
	})
45
}
46

47
func (mb *MenuBar) SetBookmarksMenu(bookmarks map[string]string) {
48
	mb.wnd.Synchronize(func() {
49
		actions := mb.bookmarkMenu.Actions()
50
		for i := actions.Len(); i > 1; i-- {
51
			actions.RemoveAt(0)
52
		}
53

54
		for id, name := range bookmarks {
55
			if name == "" {
56
				continue
57
			}
58
			id := id
59
			subMenu, err := walk.NewMenu()
60
			if err != nil {
61
				panic(err)
62
			}
63
			a, err := actions.InsertMenu(0, subMenu)
64
			if err != nil {
65
				panic(err)
66
			}
67
			a.SetText(name)
68
			bookmarkActions := subMenu.Actions()
69
			moveAction := walk.NewAction()
70
			moveAction.SetText(gotext.Get("Move"))
71
			moveAction.Triggered().Attach(func() {
72
				mb.msgCH <- msg.Message{Code: msg.BOOKMARK_FETCH, Data: id}
73
			})
74
			bookmarkActions.Add(moveAction)
75
			removeAction := walk.NewAction()
76
			removeAction.SetText(gotext.Get("Remove..."))
77
			removeAction.Triggered().Attach(func() {
78
				mb.msgCH <- msg.Message{Code: msg.BOOKMARK_REMOVE, Data: id}
79
			})
80
			bookmarkActions.Add(removeAction)
81
		}
82
	})
83
}
84

85
func (mb *MenuBar) SetBookMenuEnabled(enabled bool) {
86
	mb.wnd.Synchronize(func() {
87
		mb.bookMenuEnabled.SetSatisfied(enabled)
88
	})
89
}
90

91
func (mb *MenuBar) SetOutputDeviceMenu(deviceNames []string, current string) {
92
	mb.wnd.Synchronize(func() {
93
		actions := mb.outputDeviceMenu.Actions()
94
		actions.Clear()
95

96
		for i, name := range deviceNames {
97
			a := walk.NewAction()
98
			a.SetText(name)
99
			if name == current || (current == "" && i == 0) {
100
				a.SetChecked(true)
101
			}
102
			a.Triggered().Attach(func() {
103
				actions := mb.outputDeviceMenu.Actions()
104
				for k := 0; k < actions.Len(); k++ {
105
					actions.At(k).SetChecked(false)
106
				}
107
				a.SetChecked(true)
108
				mb.msgCH <- msg.Message{Code: msg.PLAYER_OUTPUT_DEVICE, Data: a.Text()}
109
			})
110
			actions.Add(a)
111
		}
112
	})
113
}
114

115
func (mb *MenuBar) SetLanguageMenu(langs []lang.Language, current string) {
116
	mb.wnd.Synchronize(func() {
117
		actions := mb.languageMenu.Actions()
118
		actions.Clear()
119

120
		langsWithDefaultLang := make([]lang.Language, len(langs)+1)
121
		langsWithDefaultLang[0] = lang.Language{Description: gotext.Get("Default")}
122
		copy(langsWithDefaultLang[1:], langs)
123

124
		for _, lang := range langsWithDefaultLang {
125
			a := walk.NewAction()
126
			a.SetText(lang.Description)
127
			id := lang.ID
128
			if id == current {
129
				a.SetChecked(true)
130
			}
131
			a.Triggered().Attach(func() {
132
				actions := mb.languageMenu.Actions()
133
				for k := 0; k < actions.Len(); k++ {
134
					actions.At(k).SetChecked(false)
135
				}
136
				a.SetChecked(true)
137
				mb.msgCH <- msg.Message{Code: msg.SET_LANGUAGE, Data: id}
138
			})
139
			actions.Add(a)
140
		}
141
	})
142
}
143

144
func (mb *MenuBar) SetPauseTimerLabel(minutes int) {
145
	label := gotext.Get("Pause timer (no)")
146
	if minutes > 0 {
147
		label = gotext.Get("Pause timer (%d min.)", minutes)
148
	}
149
	mb.wnd.Synchronize(func() {
150
		mb.pauseTimerItem.SetText(label)
151
	})
152
}
153

154
func (mb *MenuBar) SetLogLevelMenu(levels []log.Level, current log.Level) {
155
	mb.wnd.Synchronize(func() {
156
		actions := mb.logLevelMenu.Actions()
157
		actions.Clear()
158

159
		for _, level := range levels {
160
			level := level // Avoid capturing the iteration variable
161
			a := walk.NewAction()
162
			a.SetText(level.String())
163
			if level == current {
164
				a.SetChecked(true)
165
			}
166
			a.Triggered().Attach(func() {
167
				actions := mb.logLevelMenu.Actions()
168
				for k := 0; k < actions.Len(); k++ {
169
					actions.At(k).SetChecked(false)
170
				}
171
				a.SetChecked(true)
172
				mb.msgCH <- msg.Message{Code: msg.LOG_SET_LEVEL, Data: level}
173
			})
174
			actions.Add(a)
175
		}
176
	})
177
}
178

179
func (mb *MenuBar) BookMenu() *walk.Menu {
180
	return mb.bookMenu
181
}
182

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

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

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

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