forked from UnnoTed/wireguird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·154 lines (123 loc) · 3.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//go:generate go run ./gui/get/generator/generator.go -target=./gui/get
//go:generate go run github.com/UnnoTed/fileb0x fileb0x.toml
package main
import (
"os"
"github.com/UnnoTed/go-appindicator"
"github.com/UnnoTed/horizontal"
"github.com/UnnoTed/wireguird/gui"
"github.com/UnnoTed/wireguird/static"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/rs/zerolog/log"
)
var win *gtk.ApplicationWindow
func main() {
log.Logger = log.Output(horizontal.ConsoleWriter{Out: os.Stderr})
log.Info().Uint("major", gtk.GetMajorVersion()).Uint("minor", gtk.GetMinorVersion()).Uint("micro", gtk.GetMicroVersion()).Msg("GTK Version")
if err := gui.Settings.Load(); err != nil {
log.Error().Err(err).Msg("error initial settings load")
}
const appID = "com.wireguard.desktop"
application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
if err != nil {
log.Error().Err(err).Msg("error creating application")
return
}
application.Connect("activate", func() {
if err := createWindow(application); err != nil {
log.Error().Err(err).Msg("create window error")
}
})
os.Exit(application.Run(os.Args))
}
func createTray(application *gtk.Application) (*appindicator.Indicator, error) {
menu, err := gtk.MenuNew()
if err != nil {
return nil, err
}
menuShow, err := gtk.MenuItemNewWithLabel("Show")
if err != nil {
return nil, err
}
menuQuit, err := gtk.MenuItemNewWithLabel("Quit")
if err != nil {
return nil, err
}
indicator := appindicator.New(application.GetApplicationID(), "wireguard_off", appindicator.CategoryApplicationStatus)
indicator.SetIconThemePath("/opt/wireguird/Icon")
indicator.SetTitle("Wireguird")
// indicator.SetLabel("Wireguird", "")
indicator.SetStatus(appindicator.StatusActive)
indicator.SetMenu(menu)
menuShow.Connect("activate", func() {
win.Show()
// createWindow(application)
})
if err != nil {
return nil, err
}
menuQuit.Connect("activate", func() {
application.Quit()
})
menu.Add(menuShow)
menu.Add(menuQuit)
menu.ShowAll()
return indicator, nil
}
func createWindow(application *gtk.Application) error {
data, err := static.ReadFile("wireguird.glade")
if err != nil {
log.Error().Err(err).Msg("cant read wireguird.glade")
return err
}
b, err := gtk.BuilderNew()
b.AddFromString(string(data))
if err != nil {
log.Error().Err(err).Msg("error getting main glade file")
return err
}
wobj, err := b.GetObject("main_window")
if err != nil {
log.Error().Err(err).Msg("error getting main_window")
return err
}
var ok bool
win, ok = wobj.(*gtk.ApplicationWindow)
if !ok {
panic("not window")
}
application.AddWindow(win)
win.Connect("destroy", func() {
gtk.MainQuit()
})
// win.SetDecorated(false)
css, err := gtk.CssProviderNew()
if err != nil {
log.Error().Err(err).Msg("error creating css provider")
return err
}
css.LoadFromData(`
`)
// css.LoadFromPath("./style.css")
screen, err := gdk.ScreenGetDefault()
if err != nil {
log.Error().Err(err).Msg("error getting screen")
return err
}
gtk.AddProviderForScreen(screen, css, 1)
indicator, err := createTray(application)
if err != nil {
log.Error().Err(err).Msg("create tray error")
}
if err := gui.Create(application, b, win, indicator); err != nil {
log.Error().Err(err).Msg("error gui setup")
return err
}
if !gui.Settings.StartOnTray {
win.ShowAll()
}
win.SetTitle("Wireguird")
return nil
}