Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ macOS 签名/公证为可选项:在仓库 secrets 中配置 `APPLE_CERTIFICATE

## 窗口控制

窗口控制按平台适配:macOS 使用原生红绿灯和叠加式标题栏,Windows 使用右上角应用内按钮,Linux 默认使用桌面环境提供的原生标题栏和按钮。Linux 用户可在设置中切换“使用应用内窗口按钮”,立即生效;偏好保存在应用配置目录的 `window-preferences.json`,下次启动会在窗口显示前恢复。按钮位置和外观由 Linux 桌面环境决定,程序不识别或模拟红绿灯主题。Web 版不显示桌面窗口控制选项。
窗口控制按平台适配:macOS 使用原生红绿灯和叠加式标题栏,Windows 使用右上角应用内按钮,Linux 默认开启“使用应用内窗口按钮”,用户可在设置中关闭以改用桌面环境提供的原生标题栏和按钮,立即生效;偏好保存在应用配置目录的 `window-preferences.json`,下次启动会在窗口显示前恢复。按钮位置和外观由 Linux 桌面环境决定,程序不识别或模拟红绿灯主题。Web 版不显示桌面窗口控制选项。

Linux 会根据当前显示器的可用工作区与缩放比例调整窗口最小尺寸,并为系统标题栏预留空间,避免小屏幕或高缩放下固定的最小尺寸阻止系统最大化。移动到其他显示器或改变分辨率后会重新计算约束;启动时过大的普通窗口也会缩小到可用区域。

Expand Down
34 changes: 23 additions & 11 deletions src-tauri/src/window_preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::Manager;

#[derive(Default, Deserialize, Serialize)]
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
struct SavedPreferences {
#[serde(default)]
#[serde(default = "default_use_app_window_controls")]
use_app_window_controls: bool,
}

// Linux 未保存设置时默认使用应用内窗口按钮。
fn default_use_app_window_controls() -> bool {
true
}

impl Default for SavedPreferences {
fn default() -> Self {
Self {
use_app_window_controls: default_use_app_window_controls(),
}
}
}

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WindowPreferences {
Expand Down Expand Up @@ -80,7 +93,7 @@ pub fn initialize(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>
let path = app.path().app_config_dir()?.join("window-preferences.json");
let saved = if cfg!(target_os = "linux") {
read_preferences(&path).unwrap_or_else(|error| {
eprintln!("读取窗口设置失败,使用系统窗口装饰: {error}");
eprintln!("读取窗口设置失败,使用应用内窗口按钮: {error}");
SavedPreferences::default()
})
} else {
Expand Down Expand Up @@ -147,21 +160,23 @@ mod tests {

#[test]
fn platform_defaults_and_linux_override() {
for (platform, expected) in [("windows", true), ("linux", false), ("macos", false)] {
for (platform, expected) in [("windows", true), ("linux", true), ("macos", false)] {
assert_eq!(
preferences_for(platform, SavedPreferences::default()).use_app_window_controls,
expected
);
}
assert!(
preferences_for(
!preferences_for(
"linux",
SavedPreferences {
use_app_window_controls: true
use_app_window_controls: false
}
)
.use_app_window_controls
);
let legacy: SavedPreferences = serde_json::from_str("{}").unwrap();
assert!(legacy.use_app_window_controls);
assert!(
!preferences_for(
"macos",
Expand All @@ -183,14 +198,11 @@ mod tests {
std::env::temp_dir().join(format!("bas-window-prefs-{}-{nonce}", std::process::id()));
fs::create_dir(&directory).unwrap();
let path = directory.join("window-preferences.json");
assert!(read_preferences(&path).unwrap().use_app_window_controls);
write_preferences(&path, false).unwrap();
assert!(!read_preferences(&path).unwrap().use_app_window_controls);
write_preferences(&path, true).unwrap();
assert!(read_preferences(&path).unwrap().use_app_window_controls);
#[cfg(target_os = "linux")]
{
write_preferences(&path, false).unwrap();
assert!(!read_preferences(&path).unwrap().use_app_window_controls);
}
fs::write(&path, "invalid").unwrap();
assert!(read_preferences(&path).is_err());
assert_eq!(fs::read_to_string(&path).unwrap(), "invalid");
Expand Down
Loading