Skip to content

Commit

Permalink
fix: 修复了 macOS 无法保存剪贴板窗口大小和位置的问题 (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Oct 23, 2024
1 parent 4a33764 commit 44a39f0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src-tauri/src/core/setup/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use tauri_nspanel::{
cocoa::appkit::{NSMainMenuWindowLevel, NSWindowCollectionBehavior},
panel_delegate, WebviewWindowExt,
};
use tauri_plugin_eco_window_state::save_window_state;

#[allow(non_upper_case_globals)]
const NSWindowStyleMaskNonActivatingPanel: i32 = 1 << 7;
Expand Down Expand Up @@ -48,6 +49,9 @@ pub fn platform(app: &mut App, main_window: WebviewWindow, _preference_window: W
// 当窗口失去键盘焦点时调用
"window_did_resign_key" => {
app_handle.emit(MACOS_PANEL_FOCUS, false).unwrap();

let app_handle_clone = app_handle.clone();
save_window_state(app_handle_clone);
}
_ => (),
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/window-state/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const COMMANDS: &[&str] = &["save_state", "restore_state"];
const COMMANDS: &[&str] = &["save_window_state", "restore_window_state"];

fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

[default]
description = "Default permissions for the plugin"
permissions = ["allow-save-state", "allow-restore-state"]
permissions = ["allow-save-window-state", "allow-restore-window-state"]
77 changes: 46 additions & 31 deletions src-tauri/src/plugins/window-state/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,66 @@ pub struct WindowState {
y: i32,
}

// 获取保存路径
fn get_save_path<R: Runtime>(app_handle: AppHandle<R>) -> Option<PathBuf> {
if let Ok(app_data_dir) = app_handle.path().app_data_dir() {
return Some(app_data_dir.join(".window-state.json"));
} else {
return None;
}
}

// 保存窗口状态
#[command]
pub fn save_state<R: Runtime>(app_handle: AppHandle<R>, save_path: PathBuf) {
let windows = app_handle.webview_windows();
pub fn save_window_state<R: Runtime>(app_handle: AppHandle<R>) {
if let Some(save_path) = get_save_path(app_handle.clone()) {
let windows = app_handle.webview_windows();

let mut data: HashMap<String, WindowState> = HashMap::new();
let mut data: HashMap<String, WindowState> = HashMap::new();

for (label, window) in windows {
if let (Ok(size), Ok(position)) = (window.inner_size(), window.outer_position()) {
let window_state = WindowState {
width: size.width,
height: size.height,
x: position.x,
y: position.y,
};
for (label, window) in windows {
if let (Ok(size), Ok(position)) = (window.inner_size(), window.outer_position()) {
let window_state = WindowState {
width: size.width,
height: size.height,
x: position.x,
y: position.y,
};

data.insert(label, window_state);
data.insert(label, window_state);
}
}
}

let file = File::create(save_path).unwrap();
serde_json::to_writer_pretty(file, &data).unwrap();
let file = File::create(save_path).unwrap();
serde_json::to_writer_pretty(file, &data).unwrap();
}
}

// 恢复窗口状态
#[command]
pub fn restore_state<R: Runtime>(window: Window<R>, save_path: PathBuf) {
let exists = save_path.exists();
pub fn restore_window_state<R: Runtime>(window: Window<R>) {
let app_handle = window.app_handle().clone();

if !exists {
return;
}
if let Some(save_path) = get_save_path(app_handle) {
let exists = save_path.exists();

if !exists {
return;
}

let file = File::open(save_path).unwrap();
let data: HashMap<String, WindowState> = serde_json::from_reader(file).unwrap();
let file = File::open(save_path).unwrap();
let data: HashMap<String, WindowState> = serde_json::from_reader(file).unwrap();

if let Some(window_state) = data.get(window.label()) {
let _ = window.set_size(PhysicalSize {
width: window_state.width,
height: window_state.height,
});
if let Some(window_state) = data.get(window.label()) {
let _ = window.set_size(PhysicalSize {
width: window_state.width,
height: window_state.height,
});

let _ = window.set_position(PhysicalPosition {
x: window_state.x,
y: window_state.y,
});
let _ = window.set_position(PhysicalPosition {
x: window_state.x,
y: window_state.y,
});
}
}
}
29 changes: 13 additions & 16 deletions src-tauri/src/plugins/window-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@ pub use commands::*;
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("eco-window-state")
.invoke_handler(generate_handler![
commands::save_state,
commands::restore_state
commands::save_window_state,
commands::restore_window_state
])
.on_window_ready(|window| {
let app_handle = window.app_handle().clone();

if let Ok(app_data_dir) = app_handle.path().app_data_dir() {
let save_path = app_data_dir.join(".window-state.json");
restore_window_state(window.clone());

restore_state(window.clone(), save_path.clone());

window.on_window_event(move |e| match e {
WindowEvent::Focused(focused) => {
if *focused {
return;
}

save_state(app_handle.clone(), save_path.clone());
window.on_window_event(move |e| match e {
// 普通的 tauri 窗口触发失去焦点事件
WindowEvent::Focused(focused) => {
if *focused {
return;
}
_ => {}
});
}

save_window_state(app_handle.clone());
}
_ => {}
});
})
.build()
}

0 comments on commit 44a39f0

Please sign in to comment.