cursor position on screen stream
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use crate::services::cursor::stream_cursor_position;
|
||||
|
||||
static APP_HANDLE: std::sync::OnceLock<tauri::AppHandle<tauri::Wry>> = std::sync::OnceLock::new();
|
||||
|
||||
mod app;
|
||||
@@ -35,8 +37,7 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_positioner::init())
|
||||
// .plugin(tauri_plugin_global_shortcut::Builder::new().build())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.invoke_handler(tauri::generate_handler![stream_cursor_position])
|
||||
.setup(|app| {
|
||||
APP_HANDLE
|
||||
.set(app.handle().to_owned())
|
||||
|
||||
31
src-tauri/src/services/cursor.rs
Normal file
31
src-tauri/src/services/cursor.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use device_query::{DeviceEvents, DeviceEventsHandler};
|
||||
use serde::Serialize;
|
||||
use std::time::Duration;
|
||||
use tauri::ipc::Channel;
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CursorPosition {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn stream_cursor_position(on_event: Channel<CursorPosition>) {
|
||||
let device_state =
|
||||
DeviceEventsHandler::new(Duration::from_millis(200)).expect("Failed to start event loop");
|
||||
|
||||
let _guard = device_state.on_mouse_move(move |position| {
|
||||
let pos = CursorPosition {
|
||||
x: position.0,
|
||||
y: position.1,
|
||||
};
|
||||
// Ignore send error (e.g. frontend closed channel)
|
||||
let _ = on_event.send(pos);
|
||||
});
|
||||
|
||||
// Prevent function from exiting
|
||||
loop {
|
||||
tokio::time::sleep(Duration::ZERO).await;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod cursor;
|
||||
pub mod overlay;
|
||||
|
||||
Reference in New Issue
Block a user