Skip to content

Commit 2161723

Browse files
committed
feat: 支持 Windows 平台不抢占其他窗口焦点
1 parent a6fdef1 commit 2161723

6 files changed

Lines changed: 61 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "ayangweb",
66
"email": "ayangweb@foxmail.com"
77
},
8-
"version": "0.6.0-beta.3",
8+
"version": "0.6.0-beta.4",
99
"type": "module",
1010
"scripts": {
1111
"dev": "run-s build:icon dev:vite",

src-tauri/src/plugins/paste/src/commands/macos.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::process::Command;
99
use std::sync::Mutex;
1010
use std::thread;
1111
use tauri::{command, AppHandle, Runtime, WebviewWindow};
12-
use tauri_plugin_eco_window::{set_macos_panel, MacOSPanelStatus, MAIN_WINDOW_TITLE};
12+
use tauri_plugin_eco_window::{set_ns_panel, NsPanelStatus, MAIN_WINDOW_TITLE};
1313

1414
static PREVIOUS_WINDOW: Mutex<Option<i32>> = Mutex::new(None);
1515

@@ -82,7 +82,7 @@ pub fn get_previous_window() -> Option<i32> {
8282
// 粘贴
8383
#[command]
8484
pub async fn paste<R: Runtime>(app_handle: AppHandle<R>, window: WebviewWindow<R>) {
85-
set_macos_panel(&app_handle, &window, MacOSPanelStatus::Resign);
85+
set_ns_panel(&app_handle, &window, NsPanelStatus::Resign);
8686

8787
let script = r#"tell application "System Events" to keystroke "v" using command down"#;
8888

src-tauri/src/plugins/window/src/commands/not_macos.rs renamed to src-tauri/src/plugins/window/src/commands/linux.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
use super::{shared_hide_window, shared_show_window};
21
use tauri::{command, AppHandle, Runtime, WebviewWindow};
32

43
// 显示窗口
54
#[command]
65
pub async fn show_window<R: Runtime>(_app_handle: AppHandle<R>, window: WebviewWindow<R>) {
7-
shared_show_window(&window);
6+
let _ = window.show();
7+
let _ = window.unminimize();
8+
let _ = window.set_focus();
89
}
910

1011
// 隐藏窗口
1112
#[command]
1213
pub async fn hide_window<R: Runtime>(_app_handle: AppHandle<R>, window: WebviewWindow<R>) {
13-
shared_hide_window(&window);
14+
let _ = window.hide();
1415
}
1516

1617
// 显示任务栏图标

src-tauri/src/plugins/window/src/commands/macos.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::{is_main_window, shared_hide_window, shared_show_window};
1+
use super::is_main_window;
22
use crate::MAIN_WINDOW_LABEL;
33
use tauri::{command, AppHandle, Runtime, WebviewWindow};
44
use tauri_nspanel::{CollectionBehavior, ManagerExt};
55

6-
pub enum MacOSPanelStatus {
6+
pub enum NsPanelStatus {
77
Show,
88
Hide,
99
Resign,
@@ -13,19 +13,21 @@ pub enum MacOSPanelStatus {
1313
#[command]
1414
pub async fn show_window<R: Runtime>(app_handle: AppHandle<R>, window: WebviewWindow<R>) {
1515
if is_main_window(&window) {
16-
set_macos_panel(&app_handle, &window, MacOSPanelStatus::Show);
16+
set_ns_panel(&app_handle, &window, NsPanelStatus::Show);
1717
} else {
18-
shared_show_window(&window);
18+
let _ = window.show();
19+
let _ = window.unminimize();
20+
let _ = window.set_focus();
1921
}
2022
}
2123

2224
// 隐藏窗口
2325
#[command]
2426
pub async fn hide_window<R: Runtime>(app_handle: AppHandle<R>, window: WebviewWindow<R>) {
2527
if is_main_window(&window) {
26-
set_macos_panel(&app_handle, &window, MacOSPanelStatus::Hide);
28+
set_ns_panel(&app_handle, &window, NsPanelStatus::Hide);
2729
} else {
28-
shared_hide_window(&window);
30+
let _ = window.hide();
2931
}
3032
}
3133

@@ -40,18 +42,18 @@ pub async fn show_taskbar_icon<R: Runtime>(
4042
}
4143

4244
// 设置 macos 的 ns_panel 的状态
43-
pub fn set_macos_panel<R: Runtime>(
45+
pub fn set_ns_panel<R: Runtime>(
4446
app_handle: &AppHandle<R>,
4547
window: &WebviewWindow<R>,
46-
status: MacOSPanelStatus,
48+
status: NsPanelStatus,
4749
) {
4850
if is_main_window(window) {
4951
let app_handle_clone = app_handle.clone();
5052

5153
let _ = app_handle.run_on_main_thread(move || {
5254
if let Ok(panel) = app_handle_clone.get_webview_panel(MAIN_WINDOW_LABEL) {
5355
match status {
54-
MacOSPanelStatus::Show => {
56+
NsPanelStatus::Show => {
5557
panel.show_and_make_key();
5658

5759
panel.set_collection_behavior(
@@ -62,7 +64,7 @@ pub fn set_macos_panel<R: Runtime>(
6264
.into(),
6365
);
6466
}
65-
MacOSPanelStatus::Hide => {
67+
NsPanelStatus::Hide => {
6668
panel.hide();
6769

6870
panel.set_collection_behavior(
@@ -73,7 +75,7 @@ pub fn set_macos_panel<R: Runtime>(
7375
.into(),
7476
);
7577
}
76-
MacOSPanelStatus::Resign => {
78+
NsPanelStatus::Resign => {
7779
panel.resign_key_window();
7880
}
7981
}

src-tauri/src/plugins/window/src/commands/mod.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,26 @@ pub static MAIN_WINDOW_TITLE: &str = "EcoPaste";
1010
#[cfg(target_os = "macos")]
1111
mod macos;
1212

13-
#[cfg(not(target_os = "macos"))]
14-
mod not_macos;
13+
#[cfg(target_os = "windows")]
14+
mod windows;
15+
16+
#[cfg(target_os = "linux")]
17+
mod linux;
1518

1619
#[cfg(target_os = "macos")]
1720
pub use macos::*;
1821

19-
#[cfg(not(target_os = "macos"))]
20-
pub use not_macos::*;
22+
#[cfg(target_os = "windows")]
23+
pub use windows::*;
24+
25+
#[cfg(target_os = "linux")]
26+
pub use linux::*;
2127

2228
// 是否为主窗口
2329
pub fn is_main_window<R: Runtime>(window: &WebviewWindow<R>) -> bool {
2430
window.label() == MAIN_WINDOW_LABEL
2531
}
2632

27-
// 共享显示窗口的方法
28-
fn shared_show_window<R: Runtime>(window: &WebviewWindow<R>) {
29-
let _ = window.show();
30-
let _ = window.unminimize();
31-
let _ = window.set_focus();
32-
}
33-
34-
// 共享隐藏窗口的方法
35-
fn shared_hide_window<R: Runtime>(window: &WebviewWindow<R>) {
36-
let _ = window.hide();
37-
}
38-
3933
// 显示主窗口
4034
pub fn show_main_window(app_handle: &AppHandle) {
4135
show_window_by_label(app_handle, MAIN_WINDOW_LABEL);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use super::is_main_window;
2+
use tauri::{command, AppHandle, Runtime, WebviewWindow};
3+
4+
// 显示窗口
5+
#[command]
6+
pub async fn show_window<R: Runtime>(_app_handle: AppHandle<R>, window: WebviewWindow<R>) {
7+
let _ = window.show();
8+
let _ = window.unminimize();
9+
10+
if is_main_window(&window) {
11+
let _ = window.set_focusable(false);
12+
} else {
13+
let _ = window.set_focus();
14+
}
15+
}
16+
17+
// 隐藏窗口
18+
#[command]
19+
pub async fn hide_window<R: Runtime>(_app_handle: AppHandle<R>, window: WebviewWindow<R>) {
20+
let _ = window.hide();
21+
}
22+
23+
// 显示任务栏图标
24+
#[command]
25+
pub async fn show_taskbar_icon<R: Runtime>(
26+
_app_handle: AppHandle<R>,
27+
window: WebviewWindow<R>,
28+
visible: bool,
29+
) {
30+
let _ = window.set_skip_taskbar(!visible);
31+
}

0 commit comments

Comments
 (0)