admin管理员组文章数量:1388844
I am trying to make a Tauri application where when the main window gets closed it gets only minimized to the tray and if you press the shortcut Ctrl + E a floating window opens that gets closed on focus loss. When I use the Ctrl + E shortcut multiple times without a focus loss the keyboard shortcut event released is not occurring anymore. It starts working after hitting the shortcut multiple times. Why does this happen and how do I solve this.
use tauri::{
menu::{Menu, MenuItem}, tray::TrayIconBuilder, AppHandle, Emitter, Manager, WebviewUrl, WebviewWindowBuilder
};
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[tauri::command]
fn send(app: AppHandle, text: String) {
app.emit("text", &text).unwrap();
}
fn popup(app: &AppHandle) -> tauri::Result<()> {
WebviewWindowBuilder::new(app, "popup", WebviewUrl::App("".into()))
.decorations(false)
.always_on_top(true)
.skip_taskbar(true)
.build()?;
Ok(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![greet])
.on_window_event(|window, event| match event {
tauri::WindowEvent::Focused(is_focused) if !is_focused => {
if window.label() == "popup" {
//window.close blocks the Ctrl+E shortcut release event to occur
if let Err(e) = window.close() {
eprintln!("Error closing popup window: {:?}", e);
}
}
}
tauri::WindowEvent::CloseRequested { api, .. } => {
if window.label() == "main" {
window.hide().unwrap();
api.prevent_close();
}
}
_ => {}
})
.setup(|app| {
//let handle = app.handle().clone();
//send(handle, "test".to_string()());
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let show_i = MenuItem::with_id(app, "show", "Show", true, None::<&str>)?;
let menu = Menu::with_items(app, &[
&show_i,
&quit_i,
])?;
let _tray = TrayIconBuilder::new()
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu)
.show_menu_on_left_click(false)
.on_menu_event(|app, event| match event.id.as_ref() {
"quit" => {
app.exit(0);
}
"show" => {
let window = app.get_webview_window("main").unwrap();
if window.is_visible().unwrap() {
window.hide().unwrap();
} else {
window.show().unwrap();
window.set_focus().unwrap();
window.set_always_on_top(true).unwrap();
window.set_always_on_top(false).unwrap();
}
send(app.clone(), "show".to_string());
}
_ => {
println!("menu item {:?} not handled", event.id);
}
})
.build(app)?;
#[cfg(desktop)]
{
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
let ctrl_e_shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::KeyE);
if let Some(_main_window) = app.handle().get_webview_window("main") {
app.handle().plugin(
tauri_plugin_global_shortcut::Builder::new().with_handler(move |app, shortcut, event| {
println!("Received shortcut: {:?}", shortcut);
println!("Event state: {:?}", event.state());
if shortcut == &ctrl_e_shortcut {
match event.state() {
ShortcutState::Pressed => {
popup(app).ok();
}
ShortcutState::Released => {}
}
}
})
.build(),
)?;
// Register the global shortcut only for the main window
app.global_shortcut().register(ctrl_e_shortcut)?;
}
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
本文标签: rustWhy does the Released event state not occur when windowclose() gets calledStack Overflow
版权声明:本文标题:rust - Why does the Released event state not occur when window.close() gets called? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744605172a2615308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论