Skip to content

Commit

Permalink
fix macos
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 3, 2023
1 parent 30561ad commit c17b8f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
19 changes: 13 additions & 6 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
sealed::{ManagerBase, RuntimeOrDispatch},
utils::config::Config,
utils::{assets::Assets, Env},
window::WindowMenu,
Context, DeviceEventFilter, EventLoopMessage, Icon, Invoke, InvokeError, InvokeResponse, Manager,
Monitor, Runtime, Scopes, StateManager, Theme, Window,
};
Expand Down Expand Up @@ -640,7 +639,7 @@ macro_rules! shared_app_impl {
.init_for_gtk_window(&gtk_window, Some(&gtk_box));
}
})?;
window_menu.replace(WindowMenu {
window_menu.replace(crate::window::WindowMenu {
is_app_wide: true,
menu: menu.clone(),
});
Expand Down Expand Up @@ -1585,12 +1584,20 @@ fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
let pending = manager.prepare_window(app_handle.clone(), pending, &window_labels)?;

#[cfg(not(target_os = "macos"))]
let window_menu = app.manager.menu_lock().as_ref().map(|m| WindowMenu {
is_app_wide: true,
menu: m.clone(),
});
let window_menu = app
.manager
.menu_lock()
.as_ref()
.map(|m| crate::window::WindowMenu {
is_app_wide: true,
menu: m.clone(),
});

#[cfg(not(target_os = "macos"))]
let handler = manager.create_webview_before_creation_handler(window_menu.as_ref());
#[cfg(target_os = "macos")]
#[allow(clippy::type_complexity)]
let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;

let window_effects = pending.webview_attributes.window_effects.clone();
let detached = if let RuntimeOrDispatch::RuntimeHandle(runtime) = app_handle.runtime() {
Expand Down
17 changes: 6 additions & 11 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use std::{
sync::{Arc, Mutex, MutexGuard},
};

use crate::menu::Menu;
use crate::tray::TrayIcon;
use crate::{menu::Menu, window::WindowMenu};
use serde::Serialize;
use serde_json::Value as JsonValue;
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
use tauri_runtime::window::RawWindow;
use url::Url;

use tauri_macros::default_runtime;
Expand Down Expand Up @@ -371,11 +370,11 @@ impl<R: Runtime> WindowManager<R> {
self.inner.state.clone()
}

#[cfg(not(target_os = "macos"))]
pub(crate) fn create_webview_before_creation_handler(
&self,
window_menu: Option<&WindowMenu<R>>,
) -> Option<impl Fn(RawWindow<'_>)> {
#[cfg(not(target_os = "macos"))]
window_menu: Option<&crate::window::WindowMenu<R>>,
) -> Option<impl Fn(tauri_runtime::window::RawWindow<'_>)> {
{
if let Some(menu) = window_menu {
self
Expand All @@ -387,13 +386,9 @@ impl<R: Runtime> WindowManager<R> {
}
}

#[cfg(target_os = "macos")]
return None;

#[cfg(not(target_os = "macos"))]
if let Some(menu) = &window_menu {
let menu = menu.menu.clone();
Some(move |raw: RawWindow<'_>| {
Some(move |raw: tauri_runtime::window::RawWindow<'_>| {
#[cfg(target_os = "windows")]
let _ = menu.inner().init_for_hwnd(raw.hwnd as _);
#[cfg(any(
Expand Down Expand Up @@ -1239,7 +1234,7 @@ impl<R: Runtime> WindowManager<R> {
&self,
app_handle: AppHandle<R>,
window: DetachedWindow<EventLoopMessage, R>,
#[cfg(not(target_os = "macos"))] menu: Option<WindowMenu<R>>,
#[cfg(not(target_os = "macos"))] menu: Option<crate::window::WindowMenu<R>>,
) -> Window<R> {
let window = Window::new(
self.clone(),
Expand Down
15 changes: 13 additions & 2 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! The Tauri window types and functions.

use crate::{app::GlobalMenuEventListener, menu::ContextMenu};
use crate::menu::ContextMenu;
pub use tauri_utils::{config::Color, WindowEffect as Effect, WindowEffectState as EffectState};
use url::Url;

Expand Down Expand Up @@ -121,7 +121,8 @@ pub struct WindowBuilder<'a, R: Runtime> {
pub(crate) webview_attributes: WebviewAttributes,
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
navigation_handler: Option<Box<NavigationHandler>>,
on_menu_event: Option<GlobalMenuEventListener<Window<R>>>,
#[cfg(not(target_os = "macos"))]
on_menu_event: Option<crate::app::GlobalMenuEventListener<Window<R>>>,
}

impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
Expand Down Expand Up @@ -197,6 +198,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
webview_attributes: WebviewAttributes::new(url),
web_resource_request_handler: None,
navigation_handler: None,
#[cfg(not(target_os = "macos"))]
on_menu_event: None,
}
}
Expand Down Expand Up @@ -237,6 +239,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
web_resource_request_handler: None,
menu: None,
navigation_handler: None,
#[cfg(not(target_os = "macos"))]
on_menu_event: None,
};

Expand Down Expand Up @@ -348,6 +351,8 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// Ok(())
/// });
/// ```
#[cfg(not(target_os = "macos"))]
#[cfg_attr(doc_cfg, doc(cfg(not(target_os = "macos"))))]
pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
mut self,
f: F,
Expand Down Expand Up @@ -380,9 +385,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
.map(|menu| WindowMenu { is_app_wide, menu })
};

#[cfg(not(target_os = "macos"))]
let handler = self
.manager
.create_webview_before_creation_handler(window_menu.as_ref());
#[cfg(target_os = "macos")]
#[allow(clippy::type_complexity)]
let handler: Option<Box<dyn Fn(tauri_runtime::window::RawWindow<'_>) + Send>> = None;

let window_effects = pending.webview_attributes.window_effects.clone();
let window = match &mut self.runtime {
Expand All @@ -399,6 +408,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
)
})?;

#[cfg(not(target_os = "macos"))]
if let Some(handler) = self.on_menu_event {
window.on_menu_event(handler);
}
Expand Down Expand Up @@ -844,6 +854,7 @@ struct JsEventListenerKey {

/// A wrapper struct to hold the window menu state
/// and whether it is global per-app or specific to this window.
#[cfg(not(target_os = "macos"))]
pub(crate) struct WindowMenu<R: Runtime> {
pub(crate) is_app_wide: bool,
pub(crate) menu: Menu<R>,
Expand Down

0 comments on commit c17b8f7

Please sign in to comment.