Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/gtkbuildergen: add menu support #21

Merged
merged 3 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/gtkbuildergen/menu.tmpl
@@ -0,0 +1,19 @@
{{- $id := .ID -}}

var {{$id}} = gio.NewMenu()

func init() {
{{range $i, $_ := .Sections -}}
{{- $sid := printf "s%v" $i -}}

{{$sid}} := gio.NewMenu()
{{range $i, $_ := .Items -}}
{{- $iid := printf "%vi%v" $sid $i -}}

{{$iid}} := gio.NewMenuItem({{.AttrByName "label" | printf "%q"}}, {{.AttrByName "action" | printf "%q"}})
{{$sid}}.AppendItem({{$iid}})
{{end}}

{{$id}}.AppendSection("", {{$sid}})
{{end -}}
}
11 changes: 9 additions & 2 deletions cmd/gtkbuildergen/output.tmpl
Expand Up @@ -5,11 +5,18 @@ package {{.Package}}
import (
{{range .UI | requires -}}
{{.Import | printf "%q"}}
{{end}}
{{end -}}
{{if .UI | hasMenus -}}
"github.com/diamondburned/gotk4/pkg/gio/v2"
{{end -}}
)

{{range .UI -}}
{{range .Templates -}}
{{template "template.tmpl" .}}
{{end}}
{{end -}}

{{range .Menus -}}
{{template "menu.tmpl" .}}
{{end -}}
{{end}}
8 changes: 8 additions & 0 deletions cmd/gtkbuildergen/tmpl.go
Expand Up @@ -20,6 +20,14 @@ func init() {
// TODO: Return better values.
return ui[0].Requires
},
"hasMenus": func(ui []Interface) bool {
for _, i := range ui {
if len(i.Menus) != 0 {
return true
}
}
return false
},
})
tmpl = template.Must(tmpl.ParseFS(tmplFS, "*.tmpl"))
}
30 changes: 30 additions & 0 deletions cmd/gtkbuildergen/uidef.go
Expand Up @@ -145,6 +145,36 @@ type Menu struct {
XMLName xml.Name `xml:"menu"`

ID string `xml:"id,attr"`

Sections []Section `xml:"section"`
}

type Section struct {
XMLName xml.Name `xml:"section"`

Items []Item `xml:"item"`
}

type Item struct {
XMLName xml.Name `xml:"item"`

Attributes []Attribute `xml:"attribute"`
}

func (item Item) AttrByName(name string) string {
for _, attr := range item.Attributes {
if attr.Name == name {
return attr.Value
}
}
return ""
}

type Attribute struct {
XMLName xml.Name `xml:"attribute"`

Name string `xml:"name,attr"`
Value string `xml:",chardata"`
}

type Class string
Expand Down
11 changes: 2 additions & 9 deletions cmd/trayscale/app.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
_ "embed"
"log"
"net/netip"
"os"
Expand All @@ -24,10 +23,7 @@ import (
"tailscale.com/types/key"
)

//go:generate go run deedles.dev/trayscale/cmd/gtkbuildergen -out ui.go mainwindow.ui peerpage.ui

//go:embed menu.ui
var menuXML string
//go:generate go run deedles.dev/trayscale/cmd/gtkbuildergen -out ui.go mainwindow.ui peerpage.ui menu.ui

// App is the main type for the app, containing all of the state
// necessary to run it.
Expand Down Expand Up @@ -275,13 +271,10 @@ func (a *App) init(ctx context.Context) {
a.statusPage.SetIconName("network-offline-symbolic")
a.statusPage.SetDescription("Tailscale is not connected")

builder := gtk.NewBuilder()
builder.AddFromString(menuXML, len(menuXML))

a.win = NewMainWindow(&a.app.Application)

// Workaround for Cambalache limitations.
a.win.MainMenuButton.SetMenuModel(builder.GetObject("MainMenu").Cast().(gio.MenuModeller))
a.win.MainMenuButton.SetMenuModel(MainMenu)

a.win.StatusSwitch.ConnectStateSet(func(s bool) bool {
if s == a.win.StatusSwitch.State() {
Expand Down
13 changes: 13 additions & 0 deletions cmd/trayscale/ui.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.