systray

package module
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

Systray

systray is a cross-platform Go library to place an icon and menu in the notification area. This repository is a fork of getlantern/systray removing the GTK dependency and support for legacy linux system tray.

Features

  • Supported on Windows, macOS, Linux and many BSD systems
  • Menu items can be checked and/or disabled
  • Methods may be called from any Goroutine

API

package main

import "github.com/pirST/systray"
import "github.com/pirST/systray/example/icon"

func 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")

	// Sets the icon of a menu item.
	mQuit.SetIcon(icon.Data)
}

func onExit() {
	// clean up here
}
Running in a Fyne app

This repository is designed to allow any toolkit to integrate system tray without any additional dependencies. It is maintained by the Fyne team, but if you are using Fyne there is an even easier to use API in the main repository that wraps this project.

In your app you can use a standard fyne.Menu structure and pass it to SetSystemTrayMenu when your app is a desktop app, as follows:

	menu := fyne.NewMenu("MyApp",
		fyne.NewMenuItem("Show", func() {
			log.Println("Tapped show")
		}))

	if desk, ok := myApp.(desktop.App); ok {
		desk.SetSystemTrayMenu(menu)
	}

You can find out more in the toolkit documentation: System Tray Menu.

Run in another toolkit

Most graphical toolkits will grab the main loop so the Run code above is not possible. For this reason there is another entry point RunWithExternalLoop. This function of the library returns a start and end function that should be called when the application has started and will end, to loop in appropriate features.

See full API as well as CHANGELOG.

Note: this package requires cgo, so make sure you set CGO_ENABLED=1 before building.

Try the example app!

Have go v1.12+ or higher installed? Here's an example to get started on macOS or Linux:

git clone https://github.com/fyne-io/systray
cd systray/example
go run .

On Windows, you should follow the instructions above, but use the followign run command:

go run -ldflags "-H=windowsgui" .

Now look for Awesome App in your menu bar!

Awesome App screenshot

Platform notes

Linux/BSD

This implementation uses DBus to communicate through the SystemNotifier/AppIndicator spec, older tray implementations may not load the icon.

If you are running an older desktop environment, or system tray provider, you may require a proxy app which can convert the new DBus calls to the old format. The recommended tool for Gnome based trays is snixembed, others are available. Search for "StatusNotifierItems XEmbedded" in your package manager.

Windows
  • To avoid opening a console at application startup, use "fyne package" for your app or manually use these compile flags:
go build -ldflags -H=windowsgui
macOS

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

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

If bundling manually, you may want to add one or both of the following to your Info.plist:

	<!-- avoid having a blurry icon and text -->
	<key>NSHighResolutionCapable</key>
	<string>True</string>

	<!-- avoid showing the app on the Dock -->
	<key>LSUIElement</key>
	<string>1</string>

Consult the Official Apple Documentation here.

On macOS, it's possible to set the underlying NSStatusItemBehavior with systray.SetRemovalAllowed(true). When enabled, the user can cmd-drag the icon off the menu bar.

Credits

Documentation

Overview

Package systray is a cross-platform Go library to place an icon and menu in the notification area.

Index

Constants

This section is empty.

Variables

View Source
var (

	// TrayOpenedCh receives an entry each time the system tray menu is opened.
	TrayOpenedCh = make(chan struct{})
)

Functions

func AddSeparator

func AddSeparator()

AddSeparator adds a separator bar to the menu

func Quit

func Quit()

Quit the systray

func Register

func Register(onReady func(), onExit func())

Register initializes GUI and registers the callbacks but relies on the caller to run the event loop somewhere else. It's useful if the program needs to show other UI elements, for example, webview. To overcome some OS weirdness, On macOS versions before Catalina, calling this does exactly the same as Run().

func ResetMenu

func ResetMenu()

ResetMenu will remove all menu items

func Run

func Run(onReady, onExit func())

Run initializes GUI and starts the event loop, then invokes the onReady callback. It blocks until systray.Quit() is called.

func RunWithExternalLoop

func RunWithExternalLoop(onReady, onExit func()) (start, end func())

RunWithExternalLoop allows the systemtray module to operate with other tookits. The returned start and end functions should be called by the toolkit when the application has started and will end.

func SetIcon

func SetIcon(iconBytes []byte)

SetIcon sets the systray icon. iconBytes should be the content of .ico for windows and .ico/.jpg/.png for other platforms.

func SetRemovalAllowed

func SetRemovalAllowed(allowed bool)

SetRemovalAllowed sets whether a user can remove the systray icon or not. This is only supported on macOS.

func SetTemplateIcon

func SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte)

SetTemplateIcon sets the systray icon as a template icon (on macOS), falling back to a regular icon on other platforms. templateIconBytes and iconBytes should be the content of .ico for windows and .ico/.jpg/.png for other platforms.

func SetTitle

func SetTitle(t string)

SetTitle sets the systray title, only available on Mac and Linux.

func SetTooltip

func SetTooltip(tooltipTitle string)

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

Types

type MenuItem struct {
	// ClickedCh is the channel which will be notified when the menu item is clicked
	ClickedCh chan 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 string, tooltip string) *MenuItem

AddMenuItem adds a menu item with the designated title and tooltip. It can be safely invoked from different goroutines. Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddMenuItemCheckbox

func AddMenuItemCheckbox

func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem

AddMenuItemCheckbox adds a menu item with the designated title and tooltip and a checkbox for Linux. On other platforms there will be a check indicated next to the item if `checked` is true. It can be safely invoked from different goroutines.

func (item *MenuItem) AddSeparator()

AddSeparator adds a separator bar to the submenu

func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem

AddSubMenuItem adds a nested sub-menu item with the designated title and tooltip. It can be safely invoked from different goroutines. Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddSubMenuItemCheckbox

func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem

AddSubMenuItemCheckbox adds a nested sub-menu item with the designated title and tooltip and a checkbox for Linux. It can be safely invoked from different goroutines. On Windows and OSX this is the same as calling AddSubMenuItem

func (item *MenuItem) Check()

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()

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

func (item *MenuItem) Disabled() bool

Disabled checks if the menu item is disabled

func (item *MenuItem) Enable()

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

func (item *MenuItem) Hide()

Hide hides a menu item

func (item *MenuItem) Remove()

Remove removes a menu item

func (item *MenuItem) SetIcon(iconBytes []byte)

SetIcon sets the icon of a menu item. iconBytes should be the content of .ico/.jpg/.png

func (item *MenuItem) SetTemplateIcon(templateIconBytes []byte, regularIconBytes []byte)

SetTemplateIcon sets the icon of a menu item as a template icon (on macOS). On Windows and Linux, it falls back to the regular icon bytes. templateIconBytes and regularIconBytes should be the content of .ico for windows and .ico/.jpg/.png for other platforms.

func (item *MenuItem) SetTitle(title string)

SetTitle set the text to display on a menu item

func (item *MenuItem) SetTooltip(tooltip string)

SetTooltip set the tooltip to show when mouse hover

func (item *MenuItem) Show()

Show shows a previously hidden menu item

func (item *MenuItem) String() string
func (item *MenuItem) Uncheck()

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

type PX

type PX struct {
	W, H int
	Pix  []byte
}

PX is picture pix map structure with width and high

Directories

Path Synopsis
internal
generated/menu
Code generated by dbus-codegen-go DO NOT EDIT.
Code generated by dbus-codegen-go DO NOT EDIT.
generated/notifier
Code generated by dbus-codegen-go DO NOT EDIT.
Code generated by dbus-codegen-go DO NOT EDIT.

Jump to

Keyboard shortcuts

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