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

macOS: Add support for reloading the preview via cmd+r #6573

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all 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
17 changes: 15 additions & 2 deletions tools/lsp/preview/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ pub fn send_message_to_lsp(message: PreviewToLspMessage) {
// as the life-cycle of this process is determined by the editor. The returned menuitem must
// be kept alive for the duration of the event loop, as otherwise muda crashes.
#[cfg(target_vendor = "apple")]
fn init_apple_platform() -> Result<muda::MenuItem, i_slint_core::api::PlatformError> {
fn init_apple_platform(
) -> Result<(muda::MenuItem, muda::MenuItem), i_slint_core::api::PlatformError> {
use i_slint_backend_winit::winit;
use muda::{accelerator, Menu, MenuItem, PredefinedMenuItem, Submenu};
use winit::platform::macos::EventLoopBuilderExtMacOS;
Expand All @@ -293,6 +294,14 @@ fn init_apple_platform() -> Result<muda::MenuItem, i_slint_core::api::PlatformEr
accelerator::Code::KeyW,
)),
);
let reload_menu_item = MenuItem::new(
format!("Reload"),
true,
Some(accelerator::Accelerator::new(
Some(accelerator::Modifiers::META),
accelerator::Code::KeyR,
)),
);

let menu_bar = Menu::new();
menu_bar.init_for_nsapp();
Expand All @@ -306,6 +315,7 @@ fn init_apple_platform() -> Result<muda::MenuItem, i_slint_core::api::PlatformEr
&PredefinedMenuItem::hide(None),
&PredefinedMenuItem::hide_others(None),
&PredefinedMenuItem::show_all(None),
&reload_menu_item,
&close_app_menu_item,
])
})
Expand All @@ -314,12 +324,15 @@ fn init_apple_platform() -> Result<muda::MenuItem, i_slint_core::api::PlatformEr
})?;

let close_id = close_app_menu_item.id().clone();
let reload_id = reload_menu_item.id().clone();

muda::MenuEvent::set_event_handler(Some(move |menu_event: muda::MenuEvent| {
if menu_event.id == close_id {
close_ui();
} else if menu_event.id == reload_id {
super::reload_preview();
}
}));

Ok(close_app_menu_item)
Ok((close_app_menu_item, reload_menu_item))
}
Loading