anshdadhich commited on
Commit
fe0bbbc
Β·
verified Β·
1 Parent(s): cff0bdd

Upload fastsearch-tauri/src-tauri/src/main.rs

Browse files
Files changed (1) hide show
  1. fastsearch-tauri/src-tauri/src/main.rs +80 -107
fastsearch-tauri/src-tauri/src/main.rs CHANGED
@@ -5,7 +5,13 @@ use std::sync::Arc;
5
  use parking_lot::RwLock;
6
  use crossbeam_channel::unbounded;
7
  use once_cell::sync::Lazy;
8
- use tauri::{Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, CustomMenuItem, WindowBuilder, WindowUrl, WindowEvent, PhysicalPosition};
 
 
 
 
 
 
9
  use serde::{Serialize, Deserialize};
10
 
11
  use fastsearch_core::index::store::IndexStore;
@@ -97,23 +103,24 @@ fn cmd_rescan() {
97
  }
98
 
99
  #[tauri::command]
100
- fn cmd_toggle_window(window: tauri::Window) {
101
  toggle_window(&window);
102
  }
103
 
104
  #[tauri::command]
105
- fn cmd_open_file(path: String) {
106
- let _ = std::process::Command::new("explorer")
107
- .arg("/select,")
108
- .arg(&path)
109
- .spawn();
110
  }
111
 
112
  #[tauri::command]
113
- fn cmd_open_folder(path: String) {
114
- let _ = std::process::Command::new("explorer")
115
- .arg(&path)
116
- .spawn();
 
117
  }
118
 
119
  #[tauri::command]
@@ -145,7 +152,7 @@ fn cmd_get_exclusions() -> Vec<String> {
145
  }
146
 
147
  // ── Window helpers ───────────────────────────────────────────────
148
- fn toggle_window(window: &tauri::Window) {
149
  if window.is_visible().unwrap_or(false) {
150
  let _ = window.hide();
151
  } else {
@@ -161,7 +168,7 @@ fn toggle_window(window: &tauri::Window) {
161
  }
162
  }
163
 
164
- fn show_window(window: &tauri::Window) {
165
  if !window.is_visible().unwrap_or(false) {
166
  let _ = window.show();
167
  let _ = window.set_focus();
@@ -355,111 +362,77 @@ fn init_index() {
355
  });
356
  }
357
 
358
- // ── Hotkey listener (Win+Space) ──────────────────────────────────
359
- fn setup_hotkey_listener(app_handle: tauri::AppHandle) {
360
- std::thread::spawn(move || {
361
- use rdev::{listen, EventType, Key};
362
-
363
- let mut win_pressed = false;
364
-
365
- let callback = move |event: rdev::Event| {
366
- match event.event_type {
367
- EventType::KeyPress(Key::MetaLeft) | EventType::KeyPress(Key::MetaRight) => {
368
- win_pressed = true;
369
- }
370
- EventType::KeyRelease(Key::MetaLeft) | EventType::KeyRelease(Key::MetaRight) => {
371
- win_pressed = false;
372
- }
373
- EventType::KeyPress(Key::Space) => {
374
- if win_pressed {
375
- if let Some(window) = app_handle.get_window("main") {
376
- let _ = window.emit("toggle-search", "");
377
- }
378
- }
379
- }
380
- _ => {}
381
- }
382
- };
383
-
384
- if let Err(e) = listen(callback) {
385
- eprintln!("Hotkey listener error: {:?}", e);
386
- }
387
- });
388
- }
389
-
390
  // ── Main ───────────────────────────────────────────────────────────
391
  fn main() {
392
- let tray_menu = SystemTrayMenu::new()
393
- .add_item(CustomMenuItem::new("show", "Show"))
394
- .add_native_item(SystemTrayMenuItem::Separator)
395
- .add_item(CustomMenuItem::new("quit", "Quit"));
396
-
397
- let system_tray = SystemTray::new().with_menu(tray_menu);
398
-
399
  tauri::Builder::default()
 
 
400
  .setup(|app| {
 
401
  init_index();
402
 
403
- let app_handle = app.handle();
404
- setup_hotkey_listener(app_handle.clone());
405
-
406
- let window = WindowBuilder::new(
407
- app,
408
- "main",
409
- WindowUrl::App("index.html".into())
410
- )
411
- .title("FastSeek")
412
- .inner_size(800.0, 520.0)
413
- .decorations(false)
414
- .transparent(true)
415
- .always_on_top(true)
416
- .skip_taskbar(true)
417
- .visible(false)
418
- .build()?;
419
-
420
- // Center window initially
421
- if let Some(monitor) = window.primary_monitor()? {
422
- let size = monitor.size();
423
- let win_size = window.outer_size()?;
424
- let x = (size.width as i32 - win_size.width as i32) / 2;
425
- let y = (size.height as i32 - win_size.height as i32) / 3;
426
- window.set_position(PhysicalPosition::new(x, y))?;
427
- }
428
-
429
- Ok(())
430
- })
431
- .system_tray(system_tray)
432
- .on_system_tray_event(|app, event| {
433
- match event {
434
- SystemTrayEvent::LeftClick { .. } => {
435
- if let Some(window) = app.get_window("main") {
436
- toggle_window(&window);
437
- }
438
- }
439
- SystemTrayEvent::MenuItemClick { id, .. } => {
440
- match id.as_str() {
441
- "show" => {
442
- if let Some(window) = app.get_window("main") {
443
- show_window(&window);
444
- }
445
  }
446
- "quit" => {
447
- std::process::exit(0);
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  }
449
- _ => {}
450
  }
451
- }
452
- _ => {}
453
- }
454
- })
455
- .on_window_event(|event| {
456
- match event.event() {
457
- WindowEvent::Focused(false) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
  // Optional: hide on blur (click outside)
459
- // event.window().hide().ok();
460
  }
461
- _ => {}
462
- }
 
463
  })
464
  .invoke_handler(tauri::generate_handler![
465
  cmd_search,
 
5
  use parking_lot::RwLock;
6
  use crossbeam_channel::unbounded;
7
  use once_cell::sync::Lazy;
8
+ use tauri::{
9
+ menu::{MenuBuilder, MenuItemBuilder},
10
+ tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
11
+ Manager, Emitter, PhysicalPosition,
12
+ };
13
+ use tauri_plugin_global_shortcut::ShortcutState;
14
+ use tauri_plugin_shell::ShellExt;
15
  use serde::{Serialize, Deserialize};
16
 
17
  use fastsearch_core::index::store::IndexStore;
 
103
  }
104
 
105
  #[tauri::command]
106
+ fn cmd_toggle_window(window: tauri::WebviewWindow) {
107
  toggle_window(&window);
108
  }
109
 
110
  #[tauri::command]
111
+ fn cmd_open_file(path: String, app: tauri::AppHandle) {
112
+ let _ = app.shell().open(
113
+ format!("explorer /select,\"{}\"", path.replace('"', "\\\"")),
114
+ None,
115
+ );
116
  }
117
 
118
  #[tauri::command]
119
+ fn cmd_open_folder(path: String, app: tauri::AppHandle) {
120
+ let _ = app.shell().open(
121
+ format!("explorer \"{}\"", path.replace('"', "\\\"")),
122
+ None,
123
+ );
124
  }
125
 
126
  #[tauri::command]
 
152
  }
153
 
154
  // ── Window helpers ───────────────────────────────────────────────
155
+ fn toggle_window(window: &tauri::WebviewWindow) {
156
  if window.is_visible().unwrap_or(false) {
157
  let _ = window.hide();
158
  } else {
 
168
  }
169
  }
170
 
171
+ fn show_window(window: &tauri::WebviewWindow) {
172
  if !window.is_visible().unwrap_or(false) {
173
  let _ = window.show();
174
  let _ = window.set_focus();
 
362
  });
363
  }
