use crate::artprov::IconRole;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MenuId(pub u64);
impl MenuId {
pub const fn get(self) -> u64 {
self.0
}
}
impl std::fmt::Display for MenuId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "menu {}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MenuItemId(pub u64);
impl MenuItemId {
pub const fn get(self) -> u64 {
self.0
}
}
impl std::fmt::Display for MenuItemId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "menu item {}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MenuItemSpec {
pub id: MenuItemId,
pub label: String,
pub shortcut: Option<String>,
pub icon: Option<IconRole>,
pub enabled: bool,
pub visible: bool,
pub checkable: bool,
pub checked: bool,
pub submenu: Option<MenuId>,
}
impl MenuItemSpec {
pub fn new(id: MenuItemId, label: impl Into<String>) -> Self {
Self {
id,
label: label.into(),
shortcut: None,
icon: None,
enabled: true,
visible: true,
checkable: false,
checked: false,
submenu: None,
}
}
pub fn unassigned(label: impl Into<String>) -> Self {
Self::new(MenuItemId(0), label)
}
pub fn with_shortcut(mut self, shortcut: impl Into<String>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn with_icon(mut self, icon: IconRole) -> Self {
self.icon = Some(icon);
self
}
pub fn with_optional_icon(mut self, icon: Option<IconRole>) -> Self {
self.icon = icon;
self
}
pub fn with_icon_identifier(mut self, identifier: &str) -> Self {
self.icon = IconRole::ALL
.iter()
.copied()
.find(|r| r.as_str() == identifier);
self
}
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn with_checkable(mut self) -> Self {
self.checkable = true;
self
}
pub fn with_checked(mut self, checked: bool) -> Self {
self.checkable = true;
self.checked = checked;
self
}
pub fn with_submenu(mut self, submenu: MenuId) -> Self {
self.submenu = Some(submenu);
self
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
pub fn with_text(mut self, text: impl Into<String>) -> Self {
self.label = text.into();
self
}
pub const fn id(&self) -> MenuItemId {
self.id
}
pub fn label(&self) -> &str {
&self.label
}
pub fn text(&self) -> &str {
&self.label
}
pub fn set_label(&mut self, label: impl Into<String>) {
self.label = label.into();
}
pub fn set_text(&mut self, text: impl Into<String>) {
self.label = text.into();
}
pub fn shortcut(&self) -> Option<&str> {
self.shortcut.as_deref()
}
pub fn set_shortcut(&mut self, shortcut: Option<String>) {
self.shortcut = shortcut;
}
pub const fn icon(&self) -> Option<IconRole> {
self.icon
}
pub fn icon_identifier(&self) -> Option<&'static str> {
self.icon.map(|r| r.as_str())
}
pub fn set_icon(&mut self, icon: Option<IconRole>) {
self.icon = icon;
}
pub const fn has_icon(&self) -> bool {
self.icon.is_some()
}
pub const fn is_enabled(&self) -> bool {
self.enabled
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub const fn is_checkable(&self) -> bool {
self.checkable
}
pub const fn is_checked(&self) -> bool {
self.checked
}
pub fn set_checked(&mut self, checked: bool) {
self.checked = checked;
}
pub const fn submenu(&self) -> Option<MenuId> {
self.submenu
}
pub fn translated_label(&self, style: AccessKeyStyle) -> String {
translate_access_keys(&self.label, style)
}
}
impl Default for MenuItemSpec {
fn default() -> Self {
Self {
id: MenuItemId(0),
label: String::new(),
shortcut: None,
icon: None,
enabled: true,
visible: true,
checkable: false,
checked: false,
submenu: None,
}
}
}
pub type MenuItemDescriptor = MenuItemSpec;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MenuItem {
Item(MenuItemSpec),
Separator,
Submenu {
id: MenuId,
label: String,
icon: Option<IconRole>,
items: Vec<MenuItem>,
},
}
impl MenuItem {
pub fn item(spec: MenuItemSpec) -> Self {
Self::Item(spec)
}
pub const fn separator() -> Self {
Self::Separator
}
pub fn submenu(id: MenuId, label: impl Into<String>, items: Vec<MenuItem>) -> Self {
Self::Submenu {
id,
label: label.into(),
icon: None,
items,
}
}
pub fn with_icon(mut self, icon: IconRole) -> Self {
match &mut self {
Self::Item(spec) => {
spec.icon = Some(icon);
}
Self::Submenu { icon: sub_icon, .. } => {
*sub_icon = Some(icon);
}
Self::Separator => {}
}
self
}
pub fn icon(&self) -> Option<IconRole> {
match self {
Self::Item(spec) => spec.icon,
Self::Submenu { icon, .. } => *icon,
Self::Separator => None,
}
}
}
impl From<MenuItemSpec> for MenuItem {
fn from(spec: MenuItemSpec) -> Self {
Self::Item(spec)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MenuSpec {
pub id: Option<MenuId>,
pub title: Option<String>,
pub items: Vec<MenuItemSpec>,
}
impl MenuSpec {
pub fn new() -> Self {
Self::default()
}
pub fn with_id(mut self, id: MenuId) -> Self {
self.id = Some(id);
self
}
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn with_item(mut self, item: MenuItemSpec) -> Self {
self.items.push(item);
self
}
pub fn add_item(&mut self, item: MenuItemSpec) {
self.items.push(item);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AccessKeyStyle {
Ampersand,
Underscore,
Plain,
}
pub fn translate_access_keys(label: &str, style: AccessKeyStyle) -> String {
let marker = match style {
AccessKeyStyle::Ampersand => return label.to_owned(),
AccessKeyStyle::Underscore => Some('_'),
AccessKeyStyle::Plain => None,
};
let mut out = String::with_capacity(label.len());
let mut chars = label.chars().peekable();
while let Some(ch) = chars.next() {
if ch != '&' {
out.push(ch);
continue;
}
match chars.peek() {
Some('&') => {
out.push('&');
chars.next();
}
Some(next) => {
if let Some(marker) = marker {
out.push(marker);
}
let next = *next;
chars.next();
out.push(next);
}
None => out.push('&'),
}
}
out
}
#[cfg(test)]
mod tests {
use super::{translate_access_keys, AccessKeyStyle};
#[test]
fn access_key_markers_translate_per_lane_style() {
assert_eq!(
translate_access_keys("&File", AccessKeyStyle::Ampersand),
"&File"
);
assert_eq!(
translate_access_keys("&File", AccessKeyStyle::Underscore),
"_File"
);
assert_eq!(
translate_access_keys("&File", AccessKeyStyle::Plain),
"File"
);
assert_eq!(
translate_access_keys("New &Folder", AccessKeyStyle::Underscore),
"New _Folder"
);
assert_eq!(
translate_access_keys("New &Folder", AccessKeyStyle::Plain),
"New Folder"
);
}
#[test]
fn access_key_translation_handles_escapes_and_plain_labels() {
assert_eq!(
translate_access_keys("Find && Replace", AccessKeyStyle::Underscore),
"Find & Replace"
);
assert_eq!(
translate_access_keys("Find && Replace", AccessKeyStyle::Plain),
"Find & Replace"
);
assert_eq!(
translate_access_keys("Refresh", AccessKeyStyle::Underscore),
"Refresh"
);
assert_eq!(
translate_access_keys("Refresh", AccessKeyStyle::Ampersand),
"Refresh"
);
assert_eq!(
translate_access_keys("Odd &", AccessKeyStyle::Underscore),
"Odd &"
);
}
#[test]
fn menu_item_spec_builder_and_icon_metadata() {
use crate::artprov::IconRole;
use crate::menus::{MenuItemId, MenuItemSpec};
let spec = MenuItemSpec::new(MenuItemId(1), "&New")
.with_shortcut("Ctrl+N")
.with_icon(IconRole::New)
.with_enabled(true)
.with_checkable()
.with_checked(false);
assert_eq!(spec.id(), MenuItemId(1));
assert_eq!(spec.label(), "&New");
assert_eq!(spec.text(), "&New");
assert_eq!(spec.shortcut(), Some("Ctrl+N"));
assert_eq!(spec.icon(), Some(IconRole::New));
assert_eq!(spec.icon_identifier(), Some("new"));
assert!(spec.has_icon());
assert!(spec.is_enabled());
assert!(spec.is_checkable());
assert!(!spec.is_checked());
assert_eq!(spec.translated_label(AccessKeyStyle::Underscore), "_New");
assert_eq!(spec.translated_label(AccessKeyStyle::Plain), "New");
}
#[test]
fn menu_item_descriptor_alias_and_icon_mutation() {
use crate::artprov::IconRole;
use crate::menus::{MenuItemDescriptor, MenuItemId};
let mut desc: MenuItemDescriptor = MenuItemDescriptor::new(MenuItemId(2), "Save");
assert_eq!(desc.icon(), None);
assert!(!desc.has_icon());
assert_eq!(desc.icon_identifier(), None);
desc.set_icon(Some(IconRole::Save));
assert_eq!(desc.icon(), Some(IconRole::Save));
assert_eq!(desc.icon_identifier(), Some("save"));
assert!(desc.has_icon());
desc.set_icon(None);
assert_eq!(desc.icon(), None);
assert!(!desc.has_icon());
let desc2 = desc.with_icon_identifier("open");
assert_eq!(desc2.icon(), Some(IconRole::Open));
assert_eq!(desc2.icon_identifier(), Some("open"));
}
#[test]
fn menu_item_all_stock_icon_roles_supported() {
use crate::artprov::IconRole;
use crate::menus::{MenuItemId, MenuItemSpec};
for &role in &IconRole::ALL {
let spec = MenuItemSpec::new(MenuItemId(100), "Action").with_icon(role);
assert_eq!(spec.icon(), Some(role));
assert_eq!(spec.icon_identifier(), Some(role.as_str()));
assert!(spec.has_icon());
}
}
#[test]
fn menu_structure_composition_with_icons() {
use crate::artprov::IconRole;
use crate::menus::{MenuId, MenuItem, MenuItemId, MenuItemSpec, MenuSpec};
let item1 = MenuItemSpec::new(MenuItemId(10), "Cut")
.with_shortcut("Ctrl+X")
.with_icon(IconRole::Delete);
let item2 = MenuItemSpec::new(MenuItemId(11), "Copy")
.with_shortcut("Ctrl+C")
.with_icon(IconRole::Copy);
let menu_item1 = MenuItem::item(item1.clone());
assert_eq!(menu_item1.icon(), Some(IconRole::Delete));
let sep = MenuItem::separator();
assert_eq!(sep.icon(), None);
let submenu = MenuItem::submenu(MenuId(2), "More", vec![MenuItem::item(item2.clone())])
.with_icon(IconRole::Preferences);
assert_eq!(submenu.icon(), Some(IconRole::Preferences));
let menu_spec = MenuSpec::new()
.with_id(MenuId(1))
.with_title("Edit")
.with_item(item1)
.with_item(item2);
assert_eq!(menu_spec.items.len(), 2);
assert_eq!(menu_spec.items[0].icon, Some(IconRole::Delete));
assert_eq!(menu_spec.items[1].icon, Some(IconRole::Copy));
}
}