Skip to main content

Crate ldtray

Crate ldtray 

Source
Expand description

ldtray — cross-platform tray icons that are never linked against any GUI or platform library at compile time.

Every platform toolkit (libdbus on Linux, shell32/user32 on Windows, AppKit/objc on macOS) is resolved at runtime through libloading. The practical consequence is that a single daemon binary runs everywhere: on a headless server the only failure is a clean Error returned from Tray::new (“the tray library could not be loaded”), never a link error and never a crash. Callers can simply ignore the error and keep running without a tray.

§Example

use ldtray::{Tray, TrayConfig, Icon, Menu, MenuItem, Event, Notification};

let icon = Icon::from_rgba(1, 1, vec![255, 0, 0, 255])?;
let menu = Menu::new()
    .item(MenuItem::button(1, "Say hi"))
    .item(MenuItem::separator())
    .item(MenuItem::button(2, "Quit"));

let tray = match Tray::new(TrayConfig::new(icon).tooltip("demo").menu(menu)) {
    Ok(tray) => tray,
    Err(err) => {
        eprintln!("no tray available ({err}); continuing headless");
        return Ok(());
    }
};

let handle = tray.handle();
tray.run(move |event| match event {
    Event::Menu(id) if id.0 == 1 => {
        let _ = handle.notify(Notification::new("demo", "hi there"));
    }
    Event::Menu(id) if id.0 == 2 => {
        let _ = handle.quit();
    }
    other => println!("event: {other:?}"),
})

Structs§

ActionId
Identifier for a notification action button. You choose the number; it is echoed back in crate::Event::NotificationAction when the user clicks the action.
Icon
A tray icon stored as raw, non-premultiplied RGBA8 pixels in row-major order ([r, g, b, a, r, g, b, a, ...], top row first).
Menu
An ordered list of MenuItems shown when the tray icon is right-clicked (or, on some desktops, on any click).
MenuId
Stable identifier for a clickable menu entry.
Notification
A desktop notification (“toast”/“balloon”) to be shown via crate::TrayHandle::notify.
Tray
A live tray icon.
TrayConfig
Initial configuration for a Tray.
TrayHandle
A cloneable, thread-safe handle used to update a running tray.

Enums§

Error
Errors produced by ldtray.
Event
Something the user did to the tray icon, delivered to the callback passed to crate::Tray::run or crate::Tray::spawn.
MenuItem
A single entry in a Menu.

Type Aliases§

Result
Convenience alias for results returned by this crate.