Skip to content

Commit 4819157

Browse files
committed
根据设置选择退出应用程序或最小化到系统托盘
1 parent 563af26 commit 4819157

File tree

8 files changed

+82
-34
lines changed

8 files changed

+82
-34
lines changed

components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ declare module 'vue' {
1212
ElInput: typeof import('element-plus/es')['ElInput']
1313
ElOption: typeof import('element-plus/es')['ElOption']
1414
ElPopconfirm: typeof import('element-plus/es')['ElPopconfirm']
15+
ElRadio: typeof import('element-plus/es')['ElRadio']
16+
ElRadioGroup: typeof import('element-plus/es')['ElRadioGroup']
1517
ElSelect: typeof import('element-plus/es')['ElSelect']
1618
ElTooltip: typeof import('element-plus/es')['ElTooltip']
1719
}

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +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"
40+
itertools = "0.14.0"

src-tauri/src/api/setting_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub async fn save_setting(
3636
#[tauri::command]
3737
pub async fn read_setting(
3838
db: State<'_, DatabaseManager>,
39-
key: String,
39+
key: &str,
4040
) -> Result<Option<Settings>, OneClickLaunchError> {
41-
let setting = settings::read(&db.pool, &key).await?;
41+
let setting = settings::read(&db.pool, key).await?;
4242
Ok(setting)
4343
}
4444

src-tauri/src/constants.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ lazy_static! {
55
}
66

77
pub static APPLICATION_NAME: &str = "一键启动";
8-
pub static THEME: &str = "theme";
8+
pub static THEME_KEY: &str = "theme";
9+
pub static CLOSE_MAIN_PANEL_KEY: &str = "close_main_panel";
10+
pub static CLOSE_MAIN_PANEL_EXIT: &str = "m2";
911
pub static WINDOW_MIN_WIDTH: u32 = 800;
1012
pub static WINDOW_MIN_HEIGHT: u32 = 600;

src-tauri/src/db/settings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ where
4646
.bind(key)
4747
.fetch_optional(executor)
4848
.await?;
49+
println!("s :{:?}", settings);
4950
Ok(settings)
5051
}
5152

