-
Notifications
You must be signed in to change notification settings - Fork 15
/
trayscale.go
94 lines (80 loc) · 1.82 KB
/
trayscale.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
package main
import (
"context"
"io"
"os"
"os/signal"
"strings"
"time"
"deedles.dev/trayscale"
"deedles.dev/trayscale/tailscale"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"tailscale.com/ipn/ipnstate"
)
const (
appID = "dev.deedles-trayscale"
prefShowWindowAtStart = "showWindowAtStart"
)
func formatTime(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.StampMilli)
}
// must returns v if err is nil. If err is not nil, it panics with
// err's value.
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
// readAssetString returns the contents of the given embedded asset as
// a string. It panics if there are any errors.
func readAssetString(file string) string {
var str strings.Builder
f := must(trayscale.Assets().Open(file))
must(io.Copy(&str, f))
return str.String()
}
// withWidget gets the widget with the given name from b, asserts it
// to T, and then calls f with it.
func withWidget[T glib.Objector](b *gtk.Builder, name string, f func(T)) {
w := b.GetObject(name).Cast().(T)
f(w)
}
func peerName(peer *ipnstate.PeerStatus) string {
const maxNameLength = 30
name := peer.HostName
if len(name) > maxNameLength {
name = name[:maxNameLength-3] + "..."
}
if peer.ExitNode {
return name + " [Exit node]"
}
if peer.ExitNodeOption {
return name + " [Exit node option]"
}
return name
}
func peerIcon(peer *ipnstate.PeerStatus) string {
if peer.ExitNode {
return "network-workgroup-symbolic"
}
if peer.ExitNodeOption {
return "network-server-symbolic"
}
return "folder-remote-symbolic"
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
ts := tailscale.Client{
Command: "tailscale",
}
a := App{
TS: &ts,
}
a.Run(ctx)
}