Skip to content

Commit 8553114

Browse files
committed
DragDropResource
1 parent 11e7a8a commit 8553114

File tree

8 files changed

+295
-20
lines changed

8 files changed

+295
-20
lines changed

components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export {}
88
/* prettier-ignore */
99
declare module 'vue' {
1010
export interface GlobalComponents {
11+
ElButton: typeof import('element-plus/es')['ElButton']
12+
ElInput: typeof import('element-plus/es')['ElInput']
1113
ElOption: typeof import('element-plus/es')['ElOption']
1214
ElSelect: typeof import('element-plus/es')['ElSelect']
1315
ElTooltip: typeof import('element-plus/es')['ElTooltip']

src-tauri/src/events/types.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use serde::{Deserialize, Serialize};
24

35
use super::Event;
@@ -75,3 +77,20 @@ impl Event for SettingUpdated {
7577
"setting:updated"
7678
}
7779
}
80+
81+
/// 拖放资源
82+
pub struct DragDropResource;
83+
/// 拖放资源载荷
84+
#[derive(Serialize, Deserialize, Clone, Debug)]
85+
pub struct DragDropResourcePaylod {
86+
/// 拖放到窗口上的路径列表。
87+
pub paths: Vec<PathBuf>,
88+
}
89+
90+
impl Event for DragDropResource {
91+
type Payload = DragDropResourcePaylod;
92+
93+
fn name() -> &'static str {
94+
"launcher:drag_drop_resource"
95+
}
96+
}

src-tauri/src/lib.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ use constants::{AUTO_START_FLAG, WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH};
55
use db::{launcher, launcher_resource, settings};
66
use events::EventDispatcher;
77
use events::system_listeners::register_system_listeners;
8-
use events::types::{ApplicationStartupComplete, ApplicationStartupCompletePayload};
8+
use events::types::{
9+
ApplicationStartupComplete, ApplicationStartupCompletePayload, DragDropResource,
10+
DragDropResourcePaylod,
11+
};
912
use sqlx::{Executor, Sqlite};
1013
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions};
1114
use std::path::PathBuf;
1215
use std::sync::Mutex;
1316
use std::time::Instant;
1417
use std::{env, fs};
15-
use tauri::Emitter;
1618
use tauri::tray::{MouseButton, MouseButtonState, TrayIcon, TrayIconEvent};
1719
use tauri::{AppHandle, Manager, tray::TrayIconBuilder};
20+
use tauri::{DragDropEvent, Emitter};
1821
use tauri_plugin_autostart::MacosLauncher;
1922
use tauri_plugin_opener::OpenerExt;
2023
use tracing::{debug, info};
@@ -248,6 +251,7 @@ fn setup_tray(app: &AppHandle) -> Result<()> {
248251
Ok(())
249252
}
250253

254+
/// 用于保存上次处理分辨率变更事件的时间
251255
struct ScaleFactorChangedState {
252256
last_reset: Mutex<Option<Instant>>,
253257
}
@@ -293,18 +297,34 @@ fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
293297
}
294298
}
295299
}
296-
tauri::WindowEvent::DragDrop(drag_drop_event) => todo!(),
300+
tauri::WindowEvent::DragDrop(drag_drop_event) => {
301+
// todo 优化
302+
// 发送一个DragDropResource的事件,让前端打来导入框
303+
if let DragDropEvent::Drop { paths, .. } = drag_drop_event {
304+
if !paths.is_empty() {
305+
let _ = EventDispatcher::<DragDropResource>::send_event(
306+
window.app_handle(),
307+
DragDropResourcePaylod {
308+
paths: paths.clone(),
309+
},
310+
);
311+
}
312+
}
313+
}
297314
tauri::WindowEvent::Resized(physical_size) => {
315+
if physical_size.width == 0 && physical_size.height == 0 {
316+
// 页面最小化时忽略
317+
return;
318+
}
298319
// 如果窗口大小过小,强制调整到正常大小
299320
if physical_size.width < WINDOW_MIN_WIDTH || physical_size.height < WINDOW_MIN_HEIGHT {
300321
let _ = window.set_size(tauri::Size::Logical(tauri::LogicalSize {
301322
width: WINDOW_MIN_WIDTH as f64,
302323
height: WINDOW_MIN_HEIGHT as f64,
303324
}));
304-
debug!("窗口大小过小,触发窗口大小重置");
325+
debug!("窗口大小过小,触发窗口大小重置: ps: {:?}", physical_size);
305326
}
306327
}
307-
_ => {
308-
}
328+
_ => {}
309329
}
310330
}

