Skip to content
This repository has been archived by the owner. It is now read-only.
[WIP!] a cross platfrom Go library to work with the notification area / system tray
Go Objective-C C Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
cmd/filetobytes
example
.gitignore
LICENSE
README.md
go.mod
go.sum
systray.go
systray_darwin.go
systray_darwin.h
systray_darwin.m
systray_linux.c
systray_linux.go
systray_linux.h
systray_test.go
systray_windows.go
systray_windows_test.go

README.md

license GoDoc Coverage Status Build Status Go Report Card Release

WIP! WORK IN PROGRESS! Can be unstable!

I tries to fix multilevel menu on windows. Currently work only 2 levels.

About

Package systray is a cross platfrom Go library to place an icon and menu in the notification area. Tested on Windows 8, 10, Mac OSX, Ubuntu 14.10 and Debian 7.6.

Difference with getlantern/systray

  • [All OS] multi-level systray menu (tested only on windows)
  • [Windows] system tray code optimizations (constants)
  • [Windows] left click custom handling
  • [Windows] right click custom handling
  • [Windows] left double click custom handling

Usage

package main

import (
	"fmt"
	"io/ioutil"
	"strconv"
	"time"

	"github.com/riftbit/go-systray"
)

var (
	timezone string
)

func main() {
	//systray.SetCustomLeftClickAction()
	//systray.SetCustomRightClickAction()
	systray.Run(onReady, onExit)
}

func onReady() {
	timezone = "Local"
	systray.SetIcon(getIcon("assets/icon.ico"))

	submenu := systray.AddSubMenu("SubMenu")
	_ = submenu.AddSubMenuItem("Start", "", 0)
	_ = submenu.AddSubMenuItem("Stop", "", 0)

	localTime := systray.AddMenuItem("Local time", "Local time", 0)
	hcmcTime := systray.AddMenuItem("Ho Chi Minh time", "Asia/Ho_Chi_Minh", 0)
	sydTime := systray.AddMenuItem("Sydney time", "Australia/Sydney", 0)
	gdlTime := systray.AddMenuItem("Guadalajara time", "America/Mexico_City", 0)
	sfTime := systray.AddMenuItem("San Fransisco time", "America/Los_Angeles", 0)

	systray.AddSeparator()
	mQuit := systray.AddMenuItem("Quit", "Quits this app", 0)

	go func() {
		for {
			systray.SetTitle(getClockTime(timezone))
			systray.SetTooltip(getClockTime(timezone) + " - " + timezone + " timezone")
			time.Sleep(1 * time.Second)
		}
	}()

	go func() {
		for {
			select {
			case <-localTime.OnClickCh():
				timezone = "Local"
			case <-hcmcTime.OnClickCh():
				timezone = "Asia/Ho_Chi_Minh"
			case <-sydTime.OnClickCh():
				timezone = "Australia/Sydney"
			case <-gdlTime.OnClickCh():
				timezone = "America/Mexico_City"
			case <-sfTime.OnClickCh():
				timezone = "America/Los_Angeles"
			case <-mQuit.OnClickCh():
				systray.Quit()
				return
			}
		}
	}()
}

func onExit() {
	// Cleaning stuff here.
}

func getClockTime(tz string) string {
	t := time.Now()
	utc, _ := time.LoadLocation(tz)

	hour, min, sec := t.In(utc).Clock()
	return ItoaTwoDigits(hour) + ":" + ItoaTwoDigits(min) + ":" + ItoaTwoDigits(sec)
}

// ItoaTwoDigits time.Clock returns one digit on values, so we make sure to convert to two digits
func ItoaTwoDigits(i int) string {
	b := "0" + strconv.Itoa(i)
	return b[len(b)-2:]
}

func getIcon(s string) []byte {
	b, err := ioutil.ReadFile(s)
	if err != nil {
		fmt.Print(err)
	}
	return b
}

Menu item can be checked and / or disabled. Methods except Run() can be invoked from any goroutine. See demo code under example folder.

Platform specific concerns

Linux

sudo apt-get install libgtk-3-dev libappindicator3-dev

Checked menu item not implemented on Linux yet.

Try

Under example folder. Place tray icon under icon, and use make_icon.bat or make_icon.sh, whichever suit for your os, to convert the icon to byte array. Your icon should be .ico file under Windows, whereas .ico, .jpg and .png is supported on other platform.

go get
go run main.go

Building and the Console Window

By default, the binary created by go build will cause a console window to be opened on both Windows and macOS when run.

Windows

To prevent launching a console window when running on Windows, add these command-line build flags:

go build -ldflags -H=windowsgui

macOS

On macOS, you will need to create an application bundle to wrap the binary; simply folders with the following minimal structure and assets:

SystrayApp.app/
  Contents/
    Info.plist
    MacOS/
      go-executable
    Resources/
      SystrayApp.icns

Consult the Official Apple Documentation here.

Credits

Additional interesting packages

You can’t perform that action at this time.