From 0185644040184c43084814cb8692acb4e1004d86 Mon Sep 17 00:00:00 2001
From: SmileSky <1721503872@qq.com>
Date: Tue, 14 Jan 2025 22:05:37 +0800
Subject: [PATCH] fix(windows): Fix the issue where TAB cannot cycle through
 focus elements. (#1452)

---
 .changes/element-focus.md |  5 +++++
 src/webview2/mod.rs       | 23 +++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)
 create mode 100644 .changes/element-focus.md

diff --git a/.changes/element-focus.md b/.changes/element-focus.md
new file mode 100644
index 000000000..ec8200fab
--- /dev/null
+++ b/.changes/element-focus.md
@@ -0,0 +1,5 @@
+---
+'wry': 'patch:enhance'
+---
+
+Allow the use of TAB to cycle through focus elements in an HTML document.
\ No newline at end of file
diff --git a/src/webview2/mod.rs b/src/webview2/mod.rs
index 564ca2e11..dcc338ae3 100644
--- a/src/webview2/mod.rs
+++ b/src/webview2/mod.rs
@@ -176,6 +176,16 @@ impl InnerWebView {
       wparam: WPARAM,
       lparam: LPARAM,
     ) -> LRESULT {
+      if msg == WM_SETFOCUS {
+        // Fix https://github.com/DioxusLabs/dioxus/issues/2900
+        // Get the first child window of the window
+        let child = GetWindow(hwnd, GW_CHILD).ok();
+        if child.is_some() {
+          // Set focus to the child window(WebView document)
+          let _ = SetFocus(child);
+        }
+      }
+
       DefWindowProcW(hwnd, msg, wparam, lparam)
     }
 
@@ -1671,14 +1681,19 @@ unsafe fn set_background_color(
   controller: &ICoreWebView2Controller,
   background_color: RGBA,
 ) -> Result<()> {
-  let (R, G, B, mut A) = background_color;
-  if is_windows_7() || A != 0 {
-    A = 255;
+  let (r, g, b, mut a) = background_color;
+  if is_windows_7() || a != 0 {
+    a = 255;
   }
 
   let controller2: ICoreWebView2Controller2 = controller.cast()?;
   controller2
-    .SetDefaultBackgroundColor(COREWEBVIEW2_COLOR { R, G, B, A })
+    .SetDefaultBackgroundColor(COREWEBVIEW2_COLOR {
+      R: r,
+      G: g,
+      B: b,
+      A: a,
+    })
     .map_err(Into::into)
 }