Skip to content

Commit

Permalink
chore: update windows-common-controls-v6 example
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Feb 2, 2024
1 parent 805e5cd commit db93bf5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
6 changes: 3 additions & 3 deletions examples/windows-common-controls-v6/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"

[dependencies]
muda = { path = "../../", features = ["common-controls-v6"] }
winit = "0.28"
winit = "0.29"
image = "0.24"

[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
version = "0.48"
version = "0.52"
features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"]

[build-dependencies]
embed-resource = "1.6"
embed-resource = "2"
2 changes: 1 addition & 1 deletion examples/windows-common-controls-v6/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
#[cfg(target_os = "windows")]
embed_resource::compile("manifest.rc");
embed_resource::compile("manifest.rc", embed_resource::NONE);
}
77 changes: 50 additions & 27 deletions examples/windows-common-controls-v6/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use muda::{
accelerator::{Accelerator, Code, Modifiers},
AboutMetadata, CheckMenuItem, ContextMenu, IconMenuItem, Menu, MenuEvent, MenuItem,
PredefinedMenuItem, Submenu,
PhysicalPosition, Position, PredefinedMenuItem, Submenu,
};
#[cfg(target_os = "macos")]
use winit::platform::macos::{EventLoopBuilderExtMacOS, WindowExtMacOS};
Expand All @@ -25,20 +25,20 @@ fn main() {

#[cfg(target_os = "windows")]
{
let menu_bar_c = menu_bar.clone();
let menu_bar = menu_bar.clone();
event_loop_builder.with_msg_hook(move |msg| {
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
unsafe {
let msg = msg as *const MSG;
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar_c.haccel(), msg);
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel(), msg);
translated == 1
}
});
}
#[cfg(target_os = "macos")]
event_loop_builder.with_default_menu(false);

let event_loop = event_loop_builder.build();
let event_loop = event_loop_builder.build().unwrap();

let window = WindowBuilder::new()
.with_title("Window 1")
Expand Down Expand Up @@ -109,6 +109,7 @@ fn main() {
&PredefinedMenuItem::maximize(None),
&PredefinedMenuItem::close_window(Some("Close")),
&PredefinedMenuItem::fullscreen(None),
&PredefinedMenuItem::bring_all_to_front(None),
&PredefinedMenuItem::about(
None,
Some(AboutMetadata {
Expand All @@ -127,8 +128,13 @@ fn main() {

#[cfg(target_os = "windows")]
{
menu_bar.init_for_hwnd(window.hwnd() as _);
menu_bar.init_for_hwnd(window2.hwnd() as _);
use winit::raw_window_handle::*;
if let RawWindowHandle::Win32(handle) = window.window_handle().unwrap().as_raw() {
menu_bar.init_for_hwnd(handle.hwnd.get());
}
if let RawWindowHandle::Win32(handle) = window2.window_handle().unwrap().as_raw() {
menu_bar.init_for_hwnd(handle.hwnd.get());
}
}
#[cfg(target_os = "macos")]
{
Expand All @@ -137,26 +143,24 @@ fn main() {
}

let menu_channel = MenuEvent::receiver();
let mut window_cursor_position = PhysicalPosition { x: 0., y: 0. };
let mut use_window_pos = false;

let mut x = 0_f64;
let mut y = 0_f64;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
event_loop.run(move |event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
} => event_loop.exit(),
Event::WindowEvent {
event: WindowEvent::CursorMoved { position, .. },
window_id,
..
} => {
if window_id == window2.id() {
x = position.x;
y = position.y;
}
window_cursor_position.x = position.x;
window_cursor_position.y = position.y;
}
Event::WindowEvent {
event:
Expand All @@ -168,12 +172,20 @@ fn main() {
window_id,
..
} => {
if window_id == window2.id() {
show_context_menu(&window2, &file_m, x, y);
}
}
Event::MainEventsCleared => {
window.request_redraw();
show_context_menu(
if window_id == window.id() {
&window
} else {
&window2
},
&file_m,
if use_window_pos {
Some(window_cursor_position.into())
} else {
None
},
);
use_window_pos = !use_window_pos;
}
_ => (),
}
Expand All @@ -184,17 +196,28 @@ fn main() {
}
println!("{event:?}");
}
})
});
}

fn show_context_menu(window: &Window, menu: &dyn ContextMenu, x: f64, y: f64) {
fn show_context_menu(window: &Window, menu: &dyn ContextMenu, position: Option<Position>) {
println!("Show context menu at position {position:?}");
#[cfg(target_os = "windows")]
menu.show_context_menu_for_hwnd(window.hwnd() as _, x, y);
{
use winit::raw_window_handle::*;
if let RawWindowHandle::Win32(handle) = window.window_handle().unwrap().as_raw() {
menu.show_context_menu_for_hwnd(handle.hwnd.get(), position);
}
}
#[cfg(target_os = "macos")]
menu.show_context_menu_for_nsview(window.ns_view() as _, x, y);
{
use winit::raw_window_handle::*;
if let RawWindowHandle::AppKit(handle) = window.window_handle().unwrap().as_raw() {
menu.show_context_menu_for_nsview(handle.ns_view.as_ptr() as _, position);
}
}
}

fn load_icon(path: &std::path::Path) -> muda::icon::Icon {
fn load_icon(path: &std::path::Path) -> muda::Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
Expand All @@ -203,5 +226,5 @@ fn load_icon(path: &std::path::Path) -> muda::icon::Icon {
let rgba = image.into_raw();
(rgba, width, height)
};
muda::icon::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
muda::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

0 comments on commit db93bf5

Please sign in to comment.