Skip to content

Commit 548e0df

Browse files
Desktop: Mac menu workaround (#3398)
1 parent 788e82a commit 548e0df

File tree

13 files changed

+139
-122
lines changed

13 files changed

+139
-122
lines changed

Cargo.lock

Lines changed: 51 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ image = { version = "0.25", default-features = false, features = [
182182
"jpeg",
183183
"bmp",
184184
] }
185-
parley = "0.5"
185+
parley = "0.6"
186186
skrifa = "0.36"
187187
pretty_assertions = "1.4"
188188
fern = { version = "0.7", features = ["colored"] }

desktop/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ windows = { version = "0.58.0", features = [
5858

5959
# macOS-specific dependencies
6060
[target.'cfg(target_os = "macos")'.dependencies]
61+
objc2 = { version = "0.6.1", default-features = false }
62+
objc2-foundation = { version = "0.3.2", default-features = false }
63+
objc2-app-kit = { version = "0.3.2", default-features = false }
6164
muda = { git = "https://github.com/tauri-apps/muda.git", rev = "3f460b8fbaed59cda6d95ceea6904f000f093f15", default-features = false }
6265

desktop/src/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub(crate) struct App {
4444
}
4545

4646
impl App {
47+
pub(crate) fn init() {
48+
Window::init();
49+
}
50+
4751
pub(crate) fn new(
4852
cef_context: Box<dyn cef::CefContext>,
4953
cef_view_info_sender: Sender<cef::ViewInfoUpdate>,

desktop/src/cef/input.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,27 @@ pub(crate) fn handle_window_event(browser: &Browser, input_state: &mut InputStat
8888
key_event.native_key_code = event.physical_key.to_native_keycode();
8989

9090
key_event.character = event.logical_key.to_char_representation() as u16;
91-
key_event.unmodified_character = event.key_without_modifiers.to_char_representation() as u16;
91+
92+
// Mitigation for CEF on Mac bug to prevent NSMenu being triggered by this key event.
93+
//
94+
// CEF converts the key event into an `NSEvent` internally and passes that to Chromium.
95+
// In some cases the `NSEvent` gets to the native Cocoa application, is considered "unhandled" and can trigger menus.
96+
//
97+
// Why mitigation works:
98+
// Leaving `key_event.unmodified_character = 0` still leads to CEF forwarding a "unhandled" event to the native application
99+
// but that event is discarded because `key_event.unmodified_character = 0` is considered non-printable and not used for shortcut matching.
100+
//
101+
// See https://github.com/chromiumembedded/cef/issues/3857
102+
//
103+
// TODO: Remove mitigation once bug is fixed or a better solution is found.
104+
#[cfg(not(target_os = "macos"))]
105+
{
106+
key_event.unmodified_character = event.key_without_modifiers.to_char_representation() as u16;
107+
}
92108

93109
#[cfg(target_os = "macos")] // See https://www.magpcss.org/ceforum/viewtopic.php?start=10&t=11650
94110
if key_event.character == 0 && key_event.unmodified_character == 0 && event.text_with_all_modifiers.is_some() {
95-
key_event.unmodified_character = 1;
111+
key_event.character = 1;
96112
}
97113

98114
if key_event.type_ == cef_key_event_type_t::KEYEVENT_CHAR.into() {

desktop/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub fn start() {
3636
return;
3737
}
3838

39+
App::init();
40+
3941
let cli = Cli::parse();
4042

4143
let wgpu_context = futures::executor::block_on(gpu_context::create_wgpu_context());

desktop/src/window.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use winit::window::{Window as WinitWindow, WindowAttributes};
44

55
use crate::consts::APP_NAME;
66
use crate::event::AppEventScheduler;
7+
use crate::window::mac::NativeWindowImpl;
78
use crate::wrapper::messages::MenuItem;
89

910
pub(crate) trait NativeWindow {
11+
fn init() {}
1012
fn configure(attributes: WindowAttributes, event_loop: &dyn ActiveEventLoop) -> WindowAttributes;
1113
fn new(window: &dyn WinitWindow, app_event_scheduler: AppEventScheduler) -> Self;
1214
fn update_menu(&self, _entries: Vec<MenuItem>) {}
@@ -34,6 +36,10 @@ pub(crate) struct Window {
3436
}
3537

3638
impl Window {
39+
pub(crate) fn init() {
40+
NativeWindowImpl::init();
41+
}
42+
3743
pub(crate) fn new(event_loop: &dyn ActiveEventLoop, app_event_scheduler: AppEventScheduler) -> Self {
3844
let mut attributes = WindowAttributes::default()
3945
.with_title(APP_NAME)

0 commit comments

Comments
 (0)