2020-05-26 06:51:06 +00:00
|
|
|
package dialog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
2020-07-14 07:24:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
2020-10-28 06:04:00 +00:00
|
|
|
type Dialog = gtk.Dialog
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
type Modal struct {
|
2020-10-28 06:04:00 +00:00
|
|
|
*Dialog
|
2020-07-10 23:26:07 +00:00
|
|
|
Cancel *gtk.Button
|
|
|
|
Action *gtk.Button
|
|
|
|
Header *gtk.HeaderBar
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
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.SetHAlign(gtk.ALIGN_START)
|
|
|
|
cancel.SetRelief(gtk.RELIEF_NONE)
|
2020-10-28 06:04:00 +00:00
|
|
|
cancel.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
action, _ := gtk.ButtonNewWithMnemonic(button)
|
2020-05-26 06:51:06 +00:00
|
|
|
action.SetHAlign(gtk.ALIGN_END)
|
|
|
|
action.SetRelief(gtk.RELIEF_NONE)
|
2020-10-28 06:04:00 +00:00
|
|
|
action.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
header, _ := gtk.HeaderBarNew()
|
|
|
|
header.SetTitle(title)
|
|
|
|
header.PackStart(cancel)
|
|
|
|
header.PackEnd(action)
|
2020-10-28 06:04:00 +00:00
|
|
|
header.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
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
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
cancel.Connect("clicked", func(interface{}) { dialog.Destroy() })
|
|
|
|
action.Connect("clicked", func(interface{}) { 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)
|
2020-10-28 06:04:00 +00:00
|
|
|
dialog.Connect("response", func(_ *gtk.Dialog, resp gtk.ResponseType) {
|
2020-05-26 06:51:06 +00:00
|
|
|
if resp == gtk.RESPONSE_DELETE_EVENT {
|
|
|
|
dialog.Destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCSD(body, header gtk.IWidget) *gtk.Dialog {
|
2020-10-28 06:04:00 +00:00
|
|
|
dialog, err := gts.NewEmptyModalDialog()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-08 09:07:00 +00:00
|
|
|
dialog.SetDefaultSize(450, 300)
|
|
|
|
dialog.Add(body)
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
if oldh, _ := dialog.GetHeaderBar(); oldh != nil {
|
2020-12-30 06:30:41 +00:00
|
|
|
oldh.ToWidget().Destroy()
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
dialog.SetTitlebar(header)
|
|
|
|
|
|
|
|
return dialog
|
|
|
|
}
|