window focus status not correct under windows system
Describe the bug After the application launches, a Focussed event should be triggered, but no such event occurs. At this point, calling the window's is_focused method should return true, but the actual result is false. Subsequently, when the window loses focus for the first time, a Focussed event should be emitted, and the focus state should be false, yet nothing happens.
Steps To Reproduce
use tao::{
event::{self, DeviceEvent, Event, RawKeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::KeyCode,
window::WindowBuilder,
};
fn main() {
let event_loop = EventLoop::new();
let mut window = Some(WindowBuilder::new().build(&event_loop).unwrap());
println!("Press '1' to display is window focused status.");
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::DeviceEvent {
event:
DeviceEvent::Key(RawKeyEvent {
physical_key,
state: event::ElementState::Pressed,
}),
..
} => {
if KeyCode::Digit1 == physical_key {
if let Some(ref win) = window {
// status should always be true here, but get false after app launched
println!("window focues status={} when press '1'", win.is_focused())
}
}
}
Event::WindowEvent {
event,
window_id: _,
..
} => match event {
WindowEvent::CloseRequested => {
window = None;
*control_flow = ControlFlow::Exit;
}
WindowEvent::Focused(focues_flag) => {
if let Some(_) = window {
// should print `window gain focus` after app launched, but nothing happened
if focues_flag {
println!("window gain focus")
} else {
println!("window lose focus")
}
} else {
println!("window is destoryed")
}
}
_ => (),
},
_ => (),
}
});
}
Expected behavior
- console print
window gain focusafter app launched, actually nothing happened - console print
window focues status=true when press '1'after app launched, actuallystatus=false - console print
window lose focuswhen windown lose focus first time, actually nothing happened
Platform and Versions (please complete the following information): OS: Windows 11 Rustc: 1.83.0 dao: 0.31.1
@Legend-Master Hey👋, excuse me. I am experiencing the same problem. My app window is set to auto-hide when it loses focus. On macOS, the window gains focus normally when displayed, but on Windows it doesn't autofocus and must be manually clicked on to gain focus.
macOS:
https://github.com/user-attachments/assets/1e5693cc-7ac3-4f0e-a1c7-a820176c46d0
Windows:
https://github.com/user-attachments/assets/5399f299-91f5-49cb-83a0-95b43c3fc939
Sorry but I can't reproduce this
with this code
use anyhow::Context;
use tauri::{
menu::MenuBuilder,
tray::{MouseButton, TrayIconBuilder, TrayIconEvent},
AppHandle, Manager,
};
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello {name}, You have been greeted from Rust!")
}
pub fn show_main_window(app_handle: &AppHandle) -> anyhow::Result<()> {
let window = app_handle
.get_webview_window("main")
.context("Main window not found")?;
window.unminimize()?;
window.show()?;
window.set_focus()?;
Ok(())
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.setup(|app| {
let app_handle = app.handle();
let menu = MenuBuilder::new(app_handle).quit().build()?;
TrayIconBuilder::with_id("main")
.icon(app_handle.default_window_icon().unwrap().clone())
.menu(&menu)
.on_tray_icon_event(|tray_icon, event| {
if matches!(
event,
TrayIconEvent::Click {
button: MouseButton::Left,
..
}
) {
show_main_window(tray_icon.app_handle()).unwrap();
}
})
.build(app_handle)?;
Ok(())
})
.on_window_event(|window, event| match event {
tauri::WindowEvent::Focused(focused) => {
// dbg!(focused);
if !*focused {
window.hide().unwrap();
}
}
_ => {}
})
.run(tauri::generate_context!(
"../../examples/helloworld/tauri.conf.json"
))
.expect("error while running tauri application");
}
https://github.com/user-attachments/assets/7a41f527-0981-4e99-8668-4aca221aac4e
Okay, thanks! I'll dig a little deeper into it. I really didn't have a problem when testing the other software.