src-tauri/src/events/system_listeners.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
DatabaseManager,
66
api::{launcher_api, window_api},
77
check_launch_then_exit,
8-
constants::{AUTO_START_FLAG, THEME},
8+
constants::{AUTO_START_FLAG, THEME_KEY},
99
db::{launcher_resource, settings},
1010
events::EventDispatcher,
1111
};
@@ -164,7 +164,7 @@ fn hide_window(app: &AppHandle, payload: &LauncherLaunchedPayload) {
164164

165165
/// 隐藏窗口: 启动器启动隐藏窗口
166166
fn change_theme(app: &AppHandle, payload: &SettingUpdatedPayload) {
167-
if THEME == payload.key {
167+
if THEME_KEY == payload.key {
168168
match window_api::change_windows_theme(app, &payload.value) {
169169
Ok(_) => {
170170
debug!("修改主题成功,新主题: {}", payload.value)

src-tauri/src/lib.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,19 @@ fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
262262
tauri::WindowEvent::CloseRequested { api, .. } => {
263263
api.prevent_close();
264264
let _ = window.hide();
265+
266+
let app_handle = window.app_handle().clone();
267+
tauri::async_runtime::spawn(async move {
268+
let db = app_handle.state::<DatabaseManager>();
269+
let setting = setting_api::read_setting(db, constants::CLOSE_MAIN_PANEL_KEY).await;
270+
271+
match setting {
272+
Ok(Some(setting)) if constants::CLOSE_MAIN_PANEL_EXIT == setting.value => {
273+
app_handle.exit(0);
274+
}
275+
_ => {}
276+
}
277+
});
265278
}
266279
tauri::WindowEvent::ScaleFactorChanged {
267280
scale_factor,
@@ -298,19 +311,17 @@ fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
298311
}
299312
}
300313
}
301-
tauri::WindowEvent::DragDrop(drag_drop_event) => {
302-
match drag_drop_event {
303-
DragDropEvent::Drop { paths, .. } if !paths.is_empty() => {
304-
let _ = EventDispatcher::<DragDropResource>::send_event(
305-
window.app_handle(),
306-
DragDropResourcePaylod {
307-
paths: paths.clone(),
308-
},
309-
);
310-
}
311-
_ => {}
314+
tauri::WindowEvent::DragDrop(drag_drop_event) => match drag_drop_event {
315+
DragDropEvent::Drop { paths, .. } if !paths.is_empty() => {
316+
let _ = EventDispatcher::<DragDropResource>::send_event(
317+
window.app_handle(),
318+
DragDropResourcePaylod {
319+
paths: paths.clone(),
320+
},
321+
);
312322
}
313-
}
323+
_ => {}
324+
},
314325
tauri::WindowEvent::Resized(physical_size) => {
315326
if physical_size.width == 0 && physical_size.height == 0 {
316327
// 页面最小化时忽略

src/settings.vue

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
<input type="checkbox" v-model="autoLaunch" @change="toggleAutoLaunch" />
88
</div>
99

10-
<div class="m-4" style="display: flex; justify-content: space-between;align-items: center;">
11-
<p style="margin-right: 10px;">主题</p>
12-
<el-select append-to=".home" v-model="theme" @change="changeTheme" placeholder="Select" style="width: 240px">
13-
<el-option v-for="item in themes" :key="item.id" :label="item.value" :value="item.id" />
14-
</el-select>
10+
<div class="m-4" style="display: flex;justify-content: space-between; align-items: center;">
11+
<div style="display: flex;align-items: center;">
12+
<p style="margin-right: 10px;">启动编组后退出</p>
13+
<el-tooltip content="在启动某个编组后退出应用程序" placement="top">
14+
<Help theme="outline" size="15" fill="#333" />
15+
</el-tooltip>
16+
</div>
17+
<input type="checkbox" v-model="launchThenExit" @change="toggleLaunchThenExit" />
1518
</div>
1619

1720
<div class="m-4" style="display: flex; justify-content: space-between;align-items: center;">
@@ -21,25 +24,35 @@
2124
<Help theme="outline" size="15" fill="#333" />
2225
</el-tooltip>
2326
</div>
24-
<el-select append-to=".home" v-model="autoStartLauncherIds" @change="saveAutoStartLauncher" style="width: 240px;" multiple
25-
collapse-tags collapse-tags-tooltip placeholder="Select">
27+
<el-select append-to=".home" v-model="autoStartLauncherIds" @change="saveAutoStartLauncher"
28+
style="width: 240px;" multiple collapse-tags collapse-tags-tooltip placeholder="Select">
2629
<el-option v-for="item in launchers" :key="item.id" :label="item.name" :value="item.id">
2730
</el-option>
2831
</el-select>
2932
</div>
3033

34+
<div class="m-4" style="display: flex; justify-content: space-between;align-items: center;">
35+
<p style="margin-right: 10px;">主题</p>
36+
<el-select append-to=".home" v-model="theme" @change="changeTheme" placeholder="Select"
37+
style="width: 240px">
38+
<el-option v-for="item in themes" :key="item.id" :label="item.value" :value="item.id" />
39+
</el-select>
40+
</div>
41+
3142
<div class="m-4" style="display: flex;justify-content: space-between; align-items: center;">
3243
<div style="display: flex;align-items: center;">
33-
<p style="margin-right: 10px;">启动编组后退出</p>
34-
<el-tooltip content="在启动某个编组后退出应用程序" placement="top">
35-
<Help theme="outline" size="15" fill="#333" />
36-
</el-tooltip>
44+
<p style="margin-right: 10px;">关闭主面板</p>
45+
</div>
46+
<div class="my-2 ml-4">
47+
<el-radio-group v-model="closeMainPanel" @change="saveCloseMainPanel">
48+
<el-radio value="m1">最小化到系统托盘</el-radio>
49+
<el-radio value="m2">退出一键启动</el-radio>
50+
</el-radio-group>
3751
</div>
38-
<input type="checkbox" v-model="launchThenExit" @change="toggleLaunchThenExit" />
3952
</div>
4053

4154
<!-- 按钮 -->
42-
<button>刷新</button>
55+
<button @click="loadCloseMainPanel">刷新</button>
4356

4457
</div>
4558
</template>
@@ -62,6 +75,7 @@ export default {
6275
const autoStartLauncherIds = ref([]);
6376
const launchThenExit = ref(false);
6477
const themes = ref([{ "id": "light", "value": "浅色主题" }, { "id": "dark", "value": "深色主题" }]);
78+
const closeMainPanel = ref();
6579
6680
// 从后端加载主题
6781
const loadTheme = async () => {
@@ -130,6 +144,19 @@ export default {
130144
launchThenExit.value = kv == null || kv.value === "true";
131145
};
132146
147+
const loadCloseMainPanel = async () => {
148+
const kv = await invoke("read_setting", { key: "close_main_panel" });
149+
if (null == kv?.value) {
150+
closeMainPanel.value = "m1";
151+
} else {
152+
closeMainPanel.value = kv.value;
153+
}
154+
};
155+
156+
const saveCloseMainPanel = async () => {
157+
await invoke("save_setting", { key: "close_main_panel", value: closeMainPanel.value });
158+
};
159+
133160
const toggleLaunchThenExit = async () => {
134161
await invoke("save_setting", { key: "launch_then_exit", value: launchThenExit.value ? "true" : "false" });
135162
};
@@ -140,6 +167,7 @@ export default {
140167
fetchAutoLaunchStatus();
141168
refreshAutoStartLaunchers();
142169
loadLaunchThenExit();
170+
loadCloseMainPanel();
143171
});
144172
145173
return {
@@ -152,7 +180,10 @@ export default {
152180
changeTheme,
153181
toggleAutoLaunch,
154182
launchThenExit,
155-
toggleLaunchThenExit
183+
toggleLaunchThenExit,
184+
closeMainPanel,
185+
saveCloseMainPanel,
186+
loadCloseMainPanel
156187
};
157188
}
158189
};
@@ -188,8 +219,9 @@ label {
188219
189220
button {
190221
padding: 10px 20px;
191-
background-color: #409eff;;
192-
border-color: #409eff;
222+
background-color: #409eff;
223+
;
224+
border-color: #409eff;
193225
color: white;
194226
border: none;
195227
border-radius: 5px;

0 commit comments

Comments
 (0)