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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ macOS 签名/公证为可选项:在仓库 secrets 中配置 `APPLE_CERTIFICATE

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

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

## 数据存储

项目以 `<项目名>.basproj` 保存在用户选定的目录,不要求目录为空;同名项目文件不会被覆盖。已有的 `project.basproj` 仍可打开。桌面端近期项目列表保存在 Tauri 应用配置目录的 `recent-projects.json`。Web 端在 localStorage 保存近期列表,并在 IndexedDB 中仅保存项目文件夹句柄;项目内容和素材仍保存在文件系统。重新打开时会检查文件夹权限;如原文件夹不可用,可通过“打开本地项目”重新选择。
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod app_update;
mod commands;
mod project_files;
mod recent_projects;
#[cfg(target_os = "linux")]
mod window_geometry;
mod window_preferences;

use tauri::Manager;
Expand Down
148 changes: 148 additions & 0 deletions src-tauri/src/window_geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use std::sync::{Arc, Mutex};
use tauri::{LogicalSize, Manager, PhysicalSize, WebviewWindow, WindowEvent};

// GTK 窗口未映射时还拿不到主题标题栏的实际尺寸,为系统装饰保留逻辑像素余量。
const DECORATION_MARGIN: f64 = 64.0;

fn available_inner_size(
work_area: PhysicalSize<u32>,
scale_factor: f64,
) -> Option<LogicalSize<f64>> {
if work_area.width == 0
|| work_area.height == 0
|| !scale_factor.is_finite()
|| scale_factor <= 0.0
{
return None;
}
let area = work_area.to_logical::<f64>(scale_factor);
Some(LogicalSize::new(
(area.width - DECORATION_MARGIN).max(1.0),
(area.height - DECORATION_MARGIN).max(1.0),
))
}

fn fit_size(size: LogicalSize<f64>, available: LogicalSize<f64>) -> LogicalSize<f64> {
LogicalSize::new(
size.width.min(available.width),
size.height.min(available.height),
)
}

fn update_minimum_size(
window: &WebviewWindow,
preferred: LogicalSize<f64>,
applied: &Mutex<Option<LogicalSize<f64>>>,
) -> tauri::Result<Option<LogicalSize<f64>>> {
let monitor = match window.current_monitor()? {
Some(monitor) => Some(monitor),
None => window.primary_monitor()?,
};
let Some(monitor) = monitor else {
return Ok(None);
};
let Some(available) = available_inner_size(monitor.work_area().size, monitor.scale_factor())
else {
return Ok(None);
};
let minimum = fit_size(preferred, available);
let mut applied = applied.lock().unwrap();
// 调整约束可能产生 resize 事件,只在工作区或缩放确实改变时通知 GTK。
if *applied != Some(minimum) {
window.set_min_size(Some(minimum))?;
*applied = Some(minimum);
}
Ok(Some(available))
}

pub fn initialize(window: &WebviewWindow) -> tauri::Result<()> {
let config = window
.app_handle()
.config()
.app
.windows
.iter()
.find(|config| config.label == window.label());
let preferred = LogicalSize::new(
config.and_then(|config| config.min_width).unwrap_or(1.0),
config.and_then(|config| config.min_height).unwrap_or(1.0),
);
let applied = Arc::new(Mutex::new(None));
if let Some(available) = update_minimum_size(window, preferred, &applied)? {
// 旧状态或默认尺寸也可能来自更大的屏幕;不主动改动最大化和全屏状态。
if !window.is_maximized()? && !window.is_fullscreen()? {
let size = window
.inner_size()?
.to_logical::<f64>(window.scale_factor()?);
let fitted = fit_size(size, available);
if fitted != size {
window.set_size(fitted)?;
window.center()?;
}
}
}

let target = window.clone();
window.on_window_event(move |event| {
if matches!(
event,
WindowEvent::Moved(_)
| WindowEvent::Resized(_)
| WindowEvent::ScaleFactorChanged { .. }
| WindowEvent::Focused(true)
) {
if let Err(error) = update_minimum_size(&target, preferred, &applied) {
eprintln!("更新 Linux 窗口尺寸约束失败: {error}");
}
}
});
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

const PREFERRED: LogicalSize<f64> = LogicalSize::new(1100.0, 680.0);

#[test]
fn minimum_fits_small_desktop_and_native_titlebar() {
let available = available_inner_size(PhysicalSize::new(1024, 704), 1.0).unwrap();
assert_eq!(
fit_size(PREFERRED, available),
LogicalSize::new(960.0, 640.0)
);
assert_eq!(
fit_size(LogicalSize::new(1440.0, 900.0), available),
available
);
}

#[test]
fn uses_logical_work_area_including_display_scaling_and_panels() {
let available = available_inner_size(PhysicalSize::new(1920, 1000), 2.0).unwrap();
assert_eq!(
fit_size(PREFERRED, available),
LogicalSize::new(896.0, 436.0)
);
}

#[test]
fn returning_to_large_monitor_restores_preferred_minimum() {
let available = available_inner_size(PhysicalSize::new(1920, 1040), 1.0).unwrap();
assert_eq!(fit_size(PREFERRED, available), PREFERRED);
assert_eq!(
fit_size(LogicalSize::new(1440.0, 900.0), available),
LogicalSize::new(1440.0, 900.0)
);
}

#[test]
fn ignores_unavailable_monitor_geometry() {
assert!(available_inner_size(PhysicalSize::new(0, 768), 1.0).is_none());
assert!(available_inner_size(PhysicalSize::new(1024, 0), 1.0).is_none());
for scale in [0.0, -1.0, f64::NAN, f64::INFINITY] {
assert!(available_inner_size(PhysicalSize::new(1024, 768), scale).is_none());
}
}
}
2 changes: 2 additions & 0 deletions src-tauri/src/window_preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub fn initialize(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>
#[cfg(not(target_os = "macos"))]
if let Some(window) = app.get_webview_window("main") {
window.set_decorations(!preferences.use_app_window_controls)?;
#[cfg(target_os = "linux")]
super::window_geometry::initialize(&window)?;
#[cfg(target_os = "windows")]
window.set_shadow(true)?;
}
Expand Down
Loading