Skip to main content

TrayConfig

Struct TrayConfig 

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

Initial configuration for a Tray.

Implementations§

Source§

impl TrayConfig

Source

pub fn new(icon: Icon) -> TrayConfig

Start a configuration with the required tray icon.

Examples found in repository?
examples/basic.rs (line 30)
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                let _ = handle.notify(
49                    Notification::new("ldtray", "Hello from the tray!")
50                        .with_icon(notify_icon.clone()),
51                );
52            }
53            Event::Menu(id) if id.0 == QUIT => {
54                let _ = handle.quit();
55            }
56            _ => {}
57        }
58    });
59
60    if let Err(err) = result {
61        eprintln!("event loop ended with error: {err}");
62    }
63}
Source

pub fn tooltip(self, tooltip: impl Into<String>) -> TrayConfig

Builder: set the hover tooltip text.

Examples found in repository?
examples/basic.rs (line 30)
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                let _ = handle.notify(
49                    Notification::new("ldtray", "Hello from the tray!")
50                        .with_icon(notify_icon.clone()),
51                );
52            }
53            Event::Menu(id) if id.0 == QUIT => {
54                let _ = handle.quit();
55            }
56            _ => {}
57        }
58    });
59
60    if let Err(err) = result {
61        eprintln!("event loop ended with error: {err}");
62    }
63}
Source

pub fn menu(self, menu: Menu) -> TrayConfig

Builder: set the context menu.

Examples found in repository?
examples/basic.rs (line 30)
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                let _ = handle.notify(
49                    Notification::new("ldtray", "Hello from the tray!")
50                        .with_icon(notify_icon.clone()),
51                );
52            }
53            Event::Menu(id) if id.0 == QUIT => {
54                let _ = handle.quit();
55            }
56            _ => {}
57        }
58    });
59
60    if let Err(err) = result {
61        eprintln!("event loop ended with error: {err}");
62    }
63}

Trait Implementations§

Source§

impl Clone for TrayConfig

Source§

fn clone(&self) -> TrayConfig

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 TrayConfig

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.