Skip to content

Commit

Permalink
refactor: remove unneeded focus code (#5065)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Sep 3, 2022
1 parent 8183153 commit e6d9b67
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 58 deletions.
8 changes: 8 additions & 0 deletions .changes/windows-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri-runtime-wry": minor
"tauri-runtime": minor
"tauri-utils": minor
"tauri": minor
---

Update windows to 0.39.0 and webview2-com to 0.19.1.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ codegen-units = 1
lto = true
incremental = false
opt-level = "s"

[patch.crates-io]
tao = { git = "https://github.com/tauri-apps/tao", branch = "dev" }
6 changes: 3 additions & 3 deletions core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ exclude = [ "CHANGELOG.md", "/target" ]
readme = "README.md"

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

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

[target."cfg(windows)".dependencies.windows]
version = "0.37.0"
version = "0.39.0"
features = [ "Win32_Foundation" ]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
Expand Down
21 changes: 2 additions & 19 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2669,23 +2669,6 @@ fn handle_event_loop<T: UserEvent>(
event, window_id, ..
} => {
if let Some(window_id) = webview_id_map.get(&window_id) {
// NOTE(amrbashir): we handle this event here instead of `match` statement below because
// we want to focus the webview as soon as possible, especially on windows.
if event == WryWindowEvent::Focused(true) {
let w = windows
.borrow()
.get(&window_id)
.and_then(|w| w.inner.clone());
if let Some(WindowHandle::Webview(webview)) = w {
// only focus the webview if the window is visible
// somehow tao is sending a Focused(true) event even when the window is invisible,
// which causes a deadlock: https://github.com/tauri-apps/tauri/issues/3534
if webview.window().is_visible() {
webview.focus();
}
}
}

{
let windows_ref = windows.borrow();
if let Some(window) = windows_ref.get(&window_id) {
Expand Down Expand Up @@ -2980,7 +2963,7 @@ fn create_webview<T: UserEvent>(
let mut token = EventRegistrationToken::default();
unsafe {
controller.add_GotFocus(
FocusChangedEventHandler::create(Box::new(move |_, _| {
&FocusChangedEventHandler::create(Box::new(move |_, _| {
let _ = proxy_.send_event(Message::Webview(
window_id,
WebviewMessage::WebviewEvent(WebviewEvent::Focused(true)),
Expand All @@ -2993,7 +2976,7 @@ fn create_webview<T: UserEvent>(
.unwrap();
unsafe {
controller.add_LostFocus(
FocusChangedEventHandler::create(Box::new(move |_, _| {
&FocusChangedEventHandler::create(Box::new(move |_, _| {
let _ = proxy.send_event(Message::Webview(
window_id,
WebviewMessage::WebviewEvent(WebviewEvent::Focused(false)),
Expand Down
4 changes: 2 additions & 2 deletions core/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ raw-window-handle = "0.5"
rand = "0.8"

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

[target."cfg(windows)".dependencies.windows]
version = "0.37.0"
version = "0.39.0"
features = [ "Win32_Foundation" ]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
Expand Down
3 changes: 1 addition & 2 deletions core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ semver = "1"
heck = "0.4"

[target."cfg(windows)".dependencies.windows]
version = "0.37.0"
version = "0.39.0"
features = [
"alloc",
"implement",
"Win32_Foundation",
"Win32_System_Com",
Expand Down
26 changes: 19 additions & 7 deletions core/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,15 @@ pub use windows_platform::{is_windows_7, windows_version};

#[cfg(windows)]
mod windows_platform {
use windows::Win32::{
Foundation::FARPROC,
System::{
LibraryLoader::{GetProcAddress, LoadLibraryA},
SystemInformation::OSVERSIONINFOW,
use std::{iter::once, os::windows::prelude::OsStrExt};
use windows::{
core::{PCSTR, PCWSTR},
Win32::{
Foundation::FARPROC,
System::{
LibraryLoader::{GetProcAddress, LoadLibraryW},
SystemInformation::OSVERSIONINFOW,
},
},
};

Expand All @@ -221,11 +225,19 @@ mod windows_platform {
false
}

fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
string.as_ref().encode_wide().chain(once(0)).collect()
}

// Helper function to dynamically load function pointer.
// `library` and `function` must be zero-terminated.
fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
assert_eq!(library.chars().last(), Some('\0'));
let library = encode_wide(library);
assert_eq!(function.chars().last(), Some('\0'));
let function = PCSTR::from_raw(function.as_ptr());

let module = unsafe { LoadLibraryA(library) }.unwrap_or_default();
// Library names we will use are ASCII so we can use the A version to avoid string conversion.
let module = unsafe { LoadLibraryW(PCWSTR::from_raw(library.as_ptr())) }.unwrap_or_default();
if module.is_invalid() {
None
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ cocoa = "0.24"
objc = "0.2"

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

[target."cfg(windows)".dependencies.windows]
version = "0.37.0"
version = "0.39.0"
features = [ "Win32_Foundation" ]

[build-dependencies]
Expand Down

0 comments on commit e6d9b67

Please sign in to comment.