mirror of
https://github.com/ayangweb/BongoCat.git
synced 2026-03-12 17:51:48 +08:00
fix: 修复 macOS 26 上无法使用的问题 (#548)
This commit is contained in:
@@ -2,7 +2,7 @@ use rdev::{Event, EventType, listen};
|
||||
use serde::Serialize;
|
||||
use serde_json::{Value, json};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tauri::{AppHandle, Emitter};
|
||||
use tauri::{AppHandle, Emitter, Runtime, command};
|
||||
|
||||
static IS_RUNNING: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
@@ -21,15 +21,16 @@ pub struct DeviceEvent {
|
||||
value: Value,
|
||||
}
|
||||
|
||||
pub fn start_listening(app_handle: AppHandle) {
|
||||
#[command]
|
||||
pub async fn start_device_listening<R: Runtime>(app_handle: AppHandle<R>) -> Result<(), String> {
|
||||
if IS_RUNNING.load(Ordering::SeqCst) {
|
||||
return;
|
||||
return Err("Device is already listening".to_string());
|
||||
}
|
||||
|
||||
IS_RUNNING.store(true, Ordering::SeqCst);
|
||||
|
||||
let callback = move |event: Event| {
|
||||
let device = match event.event_type {
|
||||
let device_event = match event.event_type {
|
||||
EventType::ButtonPress(button) => DeviceEvent {
|
||||
kind: DeviceKind::MousePress,
|
||||
value: json!(format!("{:?}", button)),
|
||||
@@ -53,20 +54,10 @@ pub fn start_listening(app_handle: AppHandle) {
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if let Err(e) = app_handle.emit("device-changed", device) {
|
||||
eprintln!("Failed to emit event: {:?}", e);
|
||||
}
|
||||
let _ = app_handle.emit("device-changed", device_event);
|
||||
};
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Err(e) = listen(callback) {
|
||||
eprintln!("Device listening error: {:?}", e);
|
||||
}
|
||||
listen(callback).map_err(|err| format!("Failed to listen device: {:?}", err))?;
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
std::thread::spawn(move || {
|
||||
if let Err(e) = listen(callback) {
|
||||
eprintln!("Device listening error: {:?}", e);
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod core;
|
||||
mod utils;
|
||||
|
||||
use core::{device, prevent_default, setup};
|
||||
use core::{device::start_device_listening, prevent_default, setup};
|
||||
use tauri::{Manager, WindowEvent, generate_handler};
|
||||
use tauri_plugin_autostart::MacosLauncher;
|
||||
use tauri_plugin_custom_window::{
|
||||
@@ -21,11 +21,9 @@ pub fn run() {
|
||||
|
||||
setup::default(&app_handle, main_window.clone(), preference_window.clone());
|
||||
|
||||
device::start_listening(app_handle.clone());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(generate_handler![copy_dir])
|
||||
.invoke_handler(generate_handler![copy_dir, start_device_listening])
|
||||
.plugin(tauri_plugin_custom_window::init())
|
||||
.plugin(tauri_plugin_os::init())
|
||||
.plugin(tauri_plugin_process::init())
|
||||
|
||||
@@ -11,4 +11,5 @@ export const LISTEN_KEY = {
|
||||
|
||||
export const INVOKE_KEY = {
|
||||
COPY_DIR: 'copy_dir',
|
||||
START_DEVICE_LISTENING: 'start_device_listening',
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import { convertFileSrc } from '@tauri-apps/api/core'
|
||||
import { convertFileSrc, invoke } from '@tauri-apps/api/core'
|
||||
import { Menu } from '@tauri-apps/api/menu'
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { useDebounceFn, useEventListener } from '@vueuse/core'
|
||||
import { onUnmounted, ref, watch } from 'vue'
|
||||
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
||||
|
||||
import { useDevice } from '@/composables/useDevice'
|
||||
import { useModel } from '@/composables/useModel'
|
||||
import { useSharedMenu } from '@/composables/useSharedMenu'
|
||||
import { INVOKE_KEY } from '@/constants'
|
||||
import { hideWindow, setAlwaysOnTop, showWindow } from '@/plugins/window'
|
||||
import { useCatStore } from '@/stores/cat'
|
||||
import { useModelStore } from '@/stores/model'
|
||||
@@ -21,6 +22,10 @@ const { getSharedMenu } = useSharedMenu()
|
||||
const modelStore = useModelStore()
|
||||
const resizing = ref(false)
|
||||
|
||||
onMounted(() => {
|
||||
invoke(INVOKE_KEY.START_DEVICE_LISTENING)
|
||||
})
|
||||
|
||||
onUnmounted(handleDestroy)
|
||||
|
||||
const debouncedResize = useDebounceFn(async () => {
|
||||
|
||||
Reference in New Issue
Block a user