forked from tailscale/tailscale
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/systray: add a basic linux systray app
This adds a systray app for linux, similar to the apps for macOS and windows. There are already a number of community-developed systray apps, but most of them are either long abandoned, are built for a specific desktop environment, or simply wrap the tailscale CLI. This uses fyne.io/systray (a fork of github.com/getlantern/systray) which uses newer D-Bus specifications to render the tray icon and menu. This results in a pretty broad support for modern desktop environments. This initial commit lacks a number of features like profile switching, device listing, and exit node selection. This is really focused on the application structure, the interaction with LocalAPI, and some system integration pieces like the app icon, notifications, and the clipboard. Updates tailscale#1708 Signed-off-by: Will Norris <will@tailscale.com>
- Loading branch information
1 parent
6280c44
commit 3c66ee3
Showing
5 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# systray | ||
|
||
The systray command is a minimal Tailscale systray application for Linux. | ||
It is designed to provide quick access to common operations like profile switching | ||
and exit node selection. | ||
|
||
## Supported platforms | ||
|
||
The `fyne.io/systray` package we use supports Windows, macOS, Linux, and many BSDs, | ||
so the systray application will likely work for the most part on those platforms. | ||
Notifications currently only work on Linux, as that is the main target. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
// Copyright (c) Tailscale Inc & AUTHORS | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
//go:build cgo || !darwin | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"image/color" | ||
"image/png" | ||
"sync" | ||
"time" | ||
|
||
"fyne.io/systray" | ||
"github.com/fogleman/gg" | ||
) | ||
|
||
// tsLogo represents the state of the 3x3 dot grid in the Tailscale logo. | ||
// A 0 represents a gray dot, any other value is a white dot. | ||
type tsLogo [9]byte | ||
|
||
var ( | ||
// disconnected is all gray dots | ||
disconnected = tsLogo{ | ||
0, 0, 0, | ||
0, 0, 0, | ||
0, 0, 0, | ||
} | ||
|
||
// connected is the normal Tailscale logo | ||
connected = tsLogo{ | ||
0, 0, 0, | ||
1, 1, 1, | ||
0, 1, 0, | ||
} | ||
|
||
// loading is a special tsLogo value that is not meant to be rendered directly, | ||
// but indicates that the loading animation should be shown. | ||
loading = tsLogo{'l', 'o', 'a', 'd', 'i', 'n', 'g'} | ||
|
||
// loadingIcons are shown in sequence as an animated loading icon. | ||
loadingLogos = []tsLogo{ | ||
{ | ||
0, 1, 1, | ||
1, 0, 1, | ||
0, 0, 1, | ||
}, | ||
{ | ||
0, 1, 1, | ||
0, 0, 1, | ||
0, 1, 0, | ||
}, | ||
{ | ||
0, 1, 1, | ||
0, 0, 0, | ||
0, 0, 1, | ||
}, | ||
{ | ||
0, 0, 1, | ||
0, 1, 0, | ||
0, 0, 0, | ||
}, | ||
{ | ||
0, 1, 0, | ||
0, 0, 0, | ||
0, 0, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
0, 0, 1, | ||
0, 0, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
0, 0, 0, | ||
0, 0, 0, | ||
}, | ||
{ | ||
0, 0, 1, | ||
0, 0, 0, | ||
0, 0, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
0, 0, 0, | ||
1, 0, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
0, 0, 0, | ||
1, 1, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
1, 0, 0, | ||
1, 1, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
1, 1, 0, | ||
0, 1, 0, | ||
}, | ||
{ | ||
0, 0, 0, | ||
1, 1, 0, | ||
0, 1, 1, | ||
}, | ||
{ | ||
0, 0, 0, | ||
1, 1, 1, | ||
0, 0, 1, | ||
}, | ||
{ | ||
0, 1, 0, | ||
0, 1, 1, | ||
1, 0, 1, | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
black = color.NRGBA{0, 0, 0, 255} | ||
white = color.NRGBA{255, 255, 255, 255} | ||
gray = color.NRGBA{255, 255, 255, 102} | ||
) | ||
|
||
// render returns a PNG image of the logo. | ||
func (logo tsLogo) render() *bytes.Buffer { | ||
const radius = 25 | ||
const borderUnits = 1 | ||
dim := radius * (8 + borderUnits*2) | ||
|
||
dc := gg.NewContext(dim, dim) | ||
dc.DrawRectangle(0, 0, float64(dim), float64(dim)) | ||
dc.SetColor(black) | ||
dc.Fill() | ||
|
||
for y := 0; y < 3; y++ { | ||
for x := 0; x < 3; x++ { | ||
px := (borderUnits + 1 + 3*x) * radius | ||
py := (borderUnits + 1 + 3*y) * radius | ||
col := white | ||
if logo[y*3+x] == 0 { | ||
col = gray | ||
} | ||
dc.DrawCircle(float64(px), float64(py), radius) | ||
dc.SetColor(col) | ||
dc.Fill() | ||
} | ||
} | ||
|
||
b := bytes.NewBuffer(nil) | ||
png.Encode(b, dc.Image()) | ||
return b | ||
} | ||
|
||
// setAppIcon renders logo and sets it as the systray icon. | ||
func setAppIcon(icon tsLogo) { | ||
if icon == loading { | ||
startLoadingAnimation() | ||
} else { | ||
stopLoadingAnimation() | ||
systray.SetIcon(icon.render().Bytes()) | ||
} | ||
} | ||
|
||
var ( | ||
loadingMu sync.Mutex // protects loadingCancel | ||
|
||
// loadingCancel stops the loading animation in the systray icon. | ||
// This is nil if the animation is not currently active. | ||
loadingCancel func() | ||
) | ||
|
||
// startLoadingAnimation starts the animated loading icon in the system tray. | ||
// The animation continues until [stopLoadingAnimation] is called. | ||
// If the loading animation is already active, this func does nothing. | ||
func startLoadingAnimation() { | ||
loadingMu.Lock() | ||
defer loadingMu.Unlock() | ||
|
||
if loadingCancel != nil { | ||
// loading icon already displayed | ||
return | ||
} | ||
|
||
ctx := context.Background() | ||
ctx, loadingCancel = context.WithCancel(ctx) | ||
|
||
go func() { | ||
t := time.NewTicker(500 * time.Millisecond) | ||
var i int | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-t.C: | ||
systray.SetIcon(loadingLogos[i].render().Bytes()) | ||
i++ | ||
if i >= len(loadingLogos) { | ||
i = 0 | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// stopLoadingAnimation stops the animated loading icon in the system tray. | ||
// If the loading animation is not currently active, this func does nothing. | ||
func stopLoadingAnimation() { | ||
loadingMu.Lock() | ||
defer loadingMu.Unlock() | ||
|
||
if loadingCancel != nil { | ||
loadingCancel() | ||
loadingCancel = nil | ||
} | ||
} |
Oops, something went wrong.