MenuManager

Struct MenuManager 

Source
pub struct MenuManager<G>
where G: Clone + Eq + Hash + PartialEq,
{ /* private fields */ }
Expand description

Menu manager that provides centralized menu item management and group state handling

Core features:

  1. Menu storage: Unified storage for MenuItem, IconMenuItem, and CheckMenuItem
  2. Group management: Organizes checkbox and radio button groups, ensuring proper radio button logic
  3. Easy access: Quick access to menu items and their properties via ID
  4. State synchronization: Automatically updates other buttons in radio groups when one is selected

The type parameter G represents the group identifier for Radio and CheckBox menu items. Must implement: Clone + Eq + Hash + PartialEq Recommended to use enums or string constants for type safety and readability.

§Example

use std::rc::Rc;
use tray_controls::{CheckMenuKind, MenuControl, MenuManager};
use tray_icon::menu::{CheckMenuItem, MenuId};

let mut manager = MenuManager::<&str>::new();

// Add a checkbox with group ID "display_group"
let checkbox = CheckMenuItem::with_id("show_toolbar", "Show Toolbar", true, true, None);
manager.insert(MenuControl::CheckMenu(
    CheckMenuKind::CheckBox(Rc::new(checkbox), "display_group")
));

// Add radio buttons with group ID "color_group"
let radio = CheckMenuItem::with_id("red", "Red", true, true, None);
manager.insert(MenuControl::CheckMenu(
    CheckMenuKind::Radio(
        Rc::new(radio),
        Some(Rc::new(MenuId::new("radio default id"))),
        "color_group"
    )
));

// Handle menu clicks - radio groups are automatically synchronized
let click_menu_id = MenuId::new("");

manager.update(&click_menu_id, |menu| {
    if let Some(menu) = menu {
        println!("Clicked menu: {}", menu.text());
    }
});

§Example

use std::rc::Rc;
use tray_controls::{CheckMenuKind, MenuControl, MenuManager};
use tray_icon::menu::{CheckMenuItem, MenuId};

#[derive(Clone, Eq, Hash, PartialEq)]
enum MenuGroup {
    CheckBoxDisplay,
    RadioColor,
}

let mut manager = MenuManager::<MenuGroup>::new();

// Add a checkbox with group ID "CheckBoxDisplay"
let checkbox = CheckMenuItem::with_id("show_toolbar", "Show Toolbar", true, true, None);
manager.insert(MenuControl::CheckMenu(
    CheckMenuKind::CheckBox(Rc::new(checkbox), MenuGroup::CheckBoxDisplay)
));

// Add radio buttons with group ID "RadioColor", and set the default radio menu ID
let radio = CheckMenuItem::with_id("red", "Red", true, true, None);
manager.insert(MenuControl::CheckMenu(
    CheckMenuKind::Radio(
        Rc::new(radio),
        Some(Rc::new(MenuId::new("red"))),
        MenuGroup::RadioColor
    )
));

// Handle menu clicks - radio groups are automatically synchronized
let click_menu_id = MenuId::new("");

manager.update(&click_menu_id, |menu| {
    if let Some(menu) = menu {
        println!("Clicked menu: {}", menu.text());
    }
});

Implementations§

Source§

impl<G> MenuManager<G>
where G: Clone + Eq + Hash + PartialEq,

Source

pub fn new() -> Self

Source

pub fn insert(&mut self, menu_control: MenuControl<G>)

Inserts a menu control from the menu manager.

Source

pub fn remove(&mut self, menu_id: &MenuId)

Removes a menu control from the menu manager.

Source

pub fn update( &mut self, menu_id: &MenuId, callback: impl Fn(Option<&MenuControl<G>>), )

Updates the menu control state based on the provided menu ID, and callback the menu control.

NOTE: If the menu control is a radio:
there is a default radio menu, the cllback menu control is the cheked menu
there is no default radio menu, the callback menu control is the click menu

Source

pub fn get_menu_item_from_id(&self, menu_id: &MenuId) -> Option<&MenuControl<G>>

Gets a menu control from the menu manager based on the provided menu ID.

Source

pub fn get_check_items_from_grouped( &self, group_id: &G, ) -> Option<&HashMap<Rc<MenuId>, Rc<CheckMenuItem>>>

Gets grouped check menu items from the menu manager based on the provided menu group id.

Trait Implementations§

Source§

impl<G> Clone for MenuManager<G>
where G: Clone + Eq + Hash + PartialEq + Clone,

Source§

fn clone(&self) -> MenuManager<G>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<G> Default for MenuManager<G>
where G: Clone + Eq + Hash + PartialEq,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<G> Freeze for MenuManager<G>

§

impl<G> !RefUnwindSafe for MenuManager<G>

§

impl<G> !Send for MenuManager<G>

§

impl<G> !Sync for MenuManager<G>

§

impl<G> Unpin for MenuManager<G>
where G: Unpin,

§

impl<G> !UnwindSafe for MenuManager<G>

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.