diff --git a/src/lib.rs b/src/lib.rs index 974c227..12004c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -529,6 +529,10 @@ impl Menu { self.0.add_sub_menu(name, &menu.0) } + pub fn add_separator(&mut self) { + self.add_menu_item(&MenuItem { id: MENU_ID_SEPARATOR, ..MenuItem::default() }); + } + #[inline] pub fn add_menu_item(&mut self, item: &MenuItem) -> MenuItemHandle { self.0.add_menu_item(item) diff --git a/src/native/macosx/MacMiniFB.m b/src/native/macosx/MacMiniFB.m index fea6c83..a22331f 100644 --- a/src/native/macosx/MacMiniFB.m +++ b/src/native/macosx/MacMiniFB.m @@ -581,6 +581,14 @@ void build_submenu(NSMenu* menu, MenuDesc* desc) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +static NSString* get_string_for_key(uint32_t t) { + unichar c = (unichar)t; + NSString* key = [NSString stringWithCharacters:&c length:1]; + return key; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + uint64_t mfb_add_menu_item( void* in_menu, int32_t menu_id, @@ -601,10 +609,13 @@ uint64_t mfb_add_menu_item( } else { + NSString* key_string = 0; int mask = 0; NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(onMenuPress:) keyEquivalent:@""]; [newItem setTag:menu_id]; + printf("set menu id %d\n", menu_id); + // This code may look a bit weird but is here for a reason: // // In order to make it easier to bulid cross-platform apps Ctrl is often used as @@ -628,15 +639,30 @@ uint64_t mfb_add_menu_item( mask |= NSAlternateKeyMask; } - if (key != 0x7f) { - NSString* key_string = convert_key_code_to_string(key); - - if (key_string) { - [newItem setKeyEquivalentModifierMask: mask]; - [newItem setKeyEquivalent:key_string]; + switch (key) { + case 0x7a: { key_string = get_string_for_key(NSF1FunctionKey); break; } // F1 + case 0x78: { key_string = get_string_for_key(NSF2FunctionKey); break; } // F2 + case 0x63: { key_string = get_string_for_key(NSF3FunctionKey); break; } // F3 + case 0x76: { key_string = get_string_for_key(NSF4FunctionKey); break; } // F4 + case 0x60: { key_string = get_string_for_key(NSF5FunctionKey); break; } // F5 + case 0x61: { key_string = get_string_for_key(NSF6FunctionKey); break; } // F6 + case 0x62: { key_string = get_string_for_key(NSF7FunctionKey); break; } // F7 + case 0x64: { key_string = get_string_for_key(NSF8FunctionKey); break; } // F8 + case 0x65: { key_string = get_string_for_key(NSF9FunctionKey); break; } // F9 + case 0x6d: { key_string = get_string_for_key(NSF10FunctionKey); break; } // F10 + case 0x67: { key_string = get_string_for_key(NSF11FunctionKey); break; } // F11 + case 0x6f: { key_string = get_string_for_key(NSF12FunctionKey); break; } // F12 + case 0x7f: break; + default: { + key_string = convert_key_code_to_string(key); } } + if (key_string) { + [newItem setKeyEquivalentModifierMask: mask]; + [newItem setKeyEquivalent:key_string]; + } + if (enabled) { [newItem setEnabled:YES]; } else { diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 0f2f558..2be88ae 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -173,6 +173,7 @@ extern "C" { fn mfb_is_active(window: *mut c_void) -> u32; fn mfb_add_menu(window: *mut c_void, menu: *mut c_void); fn mfb_add_sub_menu(parent_menu: *mut c_void, name: *const c_char, menu: *mut c_void); + fn mfb_active_menu(window: *mut c_void) -> i32; fn mfb_create_menu(name: *const c_char) -> *mut c_void; // fn mfb_destroy_menu(menu_item: *mut c_void); @@ -382,7 +383,7 @@ impl Window { } pub fn is_menu_pressed(&mut self) -> Option { - let menu_id = 0; //unsafe { mfb_active_menu(self.window_handle) }; + let menu_id = unsafe { mfb_active_menu(self.window_handle) }; if menu_id < 0 { None @@ -578,6 +579,8 @@ impl Menu { let item_name = CString::new(item.label.as_str()).unwrap(); let conv_key = Self::map_key_to_menu_key(item.key); + println!("menu id {}", item.id); + MenuItemHandle(mfb_add_menu_item(self.menu_handle, item.id as i32, item_name.as_ptr(),