systray

repository·master·Indexed 25 days ago

https://github.com/getlantern/systray

A cross-platform Go library for placing icons and menus in the system notification area (tray) on Windows, macOS, and Linux. It provides functionality to configure tray icons, titles, and tooltips, as well as create hierarchical menus with checkboxes and separators. The library supports both blocking initialization via systray.Run() and non-blocking registration via systray.Register().

Tokens
1.2K
Snippets
3
Records
12
Agent score
37%

What's inside systray

  1. Create a macOS application bundle

    master

    On macOS, you must wrap your binary in an application bundle. Create a folder structure like this:

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

    To avoid a blurry icon/text, add this to Info.plist:

    <key>NSHighResolutionCapable</key>
    <string>True</string>

    To prevent the app from showing in the Dock, add this to Info.plist:

    <key>LSUIElement</key>
    <string>1</string>
  2. Build for Windows without a console window

    master

    To prevent a command prompt/console window from appearing when your application starts on Windows, use the -H=windowsgui linker flag during build:

    env GO111MODULE=on go build -ldflags "-H=windowsgui"
  3. Install dependencies for Linux

    master

    Building systray on Linux requires gcc and the gtk3 and libayatana-appindicator3 development headers.

    For Debian or Ubuntu, install them with:

    sudo apt-get install gcc libgtk-3-dev libayatana-appindicator3-dev

    On Linux Mint, libxapp-dev is also required.

    If you need to support the older libappindicator3 library, build with the legacy_appindicator tag:

    go build -tags=legacy_appindicator
  4. Add and configure menu items

    master

    Use systray.AddMenuItem(title, tooltip) to create a new menu item.

    Menu items support being checked or disabled. On macOS and Windows, you can also set a specific icon for a menu item using mQuit.SetIcon(iconData).

    func onReady() {
    	mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
    
    	// Sets the icon of a menu item. Only available on Mac and Windows.
    	mQuit.SetIcon(iconData)
    }
  5. Configure tray icon, title, and tooltip

    master

    Inside the onReady callback, you can configure the visual properties of the system tray icon using the following methods:

    • systray.SetIcon(data): Sets the icon image.
    • systray.SetTitle(title): Sets the title text.
    • systray.SetTooltip(tooltip): Sets the tooltip text.
  6. Initialize systray in your application

    master

    To start the system tray application, call systray.Run(onReady, onExit).

    • onReady: A callback function executed when the tray is ready. Use this to set the icon, title, tooltip, and add menu items.
    • onExit: A callback function executed when the application is exiting. Use this for cleanup tasks.
    func main() {
    	systray.Run(onReady, onExit)
    }
    
    func onReady() {
    	// Setup tray here
    }
    
    func onExit() {
    	// Cleanup here
    }
  7. Add menu items and sub-menus

    master

    You can build a menu hierarchy using the following methods. These can be safely called from different goroutines.

    • systray.AddMenuItem(title, tooltip): Adds a standard menu item.
    • systray.AddMenuItemCheckbox(title, tooltip, checked): Adds a menu item with a checkbox. Note: On Windows and OSX, items are checkable by default, but on Linux, you must use this method or the AddSubMenuItemCheckbox variant.
    • systray.AddSeparator(): Adds a visual separator bar to the menu.
    • item.AddSubMenuItem(title, tooltip): Adds a nested menu item under an existing MenuItem.
    • item.AddSubMenuItemCheckbox(title, tooltip, checked): Adds a nested checkbox item under an existing MenuItem (required for Linux checkbox support).
  8. Initialize the systray with Run() or Register()

    master

    To start the system tray, use either systray.Run() or systray.Register().

    • systray.Run(onReady, onExit): Initializes the GUI and starts the event loop. This is a blocking call and must be run from the main thread on macOS. It executes onReady when the tray is ready and onExit when the tray is quitting.
    • systray.Register(onReady, onExit): Initializes the GUI and registers callbacks but does not block. This is useful if your application needs to run other UI elements (like a webview) alongside the tray. You must manage the event loop elsewhere.

    onReady is called when the tray is initialized. onExit is called when the tray is being shut down.

  9. Modify MenuItem properties at runtime

    master

    You can dynamically update the state of a MenuItem using the following methods:

    • item.SetTitle(title): Changes the displayed text.
    • item.SetTooltip(tooltip): Changes the hover tooltip.
    • item.Enable() / item.Disable(): Toggles whether the item is interactive (grayed out if disabled).
    • item.Check() / item.Uncheck(): Toggles the check mark.
    • item.Show() / item.Hide(): Controls the visibility of the item.
    • item.Checked(): Returns true if the item is currently checked.
    • item.Disabled(): Returns true if the item is currently disabled.