1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-12-23 12:46:45 +00:00
cchat-gtk/internal/ui/header.go

153 lines
3 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package ui
2020-06-06 00:47:28 +00:00
import (
"html"
"strings"
"github.com/diamondburned/cchat-gtk/icons"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-07-16 05:41:21 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/actions"
2020-06-06 00:47:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/breadcrumb"
2020-07-16 05:41:21 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/session"
"github.com/gotk3/gotk3/glib"
2020-06-06 00:47:28 +00:00
"github.com/gotk3/gotk3/gtk"
)
2020-05-26 06:51:06 +00:00
type header struct {
*gtk.Box
2020-07-16 05:41:21 +00:00
left *headerLeft // middle-ish
2020-06-06 00:47:28 +00:00
right *headerRight
menu *glib.Menu
2020-05-26 06:51:06 +00:00
}
func newHeader() *header {
2020-06-20 04:40:34 +00:00
left := newHeaderLeft()
2020-06-06 00:47:28 +00:00
left.Show()
right := newHeaderRight()
right.Show()
separator, _ := gtk.SeparatorNew(gtk.ORIENTATION_VERTICAL)
separator.Show()
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
box.PackStart(left, false, false, 0)
box.PackStart(separator, false, false, 0)
box.PackStart(right, true, true, 0)
2020-05-26 06:51:06 +00:00
box.Show()
2020-06-06 00:47:28 +00:00
menu := glib.MenuNew()
menu.Append("Preferences", "app.preferences")
menu.Append("Quit", "app.quit")
left.appmenu.SetMenuModel(&menu.MenuModel)
2020-05-26 06:51:06 +00:00
// TODO
2020-06-06 00:47:28 +00:00
return &header{
box,
left,
right,
menu,
2020-06-06 00:47:28 +00:00
}
}
const BreadcrumbSlash = `<span rise="-1024" size="x-large">/</span>`
2020-06-06 00:47:28 +00:00
2020-07-16 05:41:21 +00:00
func (h *header) SetBreadcrumber(b breadcrumb.Breadcrumber) {
if b == nil {
h.right.breadcrumb.SetText("")
return
}
var crumb = b.Breadcrumb()
for i := range crumb {
crumb[i] = html.EscapeString(crumb[i])
2020-06-06 00:47:28 +00:00
}
h.right.breadcrumb.SetMarkup(
2020-07-16 05:41:21 +00:00
BreadcrumbSlash + " " + strings.Join(crumb, " "+BreadcrumbSlash+" "),
2020-06-06 00:47:28 +00:00
)
}
2020-07-16 05:41:21 +00:00
func (h *header) SetSessionMenu(s *session.Row) {
h.left.sesmenu.Bind(s.ActionsMenu)
}
type appMenu struct {
*gtk.MenuButton
}
func newAppMenu() *appMenu {
img, _ := gtk.ImageNew()
img.SetFromPixbuf(icons.Logo256(24))
img.Show()
appmenu, _ := gtk.MenuButtonNew()
appmenu.SetImage(img)
appmenu.SetUsePopover(true)
appmenu.SetHAlign(gtk.ALIGN_CENTER)
appmenu.SetMarginStart(8)
appmenu.SetMarginEnd(8)
return &appMenu{appmenu}
}
func (a *appMenu) SetSizeRequest(w, h int) {
// Subtract the margin size.
if w -= 8 * 2; w < 0 {
w = 0
}
a.MenuButton.SetSizeRequest(w, h)
2020-07-16 05:41:21 +00:00
}
2020-06-20 04:40:34 +00:00
type headerLeft struct {
*gtk.Box
appmenu *appMenu
sesmenu *actions.MenuButton
2020-06-20 04:40:34 +00:00
}
func newHeaderLeft() *headerLeft {
appmenu := newAppMenu()
appmenu.Show()
sep, _ := gtk.SeparatorNew(gtk.ORIENTATION_VERTICAL)
sep.Show()
primitives.AddClass(sep, "titlebutton")
sesmenu := actions.NewMenuButton()
sesmenu.Show()
2020-06-20 04:40:34 +00:00
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
box.PackStart(appmenu, false, false, 0)
box.PackStart(sep, false, false, 0)
box.PackStart(sesmenu, false, false, 5)
2020-06-20 04:40:34 +00:00
return &headerLeft{
Box: box,
appmenu: appmenu,
sesmenu: sesmenu,
2020-06-20 04:40:34 +00:00
}
}
2020-06-06 00:47:28 +00:00
type headerRight struct {
*gtk.Box
breadcrumb *gtk.Label
}
func newHeaderRight() *headerRight {
bc, _ := gtk.LabelNew(BreadcrumbSlash)
bc.SetUseMarkup(true)
bc.SetXAlign(0.0)
bc.Show()
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
box.PackStart(bc, true, true, 14)
box.Show()
return &headerRight{
Box: box,
breadcrumb: bc,
}
2020-05-26 06:51:06 +00:00
}