mirror of
https://github.com/ayangweb/BongoCat.git
synced 2026-03-12 17:51:48 +08:00
fix: fix windows startup crash (#1)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use rdev::{listen, Event, EventType};
|
||||
use serde::Serialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tauri::{command, AppHandle, Emitter};
|
||||
use std::{
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
thread::spawn,
|
||||
};
|
||||
use tauri::{AppHandle, Emitter};
|
||||
|
||||
static IS_RUNNING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
@@ -21,41 +24,46 @@ pub struct DeviceEvent {
|
||||
value: Value,
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn start_listening(app_handle: AppHandle) -> Result<(), String> {
|
||||
pub fn start_listening(app_handle: AppHandle) {
|
||||
if IS_RUNNING.load(Ordering::SeqCst) {
|
||||
return Ok(());
|
||||
return;
|
||||
}
|
||||
|
||||
IS_RUNNING.store(true, Ordering::SeqCst);
|
||||
|
||||
let callback = move |event: Event| {
|
||||
let device = match event.event_type {
|
||||
EventType::ButtonPress(button) => DeviceEvent {
|
||||
kind: DeviceKind::MousePress,
|
||||
value: json!(format!("{:?}", button)),
|
||||
},
|
||||
EventType::ButtonRelease(button) => DeviceEvent {
|
||||
kind: DeviceKind::MouseRelease,
|
||||
value: json!(format!("{:?}", button)),
|
||||
},
|
||||
EventType::MouseMove { x, y } => DeviceEvent {
|
||||
kind: DeviceKind::MouseMove,
|
||||
value: json!({ "x": x, "y": y }),
|
||||
},
|
||||
EventType::KeyPress(key) => DeviceEvent {
|
||||
kind: DeviceKind::KeyboardPress,
|
||||
value: json!(format!("{:?}", key)),
|
||||
},
|
||||
EventType::KeyRelease(key) => DeviceEvent {
|
||||
kind: DeviceKind::KeyboardRelease,
|
||||
value: json!(format!("{:?}", key)),
|
||||
},
|
||||
_ => return,
|
||||
spawn(move || {
|
||||
let callback = move |event: Event| {
|
||||
let device = match event.event_type {
|
||||
EventType::ButtonPress(button) => DeviceEvent {
|
||||
kind: DeviceKind::MousePress,
|
||||
value: json!(format!("{:?}", button)),
|
||||
},
|
||||
EventType::ButtonRelease(button) => DeviceEvent {
|
||||
kind: DeviceKind::MouseRelease,
|
||||
value: json!(format!("{:?}", button)),
|
||||
},
|
||||
EventType::MouseMove { x, y } => DeviceEvent {
|
||||
kind: DeviceKind::MouseMove,
|
||||
value: json!({ "x": x, "y": y }),
|
||||
},
|
||||
EventType::KeyPress(key) => DeviceEvent {
|
||||
kind: DeviceKind::KeyboardPress,
|
||||
value: json!(format!("{:?}", key)),
|
||||
},
|
||||
EventType::KeyRelease(key) => DeviceEvent {
|
||||
kind: DeviceKind::KeyboardRelease,
|
||||
value: json!(format!("{:?}", key)),
|
||||
},
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if let Err(e) = app_handle.emit("change", device) {
|
||||
eprintln!("Failed to emit event: {:?}", e);
|
||||
}
|
||||
};
|
||||
|
||||
let _ = app_handle.emit("change", device);
|
||||
};
|
||||
|
||||
listen(callback).map_err(|err| format!("{:?}", err))
|
||||
if let Err(e) = listen(callback) {
|
||||
eprintln!("Device listening error: {:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ pub fn run() {
|
||||
|
||||
setup::default(&app_handle, main_window.clone(), preference_window.clone());
|
||||
|
||||
let _ = device::start_listening(app_handle.clone());
|
||||
device::start_listening(app_handle.clone());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user