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. Supported is false when nothing on the bus hosts tray icons; LastError says why.
•
Windows: a notification area icon owned by a hidden window on its own thread. A left click is Click, a double click is DoubleClick (after a Click), 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 Menu event is what you get. Call the functions from the main thread; the events arrive while the gui.window or gui.ui event 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 .ico file, and macOS takes any image format the system reads.
•
Menu items are flat. Each item has an id that 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.OnCloseRequest returning false and ui.HideWindow, closing the window keeps the app running in the tray; ui.ShowWindow brings 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.
gui-tray-guide-menu.salam
/*
* 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 := falsemut running := truefuncmenu(): Vector<tray.Item>:
mut items := Vector {} asVector<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
endfuncrefresh():
tray.SetMenu(menu())
tray.SetTooltip(connected ? "Connected" : "Disconnected")
endfuncmain:
ifnot tray.Show("assets/icon.png", "Disconnected", menu()):
println"no tray:", tray.LastError()
retenduntil 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 = falseendend
time.Sleep(50)
end
tray.Hide()
end
Close 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.
gui-tray-guide-hide.salam
/*
* 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 := falsemut quitting := falsefunchandle_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()
endendendfuncmain:
mut items := Vector {} asVector<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 andnot quitting: ui.HideWindow() retfalseendrettrueend)
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)
end
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.
gui-tray-Items.salam
/*
* 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
funcmain:
mut items := Vector {} asVector<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()
end
Output
show Show window true false false
auto Connect at login true true false
true false true
update Check for updates false false false
quit Quit true false false
shown false
Shows the icon with a tooltip and a menu, or updates it when it is already shown. False when the tray is not available or the icon file cannot be read.