refactor: rename TextMenuItem to just MeuItem

This commit is contained in:
amrbashir 2022-06-14 14:00:00 +02:00
parent ccf548d199
commit 00a0270b8f
No known key found for this signature in database
GPG key ID: BBD7A47A2003FF33
7 changed files with 60 additions and 64 deletions

View file

@ -18,8 +18,8 @@ fn main() {
let mut menu_bar = Menu::new();
let mut file_menu = menu_bar.add_submenu("&File", true);
let mut open_item = file_menu.add_text_item("&Open", true, None);
let mut save_item = file_menu.add_text_item("&Save", true, Some("CommandOrCtrl+S"));
let mut open_item = file_menu.add_item("&Open", true, None);
let mut save_item = file_menu.add_item("&Save", true, Some("CommandOrCtrl+S"));
file_menu.add_native_item(NativeMenuItem::Minimize);
file_menu.add_native_item(NativeMenuItem::CloseWindow);
file_menu.add_native_item(NativeMenuItem::Quit);

View file

@ -36,8 +36,8 @@ fn main() {
let _window2 = WindowBuilder::new().build(&event_loop).unwrap();
let mut file_menu = menu_bar.add_submenu("&File", true);
let mut open_item = file_menu.add_text_item("&Open", true, None);
let mut save_item = file_menu.add_text_item("&Save", true, Some("CommandOrCtrl+S"));
let mut open_item = file_menu.add_item("&Open", true, None);
let mut save_item = file_menu.add_item("&Save", true, Some("CommandOrCtrl+S"));
file_menu.add_native_item(NativeMenuItem::Minimize);
file_menu.add_native_item(NativeMenuItem::CloseWindow);
file_menu.add_native_item(NativeMenuItem::Quit);

View file

@ -17,7 +17,7 @@
//!
//! # Aadding menu items and submenus within another submenu
//!
//! Once you have a [`Submenu`] you can star creating more [`Submenu`]s or [`TextMenuItem`]s.
//! Once you have a [`Submenu`] you can star creating more [`Submenu`]s or [`MenuItem`]s.
//! ```no_run
//! let mut menu = Menu::new();
//!
@ -67,12 +67,9 @@ mod platform_impl;
static MENU_CHANNEL: Lazy<(Sender<MenuEvent>, Receiver<MenuEvent>)> = Lazy::new(|| unbounded());
/// A type alias to the receiver of the menu events channel.
pub type MenuEventReceiver = Receiver<MenuEvent>;
/// Gets a reference to the event channel's [MenuEventReceiver]
/// Gets a reference to the event channel's [Receiver<MenuEvent>]
/// which can be used to listen for menu events.
pub fn menu_event_receiver<'a>() -> &'a MenuEventReceiver {
pub fn menu_event_receiver<'a>() -> &'a Receiver<MenuEvent> {
&MENU_CHANNEL.1
}
@ -269,7 +266,7 @@ impl Submenu {
Submenu(self.0.add_submenu(label, enabled))
}
/// Creates a new [`TextMenuItem`] whithin this submenu.
/// Creates a new [`MenuItem`] whithin this submenu.
///
/// ## Platform-specific:
///
@ -277,13 +274,13 @@ impl Submenu {
/// For example, using `&Save` for the save menu item would result in the label gets an underline under the `S`,
/// and the `&` character is not displayed on menu item label.
/// Then the menu item can be activated by press `S` when its parent menu is active.
pub fn add_text_item<S: AsRef<str>>(
pub fn add_item<S: AsRef<str>>(
&mut self,
label: S,
enabled: bool,
accelerator: Option<&str>,
) -> TextMenuItem {
TextMenuItem(self.0.add_text_item(label, enabled, accelerator))
) -> MenuItem {
MenuItem(self.0.add_item(label, enabled, accelerator))
}
/// Creates a new [`NativeMenuItem`] within this submenu.
@ -303,11 +300,11 @@ impl Submenu {
}
}
/// This is a Text menu item within a [`Submenu`].
/// This is a normal menu item within a [`Submenu`].
#[derive(Clone)]
pub struct TextMenuItem(platform_impl::TextMenuItem);
pub struct MenuItem(platform_impl::MenuItem);
impl TextMenuItem {
impl MenuItem {
/// Gets the menu item's current label.
pub fn label(&self) -> String {
self.0.label()
@ -334,7 +331,7 @@ impl TextMenuItem {
}
}
/// This is a Check menu item within a [`Submenu`].
/// This is a menu item with a checkmark icon within a [`Submenu`].
#[derive(Clone)]
pub struct CheckMenuItem(platform_impl::CheckMenuItem);

View file

@ -26,14 +26,14 @@ enum MenuEntryType {
// keeps a vector of a [`gtk::MenuItem`] or a tuple of [`gtk::MenuItem`] and [`gtk::Menu`] if its a menu
// and push to it every time [`Menu::init_for_gtk_window`] is called.
Submenu(Vec<(gtk::MenuItem, gtk::Menu)>),
Text(Vec<gtk::MenuItem>),
Check(Vec<gtk::CheckMenuItem>),
Native(NativeMenuItem),
MenuItem(Vec<gtk::MenuItem>),
CheckMenuItem(Vec<gtk::CheckMenuItem>),
NativeMenuItem(NativeMenuItem),
}
impl Default for MenuEntryType {
fn default() -> Self {
Self::Text(Default::default())
Self::MenuItem(Default::default())
}
}
@ -243,19 +243,19 @@ impl Submenu {
Submenu(entry, Rc::clone(&self.1))
}
pub fn add_text_item<S: AsRef<str>>(
pub fn add_item<S: AsRef<str>>(
&mut self,
label: S,
enabled: bool,
accelerator: Option<&str>,
) -> TextMenuItem {
) -> MenuItem {
let label = label.as_ref().to_string();
let id = COUNTER.next();
let entry = Rc::new(RefCell::new(MenuEntry {
label: label.clone(),
enabled,
r#type: MenuEntryType::Text(Vec::new()),
r#type: MenuEntryType::MenuItem(Vec::new()),
id,
accelerator: accelerator.map(|s| s.to_string()),
..Default::default()
@ -265,7 +265,7 @@ impl Submenu {
if let MenuEntryType::Submenu(native_menus) = &mut inner.r#type {
for (_, menu) in native_menus {
let item = create_gtk_text_menu_item(
let item = create_gtk_menu_item(
&label,
enabled,
&accelerator.map(|s| s.to_string()),
@ -273,14 +273,14 @@ impl Submenu {
&*self.1,
);
menu.append(&item);
if let MenuEntryType::Text(native_items) = &mut entry.borrow_mut().r#type {
if let MenuEntryType::MenuItem(native_items) = &mut entry.borrow_mut().r#type {
native_items.push(item);
}
}
}
inner.entries.as_mut().unwrap().push(entry.clone());
TextMenuItem(entry)
MenuItem(entry)
}
pub fn add_native_item(&mut self, item: NativeMenuItem) {
@ -293,7 +293,7 @@ impl Submenu {
}
let entry = Rc::new(RefCell::new(MenuEntry {
r#type: MenuEntryType::Native(item),
r#type: MenuEntryType::NativeMenuItem(item),
..Default::default()
}));
inner.entries.as_mut().unwrap().push(entry);
@ -313,7 +313,7 @@ impl Submenu {
label: label.clone(),
enabled,
checked,
r#type: MenuEntryType::Check(Vec::new()),
r#type: MenuEntryType::CheckMenuItem(Vec::new()),
id,
accelerator: accelerator.map(|s| s.to_string()),
..Default::default()
@ -332,7 +332,7 @@ impl Submenu {
&*self.1,
);
menu.append(&item);
if let MenuEntryType::Check(native_items) = &mut entry.borrow_mut().r#type {
if let MenuEntryType::CheckMenuItem(native_items) = &mut entry.borrow_mut().r#type {
native_items.push(item);
}
}
@ -344,9 +344,9 @@ impl Submenu {
}
#[derive(Clone)]
pub struct TextMenuItem(Rc<RefCell<MenuEntry>>);
pub struct MenuItem(Rc<RefCell<MenuEntry>>);
impl TextMenuItem {
impl MenuItem {
pub fn label(&self) -> String {
self.0.borrow().label.clone()
}
@ -354,7 +354,7 @@ impl TextMenuItem {
pub fn set_label<S: AsRef<str>>(&mut self, label: S) {
let label = label.as_ref().to_string();
let mut entry = self.0.borrow_mut();
if let MenuEntryType::Text(native_items) = &mut entry.r#type {
if let MenuEntryType::MenuItem(native_items) = &mut entry.r#type {
for item in native_items {
item.set_label(&to_gtk_menemenoic(&label));
}
@ -368,7 +368,7 @@ impl TextMenuItem {
pub fn set_enabled(&mut self, enabled: bool) {
let mut entry = self.0.borrow_mut();
if let MenuEntryType::Text(native_items) = &mut entry.r#type {
if let MenuEntryType::MenuItem(native_items) = &mut entry.r#type {
for item in native_items {
item.set_sensitive(enabled);
}
@ -392,7 +392,7 @@ impl CheckMenuItem {
pub fn set_label<S: AsRef<str>>(&mut self, label: S) {
let label = label.as_ref().to_string();
let mut entry = self.0.borrow_mut();
if let MenuEntryType::Text(native_items) = &mut entry.r#type {
if let MenuEntryType::CheckMenuItem(native_items) = &mut entry.r#type {
for item in native_items {
item.set_label(&to_gtk_menemenoic(&label));
}
@ -406,7 +406,7 @@ impl CheckMenuItem {
pub fn set_enabled(&mut self, enabled: bool) {
let mut entry = self.0.borrow_mut();
if let MenuEntryType::Check(native_items) = &mut entry.r#type {
if let MenuEntryType::CheckMenuItem(native_items) = &mut entry.r#type {
for item in native_items {
item.set_sensitive(enabled);
}
@ -417,19 +417,18 @@ impl CheckMenuItem {
pub fn checked(&self) -> bool {
let entry = self.0.borrow();
let mut checked = entry.checked;
if let MenuEntryType::Check(native_items) = &entry.r#type {
if let MenuEntryType::CheckMenuItem(native_items) = &entry.r#type {
if let Some(item) = native_items.get(0) {
checked = item.is_active();
checked = item.is_active();
}
}
checked
}
pub fn set_checked(&mut self, checked: bool) {
let mut entry = self.0.borrow_mut();
if let MenuEntryType::Check(native_items) = &mut entry.r#type {
if let MenuEntryType::CheckMenuItem(native_items) = &mut entry.r#type {
for item in native_items {
item.set_active(checked);
}
@ -456,8 +455,8 @@ fn add_entries_to_menu<M: IsA<gtk::MenuShell>>(
add_entries_to_menu(&submenu, entry.entries.as_ref().unwrap(), accel_group);
(Some(item), Some(submenu))
}
MenuEntryType::Text(_) => {
let item = create_gtk_text_menu_item(
MenuEntryType::MenuItem(_) => {
let item = create_gtk_menu_item(
&entry.label,
entry.enabled,
&entry.accelerator,
@ -467,7 +466,7 @@ fn add_entries_to_menu<M: IsA<gtk::MenuShell>>(
gtk_menu.append(&item);
(Some(item), None)
}
MenuEntryType::Check(_) => {
MenuEntryType::CheckMenuItem(_) => {
let item = create_gtk_check_menu_item(
&entry.label,
entry.enabled,
@ -479,7 +478,7 @@ fn add_entries_to_menu<M: IsA<gtk::MenuShell>>(
gtk_menu.append(&item);
(Some(item.upcast::<gtk::MenuItem>()), None)
}
MenuEntryType::Native(native_menu_item) => {
MenuEntryType::NativeMenuItem(native_menu_item) => {
native_menu_item.add_to_gtk_menu(gtk_menu);
(None, None)
}
@ -489,13 +488,13 @@ fn add_entries_to_menu<M: IsA<gtk::MenuShell>>(
MenuEntryType::Submenu(native_menus) => {
native_menus.push((item.unwrap(), submenu.unwrap()));
}
MenuEntryType::Text(native_items) => {
MenuEntryType::MenuItem(native_items) => {
native_items.push(item.unwrap());
}
MenuEntryType::Check(native_items) => {
MenuEntryType::CheckMenuItem(native_items) => {
native_items.push(item.unwrap().downcast().unwrap());
}
MenuEntryType::Native(_) => {}
MenuEntryType::NativeMenuItem(_) => {}
};
}
}
@ -509,7 +508,7 @@ fn create_gtk_submenu(label: &str, enabled: bool) -> (gtk::MenuItem, gtk::Menu)
(item, menu)
}
fn create_gtk_text_menu_item(
fn create_gtk_menu_item(
label: &str,
enabled: bool,
accelerator: &Option<String>,

View file

@ -18,13 +18,13 @@ use std::sync::Once;
static COUNTER: Counter = Counter::new();
#[derive(Debug, Clone)]
pub struct TextMenuItem {
pub struct MenuItem {
pub(crate) id: u64,
pub(crate) ns_menu_item: id,
label: Rc<str>,
}
impl TextMenuItem {
impl MenuItem {
pub fn new<S: AsRef<str>>(
label: S,
enabled: bool,

View file

@ -9,7 +9,7 @@ use cocoa::{
};
use objc::{msg_send, sel, sel_impl};
pub use menu_item::TextMenuItem;
pub use menu_item::MenuItem;
use self::accelerator::remove_mnemonic;
@ -27,7 +27,7 @@ impl Menu {
pub fn add_submenu<S: AsRef<str>>(&mut self, label: S, enabled: bool) -> Submenu {
let menu = Menu::new();
let menu_item = TextMenuItem::new("", enabled, sel!(fireMenubarAction:), None);
let menu_item = MenuItem::new("", enabled, sel!(fireMenubarAction:), None);
unsafe {
menu_item.ns_menu_item.setSubmenu_(menu.0);
@ -56,7 +56,7 @@ impl Menu {
#[derive(Debug, Clone)]
pub struct Submenu {
pub(crate) menu: Menu,
pub(crate) menu_item: TextMenuItem,
pub(crate) menu_item: MenuItem,
}
impl Submenu {
@ -85,13 +85,13 @@ impl Submenu {
self.menu.add_submenu(label, enabled)
}
pub fn add_text_item<S: AsRef<str>>(
pub fn add_item<S: AsRef<str>>(
&mut self,
label: S,
enabled: bool,
accelerator: Option<&str>,
) -> TextMenuItem {
let item = TextMenuItem::new(label, enabled, sel!(fireMenubarAction:), accelerator);
) -> MenuItem {
let item = MenuItem::new(label, enabled, sel!(fireMenubarAction:), accelerator);
unsafe {
self.menu.0.addItem_(item.ns_menu_item);
}

View file

@ -191,12 +191,12 @@ impl Submenu {
}
}
pub fn add_text_item<S: AsRef<str>>(
pub fn add_item<S: AsRef<str>>(
&mut self,
label: S,
enabled: bool,
accelerator: Option<&str>,
) -> TextMenuItem {
) -> MenuItem {
let id = COUNTER.next();
let mut flags = MF_STRING;
if !enabled {
@ -222,7 +222,7 @@ impl Submenu {
}
unsafe { AppendMenuW(self.hmenu, flags, id as _, encode_wide(label).as_ptr()) };
TextMenuItem {
MenuItem {
id,
parent_hmenu: self.hmenu,
}
@ -270,7 +270,7 @@ impl Submenu {
checked: bool,
accelerator: Option<&str>,
) -> CheckMenuItem {
let mut item = CheckMenuItem(self.add_text_item(label, enabled, accelerator));
let mut item = CheckMenuItem(self.add_item(label, enabled, accelerator));
item.set_checked(checked);
unsafe { CHECK_MENU_ITEMS.push(item.clone()) };
item
@ -278,12 +278,12 @@ impl Submenu {
}
#[derive(Clone)]
pub struct TextMenuItem {
pub struct MenuItem {
id: u64,
parent_hmenu: HMENU,
}
impl TextMenuItem {
impl MenuItem {
pub fn label(&self) -> String {
self.label_with_accel()
.split("\t")
@ -352,7 +352,7 @@ impl TextMenuItem {
}
#[derive(Clone)]
pub struct CheckMenuItem(TextMenuItem);
pub struct CheckMenuItem(MenuItem);
impl CheckMenuItem {
pub fn label(&self) -> String {