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 .focus() to Webview #325

Merged
merged 12 commits into from
Jul 5, 2021
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/webview-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---
- Add `focus` method to `Webview`
- Add `WebviewExtWindows` trait with `on_focus` and `on_blur`
25 changes: 25 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ impl WebView {
self.webview.resize(self.window.hwnd())?;
Ok(())
}

/// Moves Focus to the Webview control.
pub fn focus(&self) {
self.webview.focus();
}
}

// Helper so all platforms handle RPC messages consistently.
Expand Down Expand Up @@ -458,6 +463,26 @@ pub fn webview_version() -> Result<String> {
platform_webview_version()
}

#[cfg(target_os = "windows")]
pub trait WebviewExtWindows {
/// Hook into webview2 got_focus event
fn on_focus(&self, f: impl Fn() + 'static);

/// Hook into webview2 lost_focus event
fn on_blur(&self, f: impl Fn() + 'static);
}

#[cfg(target_os = "windows")]
impl WebviewExtWindows for WebView {
fn on_focus(&self, f: impl Fn() + 'static) {
self.webview.on_focus(f);
}

fn on_blur(&self, f: impl Fn() + 'static) {
self.webview.on_blur(f);
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ impl InnerWebView {
}
Ok(())
}

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

pub fn platform_webview_version() -> Result<String> {
Expand Down
24 changes: 24 additions & 0 deletions src/webview/webview2/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl InnerWebView {
}

controller.put_is_visible(true)?;
controller.move_focus(webview2::MoveFocusReason::Programmatic)?;
let _ = controller_clone.set(controller);

if let Some(file_drop_handler) = attributes.file_drop_handler {
Expand Down Expand Up @@ -275,6 +276,29 @@ impl InnerWebView {

Ok(())
}

pub fn focus(&self) {
if let Some(c) = self.controller.get() {
let _ = c.move_focus(webview2::MoveFocusReason::Programmatic);
}
}
pub fn on_focus(&self, f: impl Fn() + 'static) {
if let Some(c) = self.controller.get() {
let _ = c.add_got_focus(move |_| {
f();
Ok(())
});
}
}

pub fn on_blur(&self, f: impl Fn() + 'static) {
if let Some(c) = self.controller.get() {
let _ = c.add_lost_focus(move |_| {
f();
Ok(())
});
}
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down
2 changes: 2 additions & 0 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ impl InnerWebView {
let () = msg_send![print_operation, runOperationModalForWindow: self.ns_window delegate: null::<*const c_void>() didRunSelector: null::<*const c_void>() contextInfo: null::<*const c_void>()];
}
}

pub fn focus(&self) {}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down