systray

package module
v0.0.0-...-26ec25a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2018 License: Apache-2.0 Imports: 11 Imported by: 11

README

Package systray is a cross platfrom Go library to place an icon and menu in the notification area. Tested on Windows XP,7,10 and Linux Mint 18.3.

Usage

func main() {
	// Should be called at the very beginning of main().
	systray.Run(onReady, onExit)
}

func onReady() {
	systray.SetIcon(icon.Data)
	systray.SetTitle("Awesome App")
	systray.SetTooltip("Pretty awesome超级棒")
	mQuit := systray.AddMenuItem("Quit", "Quit the whole app", systray.ItemDefault)
}

func onExit() {
	// clean up here
}

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.

Warning: GtkStatusIcon has been deprecated in GTK 3.14!

Prepare icons

You can use filetobyte utility at cmd folder. Two ways:

  1. Do
go get github.com/amkulikov/systray/cmd/filetobytes

and after that use compiled binary directly, or use code generation in your project

//go:generate filetobytes -package=tray -dest=tray/data.go path/to/iconfile/or/iconsfolder
  1. Use go run to execute util.
//go:generate go run github.com/amkulikov/systray/cmd/filetobytes/main.go -package=tray -dest=tray/data.go example/icon

It will generate file data.go in tray folder that will contain

var filesByteData = map[string][]byte{...}

where key is a path to sourcefile and value is a binary representation of resource.

Credits

Documentation

Overview

Package systray is a cross-platform Go library to place an icon and menu in the notification area. Based on https://github.com/getlantern/systray and provides few improvements:

  • Windows system calls instead of custom DLL
  • Caching icon files
  • Allows to set direct path to icon file
  • Error handling

Supports Windows, Mac OSX and Linux currently. Methods can be called from any goroutine except Run(), which should be called at the very beginning of main() to lock at main thread.

Index

Examples

Constants

View Source
const (
	ItemDefault        = 0
	ItemSeparator byte = 1 << iota
	ItemChecked
	ItemCheckable
	ItemDisabled
)

Variables

This section is empty.

Functions

func Quit

func Quit()

Quit the systray

func Run

func Run(onReady func(), onExit func()) error

Run initializes GUI and starts the event loop, then invokes the onReady callback. It blocks until systray.Quit() is called. Should be called at the very beginning of main() to lock at main thread.

Example
if err := SetIconPath("example/icon/iconwin.ico"); err != nil {
	log.Fatalf("Can't set icon: %s", err)
}
if err := SetTitle("Test title с кириллицей"); err != nil {
	log.Fatalf("Can't set title: %s", err)
}

bBtn := AddMenuItem("Йа Кнопко", "", ItemCheckable)
AddSeparator()
bQuit := AddMenuItem("Quit", "Quit the whole app", ItemDefault)
go func() {
	for {
		select {
		case <-bBtn.OnClickCh():
			log.Println("Btn clicked")
		case <-bQuit.OnClickCh():
			log.Println("Quit reqested")
			Quit()
		}
	}
}()
onReady := func() {
	log.Println("Systray started")
}

Run(onReady, nil)
Output:

func SetIcon

func SetIcon(iconBytes []byte) error

SetIcon sets the systray icon. iconBytes should be the content of *.ico for windows and *.ico/*.jpg/*.png for other platforms. On windows and linux it creates reusable temporary file with icon content. This file

func SetIconPath

func SetIconPath(path string) error

SetIconPath sets icon by direct path. It should be *.ico for windows and *.ico/*.jpg/*.png for other platforms.

func SetTitle

func SetTitle(title string) error

SetTitle sets the systray title, only available on Mac.

func SetTooltip

func SetTooltip(tooltip string) error

SetTooltip sets the systray tooltip to display on mouse hover of the tray icon, only available on Mac and Windows.

Types

type MenuItem struct {
	// contains filtered or unexported fields
}

MenuItem is used to keep track each menu item of systray Don't create it directly, use the one systray.AddMenuItem() returned

func AddMenuItem

func AddMenuItem(title, tooltip string, flags byte) *MenuItem

AddMenuItem adds menu item with designated title and tooltip, returning a channel that notifies whenever that menu item is clicked.

It can be safely invoked from different goroutines.

func AddSeparator

func AddSeparator() *MenuItem

AddSeparator adds a separator bar to the menu.

func AddSubMenu

func AddSubMenu(title string) *MenuItem

AddSubMenu adds a sub menu to the systray and and returns an id to access to accesss the menu

func (item *MenuItem) AddBitmap(bitmapByte []byte) error
func (item *MenuItem) AddBitmapPath(filepath string) error
func (sitem *MenuItem) AddSubMenuItem(title, tooltip string, flags byte) *MenuItem
func (item *MenuItem) Check() error

Check a menu item regardless if it's previously checked or not.

func (item *MenuItem) Checked() bool

Checked returns if the menu item has a check mark.

func (item *MenuItem) Disable() error

Disable a menu item regardless if it's previously disabled or not.

func (item *MenuItem) Disabled() bool

Disabled checkes if the menu item is disabled.

func (item *MenuItem) Enable() error

Enable a menu item regardless if it's previously enabled or not.

func (item *MenuItem) Hide() error

Hide hides a menu item.

func (item *MenuItem) OnClickCh() <-chan struct{}
func (item *MenuItem) SetTitle(title string) error

SetTitle set the text to display on a menu item.

func (item *MenuItem) SetTooltip(tooltip string) error

SetTooltip set the tooltip to show when mouse hover.

func (item *MenuItem) Show() error

Show shows a previously hidden menu item.

func (item *MenuItem) Uncheck() error

Uncheck a menu item regardless if it's previously unchecked or not.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL