Skip to content

Commit

Permalink
feat(core): add window accept_first_mouse option, closes #5347 (#5374)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 17, 2022
1 parent b8bf8e0 commit 95f467a
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/accept-first-mouse-api.md
@@ -0,0 +1,5 @@
---
"api": minor
---

Added the `acceptFirstMouse` window option.
6 changes: 6 additions & 0 deletions .changes/accept-first-mouse.md
@@ -0,0 +1,6 @@
---
"tauri": minor
"tauri-runtime-wry": minor
---

Add `accept_first_mouse` option for macOS windows.
3 changes: 2 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -2950,7 +2950,8 @@ fn create_webview<T: UserEvent>(
.map_err(|e| Error::CreateWebview(Box::new(e)))?
.with_url(&url)
.unwrap() // safe to unwrap because we validate the URL beforehand
.with_transparent(is_window_transparent);
.with_transparent(is_window_transparent)
.with_accept_first_mouse(webview_attributes.accept_first_mouse);
if webview_attributes.file_drop_handler_enabled {
webview_builder = webview_builder
.with_file_drop_handler(create_file_drop_handler(window_event_listeners.clone()));
Expand Down
9 changes: 9 additions & 0 deletions core/tauri-runtime/src/webview.rs
Expand Up @@ -27,6 +27,7 @@ pub struct WebviewAttributes {
pub data_directory: Option<PathBuf>,
pub file_drop_handler_enabled: bool,
pub clipboard: bool,
pub accept_first_mouse: bool,
}

impl WebviewAttributes {
Expand All @@ -39,6 +40,7 @@ impl WebviewAttributes {
data_directory: None,
file_drop_handler_enabled: true,
clipboard: false,
accept_first_mouse: false,
}
}

Expand Down Expand Up @@ -79,6 +81,13 @@ impl WebviewAttributes {
self.clipboard = true;
self
}

/// Sets whether clicking an inactive window also clicks through to the webview.
#[must_use]
pub fn accept_first_mouse(mut self, accept: bool) -> Self {
self.accept_first_mouse = accept;
self
}
}

/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
Expand Down
8 changes: 7 additions & 1 deletion core/tauri-utils/src/config.rs
Expand Up @@ -870,6 +870,9 @@ pub struct WindowConfig {
/// If `true`, sets the window title to be hidden on macOS.
#[serde(default, alias = "hidden-title")]
pub hidden_title: bool,
/// Whether clicking an inactive window also clicks through to the webview.
#[serde(default, alias = "accept-first-mouse")]
pub accept_first_mouse: bool,
}

impl Default for WindowConfig {
Expand Down Expand Up @@ -901,6 +904,7 @@ impl Default for WindowConfig {
theme: None,
title_bar_style: Default::default(),
hidden_title: false,
accept_first_mouse: false,
}
}
}
Expand Down Expand Up @@ -3032,6 +3036,7 @@ mod build {
let theme = opt_lit(self.theme.as_ref());
let title_bar_style = &self.title_bar_style;
let hidden_title = self.hidden_title;
let accept_first_mouse = self.accept_first_mouse;

literal_struct!(
tokens,
Expand Down Expand Up @@ -3061,7 +3066,8 @@ mod build {
skip_taskbar,
theme,
title_bar_style,
hidden_title
hidden_title,
accept_first_mouse
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions core/tauri/src/app.rs
Expand Up @@ -1484,14 +1484,13 @@ impl<R: Runtime> Builder<R> {
for config in manager.config().tauri.windows.clone() {
let url = config.url.clone();
let label = config.label.clone();
let file_drop_enabled = config.file_drop_enabled;
let user_agent = config.user_agent.clone();

let mut webview_attributes = WebviewAttributes::new(url);
if let Some(ua) = user_agent {
let mut webview_attributes =
WebviewAttributes::new(url).accept_first_mouse(config.accept_first_mouse);
if let Some(ua) = &config.user_agent {
webview_attributes = webview_attributes.user_agent(&ua.to_string());
}
if !file_drop_enabled {
if !config.file_drop_enabled {
webview_attributes = webview_attributes.disable_file_drop_handler();
}

Expand Down
7 changes: 7 additions & 0 deletions core/tauri/src/window.rs
Expand Up @@ -535,6 +535,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
self.webview_attributes.clipboard = true;
self
}

/// Sets whether clicking an inactive window also clicks through to the webview.
#[must_use]
pub fn accept_first_mouse(mut self, accept: bool) -> Self {
self.webview_attributes.accept_first_mouse = accept;
self
}
}

// TODO: expand these docs since this is a pretty important type
Expand Down
4 changes: 4 additions & 0 deletions tooling/api/src/window.ts
Expand Up @@ -2042,6 +2042,10 @@ interface WindowOptions {
* If `true`, sets the window title to be hidden on macOS.
*/
hiddenTitle?: boolean
/**
* Whether clicking an inactive window also clicks through to the webview.
*/
acceptFirstMouse?: boolean
/**
* The user agent for the webview.
*/
Expand Down
5 changes: 5 additions & 0 deletions tooling/cli/schema.json
Expand Up @@ -670,6 +670,11 @@
"description": "If `true`, sets the window title to be hidden on macOS.",
"default": false,
"type": "boolean"
},
"acceptFirstMouse": {
"description": "Whether clicking an inactive window also clicks through to the webview.",
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down

0 comments on commit 95f467a

Please sign in to comment.