Skip to content

Commit

Permalink
feat(windows): auto-focus the webview (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Aug 27, 2022
1 parent 917bb43 commit f338df7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changes/remove-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking change** Removed `WebView::focus`.
5 changes: 5 additions & 0 deletions .changes/windows-auto-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Windows, automatically focus the webview when the window gains focus to match other platforms.
10 changes: 0 additions & 10 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,6 @@ impl WebView {
Ok(())
}

/// Moves Focus to the Webview control.
///
/// It's usually safe to call `focus` method on `Window` which would also focus to `WebView` except Windows.
/// Focussing to `Window` doesn't mean focussing to `WebView` on Windows. For example, if you have
/// an input field on webview and lost focus, you will have to explicitly click the field even you
/// re-focus the window. And if you focus to `WebView`, it will lost focus to the `Window`.
pub fn focus(&self) {
self.webview.focus();
}

/// Open the web inspector which is usually called dev tool.
///
/// ## Platform-specific
Expand Down
4 changes: 0 additions & 4 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,6 @@ impl InnerWebView {
Ok(())
}

pub fn focus(&self) {
self.webview.grab_focus();
}

#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn open_devtools(&self) {
if let Some(inspector) = WebViewExt::inspector(&*self.webview) {
Expand Down
42 changes: 21 additions & 21 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,16 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
}
}

unsafe {
unsafe extern "system" fn subclass_proc(
hwnd: HWND,
msg: u32,
wparam: WPARAM,
lparam: LPARAM,
_uidsubclass: usize,
dwrefdata: usize,
) -> LRESULT {
if msg == win32wm::WM_SIZE {
unsafe extern "system" fn subclass_proc(
hwnd: HWND,
msg: u32,
wparam: WPARAM,
lparam: LPARAM,
_uidsubclass: usize,
dwrefdata: usize,
) -> LRESULT {
match msg {
win32wm::WM_SIZE => {
let controller = dwrefdata as *mut ICoreWebView2Controller;
let mut client_rect = RECT::default();
win32wm::GetClientRect(hwnd, std::mem::transmute(&mut client_rect));
Expand All @@ -613,12 +613,20 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
});
}

if msg == win32wm::WM_DESTROY {
Box::from_raw(dwrefdata as *mut ICoreWebView2Controller);
win32wm::WM_SETFOCUS => {
let controller = dwrefdata as *mut ICoreWebView2Controller;
let _ = (*controller).MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
}

DefSubclassProc(hwnd, msg, wparam, lparam)
win32wm::WM_DESTROY => {
Box::from_raw(dwrefdata as *mut ICoreWebView2Controller);
}
_ => (),
}

DefSubclassProc(hwnd, msg, wparam, lparam)
}
unsafe {
SetWindowSubclass(
hwnd,
Some(subclass_proc),
Expand Down Expand Up @@ -672,14 +680,6 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
.map_err(|err| Error::WebView2Error(webview2_com::Error::WindowsError(err)))
}

pub fn focus(&self) {
let _ = unsafe {
self
.controller
.MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)
};
}

#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn open_devtools(&self) {
let _ = unsafe { self.webview.OpenDevToolsWindow() };
Expand Down
2 changes: 0 additions & 2 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,6 @@ r#"Object.defineProperty(window, 'ipc', {
}
}

pub fn focus(&self) {}

#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn open_devtools(&self) {
#[cfg(target_os = "macos")]
Expand Down

0 comments on commit f338df7

Please sign in to comment.