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: add WebViewBuilder::with_focused #1015

Merged
merged 1 commit into from
Aug 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/with_focused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

Add `WebViewAtrributes.focused` and `WebViewBuilder::with_focused` to control whether to focus the webview upon creation or not. Supported on Windows and Linux only.
18 changes: 18 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ pub struct WebViewAttributes {
/// - **macOS**: Requires macOS 14.0+ and the `mac-proxy` feature flag to be enabled.
/// - **Android / iOS:** Not supported.
pub proxy_config: Option<ProxyConfig>,

/// Whether the webview should be focused when created.
///
/// ## Platform-specific:
///
/// - **macOS / Android / iOS:** Unsupported.
pub focused: bool,
}

impl Default for WebViewAttributes {
Expand Down Expand Up @@ -276,6 +283,7 @@ impl Default for WebViewAttributes {
autoplay: true,
on_page_load_handler: None,
proxy_config: None,
focused: true,
}
}
}
Expand Down Expand Up @@ -674,6 +682,16 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Set whether the webview should be focused when created.
///
/// ## Platform-specific:
///
/// - **macOS / Android / iOS:** Unsupported.
pub fn with_focused(mut self, focused: bool) -> Self {
self.webview.focused = focused;
self
}

/// Consume the builder and create the [`WebView`].
///
/// Platform-specific behavior:
Expand Down
5 changes: 4 additions & 1 deletion src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ impl InnerWebView {
} else {
window.add(&*webview);
}
webview.grab_focus();

if attributes.focused {
webview.grab_focus();
}

if let Some(context) = WebViewExt::context(&*webview) {
use webkit2gtk::WebContextExt;
Expand Down
8 changes: 5 additions & 3 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,11 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
controller
.SetIsVisible(true)
.map_err(webview2_com::Error::WindowsError)?;
controller
.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)
.map_err(webview2_com::Error::WindowsError)?;
if attributes.focused {
controller
.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)
.map_err(webview2_com::Error::WindowsError)?;
}
}

Ok(webview)
Expand Down
Loading