Skip to content

Commit

Permalink
fix(windows): use webview events on windows (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jul 23, 2021
1 parent a8c1de5 commit d832d57
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changes/core-webview-events.md
@@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-runtime": patch
"tauri-runtime-wry": patch
---

Fix blur/focus events being incorrect on Windows.
69 changes: 68 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -57,6 +57,9 @@ use wry::{
},
};

#[cfg(target_os = "windows")]
use wry::webview::WebviewExtWindows;

use std::{
collections::HashMap,
convert::TryFrom,
Expand Down Expand Up @@ -281,7 +284,6 @@ impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
}
WryWindowEvent::CloseRequested => WindowEvent::CloseRequested,
WryWindowEvent::Destroyed => WindowEvent::Destroyed,
WryWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
WryWindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
Expand All @@ -295,6 +297,15 @@ impl<'a> From<&WryWindowEvent<'a>> for WindowEventWrapper {
}
}

impl From<&WebviewEvent> for WindowEventWrapper {
fn from(event: &WebviewEvent) -> Self {
let event = match event {
WebviewEvent::Focused(focused) => WindowEvent::Focused(*focused),
};
Self(Some(event))
}
}

pub struct MonitorHandleWrapper(MonitorHandle);

impl From<MonitorHandleWrapper> for Monitor {
Expand Down Expand Up @@ -690,9 +701,17 @@ enum WindowMessage {
#[derive(Debug, Clone)]
enum WebviewMessage {
EvaluateScript(String),
#[allow(dead_code)]
WebviewEvent(WebviewEvent),
Print,
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
enum WebviewEvent {
Focused(bool),
}

#[cfg(feature = "system-tray")]
#[derive(Clone)]
pub(crate) enum TrayMessage {
Expand Down Expand Up @@ -1330,6 +1349,33 @@ impl Runtime for Wry {
pending,
)?;

#[cfg(target_os = "windows")]
{
let id = webview.inner.window().id();
if let Some(controller) = webview.inner.controller() {
let proxy = self.event_loop.create_proxy();
controller
.add_got_focus(move |_| {
let _ = proxy.send_event(Message::Webview(
id,
WebviewMessage::WebviewEvent(WebviewEvent::Focused(true)),
));
Ok(())
})
.unwrap();
let proxy = self.event_loop.create_proxy();
controller
.add_lost_focus(move |_| {
let _ = proxy.send_event(Message::Webview(
id,
WebviewMessage::WebviewEvent(WebviewEvent::Focused(false)),
));
Ok(())
})
.unwrap();
};
}

let dispatcher = WryDispatcher {
window_id: webview.inner.window().id(),
context: DispatcherContext {
Expand Down Expand Up @@ -1567,6 +1613,12 @@ fn handle_event_loop(
Event::WindowEvent {
event, window_id, ..
} => {
if event == WryWindowEvent::Focused(true) {
if let Some(webview) = webviews.get(&window_id) {
webview.inner.focus();
};
}

if let Some(event) = WindowEventWrapper::from(&event).0 {
for handler in window_event_listeners
.lock()
Expand Down Expand Up @@ -1768,6 +1820,21 @@ fn handle_event_loop(
WebviewMessage::Print => {
let _ = webview.inner.print();
}
WebviewMessage::WebviewEvent(event) => {
if let Some(event) = WindowEventWrapper::from(&event).0 {
for handler in window_event_listeners
.lock()
.unwrap()
.get(&id)
.unwrap()
.lock()
.unwrap()
.values()
{
handler(&event);
}
}
}
}
}
}
Expand Down

0 comments on commit d832d57

Please sign in to comment.