cchat-gtk/internal/ui/config/preferences/preferences.go

92 lines
1.8 KiB
Go
Raw Normal View History

2020-06-20 04:40:34 +00:00
package preferences
import (
"github.com/diamondburned/cchat-gtk/internal/log"
2020-06-20 04:40:34 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/config"
"github.com/diamondburned/cchat-gtk/internal/ui/dialog"
2020-06-29 02:55:13 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-06-20 04:40:34 +00:00
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
2020-06-20 04:40:34 +00:00
)
type Dialog struct {
*dialog.Dialog
2020-06-20 04:40:34 +00:00
switcher *gtk.StackSwitcher
stack *gtk.Stack
}
func NewDialog() *Dialog {
stack, _ := gtk.StackNew()
stack.SetMarginTop(8)
stack.SetMarginBottom(8)
stack.SetMarginStart(16)
stack.SetMarginEnd(16)
2020-06-20 04:40:34 +00:00
stack.Show()
switcher, _ := gtk.StackSwitcherNew()
switcher.SetStack(stack)
switcher.Show()
h, _ := gtk.HeaderBarNew()
h.SetShowCloseButton(true)
h.SetCustomTitle(switcher)
h.Show()
d := dialog.NewCSD(stack, h)
2020-06-20 04:40:34 +00:00
d.SetDefaultSize(400, 300)
d.SetTitle("Preferences")
return &Dialog{
Dialog: d,
stack: stack,
switcher: switcher,
}
}
func Section(entries []config.Entry) *gtk.Grid {
var grid, _ = gtk.GridNew()
for i, entry := range entries {
l, _ := gtk.LabelNew(entry.Name)
l.SetHExpand(true)
l.SetXAlign(0)
l.Show()
grid.Attach(l, 0, i, 1, 1)
grid.Attach(entry.Value.Construct(), 1, i, 1, 1)
}
grid.SetRowSpacing(4)
2020-06-29 01:38:09 +00:00
grid.SetColumnSpacing(8)
2020-06-20 04:40:34 +00:00
grid.Show()
2020-06-29 02:55:13 +00:00
primitives.AddClass(grid, "config")
2020-06-20 04:40:34 +00:00
return grid
}
func NewPreferenceDialog() *Dialog {
var dialog = NewDialog()
2020-06-28 23:01:08 +00:00
for i, section := range config.Sections() {
2020-06-20 04:40:34 +00:00
grid := Section(section)
name := config.Section(i).String()
dialog.stack.AddTitled(grid, name, name)
}
return dialog
}
func SpawnPreferenceDialog() {
p := NewPreferenceDialog()
p.Connect("destroy", func(interface{}) {
// On close, save the settings.
if err := config.Save(); err != nil {
log.Error(errors.Wrap(err, "Failed to save settings"))
}
})
p.Show()
2020-06-20 04:40:34 +00:00
}