Skip to content

Commit

Permalink
refactor: refactor system_tray impl on windows (rust-windowing#153)
Browse files Browse the repository at this point in the history
* refactor: refactor `system_tray` impl on windows

* update ids

* move `remove()` into drop impl

* simplify window class

* update comment

* cleanup poniters

* let all subclasses handle `WM_DESTROY`
  • Loading branch information
amrbashir authored Jul 22, 2021
1 parent 999a2cc commit cc9d2b1
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 175 deletions.
5 changes: 5 additions & 0 deletions .changes/remove-tray-remove-windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Removed `SystemTrayExtWindows::remove()`, the icon will be automatically removed when `SystemTray` is dropped.
7 changes: 0 additions & 7 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ fn main() {
use std::path::Path;
#[cfg(target_os = "macos")]
use tao::platform::macos::{CustomMenuItemExtMacOS, NativeImage};
#[cfg(target_os = "windows")]
use tao::platform::windows::SystemTrayExtWindows;
use tao::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
Expand Down Expand Up @@ -152,11 +150,6 @@ fn main() {
}
// click on `quit` item
if menu_id == quit_element.clone().id() {
// on windows, we make sure to remove the icon from the tray
// it require the `SystemTrayExtWindows`
#[cfg(target_os = "windows")]
system_tray.remove();

// tell our app to close at the end of the loop.
*control_flow = ControlFlow::Exit;
}
Expand Down
8 changes: 7 additions & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ use crate::{
};

/// Object that allows you to create a `ContextMenu`.
///
/// ## Platform-specific
///
pub struct ContextMenu(pub(crate) Menu);
/// Object that allows you to create a `MenuBar`, menu.
///
/// Used by the **Window** menu in Windows and Linux and the **Menu bar** on macOS
/// ## Platform-specific
///
/// **macOs:** The menu will show in the **Menu Bar**.
/// **Linux / Windows:** The menu will be show at the top of the window.
pub struct MenuBar(pub(crate) Menu);

/// A custom menu item.
Expand Down
15 changes: 0 additions & 15 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,3 @@ impl IconExtWindows for Icon {
Ok(Icon { inner: win_icon })
}
}

/// Additional methods on `SystemTray` that are specific to Windows.
#[cfg(feature = "tray")]
pub trait SystemTrayExtWindows {
fn remove(&mut self);
}

#[cfg(feature = "tray")]
impl SystemTrayExtWindows for crate::system_tray::SystemTray {
/// Remove the tray icon.
/// Call this when your application is goind to close, to make sure the icon is correctly removed from the system tray.
fn remove(&mut self) {
self.0.remove()
}
}
48 changes: 26 additions & 22 deletions src/platform_impl/windows/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl MenuItemAttributes {

#[derive(Debug, Clone)]
pub struct Menu {
pub(crate) hmenu: windef::HMENU,
hmenu: windef::HMENU,
accels: HashMap<u16, AccelWrapper>,
}

Expand Down Expand Up @@ -156,10 +156,8 @@ impl Menu {
}
}

pub fn into_hmenu(self) -> windef::HMENU {
let hmenu = self.hmenu;
std::mem::forget(self);
hmenu
pub fn hmenu(&self) -> windef::HMENU {
self.hmenu
}

// Get the accels table
Expand Down Expand Up @@ -226,7 +224,7 @@ impl Menu {
winuser::AppendMenuW(
self.hmenu,
flags,
submenu.into_hmenu() as _,
submenu.hmenu() as _,
to_wstring(&title).as_mut_ptr(),
);
}
Expand Down Expand Up @@ -341,7 +339,7 @@ pub fn initialize(
) -> Option<windef::HMENU> {
if let RawWindowHandle::Windows(handle) = window_handle {
let sender: *mut MenuHandler = Box::into_raw(Box::new(menu_handler));
let menu = menu_builder.clone().into_hmenu();
let menu = menu_builder.clone().hmenu();

unsafe {
commctrl::SetWindowSubclass(
Expand Down Expand Up @@ -376,43 +374,49 @@ pub(crate) unsafe extern "system" fn subclass_proc(
wparam: minwindef::WPARAM,
lparam: minwindef::LPARAM,
_id: basetsd::UINT_PTR,
data: basetsd::DWORD_PTR,
subclass_input_ptr: basetsd::DWORD_PTR,
) -> minwindef::LRESULT {
let proxy = &mut *(data as *mut MenuHandler);
let subclass_input_ptr = subclass_input_ptr as *mut MenuHandler;
let subclass_input = &*(subclass_input_ptr);

if msg == winuser::WM_DESTROY {
Box::from_raw(subclass_input_ptr);
}

match msg {
winuser::WM_COMMAND => {
match wparam {
CUT_ID => {
execute_edit_command(EditCommands::Cut);
execute_edit_command(EditCommand::Cut);
}
COPY_ID => {
execute_edit_command(EditCommands::Copy);
execute_edit_command(EditCommand::Copy);
}
PASTE_ID => {
execute_edit_command(EditCommands::Paste);
execute_edit_command(EditCommand::Paste);
}
SELECT_ALL_ID => {
execute_edit_command(EditCommands::SelectAll);
execute_edit_command(EditCommand::SelectAll);
}
HIDE_ID => {
winuser::ShowWindow(hwnd, winuser::SW_HIDE);
}
CLOSE_ID => {
proxy.send_event(Event::WindowEvent {
subclass_input.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(hwnd)),
event: WindowEvent::CloseRequested,
});
}
QUIT_ID => {
proxy.send_event(Event::LoopDestroyed);
subclass_input.send_event(Event::LoopDestroyed);
}
MINIMIZE_ID => {
winuser::ShowWindow(hwnd, winuser::SW_MINIMIZE);
}
_ => {
let menu_id = minwindef::LOWORD(wparam as _);
if MENU_IDS.lock().unwrap().contains(&menu_id) {
proxy.send_menu_event(menu_id);
subclass_input.send_menu_event(menu_id);
}
}
}
Expand All @@ -422,18 +426,18 @@ pub(crate) unsafe extern "system" fn subclass_proc(
}
}

enum EditCommands {
enum EditCommand {
Copy,
Cut,
Paste,
SelectAll,
}
fn execute_edit_command(command: EditCommands) {
fn execute_edit_command(command: EditCommand) {
let key = match command {
EditCommands::Copy => 0x43, // c
EditCommands::Cut => 0x58, // x
EditCommands::Paste => 0x56, // v
EditCommands::SelectAll => 0x41, // a
EditCommand::Copy => 0x43, // c
EditCommand::Cut => 0x58, // x
EditCommand::Paste => 0x56, // v
EditCommand::SelectAll => 0x41, // a
};

unsafe {
Expand Down
Loading

0 comments on commit cc9d2b1

Please sign in to comment.