дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
On Windows, the display of the last focused or moved project window is saved to last-display.json. A project window opened without a position of its own is centred on that display and sized to its work area while the display is still connected and unchanged; otherwise it opens on the primary display. Cascaded windows keep their place, and macOS and Linux are unchanged. Fixes #7031
136 рядки
5.4 KiB
Rust
136 рядки
5.4 KiB
Rust
//! The display the user last worked on, on Windows. The window-state plugin restores only the
|
|
//! maximized and full-screen flags there (`window_state_flags`), so without this every project
|
|
//! window that opens at launch, or after the last one closed, lands on the primary display.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// A display by its physical position and size in the virtual screen.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
struct Display {
|
|
x: i32,
|
|
y: i32,
|
|
width: u32,
|
|
height: u32,
|
|
}
|
|
|
|
/// The connected display that is the saved one: same position and size. Displays rearranged or
|
|
/// resized since then are not matched, so the window opens on the primary display as before.
|
|
fn find(connected: &[Display], saved: Display) -> Option<usize> {
|
|
connected.iter().position(|d| *d == saved)
|
|
}
|
|
|
|
/// The top-left corner, in the display's logical pixels, that centres a window of `size` in a
|
|
/// work area at `origin` of `area` (both logical).
|
|
fn centred(origin: (f64, f64), area: (f64, f64), size: (f64, f64)) -> (f64, f64) {
|
|
(origin.0 + ((area.0 - size.0) / 2.0).max(0.0), origin.1 + ((area.1 - size.1) / 2.0).max(0.0))
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
pub use native::{place, remember};
|
|
|
|
#[cfg(windows)]
|
|
mod native {
|
|
use super::{Display, centred, find};
|
|
use parking_lot::Mutex;
|
|
use std::path::PathBuf;
|
|
use tauri::{AppHandle, Manager, Monitor, Runtime, Window, utils::config::WindowConfig};
|
|
|
|
const FILE: &str = "last-display.json";
|
|
|
|
/// The saved display, read from disk once.
|
|
static SAVED: Mutex<Option<Option<Display>>> = Mutex::new(None);
|
|
|
|
fn display(monitor: &Monitor) -> Display {
|
|
let (position, size) = (monitor.position(), monitor.size());
|
|
Display { x: position.x, y: position.y, width: size.width, height: size.height }
|
|
}
|
|
|
|
fn file<R: Runtime>(app: &AppHandle<R>) -> Option<PathBuf> {
|
|
ide_host::data_dirs::config_dir(&app.config().identifier).ok().map(|dir| dir.join(FILE))
|
|
}
|
|
|
|
fn saved<R: Runtime>(app: &AppHandle<R>) -> Option<Display> {
|
|
*SAVED.lock().get_or_insert_with(|| {
|
|
let text = std::fs::read_to_string(file(app)?).ok()?;
|
|
serde_json::from_str(&text).ok()
|
|
})
|
|
}
|
|
|
|
/// Records the display a project window is on, when it is focused or moved.
|
|
pub fn remember<R: Runtime>(window: &Window<R>) {
|
|
if crate::windows::auxiliary_parent(window.label()).is_some() {
|
|
return;
|
|
}
|
|
let Ok(Some(monitor)) = window.current_monitor() else { return };
|
|
let now = display(&monitor);
|
|
let app = window.app_handle();
|
|
if saved(app) == Some(now) {
|
|
return;
|
|
}
|
|
*SAVED.lock() = Some(Some(now));
|
|
let Some(path) = file(app) else { return };
|
|
if let Ok(text) = serde_json::to_string(&now)
|
|
&& let Err(e) = std::fs::write(&path, text)
|
|
{
|
|
tracing::debug!("cannot save {}: {e}", path.display());
|
|
}
|
|
}
|
|
|
|
/// Where a new project window opens: the saved display when it is still connected, else the
|
|
/// primary one. A window without a position of its own (not cascaded from another window) is
|
|
/// centred on it. Returns the display, whose work area sizes the window.
|
|
pub fn place<R: Runtime>(app: &AppHandle<R>, config: &mut WindowConfig) -> Option<Monitor> {
|
|
let primary = app.primary_monitor().ok().flatten();
|
|
if config.x.is_some() {
|
|
return primary;
|
|
}
|
|
let connected = app.available_monitors().unwrap_or_default();
|
|
let displays: Vec<Display> = connected.iter().map(display).collect();
|
|
let Some(monitor) = saved(app).and_then(|s| find(&displays, s)).map(|i| connected[i].clone()) else {
|
|
return primary;
|
|
};
|
|
if primary.as_ref().is_some_and(|p| display(p) == display(&monitor)) {
|
|
return primary;
|
|
}
|
|
let scale = monitor.scale_factor();
|
|
let area = monitor.work_area();
|
|
let origin = area.position.to_logical::<f64>(scale);
|
|
let size = area.size.to_logical::<f64>(scale);
|
|
let window = (config.width.min(size.width), config.height.min(size.height));
|
|
let (x, y) = centred((origin.x, origin.y), (size.width, size.height), window);
|
|
// tao places a logical position on the display it falls on, using that display's scale.
|
|
config.x = Some(x);
|
|
config.y = Some(y);
|
|
config.center = false;
|
|
Some(monitor)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const PRIMARY: Display = Display { x: 0, y: 0, width: 2560, height: 1440 };
|
|
const SECOND: Display = Display { x: 2560, y: 0, width: 1920, height: 1080 };
|
|
|
|
#[test]
|
|
fn finds_the_saved_display_only_while_it_is_connected_unchanged() {
|
|
assert_eq!(find(&[PRIMARY, SECOND], SECOND), Some(1));
|
|
assert_eq!(find(&[PRIMARY], SECOND), None);
|
|
// Rearranged to the left of the primary display: not the saved one any more.
|
|
assert_eq!(find(&[PRIMARY, Display { x: -1920, ..SECOND }], SECOND), None);
|
|
}
|
|
|
|
#[test]
|
|
fn centres_in_the_work_area_and_keeps_a_larger_window_at_its_corner() {
|
|
assert_eq!(centred((2560.0, 0.0), (1920.0, 1040.0), (1440.0, 900.0)), (2800.0, 70.0));
|
|
assert_eq!(centred((2560.0, 0.0), (1280.0, 680.0), (1440.0, 900.0)), (2560.0, 0.0));
|
|
}
|
|
|
|
#[test]
|
|
fn the_saved_display_round_trips_through_json() {
|
|
let text = serde_json::to_string(&SECOND).unwrap();
|
|
assert_eq!(serde_json::from_str::<Display>(&text).unwrap(), SECOND);
|
|
}
|
|
}
|