Format code

This commit is contained in:
Federico Terzi 2021-01-31 14:27:45 +01:00
parent b04ca73641
commit e77e013ae7
3 changed files with 37 additions and 63 deletions

View File

@ -1,6 +1,6 @@
pub mod menu;
pub mod event; pub mod event;
pub mod icons; pub mod icons;
pub mod menu;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub mod win32; pub mod win32;

View File

@ -33,10 +33,16 @@ impl Menu {
self.raw_ids.get(&raw_id).cloned() self.raw_ids.get(&raw_id).cloned()
} }
fn generate_raw_id(raw_ids: &mut HashMap<u32, String>, ids: &mut HashSet<String>, current: &mut u32, item: &mut MenuItem) -> Result<()> { fn generate_raw_id(
raw_ids: &mut HashMap<u32, String>,
ids: &mut HashSet<String>,
current: &mut u32,
item: &mut MenuItem,
) -> Result<()> {
match item { match item {
MenuItem::Simple(simple_item) => { MenuItem::Simple(simple_item) => {
if ids.contains::<str>(&simple_item.id) { // Duplicate id, throw error if ids.contains::<str>(&simple_item.id) {
// Duplicate id, throw error
Err(MenuError::DuplicateMenuId(simple_item.id.to_string()).into()) Err(MenuError::DuplicateMenuId(simple_item.id.to_string()).into())
} else { } else {
ids.insert(simple_item.id.to_string()); ids.insert(simple_item.id.to_string());
@ -46,7 +52,7 @@ impl Menu {
Ok(()) Ok(())
} }
} }
MenuItem::Sub(SubMenuItem { items, ..}) => { MenuItem::Sub(SubMenuItem { items, .. }) => {
for sub_item in items.iter_mut() { for sub_item in items.iter_mut() {
Self::generate_raw_id(raw_ids, ids, current, sub_item)? Self::generate_raw_id(raw_ids, ids, current, sub_item)?
} }
@ -114,25 +120,17 @@ mod tests {
#[test] #[test]
fn test_context_menu_serializes_correctly() { fn test_context_menu_serializes_correctly() {
let menu = Menu::from(vec![ let menu = Menu::from(vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("open", "Open")),
"open",
"Open",
)),
MenuItem::Separator, MenuItem::Separator,
MenuItem::Sub(SubMenuItem::new( MenuItem::Sub(SubMenuItem::new(
"Sub", "Sub",
vec![ vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("sub1", "Sub 1")),
"sub1", MenuItem::Simple(SimpleMenuItem::new("sub2", "Sub 2")),
"Sub 1", ],
)), )),
MenuItem::Simple(SimpleMenuItem::new( ])
"sub2", .unwrap();
"Sub 2",
)),
]),
),
]).unwrap();
assert_eq!( assert_eq!(
menu.to_json().unwrap(), menu.to_json().unwrap(),
r#"[{"type":"simple","id":"open","label":"Open","raw_id":0},{"type":"separator"},{"type":"sub","label":"Sub","items":[{"type":"simple","id":"sub1","label":"Sub 1","raw_id":1},{"type":"simple","id":"sub2","label":"Sub 2","raw_id":2}]}]"# r#"[{"type":"simple","id":"open","label":"Open","raw_id":0},{"type":"separator"},{"type":"sub","label":"Sub","items":[{"type":"simple","id":"sub1","label":"Sub 1","raw_id":1},{"type":"simple","id":"sub2","label":"Sub 2","raw_id":2}]}]"#
@ -142,62 +140,38 @@ mod tests {
#[test] #[test]
fn test_context_menu_raw_ids_work_correctly() { fn test_context_menu_raw_ids_work_correctly() {
let menu = Menu::from(vec![ let menu = Menu::from(vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("open", "Open")),
"open",
"Open",
)),
MenuItem::Separator, MenuItem::Separator,
MenuItem::Sub(SubMenuItem::new( MenuItem::Sub(SubMenuItem::new(
"Sub", "Sub",
vec![ vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("sub1", "Sub 1")),
"sub1", MenuItem::Simple(SimpleMenuItem::new("sub2", "Sub 2")),
"Sub 1", ],
)), )),
MenuItem::Simple(SimpleMenuItem::new( ])
"sub2", .unwrap();
"Sub 2", assert_eq!(menu.get_item_id(0).unwrap(), "open");
)), assert_eq!(menu.get_item_id(1).unwrap(), "sub1");
]), assert_eq!(menu.get_item_id(2).unwrap(), "sub2");
),
]).unwrap();
assert_eq!(
menu.get_item_id(0).unwrap(),
"open"
);
assert_eq!(
menu.get_item_id(1).unwrap(),
"sub1"
);
assert_eq!(
menu.get_item_id(2).unwrap(),
"sub2"
);
} }
#[test] #[test]
fn test_context_menu_detects_duplicate_ids() { fn test_context_menu_detects_duplicate_ids() {
let menu = Menu::from(vec![ let menu = Menu::from(vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("open", "Open")),
"open",
"Open",
)),
MenuItem::Separator, MenuItem::Separator,
MenuItem::Sub(SubMenuItem::new( MenuItem::Sub(SubMenuItem::new(
"Sub", "Sub",
vec![ vec![
MenuItem::Simple(SimpleMenuItem::new( MenuItem::Simple(SimpleMenuItem::new("open", "Sub 1")),
"open", MenuItem::Simple(SimpleMenuItem::new("sub2", "Sub 2")),
"Sub 1", ],
)), )),
MenuItem::Simple(SimpleMenuItem::new(
"sub2",
"Sub 2",
)),
]),
),
]); ]);
assert!(matches!(menu.unwrap_err().downcast::<MenuError>().unwrap(), assert!(
MenuError::DuplicateMenuId(id) if id == "open")); matches!(menu.unwrap_err().downcast::<MenuError>().unwrap(),
MenuError::DuplicateMenuId(id) if id == "open")
);
} }
} }

View File

@ -186,7 +186,7 @@ impl Win32EventLoop {
self.handle.store(handle, Ordering::Release); self.handle.store(handle, Ordering::Release);
// TODO: explain // Make sure the run() method is called in the same thread as initialize()
self self
._init_thread_id ._init_thread_id
.fill(std::thread::current().id()) .fill(std::thread::current().id())