native-gui-core 1.0.0

Core abstraction, widget model, and layout engine for Native GUI
Documentation
//! Portable system tray service model.
//!
//! Provides the shared, backend-agnostic specification and identifiers for
//! system tray (notification area / status item) icons.
//!
//! Tray icons are application-level services owned directly by [`Ui`](crate::Ui),
//! outside the hierarchical layout tree: they do not have a `WidgetId` and do
//! not participate in hierarchical layout.

use std::fmt;

use crate::menus::MenuId;

/// Stable identifier for a system tray / status bar icon.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TrayIconId(pub u64);

impl TrayIconId {
    /// Returns the raw numeric identifier.
    pub const fn get(self) -> u64 {
        self.0
    }
}

impl fmt::Display for TrayIconId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "tray icon {}", self.0)
    }
}

/// Specification for a system tray icon.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrayIconSpec {
    /// Stable identifier for the tray icon.
    pub id: TrayIconId,
    /// Hover tooltip text.
    pub tooltip: String,
    /// Optional context menu bound to the tray icon.
    pub menu: Option<MenuId>,
    /// Whether the tray icon is visible in the status area.
    pub visible: bool,
    /// Optional raw icon pixels.
    ///
    /// The shared encoding contract is a **square straight-alpha RGBA
    /// raster**: `side * side * 4` bytes in row-major order. A lane whose
    /// native image loader recognises container formats may additionally
    /// accept an encoded image (Cocoa decodes PNG/TIFF/ICNS/JPEG/GIF/BMP and
    /// the ISO base media family); a payload that fits neither shape is
    /// refused with a typed error. `None` leaves the icon on the platform
    /// default image.
    pub icon_bytes: Option<Vec<u8>>,
}

impl TrayIconSpec {
    /// Creates a visible tray icon specification with default settings.
    pub fn new(id: TrayIconId, tooltip: impl Into<String>) -> Self {
        Self {
            id,
            tooltip: tooltip.into(),
            menu: None,
            visible: true,
            icon_bytes: None,
        }
    }

    /// Sets the context menu for the tray icon.
    pub fn with_menu(mut self, menu: MenuId) -> Self {
        self.menu = Some(menu);
        self
    }

    /// Sets whether the tray icon is visible.
    pub fn with_visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Sets the raw icon pixels for the tray icon.
    ///
    /// See [`TrayIconSpec::icon_bytes`] for the shared encoding contract
    /// (square straight-alpha RGBA, or an encoded container the lane's loader
    /// recognises).
    pub fn with_icon_bytes(mut self, icon_bytes: Vec<u8>) -> Self {
        self.icon_bytes = Some(icon_bytes);
        self
    }
}

/// Interaction events occurring on a system tray icon.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrayEvent {
    /// Primary or single click on the tray icon.
    Clicked { id: TrayIconId },
    /// Double-click on the tray icon.
    DoubleClicked { id: TrayIconId },
    /// Secondary or right click on the tray icon.
    RightClicked { id: TrayIconId },
}

impl TrayEvent {
    /// Returns the target [`TrayIconId`] associated with this event.
    pub fn id(&self) -> TrayIconId {
        match self {
            Self::Clicked { id } | Self::DoubleClicked { id } | Self::RightClicked { id } => *id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn tray_icon_id_display_and_helpers() {
        let id = TrayIconId(42);
        assert_eq!(id.get(), 42);
        assert_eq!(format!("{id}"), "tray icon 42");
    }

    #[test]
    fn tray_icon_spec_builder() {
        let id = TrayIconId(1);
        let spec = TrayIconSpec::new(id, "Test Tooltip")
            .with_menu(MenuId(99))
            .with_visible(false)
            .with_icon_bytes(vec![1, 2, 3, 4]);

        assert_eq!(spec.id, id);
        assert_eq!(spec.tooltip, "Test Tooltip");
        assert_eq!(spec.menu, Some(MenuId(99)));
        assert!(!spec.visible);
        assert_eq!(spec.icon_bytes, Some(vec![1, 2, 3, 4]));
    }

    #[test]
    fn tray_event_helpers() {
        let id = TrayIconId(7);
        let ev1 = TrayEvent::Clicked { id };
        let ev2 = TrayEvent::DoubleClicked { id };
        let ev3 = TrayEvent::RightClicked { id };

        assert_eq!(ev1.id(), id);
        assert_eq!(ev2.id(), id);
        assert_eq!(ev3.id(), id);
    }
}