Skip to content

Commit a57719e

Browse files
authoredOct 14, 2024··
feat: add WebView::focus_parent method. (#1385)
1 parent 101973c commit a57719e

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed
 

‎.changes/focus-parent.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wry": "patch:bug"
3+
---
4+
5+
Add `WebView::focus_parent` method.

‎src/android/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ impl InnerWebView {
393393
// Unsupported
394394
Ok(())
395395
}
396+
397+
pub fn focus_parent(&self) -> Result<()> {
398+
// Unsupported
399+
Ok(())
400+
}
396401
}
397402

398403
#[derive(Clone, Copy)]

‎src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,15 @@ impl WebView {
16131613
pub fn focus(&self) -> Result<()> {
16141614
self.webview.focus()
16151615
}
1616+
1617+
/// Try moving focus away from the webview back to the parent window.
1618+
///
1619+
/// ## Platform-specific:
1620+
///
1621+
/// - **Android**: Not implemented.
1622+
pub fn focus_parent(&self) -> Result<()> {
1623+
self.webview.focus_parent()
1624+
}
16161625
}
16171626

16181627
/// An event describing drag and drop operations on the webview.

‎src/webkitgtk/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,14 @@ impl InnerWebView {
796796
Ok(())
797797
}
798798

799+
pub fn focus_parent(&self) -> Result<()> {
800+
if let Some(window) = self.webview.parent_window() {
801+
window.focus(gdk::ffi::GDK_CURRENT_TIME.try_into().unwrap_or(0));
802+
}
803+
804+
Ok(())
805+
}
806+
799807
pub fn reparent<W>(&self, container: &W) -> Result<()>
800808
where
801809
W: gtk::prelude::IsA<gtk::Container>,

‎src/webview2/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use windows::{
2121
Globalization::*,
2222
Graphics::Gdi::*,
2323
System::{Com::*, LibraryLoader::GetModuleHandleW, WinRT::EventRegistrationToken},
24-
UI::{Shell::*, WindowsAndMessaging::*},
24+
UI::{Input::KeyboardAndMouse::SetFocus, Shell::*, WindowsAndMessaging::*},
2525
},
2626
};
2727

@@ -1315,6 +1315,17 @@ impl InnerWebView {
13151315
}
13161316
}
13171317

1318+
pub fn focus_parent(&self) -> Result<()> {
1319+
unsafe {
1320+
let parent = *self.parent.borrow();
1321+
if parent != HWND::default() {
1322+
SetFocus(parent)?;
1323+
}
1324+
}
1325+
1326+
Ok(())
1327+
}
1328+
13181329
pub fn reparent(&self, parent: isize) -> Result<()> {
13191330
let parent = HWND(parent as _);
13201331

‎src/wkwebview/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub(crate) struct InnerWebView {
112112
id: String,
113113
pub webview: Retained<WryWebView>,
114114
pub manager: Retained<WKUserContentController>,
115+
ns_view: Retained<NSView>,
115116
#[allow(dead_code)]
116117
is_child: bool,
117118
pending_scripts: Arc<Mutex<Option<Vec<String>>>>,
@@ -460,6 +461,7 @@ impl InnerWebView {
460461
id: webview_id,
461462
webview: webview.clone(),
462463
manager: manager.clone(),
464+
ns_view: ns_view.retain(),
463465
pending_scripts,
464466
ipc_handler_delegate,
465467
document_title_changed_observer,
@@ -805,6 +807,19 @@ r#"Object.defineProperty(window, 'ipc', {
805807
Ok(())
806808
}
807809

810+
pub fn focus_parent(&self) -> Result<()> {
811+
if let Some(window) = self.webview.window() {
812+
#[cfg(target_os = "macos")]
813+
window.makeFirstResponder(Some(&self.ns_view));
814+
#[cfg(target_os = "ios")]
815+
unsafe {
816+
window.becomeFirstResponder()
817+
};
818+
}
819+
820+
Ok(())
821+
}
822+
808823
#[cfg(target_os = "macos")]
809824
pub(crate) fn reparent(&self, window: *mut NSWindow) -> crate::Result<()> {
810825
unsafe {

0 commit comments

Comments
 (0)
Please sign in to comment.