1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-12-23 20:56:42 +00:00
cchat-gtk/internal/ui/dialog/dialog.go

83 lines
1.8 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package dialog
import (
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-05-26 06:51:06 +00:00
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
2020-07-10 23:26:07 +00:00
type Modal struct {
*gtk.Dialog
Cancel *gtk.Button
Action *gtk.Button
Header *gtk.HeaderBar
}
var headerCSS = primitives.PrepareCSS(`
.modal-header {
padding: 0 5px;
}
`)
2020-07-10 23:26:07 +00:00
func ShowModal(body gtk.IWidget, title, button string, clicked func(m *Modal)) {
NewModal(body, title, title, clicked).Show()
}
func NewModal(body gtk.IWidget, title, button string, clicked func(m *Modal)) *Modal {
cancel, _ := gtk.ButtonNewWithMnemonic("_Cancel")
2020-05-26 06:51:06 +00:00
cancel.Show()
cancel.SetHAlign(gtk.ALIGN_START)
cancel.SetRelief(gtk.RELIEF_NONE)
2020-07-10 23:26:07 +00:00
action, _ := gtk.ButtonNewWithMnemonic(button)
2020-05-26 06:51:06 +00:00
action.Show()
action.SetHAlign(gtk.ALIGN_END)
action.SetRelief(gtk.RELIEF_NONE)
header, _ := gtk.HeaderBarNew()
header.Show()
header.SetTitle(title)
header.PackStart(cancel)
header.PackEnd(action)
primitives.AddClass(header, "modal-header")
primitives.AttachCSS(header, headerCSS)
2020-05-26 06:51:06 +00:00
dialog := newCSD(body, header)
2020-07-10 23:26:07 +00:00
modald := &Modal{
dialog,
cancel,
action,
header,
}
2020-05-26 06:51:06 +00:00
cancel.Connect("clicked", dialog.Destroy)
2020-07-10 23:26:07 +00:00
action.Connect("clicked", func() { clicked(modald) })
2020-05-26 06:51:06 +00:00
2020-07-10 23:26:07 +00:00
return modald
2020-05-26 06:51:06 +00:00
}
func NewCSD(body, header gtk.IWidget) *gtk.Dialog {
dialog := newCSD(body, header)
dialog.Connect("response", func(_ *glib.Object, resp gtk.ResponseType) {
if resp == gtk.RESPONSE_DELETE_EVENT {
dialog.Destroy()
}
})
return dialog
}
func newCSD(body, header gtk.IWidget) *gtk.Dialog {
dialog, _ := gts.NewEmptyModalDialog()
dialog.SetDefaultSize(450, 300)
dialog.Add(body)
2020-05-26 06:51:06 +00:00
if oldh, _ := dialog.GetHeaderBar(); oldh != nil {
dialog.Remove(oldh)
}
dialog.SetTitlebar(header)
return dialog
}