Support for removing menus

This commit is contained in:
Daniel Collin 2016-05-08 13:27:55 +02:00
parent c6f61804e6
commit 0ffa3bf196
4 changed files with 44 additions and 25 deletions

View file

@ -40,7 +40,7 @@ fn main() {
menu.add_sub_menu("Sub Test", &sub); menu.add_sub_menu("Sub Test", &sub);
let _ = window.add_menu(&menu); let menu_handle = window.add_menu(&menu);
let mut color_mul = 1; let mut color_mul = 1;
@ -64,7 +64,7 @@ fn main() {
} }
CLOSE_MENU_ID => { CLOSE_MENU_ID => {
println!("remove menu"); println!("remove menu");
//window.remove_menu( window.remove_menu(menu_handle);
} }
_ => (), _ => (),
} }

View file

@ -464,27 +464,27 @@ impl Window {
/// ///
#[inline] #[inline]
pub fn add_menu(&mut self, menu: &Menu) -> Result<()> { pub fn add_menu(&mut self, menu: &Menu) -> MenuHandle {
self.0.add_menu(&menu.0) self.0.add_menu(&menu.0)
} }
/*
/// ///
/// Updates an existing menu created with [add_menu] /// Updates an existing menu created with [add_menu]
/// ///
/*
#[inline] #[inline]
pub fn update_menu(&mut self, menu_name: &str, menu: &Vec<Menu>) -> Result<()> { pub fn update_menu(&mut self, menu_name: &str, menu: &Vec<Menu>) -> Result<()> {
self.0.update_menu(menu_name, menu) self.0.update_menu(menu_name, menu)
} }
*/
/// ///
/// Remove a menu that has been added with [add_menu] /// Remove a menu that has been added with [add_menu]
/// ///
#[inline] #[inline]
pub fn remove_menu(&mut self, menu_name: &str) -> Result<()> { pub fn remove_menu(&mut self, handle: MenuHandle) {
self.0.remove_menu(menu_name) self.0.remove_menu(handle)
} }
*/
/// ///
/// Check if a menu item has been pressed /// Check if a menu item has been pressed
@ -511,9 +511,12 @@ const MENU_ID_SEPARATOR:usize = 0xffffffff;
pub struct Menu(imp::Menu); pub struct Menu(imp::Menu);
#[derive(Debug)] #[derive(Copy, Clone)]
pub struct MenuItemHandle(pub u64); pub struct MenuItemHandle(pub u64);
#[derive(Copy, Clone, PartialEq)]
pub struct MenuHandle(pub u64);
impl Menu { impl Menu {
pub fn new(name: &str) -> Result<Menu> { pub fn new(name: &str) -> Result<Menu> {
imp::Menu::new(name).map(Menu) imp::Menu::new(name).map(Menu)

View file

@ -416,7 +416,7 @@ void mfb_add_menu(void* window, const char* name, void* m)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mfb_add_menu(void* window, void* m) uint64_t mfb_add_menu(void* window, void* m)
{ {
OSXWindow* win = (OSXWindow*)window; OSXWindow* win = (OSXWindow*)window;
NSMenu* menu = (NSMenu*)m; NSMenu* menu = (NSMenu*)m;
@ -426,6 +426,17 @@ void mfb_add_menu(void* window, void* m)
NSMenuItem* windowMenuItem = [main_menu addItemWithTitle:@"" action:NULL keyEquivalent:@""]; NSMenuItem* windowMenuItem = [main_menu addItemWithTitle:@"" action:NULL keyEquivalent:@""];
[NSApp setWindowsMenu:menu]; [NSApp setWindowsMenu:menu];
[windowMenuItem setSubmenu:menu]; [windowMenuItem setSubmenu:menu];
return (uint64_t)menu;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mfb_remove_menu_at(void* window, int index)
{
(void)window;
NSMenu* main_menu = [NSApp mainMenu];
[main_menu removeItemAtIndex:index];
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -8,7 +8,7 @@ use Result;
use InputCallback; use InputCallback;
use mouse_handler; use mouse_handler;
use window_flags; use window_flags;
use {MenuItem, MenuItemHandle}; use {MenuItem, MenuItemHandle, MenuHandle};
// use menu::Menu; // use menu::Menu;
use libc::{c_void, c_char, c_uchar}; use libc::{c_void, c_char, c_uchar};
@ -171,12 +171,12 @@ extern "C" {
fn mfb_should_close(window: *mut c_void) -> i32; fn mfb_should_close(window: *mut c_void) -> i32;
fn mfb_get_screen_size() -> u32; fn mfb_get_screen_size() -> u32;
fn mfb_is_active(window: *mut c_void) -> u32; fn mfb_is_active(window: *mut c_void) -> u32;
fn mfb_add_menu(window: *mut c_void, menu: *mut c_void); fn mfb_add_menu(window: *mut c_void, menu: *mut c_void) -> u64;
fn mfb_add_sub_menu(parent_menu: *mut c_void, name: *const c_char, 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_active_menu(window: *mut c_void) -> i32;
fn mfb_create_menu(name: *const c_char) -> *mut c_void; fn mfb_create_menu(name: *const c_char) -> *mut c_void;
// fn mfb_destroy_menu(menu_item: *mut c_void); fn mfb_remove_menu_at(window: *mut c_void, index: i32);
fn mfb_add_menu_item(menu_item: *mut c_void, fn mfb_add_menu_item(menu_item: *mut c_void,
menu_id: i32, menu_id: i32,
@ -206,6 +206,7 @@ pub struct Window {
pub shared_data: SharedData, pub shared_data: SharedData,
key_handler: KeyHandler, key_handler: KeyHandler,
pub has_set_data: bool, pub has_set_data: bool,
menus: Vec<MenuHandle>,
} }
unsafe extern "C" fn key_callback(window: *mut c_void, key: i32, state: i32) { unsafe extern "C" fn key_callback(window: *mut c_void, key: i32, state: i32) {
@ -265,6 +266,7 @@ impl Window {
}, },
key_handler: KeyHandler::new(), key_handler: KeyHandler::new(),
has_set_data: false, has_set_data: false,
menus: Vec::new(),
}) })
} }
} }
@ -392,12 +394,25 @@ impl Window {
} }
} }
pub fn add_menu(&mut self, menu: &Menu) -> Result<()> { pub fn add_menu(&mut self, menu: &Menu) -> MenuHandle {
unsafe { unsafe {
mfb_add_menu(self.window_handle, menu.menu_handle); let handle = MenuHandle(mfb_add_menu(self.window_handle, menu.menu_handle));
self.menus.push(handle);
handle
} }
}
Ok(()) pub fn remove_menu(&mut self, handle: MenuHandle) {
for i in 0..self.menus.len() {
if self.menus[i] == handle {
self.menus.remove(i);
unsafe {
// + 1 here as we always have a default menu we shouldn't remove
mfb_remove_menu_at(self.window_handle, (i + 1) as i32);
}
return;
}
}
} }
#[inline] #[inline]
@ -579,8 +594,6 @@ impl Menu {
let item_name = CString::new(item.label.as_str()).unwrap(); let item_name = CString::new(item.label.as_str()).unwrap();
let conv_key = Self::map_key_to_menu_key(item.key); let conv_key = Self::map_key_to_menu_key(item.key);
println!("menu id {}", item.id);
MenuItemHandle(mfb_add_menu_item(self.menu_handle, MenuItemHandle(mfb_add_menu_item(self.menu_handle,
item.id as i32, item.id as i32,
item_name.as_ptr(), item_name.as_ptr(),
@ -605,11 +618,3 @@ impl Drop for Window {
} }
} }
// impl Drop for Menu {
// fn drop(&mut self) {
// unsafe {
// mfb_destroy_menu(self.menu_handle);
// }
// }
// }
//