Skip to content

Commit 7a5dfeb

Browse files
committed
支持自定义启动参数
1 parent d3764d8 commit 7a5dfeb

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ rand = "0.8.5"
4040
lazy_static = "1.5.0"
4141
itertools = "0.14.0"
4242
dirs = "5.0.1"
43+
shlex = "1.3.0"
4344

4445
[features]
4546
portable = []

src-tauri/src/api/launcher_api.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{path::Path, process::Command};
2+
13
use anyhow::Result;
24
use rand::{Rng, distributions::Alphanumeric};
35
use serde::Deserialize;
@@ -314,17 +316,13 @@ pub async fn launch(app: AppHandle, launcher_id: i64) -> Result<(), OneClickLaun
314316

315317
let mut resources = launcher_resource::query_by_launcher_id(&db.pool, launcher_id).await?;
316318

319+
tracing::debug!("启动编组原始资源列表: {resources:?}");
320+
317321
// 必须从启动资源中排除自己,防止出现死循环
318322
let app_path = current_exe_path_str()?;
319323
resources.retain(|e| {
320-
// 检查路径是否包含空格
321-
if e.path.contains(' ') {
322-
// 按空格拆分并取第一个部分进行比较
323-
e.path.split_whitespace().next() == Some(&app_path)
324-
} else {
325-
// 不包含空格时直接比较
326-
e.path.eq(&app_path)
327-
}
324+
// 检查路径是否指向当前应用程序
325+
!e.path.starts_with(&app_path)
328326
});
329327

330328
if resources.is_empty() {
@@ -372,12 +370,48 @@ pub fn launch_resources(app: &AppHandle, resources: &[LauncherResource]) {
372370
/// - `Ok(())` 表示操作成功。
373371
/// - `Err(OneClickLaunchError)` 表示操作失败。
374372
pub fn open_using_default_program(app: &AppHandle, path: &str) -> Result<(), OneClickLaunchError> {
373+
match try_open_as_command(path) {
374+
Ok(true) => return Ok(()), // 已经作为程序执行
375+
Ok(false) => {
376+
// 不是程序,交给 opener
377+
open_path_with_opener(app, path)?;
378+
}
379+
Err(e) => {
380+
tracing::debug!("作为命令执行失败: {e:?},尝试默认打开");
381+
open_path_with_opener(app, path)?;
382+
}
383+
}
384+
385+
Ok(())
386+
}
387+
388+
fn open_path_with_opener(app: &AppHandle, path: &str) -> Result<(), OneClickLaunchError> {
375389
app.opener()
376390
.open_path(path, None::<&str>)
377391
.map_err(|e| OneClickLaunchError::ExecutionError(e.to_string()))?;
378392
Ok(())
379393
}
380394

395+
fn try_open_as_command(path: &str) -> Result<bool, OneClickLaunchError> {
396+
let parts = shlex::split(path)
397+
.ok_or_else(|| OneClickLaunchError::ExecutionError("无法解析路径".to_string()))?;
398+
399+
if parts.is_empty() {
400+
return Ok(false);
401+
}
402+
403+
let program = &parts[0];
404+
let args = &parts[1..];
405+
406+
// 如果第一个部分是存在的文件(.exe/.bat/.sh 等),才当成命令执行
407+
if Path::new(program).exists() {
408+
Command::new(program).args(args).spawn()?;
409+
return Ok(true);
410+
}
411+
412+
Ok(false)
413+
}
414+
381415
#[tauri::command]
382416
pub async fn create_handler_shortcut(
383417
launcher_id: i64,

src/launcher.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="launcher" @contextmenu="handleRightClick">
33
<div class="header">
4-
<span v-if="!isEditing" class="name" @dblclick="editLauncherName" title="双击修改名称">
4+
<span v-if="!isEditing" class="name" @dblclick="editLauncherName" :title="data.name +' (双击修改)'">
55
{{ this.data.name }}
66
</span>
77
<input v-if="isEditing" v-model="newLauncherName" class="name-input" @blur="saveLauncherName"
@@ -50,7 +50,7 @@
5050
<div class="data-row" v-for="(item, index) in data.resources" :key="item.id" :title="item.path">
5151
<span class="data-text">
5252
<!-- 名称编辑部分 -->
53-
<span v-if="!editingResourceState.get(item.id)" @dblclick="editResourceName(item)" title="双击修改名称">
53+
<span v-if="!editingResourceState.get(item.id)" @dblclick="editResourceName(item)" :title="item.name + ' (双击修改)'">
5454
<strong>{{ item.name }}:</strong>
5555
</span>
5656
<input
@@ -66,7 +66,7 @@
6666
<span
6767
v-if="!editingResourcePathState.get(item.id)"
6868
@dblclick="editResourcePath(item)"
69-
title="双击修改路径"
69+
:title="item.path + ' (双击修改)'"
7070
class="path-text"
7171
>
7272
{{ item.path }}

0 commit comments

Comments
 (0)