最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - Why does the Released event state not occur when window.close() gets called? - Stack Overflow

programmeradmin3浏览0评论

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");
}
发布评论

评论列表(0)

  1. 暂无评论