Skip to content

Commit e6d9b67

Browse files
refactor: remove unneeded focus code (#5065)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 8183153 commit e6d9b67

File tree

9 files changed

+170
-58
lines changed

9 files changed

+170
-58
lines changed

.changes/windows-update.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri-runtime-wry": minor
3+
"tauri-runtime": minor
4+
"tauri-utils": minor
5+
"tauri": minor
6+
---
7+
8+
Update windows to 0.39.0 and webview2-com to 0.19.1.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ codegen-units = 1
2929
lto = true
3030
incremental = false
3131
opt-level = "s"
32+
33+
[patch.crates-io]
34+
tao = { git = "https://github.com/tauri-apps/tao", branch = "dev" }

core/tauri-runtime-wry/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ exclude = [ "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { version = "0.20", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { git = "https://github.com/tauri-apps/wry", branch = "dev", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.10.2", path = "../tauri-runtime" }
1818
tauri-utils = { version = "1.0.3", path = "../tauri-utils" }
1919
uuid = { version = "1", features = [ "v4" ] }
2020
rand = "0.8"
2121
raw-window-handle = "0.5"
2222

2323
[target."cfg(windows)".dependencies]
24-
webview2-com = "0.16.0"
24+
webview2-com = "0.19.1"
2525

2626
[target."cfg(windows)".dependencies.windows]
27-
version = "0.37.0"
27+
version = "0.39.0"
2828
features = [ "Win32_Foundation" ]
2929

3030
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]

core/tauri-runtime-wry/src/lib.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,23 +2669,6 @@ fn handle_event_loop<T: UserEvent>(
26692669
event, window_id, ..
26702670
} => {
26712671
if let Some(window_id) = webview_id_map.get(&window_id) {
2672-
// NOTE(amrbashir): we handle this event here instead of `match` statement below because
2673-
// we want to focus the webview as soon as possible, especially on windows.
2674-
if event == WryWindowEvent::Focused(true) {
2675-
let w = windows
2676-
.borrow()
2677-
.get(&window_id)
2678-
.and_then(|w| w.inner.clone());
2679-
if let Some(WindowHandle::Webview(webview)) = w {
2680-
// only focus the webview if the window is visible
2681-
// somehow tao is sending a Focused(true) event even when the window is invisible,
2682-
// which causes a deadlock: https://github.com/tauri-apps/tauri/issues/3534
2683-
if webview.window().is_visible() {
2684-
webview.focus();
2685-
}
2686-
}
2687-
}
2688-
26892672
{
26902673
let windows_ref = windows.borrow();
26912674
if let Some(window) = windows_ref.get(&window_id) {
@@ -2980,7 +2963,7 @@ fn create_webview<T: UserEvent>(
29802963
let mut token = EventRegistrationToken::default();
29812964
unsafe {
29822965
controller.add_GotFocus(
2983-
FocusChangedEventHandler::create(Box::new(move |_, _| {
2966+
&FocusChangedEventHandler::create(Box::new(move |_, _| {
29842967
let _ = proxy_.send_event(Message::Webview(
29852968
window_id,
29862969
WebviewMessage::WebviewEvent(WebviewEvent::Focused(true)),
@@ -2993,7 +2976,7 @@ fn create_webview<T: UserEvent>(
29932976
.unwrap();
29942977
unsafe {
29952978
controller.add_LostFocus(
2996-
FocusChangedEventHandler::create(Box::new(move |_, _| {
2979+
&FocusChangedEventHandler::create(Box::new(move |_, _| {
29972980
let _ = proxy.send_event(Message::Webview(
29982981
window_id,
29992982
WebviewMessage::WebviewEvent(WebviewEvent::Focused(false)),

core/tauri-runtime/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ raw-window-handle = "0.5"
3535
rand = "0.8"
3636

3737
[target."cfg(windows)".dependencies]
38-
webview2-com = "0.16.0"
38+
webview2-com = "0.19.1"
3939

4040
[target."cfg(windows)".dependencies.windows]
41-
version = "0.37.0"
41+
version = "0.39.0"
4242
features = [ "Win32_Foundation" ]
4343

4444
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]

core/tauri-utils/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ semver = "1"
4040
heck = "0.4"
4141

4242
[target."cfg(windows)".dependencies.windows]
43-
version = "0.37.0"
43+
version = "0.39.0"
4444
features = [
45-
"alloc",
4645
"implement",
4746
"Win32_Foundation",
4847
"Win32_System_Com",

core/tauri-utils/src/platform.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,15 @@ pub use windows_platform::{is_windows_7, windows_version};
202202

203203
#[cfg(windows)]
204204
mod windows_platform {
205-
use windows::Win32::{
206-
Foundation::FARPROC,
207-
System::{
208-
LibraryLoader::{GetProcAddress, LoadLibraryA},
209-
SystemInformation::OSVERSIONINFOW,
205+
use std::{iter::once, os::windows::prelude::OsStrExt};
206+
use windows::{
207+
core::{PCSTR, PCWSTR},
208+
Win32::{
209+
Foundation::FARPROC,
210+
System::{
211+
LibraryLoader::{GetProcAddress, LoadLibraryW},
212+
SystemInformation::OSVERSIONINFOW,
213+
},
210214
},
211215
};
212216

@@ -221,11 +225,19 @@ mod windows_platform {
221225
false
222226
}
223227

228+
fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
229+
string.as_ref().encode_wide().chain(once(0)).collect()
230+
}
231+
232+
// Helper function to dynamically load function pointer.
233+
// `library` and `function` must be zero-terminated.
224234
fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
225-
assert_eq!(library.chars().last(), Some('\0'));
235+
let library = encode_wide(library);
226236
assert_eq!(function.chars().last(), Some('\0'));
237+
let function = PCSTR::from_raw(function.as_ptr());
227238

228-
let module = unsafe { LoadLibraryA(library) }.unwrap_or_default();
239+
// Library names we will use are ASCII so we can use the A version to avoid string conversion.
240+
let module = unsafe { LoadLibraryW(PCWSTR::from_raw(library.as_ptr())) }.unwrap_or_default();
229241
if module.is_invalid() {
230242
None
231243
} else {

core/tauri/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ cocoa = "0.24"
107107
objc = "0.2"
108108

109109
[target."cfg(windows)".dependencies]
110-
webview2-com = "0.16.0"
110+
webview2-com = "0.19.1"
111111
win7-notifications = { version = "0.3.0", optional = true }
112112

113113
[target."cfg(windows)".dependencies.windows]
114-
version = "0.37.0"
114+
version = "0.39.0"
115115
features = [ "Win32_Foundation" ]
116116

117117
[build-dependencies]

0 commit comments

Comments
 (0)