Add GTK application implementation
This commit is contained in:
parent
d1bb505e0a
commit
b7c0307f6b
29 changed files with 2564 additions and 266 deletions
151
internal/gui/application.go
Normal file
151
internal/gui/application.go
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
package gui
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/diamondburned/gotk4-adwaita/pkg/adw"
|
||||
"github.com/diamondburned/gotk4/pkg/gdk/v4"
|
||||
"github.com/diamondburned/gotk4/pkg/gio/v2"
|
||||
"github.com/diamondburned/gotk4/pkg/glib/v2"
|
||||
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
||||
"suruatoel.xyz/mcg/data"
|
||||
"suruatoel.xyz/mcg/internal/client"
|
||||
"suruatoel.xyz/mcg/internal/i18n"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
*gtk.Application
|
||||
window *mainWindow
|
||||
aboutDialog *adw.AboutDialog
|
||||
config Configuration
|
||||
configErr error
|
||||
}
|
||||
|
||||
func NewApplication() *Application {
|
||||
app := Application{
|
||||
Application: gtk.NewApplication("xyz.suruatoel.mcg", gio.ApplicationFlagsNone),
|
||||
}
|
||||
app.ConnectStartup(app.onStartup)
|
||||
app.ConnectActivate(app.onActivate)
|
||||
app.ConnectShutdown(app.onShutdown)
|
||||
|
||||
return &app
|
||||
}
|
||||
|
||||
func (a *Application) onStartup() {
|
||||
a.setDefaultSettings()
|
||||
a.loadCSS()
|
||||
a.registerShortcuts()
|
||||
a.registerActions()
|
||||
a.loadConfig()
|
||||
}
|
||||
|
||||
func (a *Application) loadConfig() {
|
||||
a.config = Configuration{
|
||||
Host: "localhost",
|
||||
Port: 6600,
|
||||
Panel: "library",
|
||||
SortField: "year",
|
||||
SortDesc: false,
|
||||
}
|
||||
|
||||
a.configErr = readConfig(&a.config)
|
||||
}
|
||||
|
||||
func (a *Application) setDefaultSettings() {
|
||||
styleManager := adw.StyleManagerGetDefault()
|
||||
styleManager.SetColorScheme(adw.ColorSchemePreferDark)
|
||||
}
|
||||
|
||||
func (a *Application) loadCSS() {
|
||||
styleProvider := gtk.NewCSSProvider()
|
||||
styleProvider.LoadFromString(data.MainCSS)
|
||||
|
||||
gtk.StyleContextAddProviderForDisplay(
|
||||
gdk.DisplayGetDefault(),
|
||||
styleProvider,
|
||||
gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
|
||||
)
|
||||
}
|
||||
|
||||
func (a *Application) registerShortcuts() {
|
||||
a.SetAccelsForAction("window.close", []string{"<primary>q"})
|
||||
a.SetAccelsForAction("win.show-help-overlay", []string{"<primary>k"})
|
||||
a.SetAccelsForAction("app.info", []string{"<primary>i"})
|
||||
a.SetAccelsForAction("win.connect", []string{"<primary>c"})
|
||||
a.SetAccelsForAction("win.play", []string{"<primary>p"})
|
||||
a.SetAccelsForAction("win.clear-playlist", []string{"<primary>r"})
|
||||
a.SetAccelsForAction("win.toggle-fullscreen", []string{"F11"})
|
||||
a.SetAccelsForAction("win.search-library", []string{"<primary>f"})
|
||||
a.SetAccelsForAction("win.panel(\"server\")", []string{"<primary>KP_1"})
|
||||
a.SetAccelsForAction("win.panel(\"cover\")", []string{"<primary>KP_2"})
|
||||
a.SetAccelsForAction("win.panel(\"playlist\")", []string{"<primary>KP_3"})
|
||||
a.SetAccelsForAction("win.panel(\"library\")", []string{"<primary>KP_4"})
|
||||
}
|
||||
|
||||
func (a *Application) registerActions() {
|
||||
infoAction := gio.NewSimpleAction("info", nil)
|
||||
infoAction.ConnectActivate(func(_ *glib.Variant) {
|
||||
a.showAboutDialog()
|
||||
})
|
||||
a.AddAction(infoAction)
|
||||
}
|
||||
|
||||
func (a *Application) showAboutDialog() {
|
||||
if a.aboutDialog == nil {
|
||||
a.aboutDialog = a.newAboutDialog()
|
||||
}
|
||||
|
||||
a.aboutDialog.Present(a.window)
|
||||
}
|
||||
|
||||
func (a *Application) newAboutDialog() *adw.AboutDialog {
|
||||
dialog := adw.NewAboutDialog()
|
||||
dialog.SetApplicationName("CoverGrid")
|
||||
dialog.SetApplicationIcon("xyz.suruatoel.mcg")
|
||||
dialog.SetVersion(a.Version())
|
||||
dialog.SetComments(i18n.T("CoverGrid is a client for the Music Player Daemon, focusing on albums instead of single tracks."))
|
||||
dialog.SetWebsite("https://www.suruatoel.xyz/codes/mcg")
|
||||
dialog.SetLicenseType(gtk.LicenseGPL30)
|
||||
dialog.SetIssueURL("https://git.suruatoel.xyz/coderkun/mcg")
|
||||
|
||||
return dialog
|
||||
}
|
||||
|
||||
func (a *Application) onActivate() {
|
||||
if a.window == nil {
|
||||
a.window = newMainWindow(a.Application, a.config)
|
||||
if a.configErr != nil && !os.IsNotExist(a.configErr) {
|
||||
a.window.SetConfigError(a.configErr)
|
||||
}
|
||||
a.window.ConnectCloseRequest(a.onCloseRequest)
|
||||
}
|
||||
|
||||
a.window.Present()
|
||||
if a.config.IsConnected {
|
||||
client.Instance().Connect(a.config.Host, int(a.config.Port), a.config.Password)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Application) onCloseRequest() bool {
|
||||
a.window.UpdateConfig(&a.config)
|
||||
a.config.IsConnected = client.Instance().IsConnected()
|
||||
|
||||
a.saveConfig()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *Application) saveConfig() {
|
||||
err := writeConfig(&a.config)
|
||||
if err != nil {
|
||||
log.Println("failed to save configuration file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Application) onShutdown() {
|
||||
if client.Instance().IsConnected() {
|
||||
client.Instance().Disconnect()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue