muda/src/menu.rs

324 lines
11 KiB
Rust
Raw Normal View History

2022-12-09 05:50:24 +11:00
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{cell::RefCell, rc::Rc};
use crate::{util::AddOp, ContextMenu, IsMenuItem, MenuItemKind, Position};
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
/// A root menu that can be added to a Window on Windows and Linux
/// and used as the app global menu on macOS.
#[derive(Clone)]
pub struct Menu(Rc<RefCell<crate::platform_impl::Menu>>);
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
impl Default for Menu {
fn default() -> Self {
Self::new()
}
}
impl Menu {
/// Creates a new menu.
pub fn new() -> Self {
Self(Rc::new(RefCell::new(crate::platform_impl::Menu::new())))
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Creates a new menu with given `items`. It calls [`Menu::new`] and [`Menu::append_items`] internally.
pub fn with_items(items: &[&dyn IsMenuItem]) -> crate::Result<Self> {
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
let menu = Self::new();
menu.append_items(items)?;
Ok(menu)
}
/// Returns a unique identifier associated with this menu.
pub fn id(&self) -> u32 {
self.0.borrow().id()
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Add a menu item to the end of this menu.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn append(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.0.borrow_mut().add_menu_item(item, AddOp::Append)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Add menu items to the end of this menu. It calls [`Menu::append`] in a loop internally.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn append_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
for item in items {
self.append(*item)?
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
Ok(())
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Add a menu item to the beginning of this menu.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn prepend(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.0.borrow_mut().add_menu_item(item, AddOp::Insert(0))
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Add menu items to the beginning of this menu. It calls [`Menu::insert_items`] with position of `0` internally.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn prepend_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
self.insert_items(items, 0)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Insert a menu item at the specified `postion` in the menu.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn insert(&self, item: &dyn IsMenuItem, position: usize) -> crate::Result<()> {
self.0
.borrow_mut()
.add_menu_item(item, AddOp::Insert(position))
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Insert menu items at the specified `postion` in the menu.
///
/// ## Platform-spcific:
///
/// - **macOS:** Only [`Submenu`] can be added to the menu
///
/// [`Submenu`]: crate::Submenu
pub fn insert_items(&self, items: &[&dyn IsMenuItem], position: usize) -> crate::Result<()> {
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
for (i, item) in items.iter().enumerate() {
self.insert(*item, position + i)?
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
Ok(())
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Remove a menu item from this menu.
pub fn remove(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
self.0.borrow_mut().remove(item)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Returns a list of menu items that has been added to this menu.
pub fn items(&self) -> Vec<MenuItemKind> {
self.0.borrow().items()
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Adds this menu to a [`gtk::ApplicationWindow`]
///
/// - `container`: this is an optional paramter to specify a container for the [`gtk::MenuBar`],
/// it is highly recommended to pass a container, otherwise the menubar will be added directly to the window,
/// which is usually not the desired behavior.
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
///
/// ## Example:
/// ```no_run
/// let window = gtk::ApplicationWindow::builder().build();
/// let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
/// let menu = muda::Menu::new();
/// // -- snip, add your menu items --
/// menu.init_for_gtk_window(&window, Some(&vbox));
/// // then proceed to add your widgets to the `vbox`
/// ```
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
///
/// ## Panics:
///
/// Panics if the gtk event loop hasn't been initialized on the thread.
#[cfg(target_os = "linux")]
pub fn init_for_gtk_window<W, C>(&self, window: &W, container: Option<&C>) -> crate::Result<()>
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
W: gtk::prelude::IsA<gtk::Container>,
C: gtk::prelude::IsA<gtk::Container>,
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
{
self.0.borrow_mut().init_for_gtk_window(window, container)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Adds this menu to a win32 window.
///
/// ## Note about accelerators:
///
/// For accelerators to work, the event loop needs to call
/// [`TranslateAcceleratorW`](windows_sys::Win32::UI::WindowsAndMessaging::TranslateAcceleratorW)
/// with the [`HACCEL`](windows_sys::Win32::UI::WindowsAndMessaging::HACCEL) returned from [`Menu::haccel`]
///
/// #### Example:
/// ```no_run
/// # use muda::Menu;
/// # use windows_sys::Win32::UI::WindowsAndMessaging::{MSG, GetMessageW, TranslateMessage, DispatchMessageW, TranslateAcceleratorW};
/// let menu = Menu::new();
/// unsafe {
/// let mut msg: MSG = std::mem::zeroed();
/// while GetMessageW(&mut msg, 0, 0, 0) == 1 {
/// let translated = TranslateAcceleratorW(msg.hwnd, menu.haccel(), &msg as *const _);
/// if translated != 1{
/// TranslateMessage(&msg);
/// DispatchMessageW(&msg);
/// }
/// }
/// }
/// ```
#[cfg(target_os = "windows")]
pub fn init_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
self.0.borrow_mut().init_for_hwnd(hwnd)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Returns The [`HACCEL`](windows_sys::Win32::UI::WindowsAndMessaging::HACCEL) associated with this menu
/// It can be used with [`TranslateAcceleratorW`](windows_sys::Win32::UI::WindowsAndMessaging::TranslateAcceleratorW)
/// in the event loop to enable accelerators
#[cfg(target_os = "windows")]
pub fn haccel(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HACCEL {
self.0.borrow_mut().haccel()
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Removes this menu from a [`gtk::ApplicationWindow`]
#[cfg(target_os = "linux")]
pub fn remove_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.0.borrow_mut().remove_for_gtk_window(window)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Removes this menu from a win32 window
#[cfg(target_os = "windows")]
pub fn remove_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
self.0.borrow_mut().remove_for_hwnd(hwnd)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Hides this menu from a [`gtk::ApplicationWindow`]
#[cfg(target_os = "linux")]
pub fn hide_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
{
self.0.borrow_mut().hide_for_gtk_window(window)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Hides this menu from a win32 window
#[cfg(target_os = "windows")]
pub fn hide_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
self.0.borrow().hide_for_hwnd(hwnd)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Shows this menu on a [`gtk::ApplicationWindow`]
#[cfg(target_os = "linux")]
pub fn show_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
{
self.0.borrow_mut().show_for_gtk_window(window)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Shows this menu on a win32 window
#[cfg(target_os = "windows")]
pub fn show_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
self.0.borrow().show_for_hwnd(hwnd)
}
/// Returns whether this menu visible on a [`gtk::ApplicationWindow`]
#[cfg(target_os = "linux")]
pub fn is_visible_on_gtk_window<W>(&self, window: &W) -> bool
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
{
self.0.borrow().is_visible_on_gtk_window(window)
}
#[cfg(target_os = "linux")]
/// Returns the [`gtk::MenuBar`] that is associated with this window if it exists.
/// This is useful to get information about the menubar for example its height.
pub fn gtk_menubar_for_gtk_window<W>(self, window: &W) -> Option<gtk::MenuBar>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
{
self.0.borrow().gtk_menubar_for_gtk_window(window)
}
/// Returns whether this menu visible on a on a win32 window
#[cfg(target_os = "windows")]
pub fn is_visible_on_hwnd(&self, hwnd: isize) -> bool {
self.0.borrow().is_visible_on_hwnd(hwnd)
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Adds this menu to an NSApp.
#[cfg(target_os = "macos")]
pub fn init_for_nsapp(&self) {
self.0.borrow_mut().init_for_nsapp()
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
/// Removes this menu from an NSApp.
#[cfg(target_os = "macos")]
pub fn remove_for_nsapp(&self) {
self.0.borrow_mut().remove_for_nsapp()
refactor: rewrite (#18) * refactor: rewrite * fix syncing check items and cleanup * clippy * Add `append`, `prepend` and `insert` * accept different menu items in `*_list` methods * add context menu for gtk * add `with_items` * add `items` getter * chore: unreachable! and typos * implement remove * `*_list` -> `*_items` * fix winit example * add `show_context_menu_for_gtk_window` on `Submenu` type * Add windows implementation * TextMenuItem -> MenuItem, MenuItem trait -> MenuEntry * Add `PredfinedMenuItem` * move internal mod into its own file * update tao example to latest tao's `muda` branch * fix build on linux with latest tao changes * Fix accelerators on Linux * update examples * remove recursive removal of submenus * remvoe gtk menu items recursively * fix tao example on macos * On Windows, remove parents hmenu when removing an item * Add documentation * update README.md * use insert_items with postion 0 for prepend_items * Add menu mnemonics in examples * Add `ContextMenu` trait * Add methods to `ContextMenu` trait necessary for tray icon * fix linux build * fix context menu on gtk * Expose gtk::Menu in ContextMenu trait * Revert context menu to create a gtk::Menu on each call * clippy lints * cleanup crate structure * update docs * Fix doc tests and links * more docs fixes * error handling * macOS implementation (#19) * partial macOS implementation * fix context menu examples * add accelerator support for macOS * strip ampersands from titles on macOS * add CMD_OR_CTRL shorthand for modifiers * implement actions for predefined menu items on macos * fix examples * more predefined items * implement insert for macos * refactor macOS implementation * menu state getters and setters on macOS * implement remove for macOS * code tweaks * add show_context_menu_for_nsview for Submenu on macOS * docs improvements * allow adding item to the same menu multiple times on macOS * implement `items` for macOS * strip only single ampersands from menu titles * add support for menu item actions on macOS * add app name to macOS About, Hide, Quit menu items * add methods to set app window and help menus on macOS * fix clickable submenu titles on macOS * refactor submenu for safe reuse on macOS * fmt & clippy * few cleanups * fix docs * clippy * fix docs * cleanup examples * fix tests * fix clippy?? * use cargo action instead * ??? * Replace popUpContextMenu with popUpMenuPositioningItem Co-authored-by: Caesar Schinas <caesar@caesarschinas.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me>
2022-11-24 03:29:52 +11:00
}
}
impl ContextMenu for Menu {
#[cfg(target_os = "windows")]
fn hpopupmenu(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HMENU {
self.0.borrow().hpopupmenu()
}
#[cfg(target_os = "windows")]
fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) {
self.0.borrow().show_context_menu_for_hwnd(hwnd, position)
}
#[cfg(target_os = "windows")]
fn attach_menu_subclass_for_hwnd(&self, hwnd: isize) {
self.0.borrow().attach_menu_subclass_for_hwnd(hwnd)
}
#[cfg(target_os = "windows")]
fn detach_menu_subclass_from_hwnd(&self, hwnd: isize) {
self.0.borrow().detach_menu_subclass_from_hwnd(hwnd)
}
#[cfg(target_os = "linux")]
fn show_context_menu_for_gtk_window(
&self,
window: &gtk::ApplicationWindow,
position: Option<Position>,
) {
self.0
.borrow_mut()
.show_context_menu_for_gtk_window(window, position)
}
#[cfg(target_os = "linux")]
fn gtk_context_menu(&self) -> gtk::Menu {
self.0.borrow_mut().gtk_context_menu()
}
#[cfg(target_os = "macos")]
fn show_context_menu_for_nsview(&self, view: cocoa::base::id, position: Option<Position>) {
self.0
.borrow_mut()
.show_context_menu_for_nsview(view, position)
}
#[cfg(target_os = "macos")]
fn ns_menu(&self) -> *mut std::ffi::c_void {
self.0.borrow().ns_menu()
}
}