-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.go
268 lines (228 loc) · 6.32 KB
/
mainwindow.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package mainwindow
import (
"fmt"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/swampapp/swamp/internal/downloader"
indexerd "github.com/swampapp/swamp/internal/indexer"
"github.com/swampapp/swamp/internal/resources"
"github.com/swampapp/swamp/internal/resticsettings"
"github.com/swampapp/swamp/internal/settings"
"github.com/swampapp/swamp/internal/status"
"github.com/swampapp/swamp/internal/streamer"
"github.com/swampapp/swamp/internal/ui/appmenu"
"github.com/swampapp/swamp/internal/ui/assistant"
"github.com/swampapp/swamp/internal/ui/component"
"github.com/swampapp/swamp/internal/ui/downloadlist"
"github.com/swampapp/swamp/internal/ui/filelist"
"github.com/swampapp/swamp/internal/ui/indexer"
"github.com/swampapp/swamp/internal/ui/inprogresslist"
settingsui "github.com/swampapp/swamp/internal/ui/settings"
"github.com/swampapp/swamp/internal/ui/taglist"
)
type MainWindow struct {
*component.Component
gtk.ApplicationWindow
appMenu *appmenu.AppMenu
fileList *filelist.FileList
downloadList *downloadlist.DownloadList
inprogressList *inprogresslist.InProgressList
tagList *taglist.TagList
paned *gtk.Paned
searchText string
}
var mw *MainWindow
func Instance() *MainWindow {
return mw
}
func New(a *gtk.Application) (*MainWindow, error) {
screen, _ := gdk.ScreenGetDefault()
gtk.AddProviderForScreen(screen, resources.CSS(), 1)
w, err := gtk.ApplicationWindowNew(a)
if err != nil {
return nil, errors.Wrap(err, "failed to create window")
}
// Detect if a dark theme is being used
ctx, _ := w.GetStyleContext()
color := ctx.GetColor(gtk.STATE_FLAG_NORMAL)
rgba := color.Floats()
luminace := (0.2126*rgba[0] + 0.7152*rgba[1] + 0.0722*rgba[2])
if luminace > 0.5 {
settings.SetDarkMode(true)
}
mw = &MainWindow{
Component: component.New("/ui/mainwindow"),
ApplicationWindow: *w,
appMenu: appmenu.New(),
downloadList: downloadlist.New(),
inprogressList: inprogresslist.New(),
tagList: taglist.New(),
fileList: filelist.New(),
}
resources.LoadImages()
mw.paned = mw.GladeWidget("content_panel").(*gtk.Paned)
mw.appMenu.OnSelectionChanged(mw.SetMainPanel)
w.SetTitle("Swamp")
w.SetDefaultSize(1024, 600)
img, _ := gtk.ImageNewFromResource("/swamp/images/swamp.png")
w.SetIcon(img.GetPixbuf())
w.Connect("key-press-event", func(tree *gtk.ApplicationWindow, ev *gdk.Event) bool {
kp := gdk.EventKeyNewFromEvent(ev)
cntrlMask := uint(gdk.CONTROL_MASK)
state := kp.State()
cntrl := state&cntrlMask == cntrlMask
switch kp.KeyVal() {
case gdk.KEY_q:
if cntrl {
a.Quit()
return true
}
return false
case gdk.KEY_f:
if cntrl {
mw.appMenu.SelectPath("0")
return true
}
return false
default:
return false
}
})
container := mw.GladeWidget("container").(*gtk.Box)
mw.Container.Add(container)
pane := mw.GladeWidget("content_panel").(*gtk.Paned)
if err != nil {
panic(err)
}
// App Menu
pane.Add1(mw.appMenu.Widget())
// File List
pane.Add2(mw.fileList)
pane.SetPosition(230)
if resticsettings.FirstBoot() {
a := assistant.New()
a.ShowAll()
a.WhenDone(func() {
mw.ShowAll()
})
} else {
mw.ShowAll()
}
taglist.TagSelectedEvent("taglist", func(tag string) {
mw.searchText = "tag:" + tag
mw.appMenu.SelectPath("0")
mw.searchText = ""
})
mw.StopDownloading()
mw.StopIndexing()
mw.appMenu.SelectPath("0")
//mw.SetMainPanel("Search")
streamer.OnStartStreaming(mw.StartDownloading)
streamer.OnStopStreaming(mw.StopDownloading)
indexerd.OnStart(mw.StartIndexing)
indexerd.OnStop(mw.StopIndexing)
status.OnSetRight(mw.SetStatusRight)
status.OnSet(mw.SetStatus)
downloader.Instance().AddObserver(mw)
return mw, nil
}
func (w *MainWindow) SetMainPanel(t string) {
log.Print("changing main pane to ", t)
child, _ := w.paned.GetChild2()
if child != nil {
child.ToWidget().Hide()
w.paned.Remove(child)
}
if t != "Search" {
w.searchText = ""
}
switch t {
case "Search":
w.appMenu.SelectPath("0")
w.paned.Add2(w.fileList)
if w.searchText != "" {
w.fileList.SetSearchText(w.searchText)
}
case "Tags":
w.paned.Add2(w.tagList)
case "Downloaded":
w.paned.Add2(w.downloadList)
case "In Progress":
w.paned.Add2(w.inprogressList)
case "Indexer":
w.paned.Add2(indexer.New())
case "Settings":
w.paned.Add2(settingsui.New())
default:
panic(fmt.Errorf("panel '%s' not implemented", t))
}
w.paned.ShowAll()
}
func (w *MainWindow) StatusClear() {
w.SetStatus("")
}
func (w *MainWindow) StatusWarn(msg string) {
w.SetStatus(fmt.Sprintf("⚠️ %s", msg))
}
func (w *MainWindow) StatusError(msg string) {
w.SetStatus(fmt.Sprintf("🛑 %s", msg))
}
func (w *MainWindow) SetStatus(text string) {
glib.IdleAdd(func() {
statusLabel := w.GladeWidget("statusLabel").(*gtk.Label)
statusLabel.SetText(text)
})
}
func (w *MainWindow) SetStatusRight(text string) {
glib.IdleAdd(func() {
statusLabel := w.GladeWidget("statusLabelRight").(*gtk.Label)
statusLabel.SetText(text)
})
}
func (w *MainWindow) StartDownloading() {
glib.IdleAdd(func() {
img := w.GladeWidget("downloadingIMG").(*gtk.Image)
resources.UpdateImageFromResource(img, "status-downloading")
img.SetTooltipText("Indexing documents")
img.Show()
})
}
func (w *MainWindow) StopDownloading() {
glib.IdleAdd(func() {
img := w.GladeWidget("downloadingIMG").(*gtk.Image)
resources.UpdateImageFromResource(img, "ui/statusbar/finished")
img.SetTooltipText("Indexing documents")
img.Show()
})
}
func (w *MainWindow) StartIndexing() {
glib.IdleAdd(func() {
img := w.GladeWidget("indexingIMG").(*gtk.Image)
resources.UpdateImageFromResource(img, "status-indexing")
img.SetTooltipText("Indexing documents")
img.Show()
})
}
func (w *MainWindow) StopIndexing() {
glib.IdleAdd(func() {
img := w.GladeWidget("indexingIMG").(*gtk.Image)
resources.UpdateImageFromResource(img, "ui/statusbar/indexed")
img.SetTooltipText("Index ready")
})
}
func (w *MainWindow) Name() string {
return "File List observer"
}
func (w *MainWindow) NotifyCallback(evt downloader.DownloadEvent) {
switch evt.Type {
case downloader.EventStart:
w.StartDownloading()
w.SetStatus("Downloading files...")
case downloader.EventError:
case downloader.EventQueueEmpty:
w.StopDownloading()
}
}