Skip to content

Commit 563af26

Browse files
committed
优化launcher的删除体验
1 parent 8553114 commit 563af26

File tree

9 files changed

+138
-50
lines changed

9 files changed

+138
-50
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module 'vue' {
1111
ElButton: typeof import('element-plus/es')['ElButton']
1212
ElInput: typeof import('element-plus/es')['ElInput']
1313
ElOption: typeof import('element-plus/es')['ElOption']
14+
ElPopconfirm: typeof import('element-plus/es')['ElPopconfirm']
1415
ElSelect: typeof import('element-plus/es')['ElSelect']
1516
ElTooltip: typeof import('element-plus/es')['ElTooltip']
1617
}

src-tauri/Cargo.lock

Lines changed: 11 additions & 1 deletion
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
@@ -37,3 +37,4 @@ sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "sqlite"] }
3737
tokio = { version = "1.43.0", features = ["full"] }
3838
rand = "0.8.5"
3939
lazy_static = "1.5.0"
40+
itertools = "0.14.0"

src-tauri/src/api/launcher_api.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use anyhow::Result;
22
use rand::{Rng, distributions::Alphanumeric};
3+
use serde::Deserialize;
34
use tauri::{AppHandle, State};
45
use tracing::info;
56

67
use crate::{
78
DatabaseManager,
89
db::{
910
launcher,
10-
launcher_resource::{self, LauncherResource},
11+
launcher_resource::{self, CreateResourceParam, LauncherResource},
1112
},
1213
error::OneClickLaunchError,
1314
events::{
@@ -27,7 +28,9 @@ pub async fn craete_launcher(
2728
db: State<'_, DatabaseManager>,
2829
name: Option<String>,
2930
) -> Result<i64, OneClickLaunchError> {
30-
let name = name.unwrap_or_else(generate_default_launcher_name);
31+
let name = name
32+
.filter(|s| !s.is_empty())
33+
.unwrap_or_else(generate_default_launcher_name);
3134
let launcher_id = launcher::create(&db.pool, &name, None).await?;
3235

3336
let _ = EventDispatcher::<LauncherBasicInfoUpdated>::send_event(
@@ -229,6 +232,35 @@ pub async fn add_resource(
229232
Ok(resource_id)
230233
}
231234

235+
/// 为启动器添加资源
236+
#[tauri::command]
237+
pub async fn add_resources(
238+
db: State<'_, DatabaseManager>,
239+
launcher_id: i64,
240+
resources: Vec<ResourceParam>,
241+
) -> Result<(), OneClickLaunchError> {
242+
let crps = resources
243+
.into_iter()
244+
.map(|r| CreateResourceParam {
245+
name: r
246+
.name
247+
.filter(|s| !s.is_empty())
248+
.unwrap_or_else(|| generate_name(r.path.as_str())),
249+
path: r.path,
250+
})
251+
.collect::<Vec<CreateResourceParam>>();
252+
253+
launcher_resource::create_resources(&db.pool, launcher_id, &crps).await?;
254+
255+
Ok(())
256+
}
257+
258+
#[derive(Deserialize, Debug)]
259+
pub struct ResourceParam {
260+
pub name: Option<String>,
261+
pub path: String,
262+
}
263+
232264
fn generate_name(path: &str) -> String {
233265
if path.starts_with("http") {
234266
return path.to_string();

src-tauri/src/db/launcher_resource.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Result;
2+
use itertools::Itertools;
23
use sqlx::{Executor, Sqlite};
34

45
/// 使用FromRow宏把数据库中读取出来的数据转换成LauncherResource结构
@@ -11,6 +12,11 @@ pub struct LauncherResource {
1112
pub path: String,
1213
}
1314

15+
pub struct CreateResourceParam {
16+
pub name: String,
17+
pub path: String,
18+
}
19+
1420
pub async fn initialize<'a, E>(executor: E) -> Result<()>
1521
where
1622
E: Executor<'a, Database = Sqlite>,
@@ -43,6 +49,29 @@ where
4349
Ok(id)
4450
}
4551

52+
// 批量新增
53+
pub async fn create_resources<'a, E>(
54+
executor: E,
55+
launcher_id: i64,
56+
resources: &[CreateResourceParam],
57+
) -> Result<()>
58+
where
59+
E: Executor<'a, Database = Sqlite>,
60+
{
61+
let mut query = String::from("INSERT INTO launcher_resource (launcher_id,name,path) VALUES ");
62+
63+
let values = resources
64+
.iter()
65+
.map(|r| format!(r#"('{}','{}','{}')"#, launcher_id, r.name, r.path))
66+
.join(",");
67+
68+
query.push_str(values.as_str());
69+
70+
sqlx::query(&query).execute(executor).await?;
71+
72+
Ok(())
73+
}
74+
4675
// 修改名称
4776
pub async fn modify_name<'a, E>(executor: E, resource_id: i64, name: &str) -> Result<()>
4877
where

src-tauri/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub async fn run() -> Result<()> {
174174
launcher_api::delete_launcher,
175175
launcher_api::modify_launcher_sort,
176176
launcher_api::add_resource,
177+
launcher_api::add_resources,
177178
launcher_api::modify_resource_name,
178179
launcher_api::delete_resource,
179180
launcher_api::query_launchers,
@@ -298,17 +299,16 @@ fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
298299
}
299300
}
300301
tauri::WindowEvent::DragDrop(drag_drop_event) => {
301-
// todo 优化
302-
// 发送一个DragDropResource的事件,让前端打来导入框
303-
if let DragDropEvent::Drop { paths, .. } = drag_drop_event {
304-
if !paths.is_empty() {
302+
match drag_drop_event {
303+
DragDropEvent::Drop { paths, .. } if !paths.is_empty() => {
305304
let _ = EventDispatcher::<DragDropResource>::send_event(
306305
window.app_handle(),
307306
DragDropResourcePaylod {
308307
paths: paths.clone(),
309308
},
310309
);
311310
}
311+
_ => {}
312312
}
313313
}
314314
tauri::WindowEvent::Resized(physical_size) => {

src/DragDropResource.vue

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
<div style="display: flex;align-items: center; gap: 10px;">
2121

2222
<el-tooltip v-if="isNewLauncher" content="取消新建" placement="top">
23-
<Close theme="outline" size="20" fill="#333" @click="handleNewLauncher" />
23+
<Close theme="outline" size="20" fill="#a19797" @click="handleNewLauncher" />
2424
</el-tooltip>
2525
<el-tooltip v-else content="新建编组" placement="top">
26-
<Add theme="outline" size="20" fill="#333" @click="handleNewLauncher" />
26+
<Add theme="outline" size="20" fill="#a19797" @click="handleNewLauncher" />
2727
</el-tooltip>
2828

2929
<el-select v-if="!isNewLauncher" append-to=".home" v-model="targetLauncher" style="width: 240px;"
@@ -35,7 +35,6 @@
3535
</div>
3636
</div>
3737

38-
<!-- 按钮 -->
3938
<div style="display: flex; justify-content: flex-end;gap:10px;align-items: center; margin-top: 20px;">
4039
<button class="cancel-button" @click="cancelDragDrop">取消</button>
4140
<button class="confirm-button" @click="confirmDragDrop">确定</button>
@@ -66,7 +65,7 @@ export default {
6665
const theme = inject('theme');
6766
const selectPathList = ref(props.pathList);
6867
const launchers = ref([]);
69-
const targetLauncher = ref({});
68+
const targetLauncher = ref();
7069
const isNewLauncher = ref(false);
7170
const newLauncherName = ref("");
7271
@@ -77,7 +76,9 @@ export default {
7776
7877
const initTargetLauncher = async () => {
7978
if (launchers.value.length > 0) {
80-
targetLauncher.value = launchers.value[0];
79+
targetLauncher.value = launchers.value[0].id;
80+
} else if (launchers.value.length == 0) {
81+
isNewLauncher.value = true;
8182
}
8283
};
8384
@@ -88,18 +89,25 @@ export default {
8889
8990
const cancelDragDrop = async () => {
9091
emit("cancel_drag_drop");
91-
}
92-
92+
}
93+
9394
const confirmDragDrop = async () => {
94-
// todo
95-
console.log("保存")
95+
let launcherId;
96+
if (isNewLauncher.value) {
97+
launcherId = await invoke("craete_launcher", { "name": newLauncherName.value });
98+
} else {
99+
launcherId = targetLauncher.value;
100+
}
101+
102+
await invoke("add_resources", { "launcherId": launcherId, "resources": selectPathList.value.map(path => ({ "path": path })) });
103+
96104
emit("confirm_drag_drop");
97105
}
98106
99-
// 在组件挂载时加载主题
100107
onMounted(() => {
101-
queryLaunchers();
102-
initTargetLauncher();
108+
queryLaunchers().then(() => {
109+
initTargetLauncher();
110+
})
103111
});
104112
105113
return {
@@ -142,12 +150,13 @@ label {
142150
143151
button {
144152
padding: 10px 20px;
145-
background-color: #409eff;;
153+
background-color: #409eff;
154+
;
146155
color: white;
147156
border: none;
148157
border-radius: 5px;
149158
cursor: pointer;
150-
border-color: #409eff;
159+
border-color: #409eff;
151160
}
152161
153162
button:hover {
@@ -189,11 +198,12 @@ input[type="checkbox"] {
189198
190199
.confirm-button {
191200
background-color: #409eff;
192-
border-color: #409eff;
201+
border-color: #409eff;
193202
color: #fff;
194203
}
204+
195205
.confirm-button:hover {
196-
background-color: #66b1ff;
206+
background-color: #66b1ff;
197207
border-color: #66b1ff;
198208
}
199209
</style>

src/home.vue

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
:class="['launcher-lite-container-item', theme]" @launcher-updated="refreshLaunchers"
2424
@launcher-moved="moveLauncher" />
2525
</div>
26-
<!-- 悬浮框 -->
26+
<!-- 设置 -->
2727
<div v-if="showSetting" :class="['modal-overlay', theme]" @click="closeSetting">
2828
<div :class="['modal-content', theme]" @click.stop>
2929
<span class="close-btn" @click="closeSetting">&times;</span>
3030
<settings />
3131
</div>
3232
</div>
33-
<!-- 悬浮框 -->
33+
<!-- 拖拽文件 -->
3434
<div v-if="dragDropResourcePaths.length > 0" :class="['modal-overlay', theme]" @click="closeSetting">
3535
<div :class="['modal-content', theme]" @click.stop>
3636
<dragDropResource :pathList="dragDropResourcePaths" @cancel_drag_drop="cleanDragDropResourcePaths"
37-
@confirm_drag_drop="cleanDragDropResourcePaths" />
37+
@confirm_drag_drop="confirmDragDrop" />
3838
</div>
3939
</div>
4040
</div>
@@ -134,7 +134,12 @@ export default {
134134
};
135135
136136
const cleanDragDropResourcePaths = async () => {
137-
dragDropResourcePaths.value = [];
137+
dragDropResourcePaths.value.splice(0, dragDropResourcePaths.value.length);
138+
};
139+
140+
const confirmDragDrop = async () => {
141+
dragDropResourcePaths.value.splice(0, dragDropResourcePaths.value.length);
142+
await refreshLaunchers();
138143
};
139144
140145
// 在组件挂载时加载主题
@@ -159,7 +164,8 @@ export default {
159164
openSetting,
160165
closeSetting,
161166
dragDropResourcePaths,
162-
cleanDragDropResourcePaths
167+
cleanDragDropResourcePaths,
168+
confirmDragDrop
163169
};
164170
}
165171
};

0 commit comments

Comments
 (0)