pub struct MenuManager<G>{ /* private fields */ }Expand description
Menu manager that provides centralized menu item management and group state handling
Core features:
- Menu storage: Unified storage for
MenuItem,IconMenuItem, andCheckMenuItem - Group management: Organizes checkbox and radio button groups, ensuring proper radio button logic
- Easy access: Quick access to menu items and their properties via ID
- 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>
impl<G> MenuManager<G>
pub fn new() -> Self
Sourcepub fn insert(&mut self, menu_control: MenuControl<G>)
pub fn insert(&mut self, menu_control: MenuControl<G>)
Inserts a menu control from the menu manager.
Sourcepub fn update(
&mut self,
menu_id: &MenuId,
callback: impl Fn(Option<&MenuControl<G>>),
)
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
Gets a menu control from the menu manager based on the provided menu ID.
Sourcepub fn get_check_items_from_grouped(
&self,
group_id: &G,
) -> Option<&HashMap<Rc<MenuId>, Rc<CheckMenuItem>>>
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>
impl<G> Clone for MenuManager<G>
Source§fn clone(&self) -> MenuManager<G>
fn clone(&self) -> MenuManager<G>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more