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 committed Aug 30, 2023
1 parent 022240b commit 1f83261
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 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 `WebViewBuilder::with_focused` to control whether to focus the webview upon creation or not. Supported on Windows and Linux only.
42 changes: 39 additions & 3 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
browser_accelerator_keys: bool,
theme: Option<Theme>,
https_scheme: bool,
focused: bool,
}
#[cfg(windows)]
impl Default for PlatformSpecificWebViewAttributes {
Expand All @@ -277,6 +278,7 @@ impl Default for PlatformSpecificWebViewAttributes {
browser_accelerator_keys: true, // This is WebView2's default behavior
theme: None,
https_scheme: true,
focused: true,
}
}
}
Expand All @@ -286,10 +288,24 @@ impl Default for PlatformSpecificWebViewAttributes {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "macos",
target_os = "android",
target_os = "ios",
))]
#[derive(Clone)]
pub(crate) struct PlatformSpecificWebViewAttributes {
focused: bool,
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
impl Default for PlatformSpecificWebViewAttributes {
fn default() -> Self {
Self { focused: true }
}
}
#[cfg(any(target_os = "macos", target_os = "android", target_os = "ios",))]
#[derive(Default)]
pub(crate) struct PlatformSpecificWebViewAttributes;

Expand Down Expand Up @@ -605,6 +621,26 @@ 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 {
#[cfg(any(
windows,
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
{
self.platform_specific.focused = _focused;
}
self
}

/// Consume the builder and create the [`WebView`].
///
/// Platform-specific behavior:
Expand Down
7 changes: 5 additions & 2 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
mut attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
pl_attrs: super::PlatformSpecificWebViewAttributes,
web_context: Option<&mut WebContext>,
) -> Result<Self> {
let window_rc = Rc::clone(&window);
Expand Down Expand Up @@ -264,7 +264,10 @@ impl InnerWebView {
let vbox = widget.downcast::<gtk::Box>().unwrap();
vbox.pack_start(&*webview, true, true, 0);
}
webview.grab_focus();

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

// Enable webgl, webaudio, canvas features as default.
if let Some(settings) = WebViewExt::settings(&*webview) {
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 @@ -774,9 +774,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 pl_attrs.focused {
controller
.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)
.map_err(webview2_com::Error::WindowsError)?;
}
}

Ok(webview)
Expand Down

0 comments on commit 1f83261

Please sign in to comment.