364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365
  // ── Main ───────────────────────────────────────────────────────────
366
  fn main() {
 
 
 
 
 
 
 
367
  tauri::Builder::default()
368
+ .plugin(tauri_plugin_global_shortcut::Builder::new().build())
369
+ .plugin(tauri_plugin_shell::init())
370
  .setup(|app| {
371
+ // Initialize index in background
372
  init_index();
373
 
374
+ // ── Tray Icon ──────────────────────────────────────────
375
+ let show = MenuItemBuilder::with_id("show", "Show").build(app)?;
376
+ let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
377
+ let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?;
378
+
379
+ let _tray = TrayIconBuilder::new()
380
+ .menu(&menu)
381
+ .on_menu_event(|app, event| match event.id().as_ref() {
382
+ "show" => {
383
+ if let Some(win) = app.get_webview_window("main") {
384
+ let _ = win.show();
385
+ let _ = win.set_focus();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  }
387
+ }
388
+ "quit" => {
389
+ app.exit(0);
390
+ }
391
+ _ => {}
392
+ })
393
+ .on_tray_icon_event(|tray, event| {
394
+ if let TrayIconEvent::Click {
395
+ button: MouseButton::Left,
396
+ button_state: MouseButtonState::Up,
397
+ ..
398
+ } = event {
399
+ let app = tray.app_handle();
400
+ if let Some(win) = app.get_webview_window("main") {
401
+ toggle_window(&win);
402
  }
 
403
  }
404
+ })
405
+ .build(app)?;
406
+
407
+ // ── Global Shortcut (Win+Space) ──────────────────────
408
+ app.handle().plugin(
409
+ tauri_plugin_global_shortcut::Builder::new()
410
+ .with_handler(|app, shortcut, event| {
411
+ if shortcut.matches_code(tauri_plugin_global_shortcut::Code::Space) {
412
+ if event.state == ShortcutState::Pressed {
413
+ if let Some(win) = app.get_webview_window("main") {
414
+ toggle_window(&win);
415
+ }
416
+ }
417
+ }
418
+ })
419
+ .build(),
420
+ )?;
421
+
422
+ // Register Win+Space shortcut
423
+ let shortcut_manager = app.global_shortcut();
424
+ let _ = shortcut_manager.register("Super+Space");
425
+
426
+ // ── Window Events ──────────────────────────────────────
427
+ let win = app.get_webview_window("main").unwrap();
428
+ win.on_window_event(|win, event| {
429
+ if let tauri::WindowEvent::Focused(false) = event {
430
  // Optional: hide on blur (click outside)
431
+ // let _ = win.hide();
432
  }
433
+ });
434
+
435
+ Ok(())
436
  })
437
  .invoke_handler(tauri::generate_handler![
438
  cmd_search,