1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-15 18:52:45 +00:00
cchat-gtk/internal/ui/config/widgets.go

152 lines
2.8 KiB
Go
Raw Normal View History

2020-06-20 04:40:34 +00:00
package config
import (
"encoding/json"
"github.com/gotk3/gotk3/gtk"
)
// EntryValue with JSON serde capabilities.
type EntryValue interface {
json.Marshaler
json.Unmarshaler
Construct() gtk.IWidget
}
type _combo struct {
selected *int
options []string
change func(int)
}
func Combo(selected *int, options []string, change func(int)) EntryValue {
return &_combo{selected, options, change}
}
2020-06-29 01:38:09 +00:00
func (c *_combo) set(v int) {
*c.selected = v
if c.change != nil {
c.change(v)
}
}
2020-06-20 04:40:34 +00:00
func (c *_combo) Construct() gtk.IWidget {
var combo, _ = gtk.ComboBoxTextNew()
for _, opt := range c.options {
combo.Append(opt, opt)
}
combo.Connect("changed", func(combo *gtk.ComboBoxText) { c.set(combo.GetActive()) })
2020-06-20 04:40:34 +00:00
combo.SetActive(*c.selected)
combo.SetHAlign(gtk.ALIGN_END)
combo.Show()
return combo
}
func (c *_combo) MarshalJSON() ([]byte, error) {
return json.Marshal(*c.selected)
}
func (c *_combo) UnmarshalJSON(b []byte) error {
var value int
if err := json.Unmarshal(b, &value); err != nil {
return err
}
*c.selected = value
return nil
}
type _switch struct {
value *bool
change func(bool)
}
func Switch(value *bool, change func(bool)) EntryValue {
return &_switch{value, change}
}
2020-06-29 01:38:09 +00:00
func (s *_switch) set(v bool) {
*s.value = v
if s.change != nil {
s.change(v)
}
}
2020-06-20 04:40:34 +00:00
func (s *_switch) Construct() gtk.IWidget {
sw, _ := gtk.SwitchNew()
sw.SetActive(*s.value)
sw.Connect("notify::active", func(sw *gtk.Switch) { s.set(sw.GetActive()) })
2020-06-20 04:40:34 +00:00
sw.SetHAlign(gtk.ALIGN_END)
sw.Show()
return sw
}
func (s *_switch) MarshalJSON() ([]byte, error) {
return json.Marshal(*s.value)
}
func (s *_switch) UnmarshalJSON(b []byte) error {
var value bool
if err := json.Unmarshal(b, &value); err != nil {
return err
}
2020-06-29 01:38:09 +00:00
s.set(value)
2020-06-20 04:40:34 +00:00
return nil
}
2020-06-28 23:01:08 +00:00
type _inputentry struct {
value *string
change func(string) error
}
func InputEntry(value *string, change func(string) error) EntryValue {
return &_inputentry{value, change}
}
2020-06-29 01:38:09 +00:00
func (e *_inputentry) set(v string) error {
*e.value = v
if e.change != nil {
return e.change(v)
}
return nil
}
2020-06-28 23:01:08 +00:00
func (e *_inputentry) Construct() gtk.IWidget {
entry, _ := gtk.EntryNew()
entry.SetHExpand(true)
entry.SetText(*e.value)
entry.Connect("changed", func(entry *gtk.Entry) {
2020-06-28 23:01:08 +00:00
v, err := entry.GetText()
if err != nil {
return
}
2020-06-29 01:38:09 +00:00
if err := e.set(v); err != nil {
entry.SetIconFromIconName(gtk.ENTRY_ICON_SECONDARY, "dialog-error")
entry.SetIconTooltipText(gtk.ENTRY_ICON_SECONDARY, err.Error())
} else {
entry.RemoveIcon(gtk.ENTRY_ICON_SECONDARY)
2020-06-28 23:01:08 +00:00
}
})
entry.Show()
return entry
}
func (e *_inputentry) MarshalJSON() ([]byte, error) {
return json.Marshal(*e.value)
}
func (e *_inputentry) UnmarshalJSON(b []byte) error {
var value string
if err := json.Unmarshal(b, &value); err != nil {
return err
}
2020-06-29 01:38:09 +00:00
e.set(value)
2020-06-28 23:01:08 +00:00
return nil
}