-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
162 lines (148 loc) · 4.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package library
import (
"image/color"
"sync"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/container"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
"github.com/jvatic/audible-downloader/gui/components"
"github.com/jvatic/audible-downloader/internal/common"
)
func buildConfigUI(renderQueue chan<- func(w fyne.Window), actionQueue chan<- Action, closeFunc func()) fyne.CanvasObject {
var pathTemplateMtx sync.RWMutex
pathTemplate := common.DefaultPathTemplate
setPathTemplate := func(text string) {
pathTemplateMtx.Lock()
defer pathTemplateMtx.Unlock()
pathTemplate = text
}
getPathTemplate := func() string {
pathTemplateMtx.RLock()
defer pathTemplateMtx.RUnlock()
return pathTemplate
}
var maxAuthorsMtx sync.RWMutex
maxAuthors := 1
setMaxAuthors := func(num int) {
maxAuthorsMtx.Lock()
defer maxAuthorsMtx.Unlock()
maxAuthors = num
}
getMaxAuthors := func() int {
maxAuthorsMtx.RLock()
defer maxAuthorsMtx.RUnlock()
return maxAuthors
}
var authorSeparatorMtx sync.RWMutex
authorSeparator := ", "
setAuthorSeparator := func(sep string) {
authorSeparatorMtx.Lock()
defer authorSeparatorMtx.Unlock()
authorSeparator = sep
}
getAuthorSeparator := func() string {
authorSeparatorMtx.RLock()
defer authorSeparatorMtx.RUnlock()
return authorSeparator
}
updatePathTemplateSub := func() {
actionQueue <- func(s *State) {
authorSeparatorMtx.RLock()
defer authorSeparatorMtx.RUnlock()
maxAuthorsMtx.RLock()
defer maxAuthorsMtx.RUnlock()
s.getDstPath = common.CompilePathTemplate(
getPathTemplate(),
common.PathTemplateTitle(),
common.PathTemplateShortTitle(),
common.PathTemplateAuthor(maxAuthors, authorSeparator),
)
}
}
previewText, previewTextCh := components.NewText(renderQueue, "")
updatePreviewText := func() {
previewTextCh <- GetDstPath(actionQueue, &common.SampleBook)
}
updatePreviewText()
pathTemplateInput, pathTemplateInputInCh, pathTemplateInputOutCh := components.NewEntry(renderQueue)
pathTemplateInputInCh <- getPathTemplate()
go func() {
for {
text, ok := <-pathTemplateInputOutCh
if !ok {
return
}
setPathTemplate(text)
updatePathTemplateSub()
updatePreviewText()
}
}()
maxAuthorsInput, maxAuthorsInputInCh, maxAuthorsInputOutCh := components.NewIntEntry(renderQueue)
maxAuthorsInputInCh <- getMaxAuthors()
go func() {
for {
num, ok := <-maxAuthorsInputOutCh
if !ok {
return
}
setMaxAuthors(num)
updatePathTemplateSub()
updatePreviewText()
}
}()
authorSeparatorInput, authorSeparatorInputInCh, authorSeparatorInputOutCh := components.NewEntry(renderQueue)
authorSeparatorInputInCh <- getAuthorSeparator()
go func() {
for {
sep, ok := <-authorSeparatorInputOutCh
if !ok {
return
}
setAuthorSeparator(sep)
updatePathTemplateSub()
updatePreviewText()
}
}()
return components.ApplyTemplate(
container.NewVBox(
container.NewCenter(
components.NewImmutableText("Download Settings", components.TextOptionHeading(components.H1)),
),
container.NewVBox(
components.NewImmutableText("Download Path Template", components.TextOptionBold()),
pathTemplateInput,
Indent(
components.NewImmutableText("Format Options: ", components.TextOptionBold()),
container.NewHBox(
components.NewImmutableText("%TITLE%", components.TextOptionBold()),
canvas.NewText(" - Full book title as seen in library", color.Black),
),
container.NewHBox(
components.NewImmutableText("%SHORT_TITLE%", components.TextOptionBold()),
canvas.NewText(" - Book title up to the first occurance of ", color.Black),
components.NewImmutableText(":", components.TextOptionBold()),
),
components.NewImmutableText("%AUTHOR%", components.TextOptionBold()),
Indent(
container.NewHBox(
components.NewImmutableText("Max number of authors to include (0 = unlimited): ", components.TextOptionBold()),
maxAuthorsInput,
components.NewImmutableText("Author separator: ", components.TextOptionBold()),
authorSeparatorInput,
),
),
),
components.NewImmutableText("Preview: ", components.TextOptionBold()),
previewText,
),
layout.NewSpacer(),
container.NewHBox(
layout.NewSpacer(),
widget.NewButton("Cancel", closeFunc),
widget.NewButton("Save", closeFunc),
),
),
)
}