Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor system_tray impl on windows #153

Merged
merged 7 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
14 changes: 4 additions & 10 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 @@ -51,6 +49,9 @@ fn main() {
// add quit button
let quit_element = tray_menu.add_item(MenuItemAttributes::new("Quit"));

let mut new_tray_menu = Menu::new();
new_tray_menu.add_item(MenuItemAttributes::new("Quit"));

// Windows require Vec<u8> ICO file
#[cfg(target_os = "windows")]
let icon = include_bytes!("icon.ico").to_vec();
Expand Down Expand Up @@ -152,19 +153,12 @@ 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;
}

if menu_id == change_menu.clone().id() {
let mut tray_menu = Menu::new();
tray_menu.add_item(MenuItemAttributes::new("Quit"));
system_tray.set_menu(&tray_menu);
system_tray.set_menu(&new_tray_menu);
}

println!("Clicked on {:?}", menu_id);
Expand Down
10 changes: 9 additions & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ use crate::{
};

/// Object that allows you to create a `ContextMenu`.
///
/// ## Platform-specific
///
/// **Windows:** The native menu is destroyed when this stuct is dropped and can no longer be used.
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
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.
/// **Windows:** The native menu is destroyed when this stuct is dropped and can no longer be used.
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
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()
}
}
32 changes: 15 additions & 17 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 @@ -383,16 +381,16 @@ pub(crate) unsafe extern "system" fn subclass_proc(
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);
Expand Down Expand Up @@ -422,18 +420,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