Skip to content

Commit

Permalink
添加错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
SOVLOOKUP committed Oct 19, 2023
1 parent 314f333 commit 39b7286
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 54 deletions.
4 changes: 2 additions & 2 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export interface ClipBoardContentJson {
export function getClipboardContent(): ClipBoardContentJson | null
export function onInputEvent(callback: (event: string) => void): void
export function grabInputEvent(callback: (event: string) => boolean): void
export function exeLookBase64(fileName: string): string | null
export function parseLnk(path: string): string | null
export function exeLookBase64(fileName: string): string
export function parseLnk(path: string): string
export function parseLnkFallback(path: string): LnkData
export function sendKeyboardSimulation(cmd: string): void
export const enum MouseBtn {
Expand Down
13 changes: 7 additions & 6 deletions addon/src/clipboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clipboard_files;
use copypasta::{ClipboardContext, ClipboardProvider};
use napi::Result;

// use std::{
// path::PathBuf,
Expand Down Expand Up @@ -93,25 +94,25 @@ pub struct ClipBoardContentJson {

// 获取剪切板文件或者文本
#[napi]
pub fn get_clipboard_content() -> Option<ClipBoardContentJson> {
pub fn get_clipboard_content() -> Result<Option<ClipBoardContentJson>> {
let files = clipboard_files::read();
let mut ctx = ClipboardContext::new().unwrap();
match files {
Ok(f) => Some(ClipBoardContentJson {
Ok(f) => Ok(Some(ClipBoardContentJson {
r#type: "file".to_string(),
content: f
.into_iter()
.map(|c| c.to_str().unwrap().to_string())
.collect::<Vec<String>>(),
}),
})),
Err(_) => {
let content = ctx.get_contents();
match content {
Ok(text) => Some(ClipBoardContentJson {
Ok(text) => Ok(Some(ClipBoardContentJson {
r#type: "text".to_string(),
content: vec![text],
}),
Err(_) => None,
})),
Err(_) => Ok(None),
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions addon/src/monitor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use napi::{
bindgen_prelude::*,
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsBoolean,
JsBoolean, Result,
};
use rdev::{grab, listen, Event};
use std::{
Expand All @@ -10,10 +10,9 @@ use std::{
};

#[napi(ts_args_type = "callback: (event: string) => void")]
pub fn on_input_event(callback: JsFunction) {
let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> = callback
.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))
.unwrap();
pub fn on_input_event(callback: JsFunction) -> Result<()> {
let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> =
callback.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))?;

spawn(|| {
if let Err(error) = listen(move |event| {
Expand All @@ -25,13 +24,13 @@ pub fn on_input_event(callback: JsFunction) {
println!("Error: {:?}", error)
}
});
Ok(())
}

#[napi(ts_args_type = "callback: (event: string) => boolean")]
pub fn grab_input_event(callback: JsFunction) {
let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> = callback
.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))
.unwrap();
pub fn grab_input_event(callback: JsFunction) -> Result<()> {
let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> =
callback.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))?;

let gcallback = move |event: Event| -> Option<Event> {
let (s, r): (Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel::<bool>();
Expand Down Expand Up @@ -62,4 +61,5 @@ pub fn grab_input_event(callback: JsFunction) {
println!("GrabError: {:?}", error)
}
});
Ok(())
}
10 changes: 5 additions & 5 deletions addon/src/shotcut/exelook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ fn _exelook(file_name: String) -> Result<ShorCutImg> {
}

#[napi]
pub fn exe_look_base64(file_name: String) -> Option<String> {
if let Ok(l) = _exelook(file_name) {
Some("data:image/*;base64,".to_owned() + &general_purpose::STANDARD.encode(l.data))
} else {
None
pub fn exe_look_base64(file_name: String) -> napi::Result<String> {
let look = _exelook(file_name);
match look {
Ok(l) => Ok("data:image/*;base64,".to_owned() + &general_purpose::STANDARD.encode(l.data)),
Err(e) => Err(napi::Error::from_reason(format!("{:?}", e))),
}
}
28 changes: 15 additions & 13 deletions addon/src/shotcut/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use lnk_parser::LNKParser;
use napi::Result;
use std::path::PathBuf;
pub mod exelook;

#[napi]
pub fn parse_lnk(path: String) -> Option<String> {
pub fn parse_lnk(path: String) -> Result<String> {
let lnk_file = LNKParser::from_path(&path);
if let Ok(f) = lnk_file {
Some(serde_json::to_string(&f).unwrap())
} else {
None
match lnk_file {
Ok(f) => Ok(serde_json::to_string(&f).unwrap()),
Err(e) => Err(napi::Error::from_reason(e.to_string())),
}
}

Expand All @@ -28,14 +28,16 @@ fn convert(p: Option<PathBuf>) -> Option<String> {
}

#[napi]
pub fn parse_lnk_fallback(path: String) -> LnkData {
pub fn parse_lnk_fallback(path: String) -> Result<LnkData> {
let lnk_path = std::path::Path::new(&path);
let lnk = parselnk::Lnk::try_from(lnk_path).unwrap();

LnkData {
name_string: lnk.string_data.name_string,
relative_path: convert(lnk.string_data.relative_path),
working_dir: convert(lnk.string_data.working_dir),
icon_location: convert(lnk.string_data.icon_location),
let lnk = parselnk::Lnk::try_from(lnk_path);
match lnk {
Ok(l) => Ok(LnkData {
name_string: l.string_data.name_string,
relative_path: convert(l.string_data.relative_path),
working_dir: convert(l.string_data.working_dir),
icon_location: convert(l.string_data.icon_location),
}),
Err(e) => Err(napi::Error::from_reason(e.to_string())),
}
}
9 changes: 7 additions & 2 deletions addon/src/simulation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use enigo::{Enigo, KeyboardControllable, MouseButton, MouseControllable};
use napi::bindgen_prelude::*;
use napi::Result;

#[napi]
pub fn send_keyboard_simulation(cmd: String) {
pub fn send_keyboard_simulation(cmd: String) -> Result<()> {
let mut enigo = Enigo::new();
enigo.key_sequence_parse(&cmd);
if let Err(e) = enigo.key_sequence_parse_try(&cmd) {
Err(napi::Error::from_reason(e.to_string()))
} else {
Ok(())
}
}

#[napi]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubick-native",
"version": "0.0.13-beta",
"version": "0.0.13",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/sysapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getSystemApp = (extraPath?: string[]) => {
}
}

export const getAppIcon = (path: string): string | null => {
export const getAppIcon = (path: string): string => {
switch (platform()) {
case "win32":
return exeLookBase64(path)
Expand Down
8 changes: 3 additions & 5 deletions src/sysapp/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ export const shortcutWin = (extraPath: string[] = []) => ({
for (const p of defaultPaths) {
const o = await f.crawl(p).withPromise()
for (const t of o) {
const data = parseLnk(t)
const { name, dir } = parse(t)

if (data) {
try {
const data = parseLnk(t)
const d = JSON.parse(data)
yield ({
name,
Expand All @@ -30,10 +29,9 @@ export const shortcutWin = (extraPath: string[] = []) => ({
shortCutPath: t,
workingDir: d.working_dir ?? null,
}) as Apps
} else {
} catch {
const d = parseLnkFallback(t)
const execPath = join(dir, d.relativePath ?? '')

yield ({
name,
description: d.nameString ?? null,
Expand Down
12 changes: 2 additions & 10 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { grabInputEvent } from "./src"

grabInputEvent((event) => {
// if (event.event.type === "Wheel") {
// console.log(event);
// }
console.log(event);
return true
})

import { sendKeyboardSimulation } from "./src"
sendKeyboardSimulation("{+sdfsd}")
console.log(1);

0 comments on commit 39b7286

Please sign in to comment.