src/App.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
<script>
88
import { defineComponent } from "vue";
9-
import Launcher from "./launcher.vue";
10-
import LauncherLite from "./launcherLite.vue";
11-
import Settings from "./settings.vue";
12-
import Home from "./home.vue";
9+
import Launcher from "./Launcher.vue";
10+
import LauncherLite from "./LauncherLite.vue";
11+
import Settings from "./Settings.vue";
12+
import Home from "./Home.vue";
13+
import DragDropResource from "./DragDropResource.vue";
1314
import { invoke } from "@tauri-apps/api/core";
1415
1516
import { ref, provide } from 'vue';
@@ -39,7 +40,8 @@ export default defineComponent({
3940
Launcher,
4041
LauncherLite,
4142
Settings,
42-
Home
43+
Home,
44+
DragDropResource
4345
},
4446
mounted() {
4547
this.loadTheme();

src/DragDropResource.vue

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<template>
2+
<div class="container">
3+
<h2 class="title">添加资源</h2>
4+
5+
<div class="m-4" style="display: flex; justify-content: space-between;align-items: center;">
6+
<div style="display: flex;align-items: center;">
7+
<p style="margin-right: 10px;">资源:</p>
8+
</div>
9+
<el-select append-to=".home" v-model="selectPathList" style="width: 240px;" multiple collapse-tags
10+
collapse-tags-tooltip placeholder="Select">
11+
<el-option v-for="item in pathList" :key="item" :label="item" :value="item">
12+
</el-option>
13+
</el-select>
14+
</div>
15+
16+
<div class="m-4" style="display: flex; justify-content: space-between;align-items: center;">
17+
<div style="display: flex;align-items: center;">
18+
<p style="margin-right: 10px;">保存至:</p>
19+
</div>
20+
<div style="display: flex;align-items: center; gap: 10px;">
21+
22+
<el-tooltip v-if="isNewLauncher" content="取消新建" placement="top">
23+
<Close theme="outline" size="20" fill="#333" @click="handleNewLauncher" />
24+
</el-tooltip>
25+
<el-tooltip v-else content="新建编组" placement="top">
26+
<Add theme="outline" size="20" fill="#333" @click="handleNewLauncher" />
27+
</el-tooltip>
28+
29+
<el-select v-if="!isNewLauncher" append-to=".home" v-model="targetLauncher" style="width: 240px;"
30+
collapse-tags collapse-tags-tooltip placeholder="Select">
31+
<el-option v-for="item in launchers" :key="item.id" :label="item.name" :value="item.id">
32+
</el-option>
33+
</el-select>
34+
<el-input v-else v-model="newLauncherName" style="width: 240px;" placeholder="请输入新编组名称"></el-input>
35+
</div>
36+
</div>
37+
38+
<!-- 按钮 -->
39+
<div style="display: flex; justify-content: flex-end;gap:10px;align-items: center; margin-top: 20px;">
40+
<button class="cancel-button" @click="cancelDragDrop">取消</button>
41+
<button class="confirm-button" @click="confirmDragDrop">确定</button>
42+
</div>
43+
44+
</div>
45+
</template>
46+
47+
<script>
48+
import { enable, isEnabled, disable } from '@tauri-apps/plugin-autostart';
49+
import { invoke } from "@tauri-apps/api/core";
50+
import { onMounted, inject, ref, nextTick } from 'vue';
51+
import { Help, Add, Close } from '@icon-park/vue-next';
52+
53+
export default {
54+
props: {
55+
pathList: {
56+
type: Array,
57+
required: true,
58+
},
59+
},
60+
components: {
61+
Help,
62+
Add,
63+
Close
64+
},
65+
setup(props, { emit }) {
66+
const theme = inject('theme');
67+
const selectPathList = ref(props.pathList);
68+
const launchers = ref([]);
69+
const targetLauncher = ref({});
70+
const isNewLauncher = ref(false);
71+
const newLauncherName = ref("");
72+
73+
const queryLaunchers = async () => {
74+
const launchers_data = await invoke("query_launchers");
75+
launchers.value = [...launchers_data];
76+
};
77+
78+
const initTargetLauncher = async () => {
79+
if (launchers.value.length > 0) {
80+
targetLauncher.value = launchers.value[0];
81+
}
82+
};
83+
84+
const handleNewLauncher = async () => {
85+
isNewLauncher.value = !isNewLauncher.value;
86+
87+
}
88+
89+
const cancelDragDrop = async () => {
90+
emit("cancel_drag_drop");
91+
}
92+
93+
const confirmDragDrop = async () => {
94+
// todo
95+
console.log("保存")
96+
emit("confirm_drag_drop");
97+
}
98+
99+
// 在组件挂载时加载主题
100+
onMounted(() => {
101+
queryLaunchers();
102+
initTargetLauncher();
103+
});
104+
105+
return {
106+
selectPathList,
107+
launchers,
108+
targetLauncher,
109+
isNewLauncher,
110+
handleNewLauncher,
111+
newLauncherName,
112+
cancelDragDrop,
113+
confirmDragDrop
114+
};
115+
}
116+
};
117+
</script>
118+
119+
<style scoped>
120+
.container {
121+
/* 最大宽度 */
122+
max-width: 1000px;
123+
/* 最大高度 */
124+
max-height: 400px;
125+
/* 纵向滚动条 */
126+
overflow-y: auto;
127+
padding: 20px;
128+
/* 可选:添加边框 */
129+
border: 1px solid #ccc;
130+
/* 可选:圆角 */
131+
border-radius: 8px;
132+
}
133+
134+
135+
.title {
136+
margin-top: 0px;
137+
}
138+
139+
label {
140+
margin-right: 10px;
141+
}
142+
143+
button {
144+
padding: 10px 20px;
145+
background-color: #409eff;;
146+
color: white;
147+
border: none;
148+
border-radius: 5px;
149+
cursor: pointer;
150+
border-color: #409eff;
151+
}
152+
153+
button:hover {
154+
background-color: #0056b3;
155+
}
156+
157+
.cancel-button {
158+
background-color: #fff;
159+
/* 背景色为白色 */
160+
border-color: #ccc;
161+
/* 边框颜色为浅灰色 */
162+
color: #666;
163+
/* 字体颜色为深灰色 */
164+
}
165+
166+
.cancel-button:hover {
167+
background-color: #f5f5f5;
168+
/* 悬停时背景色为浅灰色 */
169+
border-color: #bbb;
170+
/* 悬停时边框颜色为灰色 */
171+
color: #333;
172+
/* 悬停时字体颜色为更深的灰色 */
173+
}
174+
175+
.checkbox-label {
176+
/* display: flex; */
177+
align-items: center;
178+
font-size: 16px;
179+
cursor: pointer;
180+
width: 100px;
181+
}
182+
183+
input[type="checkbox"] {
184+
margin-left: 10px;
185+
transform: scale(1.2);
186+
/* 放大复选框大小 */
187+
cursor: pointer;
188+
}
189+
190+
.confirm-button {
191+
background-color: #409eff;
192+
border-color: #409eff;
193+
color: #fff;
194+
}
195+
.confirm-button:hover {
196+
background-color: #66b1ff;
197+
border-color: #66b1ff;
198+
}
199+
</style>

0 commit comments

Comments
 (0)