Skip to content

Commit

Permalink
feat(menu): Add window_id to MenuEvent (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemarier authored Jun 22, 2021
1 parent 435d17a commit 96651dc
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changes/menu-windowid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Add `window_id` to `MenuEvent`.
7 changes: 7 additions & 0 deletions examples/custom_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ fn main() {
window.request_redraw();
}
Event::MenuEvent {
window_id,
menu_id,
origin: MenuType::MenuBar,
} if menu_id == test_menu_item.clone().id() => {
println!("Clicked on `Disable menu`");
if window_id == Some(window.id()) {
println!("Window ID match!");
}
// this allow us to get access to the menu and make changes
// without re-rendering the whole menu
test_menu_item.set_enabled(false);
Expand All @@ -98,6 +102,7 @@ fn main() {
Event::MenuEvent {
menu_id,
origin: MenuType::MenuBar,
..
} if menu_id == change_menu.clone().id() => {
println!("Clicked on `Change menu`");
// set new menu
Expand All @@ -110,12 +115,14 @@ fn main() {
Event::MenuEvent {
menu_id,
origin: MenuType::MenuBar,
..
} if menu_id == custom_insert_clipboard.clone().id() => {
cliboard.write_text("This is injected from tao!!!")
}
Event::MenuEvent {
menu_id,
origin: MenuType::MenuBar,
..
} if menu_id == custom_read_clipboard.clone().id() => {
println!("Clipboard content: {:?}", cliboard.read_text());
}
Expand Down
1 change: 1 addition & 0 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ fn main() {
menu_id,
// specify only context menu's
origin: MenuType::ContextMenu,
..
} => {
// Click on Open new window or focus item
if menu_id == open_new_window_element.clone().id()
Expand Down
33 changes: 29 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ pub enum Event<'a, T: 'static> {

/// Emitted when a menu has been clicked. There are two types of menu event. One comes from the
/// menu bar, the other comes from the status bar.
MenuEvent { menu_id: MenuId, origin: MenuType },
MenuEvent {
window_id: Option<WindowId>,
menu_id: MenuId,
origin: MenuType,
},

/// Emitted when tray has been clicked.
///
Expand Down Expand Up @@ -166,7 +170,12 @@ impl<T: Clone> Clone for Event<'static, T> {
LoopDestroyed => LoopDestroyed,
Suspended => Suspended,
Resumed => Resumed,
MenuEvent { menu_id, origin } => MenuEvent {
MenuEvent {
window_id,
menu_id,
origin,
} => MenuEvent {
window_id: *window_id,
menu_id: *menu_id,
origin: *origin,
},
Expand Down Expand Up @@ -198,7 +207,15 @@ impl<'a, T> Event<'a, T> {
LoopDestroyed => Ok(LoopDestroyed),
Suspended => Ok(Suspended),
Resumed => Ok(Resumed),
MenuEvent { menu_id, origin } => Ok(MenuEvent { menu_id, origin }),
MenuEvent {
window_id,
menu_id,
origin,
} => Ok(MenuEvent {
window_id,
menu_id,
origin,
}),
TrayEvent {
bounds,
event,
Expand Down Expand Up @@ -229,7 +246,15 @@ impl<'a, T> Event<'a, T> {
LoopDestroyed => Some(LoopDestroyed),
Suspended => Some(Suspended),
Resumed => Some(Resumed),
MenuEvent { menu_id, origin } => Some(MenuEvent { menu_id, origin }),
MenuEvent {
window_id,
menu_id,
origin,
} => Some(MenuEvent {
window_id,
menu_id,
origin,
}),
TrayEvent {
bounds,
event,
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<T: 'static> EventLoop<T> {
WindowRequest::Menu(m) => match m {
(None, Some(menu_id)) => {
if let Err(e) = event_tx.send(Event::MenuEvent {
window_id: Some(RootWindowId(id)),
menu_id,
origin: MenuType::MenuBar,
}) {
Expand Down Expand Up @@ -643,6 +644,7 @@ impl<T: 'static> EventLoop<T> {
}
WindowRequest::Menu((None, Some(menu_id))) => {
if let Err(e) = event_tx.send(Event::MenuEvent {
window_id: None,
menu_id,
origin: MenuType::ContextMenu,
}) {
Expand Down
16 changes: 15 additions & 1 deletion src/platform_impl/macos/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ use crate::{
keyboard::{KeyCode, ModifiersState},
menu::{CustomMenuItem, MenuId, MenuItem, MenuType},
platform::macos::NativeImage,
window::WindowId,
};

use super::{app_state::AppState, event::EventWrapper};
use super::{app_state::AppState, event::EventWrapper, window::get_window_id};

static BLOCK_PTR: &str = "taoMenuItemBlockPtr";

Expand Down Expand Up @@ -429,7 +430,20 @@ fn send_event(this: &Object, origin: MenuType) {
let obj = ptr as *const Action;
&*obj
};

// active window
let window_id = match origin {
MenuType::MenuBar => unsafe {
let app: id = msg_send![class!(NSApplication), sharedApplication];
let window_id: id = msg_send![app, mainWindow];
Some(WindowId(get_window_id(window_id)))
},
// system tray do not send WindowId
MenuType::ContextMenu => None,
};

let event = Event::MenuEvent {
window_id,
menu_id: MenuId(*menu_id.0),
origin,
};
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ unsafe fn public_window_callback_inner<T: 'static>(
winuser::WM_COMMAND => {
let menu_id = LOWORD(wparam as u32) as u16;
subclass_input.send_event(Event::MenuEvent {
window_id: Some(RootWindowId(WindowId(window))),
menu_id: MenuId(menu_id as u16),
// todo fix menutype
origin: MenuType::MenuBar,
Expand Down
9 changes: 8 additions & 1 deletion src/platform_impl/windows/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ lazy_static! {
}

pub struct MenuHandler {
window_id: Option<RootWindowId>,
menu_type: MenuType,
send_event: Box<dyn Fn(Event<'static, ()>)>,
}

impl MenuHandler {
pub fn new(send_event: Box<dyn Fn(Event<'static, ()>)>, menu_type: MenuType) -> MenuHandler {
pub fn new(
send_event: Box<dyn Fn(Event<'static, ()>)>,
menu_type: MenuType,
window_id: Option<RootWindowId>,
) -> MenuHandler {
MenuHandler {
window_id,
send_event,
menu_type,
}
Expand All @@ -55,6 +61,7 @@ impl MenuHandler {
(self.send_event)(Event::MenuEvent {
menu_id: MenuId(menu_id),
origin: self.menu_type,
window_id: self.window_id,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/windows/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl SystemTrayBuilder {
}
}),
MenuType::ContextMenu,
None,
);

let app_system_tray = SystemTray {
Expand Down Expand Up @@ -150,6 +151,7 @@ impl SystemTrayBuilder {
}
}),
MenuType::ContextMenu,
None,
);

let sender: *mut MenuHandler = Box::into_raw(Box::new(menu_handler));
Expand Down
7 changes: 5 additions & 2 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ use crate::{
window_state::{CursorFlags, SavedWindow, WindowFlags, WindowState},
OsError, Parent, PlatformSpecificWindowBuilderAttributes, WindowId,
},
window::{CursorIcon, Fullscreen, Theme, UserAttentionType, WindowAttributes},
window::{
CursorIcon, Fullscreen, Theme, UserAttentionType, WindowAttributes, WindowId as RootWindowId,
},
};

struct HMenuWrapper(windef::HMENU);
Expand Down Expand Up @@ -953,14 +955,15 @@ unsafe fn init<T: 'static>(
if let Some(window_menu) = attributes.window_menu {
let event_loop_runner = event_loop.runner_shared.clone();
let window_handle = win.raw_window_handle();

let window_id = RootWindowId(win.id().clone());
let menu_handler = menu::MenuHandler::new(
Box::new(move |event| {
if let Ok(e) = event.map_nonuser_event() {
event_loop_runner.send_event(e)
}
}),
MenuType::MenuBar,
Some(window_id),
);

win.menu = menu::initialize(window_menu, window_handle, menu_handler).map(|m| HMenuWrapper(m));
Expand Down

0 comments on commit 96651dc

Please sign in to comment.