Skip to content

Commit

Permalink
feat: add WebViewBuilder::with_focused (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Aug 30, 2023
1 parent 3cc4d79 commit ebc4a20
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
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

0 comments on commit ebc4a20

Please sign in to comment.