Skip to main content

Icon

Struct Icon 

Source
pub struct Icon { /* private fields */ }
Expand description

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).

RGBA is the universal representation; each backend converts it to whatever the platform wants (ARGB32 pixmaps on Linux, an HICON on Windows, an NSImage on macOS). Keeping the source format toolkit-neutral is what lets the whole crate avoid linking against any image library.

Implementations§

Source§

impl Icon

Source

pub fn from_rgba(width: u32, height: u32, rgba: Vec<u8>) -> Result<Icon>

Builds an icon from raw RGBA8 pixels.

rgba must be exactly width * height * 4 bytes and non-empty, otherwise Error::Backend is returned.

Examples found in repository?
examples/smoke.rs (line 23)
17fn demo_icon() -> Icon {
18    let side = 16u32;
19    let mut rgba = Vec::with_capacity((side * side * 4) as usize);
20    for _ in 0..side * side {
21        rgba.extend_from_slice(&[10, 120, 220, 255]);
22    }
23    Icon::from_rgba(side, side, rgba).expect("valid icon")
24}
More examples
Hide additional examples
examples/basic.rs (line 21)
14fn main() {
15    // A 16x16 solid red icon.
16    let side = 16u32;
17    let mut rgba = Vec::with_capacity((side * side * 4) as usize);
18    for _ in 0..side * side {
19        rgba.extend_from_slice(&[220, 40, 40, 255]);
20    }
21    let icon = Icon::from_rgba(side, side, rgba).expect("valid icon");
22    let notify_icon = icon.clone();
23
24    let menu = Menu::new()
25        .item(MenuItem::button(SAY_HI, "Say hi"))
26        .item(MenuItem::checkbox(TOGGLE, "Toggle me", false))
27        .item(MenuItem::separator())
28        .item(MenuItem::button(QUIT, "Quit"));
29
30    let config = TrayConfig::new(icon).tooltip("ldtray example").menu(menu);
31
32    let tray = match Tray::new(config) {
33        Ok(tray) => tray,
34        Err(err) => {
35            eprintln!("tray unavailable: {err}");
36            eprintln!("(this is expected on a headless server — exiting cleanly)");
37            return;
38        }
39    };
40
41    let handle = tray.handle();
42    println!("tray is up — right-click it for the menu, or Ctrl-C to abort");
43
44    let result = tray.run(move |event| {
45        println!("event: {event:?}");
46        match event {
47            Event::Menu(id) if id.0 == SAY_HI => {
48                // Actions are shown on Linux; ignored (message still shown)
49                // elsewhere.
50                let _ = handle.notify(
51                    Notification::new("ldtray", "Hello from the tray!")
52                        .with_icon(notify_icon.clone())
53                        .action(100, "Wave back"),
54                );
55            }
56            Event::NotificationAction(action) => {
57                println!("notification action clicked: {}", action.0);
58            }
59            Event::Menu(id) if id.0 == QUIT => {
60                let _ = handle.quit();
61            }
62            _ => {}
63        }
64    });
65
66    if let Err(err) = result {
67        eprintln!("event loop ended with error: {err}");
68    }
69}
Source

pub fn width(&self) -> u32

Icon width in pixels.

Source

pub fn height(&self) -> u32

Icon height in pixels.

Source

pub fn rgba(&self) -> &[u8]

Raw RGBA8 pixels, width * height * 4 bytes, row-major.

Trait Implementations§

Source§

impl Clone for Icon

Source§

fn clone(&self) -> Icon

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Icon

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Icon

§

impl RefUnwindSafe for Icon

§

impl Send for Icon

§

impl Sync for Icon

§

impl Unpin for Icon

§

impl UnsafeUnpin for Icon

§

impl UnwindSafe for Icon

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.