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

feat(macos): add accept_first_mouse option, closes #714 #715

Merged
merged 1 commit into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/accept-first-mouse.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

Added `WebViewAttributes::with_accept_first_mouse` method for macOS.
1 change: 1 addition & 0 deletions examples/custom_titlebar.rs
Expand Up @@ -129,6 +129,7 @@ fn main() -> wry::Result<()> {
.with_url(url)?
.with_initialization_script(script)
.with_ipc_handler(handler)
.with_accept_first_mouse(true)
.build()?;
webviews.insert(webview.window().id(), webview);

Expand Down
17 changes: 17 additions & 0 deletions src/webview/mod.rs
Expand Up @@ -186,6 +186,12 @@ pub struct WebViewAttributes {
/// - Android: Open `chrome://inspect/#devices` in Chrome to get the devtools window. Wry's `WebView` devtools API isn't supported on Android.
/// - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
pub devtools: bool,
/// Whether clicking an inactive window also clicks through to the webview. Default is `false`.
///
/// ## Platform-specific
///
/// This configuration only impacts macOS.
pub accept_first_mouse: bool,
}

impl Default for WebViewAttributes {
Expand All @@ -206,6 +212,7 @@ impl Default for WebViewAttributes {
clipboard: false,
devtools: false,
zoom_hotkeys_enabled: false,
accept_first_mouse: false,
}
}
}
Expand Down Expand Up @@ -460,6 +467,16 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Sets whether clicking an inactive window also clicks through to the webview. Default is `false`.
///
/// ## Platform-specific
///
/// This configuration only impacts macOS.
pub fn with_accept_first_mouse(mut self, accept_first_mouse: bool) -> Self {
self.webview.accept_first_mouse = accept_first_mouse;
self
}

/// Consume the builder and create the [`WebView`].
///
/// Platform-specific behavior:
Expand Down
16 changes: 14 additions & 2 deletions src/webview/wkwebview/mod.rs
Expand Up @@ -259,7 +259,19 @@ impl InnerWebView {
#[allow(unused_mut)]
Some(mut decl) => {
#[cfg(target_os = "macos")]
add_file_drop_methods(&mut decl);
{
add_file_drop_methods(&mut decl);
if attributes.accept_first_mouse {
decl.add_method(
sel!(acceptsFirstMouse:),
yes as extern "C" fn(&Object, Sel, id) -> BOOL,
);
}

extern "C" fn yes(_this: &Object, _sel: Sel, _event: id) -> BOOL {
YES
}
}
decl.register()
}
_ => class!(WryWebView),
Expand Down Expand Up @@ -689,7 +701,7 @@ r#"Object.defineProperty(window, 'ipc', {
}
}

pub fn set_background_color(&self, background_color: RGBA) -> Result<()> {
pub fn set_background_color(&self, _background_color: RGBA) -> Result<()> {
Ok(())
}
}
Expand Down