gui.tray
A system tray or menu bar icon with a menu, for native apps: StatusNotifierItem on Linux, the notification area on Windows and a status item on macOS.
import gui.tray
gui.tray puts one icon in the system tray (Linux, Windows) or the menu bar (macOS), with a tooltip and a menu. It works with any app: a native gui.ui page, a gui.window program or a program with no window at all. It does not need gui.webview.
Call Show with the path of the icon, the tooltip and the menu items. Later calls to SetMenu, SetTooltip and SetIcon update the icon in place, and Hide removes it. Clicks and chosen menu items become Event values in a queue: read them with Take. SetWake gives a function that is called after every new event, from any thread, so a gui.ui app passes ui.Invalidate and reads the queue in its view.
How each system shows it:
- Linux: a StatusNotifierItem with a com.canonical.dbusmenu menu on the D-Bus session bus, served from its own thread. KDE Plasma, Xfce, Cinnamon, Budgie, LXQt, waybar and GNOME with the AppIndicator extension (the default on Ubuntu) show it, on X11 and on Wayland.
Supportedis false when nothing on the bus hosts tray icons;LastErrorsays why. - Windows: a notification area icon owned by a hidden window on its own thread. A left click is
Click, a double click isDoubleClick(after aClick), and a right click opens the menu. The icon comes back by itself when Explorer restarts. - macOS: a status item in the menu bar. Clicking it opens the menu, so a
Menuevent is what you get. Call the functions from the main thread; the events arrive while thegui.windoworgui.uievent loop runs.
Things to know:
- Icons. A PNG is decoded and scaled down to at most 64 pixels; the system scales it again to the tray size. Windows also takes an
.icofile, and macOS takes any image format the system reads. - Menu items are flat. Each item has an
idthat comes back in the event, a label, and can be disabled, checked or a separator. There are no submenus. - Close to the tray. With
ui.OnCloseRequestreturning false andui.HideWindow, closing the window keeps the app running in the tray;ui.ShowWindowbrings it back.
A menu that follows the app state
A program with no window: the menu offers Connect or Disconnect depending on the state, and the tooltip says which one is active. The loop reads the queue and rebuilds the menu with SetMenu after every change.
/*
* Salam Programming Language (2018-2026)
*
* +-------------------+
* | S A L A M |
* +-------------------+
*
* Designed by Seyyed Ali Mohammadiyeh and the Salam Team
*
* Repository: https://github.com/SalamLang/Salam
*
*/
import gui.tray
import time
mut connected := false
mut running := true
func menu(): Vector<tray.Item>:
mut items := Vector {} as Vector<tray.Item>
if connected:
items.Push(tray.NewItem("disconnect", "Disconnect"))
else:
items.Push(tray.NewItem("connect", "Connect"))
end
items.Push(tray.Separator())
items.Push(tray.NewItem("quit", "Quit"))
ret items
end
func refresh():
tray.SetMenu(menu())
tray.SetTooltip(connected ? "Connected" : "Disconnected")
end
func main:
if not tray.Show("assets/icon.png", "Disconnected", menu()):
println "no tray:", tray.LastError()
ret
end
until running:
mut ev := tray.Event {}
until tray.Take(ev):
if ev.id == "connect" or ev.id == "disconnect":
connected = ev.id == "connect"
refresh()
else ev.id == "quit":
running = false
end
end
time.Sleep(50)
end
tray.Hide()
endClose the window to the tray
A gui.ui page whose close button hides the window instead of quitting. A click on the icon or the Show item brings it back, and Quit leaves for real. SetWake asks the page to run its view again, and the view reads the tray events.
/*
* Salam Programming Language (2018-2026)
*
* +-------------------+
* | S A L A M |
* +-------------------+
*
* Designed by Seyyed Ali Mohammadiyeh and the Salam Team
*
* Repository: https://github.com/SalamLang/Salam
*
*/
import gui.ui
import gui.tray
mut has_tray := false
mut quitting := false
func handle_tray():
mut ev := tray.Event {}
until tray.Take(ev):
if ev.kind == tray.EventKind.Click or ev.id == "show":
ui.ShowWindow()
else ev.id == "quit":
quitting = true
tray.Hide()
ui.Stop()
end
end
end
func main:
mut items := Vector {} as Vector<tray.Item>
items.Push(tray.NewItem("show", "Show window"))
items.Push(tray.Separator())
items.Push(tray.NewItem("quit", "Quit"))
tray.SetWake((): ui.Invalidate() end)
has_tray = tray.Show("assets/icon.png", "My app", items)
ui.OnCloseRequest((): if has_tray and not quitting: ui.HideWindow() ret false end ret true end)
ui.RunPage((): ret ui.NoPatches() end, (b: ui.Builder): handle_tray() b.Open("text") b.Text("Close this window: it stays in the tray.") b.Close() end)
endTypes
EventKind
enum EventKind| Name | Type | Description |
|---|---|---|
Click | | The icon was clicked once (left click on Windows, activation on Linux). |
DoubleClick | | The icon was double clicked on Windows, or middle clicked on Linux. |
Menu | | A menu item was chosen; the event's id is the item's id. |
/*
* Salam Programming Language (2018-2026)
*
* +-------------------+
* | S A L A M |
* +-------------------+
*
* Designed by Seyyed Ali Mohammadiyeh and the Salam Team
*
* Repository: https://github.com/SalamLang/Salam
*
*/
import gui.tray
mut wakes := 0
func main:
tray.SetWake((): wakes++ end)
tray.Post(tray.Event { kind = tray.EventKind.Click })
tray.Post(tray.Event { kind = tray.EventKind.DoubleClick })
tray.Post(tray.Event { kind = tray.EventKind.Menu, id = "quit" })
println "pending", tray.Pending(), "wakes", wakes
mut ev := tray.Event {}
until tray.Take(ev):
if ev.kind == tray.EventKind.Menu:
println "menu item", ev.id
else ev.kind == tray.EventKind.DoubleClick:
println "double click"
else:
println "click"
end
end
println "pending", tray.Pending()
endItem
struct Item| Name | Type | Description |
|---|---|---|
id | str = "" | The id that comes back in the Menu event when the item is chosen. |
label | str = "" | The text of the item. |
enabled | bool = true | False shows the item greyed out; it cannot be chosen. |
checked | bool = false | True shows a check mark next to the item. |
separator | bool = false | True makes the item a separator line; its other fields are ignored. |
/*
* Salam Programming Language (2018-2026)
*
* +-------------------+
* | S A L A M |
* +-------------------+
*
* Designed by Seyyed Ali Mohammadiyeh and the Salam Team
*
* Repository: https://github.com/SalamLang/Salam
*
*/
import gui.tray
func main:
mut items := Vector {} as Vector<tray.Item>
items.Push(tray.NewItem("show", "Show window"))
items.Push(tray.CheckItem("auto", "Connect at login", true))
items.Push(tray.Separator())
items.Push(tray.Item { id = "update", label = "Check for updates", enabled = false })
items.Push(tray.NewItem("quit", "Quit"))
each it in items:
println it.id, it.label, it.enabled, it.checked, it.separator
end
tray.SetMenu(items)
println "shown", tray.IsShown()
endEvent
struct EventTake.| Name | Type | Description |
|---|---|---|
kind | EventKind | What happened, an EventKind. |
id | str = "" | The id of the chosen item for a Menu event, empty otherwise. |
The example for this item is shown with the ones above: gui-tray-Events.salam
Functions
Supported
func Supported(): boolLastError says which./*
* Salam Programming Language (2018-2026)
*
* +-------------------+
* | S A L A M |
* +-------------------+
*
* Designed by Seyyed Ali Mohammadiyeh and the Salam Team
*
* Repository: https://github.com/SalamLang/Salam
*
*/
import gui.tray
func main:
if not tray.Supported():
println "no tray on", tray.Backend(), "because", tray.LastError()
ret
end
mut items := Vector {} as Vector<tray.Item>
items.Push(tray.NewItem("quit", "Quit"))
if not tray.Show("assets/icon.png", "Disconnected", items):
println "tray failed:", tray.LastError()
ret
end
tray.SetTooltip("Connected")
_ok := tray.SetIcon("assets/icon-on.png")
tray.Hide()
endBackend
func Backend(): strsni, win32, cocoa or none.The example for this item is shown with the ones above: gui-tray-Show.salam
LastError
func LastError(): strShow.The example for this item is shown with the ones above: gui-tray-Show.salam
Show
func Show(icon: str, tooltip: str, items: Vector<Item>): boolThe example for this item is shown with the ones above: gui-tray-Show.salam
SetMenu
func SetMenu(items: Vector<Item>)Show too.The example for this item is shown with the ones above: gui-tray-Items.salam
SetTooltip
func SetTooltip(text: str)The example for this item is shown with the ones above: gui-tray-Show.salam
SetIcon
func SetIcon(path: str): boolThe example for this item is shown with the ones above: gui-tray-Show.salam
Hide
func Hide()Show brings it back.The example for this item is shown with the ones above: gui-tray-Show.salam
IsShown
func IsShown(): boolThe example for this item is shown with the ones above: gui-tray-Items.salam
SetWake
func SetWake(wake: func())ui.Invalidate.The example for this item is shown with the ones above: gui-tray-Events.salam
Post
func Post(event: Event)The example for this item is shown with the ones above: gui-tray-Events.salam
Take
func Take(out &: Event): boolThe example for this item is shown with the ones above: gui-tray-Events.salam
Pending
func Pending(): intThe example for this item is shown with the ones above: gui-tray-Events.salam
NewItem
func NewItem(id: str, label: str): ItemThe example for this item is shown with the ones above: gui-tray-Items.salam
CheckItem
func CheckItem(id: str, label: str, checked: bool): Itemchecked is true.The example for this item is shown with the ones above: gui-tray-Items.salam
Separator
func Separator(): ItemThe example for this item is shown with the ones above: gui-tray-Items.salam