Skip to content

Visual Studio Code, rebuilt in Rust

Rust
13.6 KB
Raw Blame
//! windowsStateHandler.ts: the windows of the last session in `storage.json` (`windowsState`), the ones a start
//! without paths opens again (`window.restoreWindows`), and their size and place.

use std::path::{Path, PathBuf};

use gpui::{App, Bounds, DisplayId, Pixels, Window, WindowBounds, point, px, size};
use serde_json::{Value, json};
use storage::state::FileStorage;

use crate::uri::Uri;

const WINDOWS_STATE_KEY: &str = "windowsState";
const EXACT_RESTORE_KEY: &str = "windowsExactRestore";

/// `WindowMode`.
pub const MAXIMIZED: u64 = 0;
pub const NORMAL: u64 = 1;
pub const FULLSCREEN: u64 = 3;

/// `IWindowState` of one window: its workspace file, folder or empty window backup folder, and `uiState`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct WindowState {
    pub workspace: Option<PathBuf>,
    pub folder: Option<PathBuf>,
    pub backup_path: Option<PathBuf>,
    pub ui: Option<UiState>,
}

/// `IWindowUIState`: the mode, and the bounds in points from the top left of the display.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UiState {
    pub mode: u64,
    pub display: Option<u64>,
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
    /// Only recorded and restored when the app restores the windows exactly.
    pub minimized: bool,
}

/// `IExactRestoreState`: the next start restores the windows exactly as they were, minimized ones minimized, after
/// the app restarted while the user was away.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ExactRestore {
    /// macOS: hide the app again after showing its windows.
    pub hide_application: bool,
}

/// windowsStateHandler.ts `nextExactRestore`, written as the app quits without a veto (`setNextExactRestore`).
#[derive(Default)]
pub struct NextExactRestore(pub Option<ExactRestore>);

impl gpui::Global for NextExactRestore {}

/// windowsStateHandler.ts `lastClosedState`: the last window closed, the last active one of a quit without windows;
/// forgotten once another window opens.
pub struct LastClosedWindow(pub Option<WindowState>);

impl gpui::Global for LastClosedWindow {}

/// `doGetNewWindowState` from the stored state: a window last seen on the same workspace, folder or empty window
/// backup takes its place, and the first window the last active one's.
pub fn stored_ui_state(user_data: &Path, window: &WindowState, first_window: bool, cx: &App) -> Option<UiState> {
    let (last_active, opened) = load(user_data);
    let same = |other: &&WindowState| match (&window.workspace, &window.folder) {
        (Some(workspace), _) => other.workspace.as_ref() == Some(workspace),
        (None, Some(folder)) => other.folder.as_ref() == Some(folder),
        (None, None) => window.backup_path.is_some() && other.backup_path == window.backup_path,
    };
    if let Some(ui) = opened.iter().find(same).and_then(|other| other.ui) {
        return Some(ui);
    }
    let last_closed = cx.try_global::<LastClosedWindow>().and_then(|closed| closed.0.clone());
    last_closed.or(last_active).filter(|_| first_window).and_then(|last| last.ui)
}

/// `ensureNoOverlap`: 30 points right and down until no open window shares the left or top edge.
pub fn cascade(mut bounds: Bounds<Pixels>, open: &[Bounds<Pixels>]) -> Bounds<Pixels> {
    while open.iter().any(|other| other.origin.x == bounds.origin.x || other.origin.y == bounds.origin.y) {
        bounds.origin = bounds.origin + point(px(30.), px(30.));
    }
    bounds
}

/// `restoreWindowsState`: the last active window and the opened windows.
pub fn load(user_data: &Path) -> (Option<WindowState>, Vec<WindowState>) {
    let storage = FileStorage::open(&storage_file(user_data));
    let Some(state) = storage.get_item(WINDOWS_STATE_KEY) else { return (None, Vec::new()) };
    let opened = state["openedWindows"].as_array().into_iter().flatten().map(parse).collect();
    (state.get("lastActiveWindow").filter(|window| window.is_object()).map(parse), opened)
}

/// `getWindowsStateStoreData` written as the app quits, with `nextExactRestore` (`onWillShutdown`).
pub fn save(user_data: &Path, last_active: Option<&WindowState>, opened: &[WindowState], exact: Option<ExactRestore>) {
    let mut storage = FileStorage::open(&storage_file(user_data));
    let mut state = json!({ "openedWindows": opened.iter().map(serialize).collect::<Vec<_>>() });
    if let Some(last_active) = last_active {
        state["lastActiveWindow"] = serialize(last_active);
    }
    storage.set_item(WINDOWS_STATE_KEY, Some(state));
    if let Some(exact) = exact {
        storage.set_item(EXACT_RESTORE_KEY, Some(json!({ "hideApplication": exact.hide_application })));
    }
}

/// windowsMainService.ts `restoreExactlyOnNextStartup`: the app hidden now is hidden again then.
pub fn restore_exactly_on_next_startup(restore: bool, cx: &mut App) {
    let next = restore.then(|| ExactRestore { hide_application: app_hidden() });
    cx.set_global(NextExactRestore(next));
}

/// `app.isHidden()`; tests run off the main thread, without an NSApplication.
fn app_hidden() -> bool {
    #[cfg(target_os = "macos")]
    return objc2::MainThreadMarker::new().is_some_and(|mtm| objc2_app_kit::NSApplication::sharedApplication(mtm).isHidden());
    #[cfg(not(target_os = "macos"))]
    false
}

/// `takeExactRestore`: the exact restore the last quit asked for, which applies to this start only.
pub fn take_exact_restore(user_data: &Path) -> Option<ExactRestore> {
    let mut storage = FileStorage::open(&storage_file(user_data));
    let hide_application = storage.get_item(EXACT_RESTORE_KEY)?["hideApplication"].as_bool();
    storage.set_item(EXACT_RESTORE_KEY, None);
    eprintln!("[WindowsStateHandler] restoring windows exactly");
    Some(ExactRestore { hide_application: hide_application? })
}

/// `doGetPathsFromLastSession`: `none` nothing, `one` the last active window, `folders` the opened windows and the
/// last active one with a folder or workspace, `all` and `preserve` every one; each window once.
pub fn to_restore(setting: &str, last_active: Option<WindowState>, opened: Vec<WindowState>) -> Vec<WindowState> {
    if setting == "none" {
        return Vec::new();
    }
    let mut windows = match setting {
        "one" => Vec::new(),
        _ => opened,
    };
    windows.extend(last_active);
    let mut restore: Vec<WindowState> = Vec::new();
    for window in windows {
        let has_workspace = window.workspace.as_ref().is_some_and(|file| file.is_file()) || window.folder.as_ref().is_some_and(|folder| folder.is_dir());
        let empty_with_backups = window.backup_path.is_some() && setting != "folders";
        if !has_workspace && !empty_with_backups {
            continue;
        }
        let same = |other: &WindowState| (other.workspace.is_some() && other.workspace == window.workspace) || (other.folder.is_some() && other.folder == window.folder) || (other.backup_path.is_some() && other.backup_path == window.backup_path);
        if let Some(index) = restore.iter().position(same) {
            restore.remove(index);
        }
        restore.push(window);
    }
    restore
}

/// `serializeWindowState` of an open window.
pub fn ui_state(window: &Window, cx: &App) -> UiState {
    let (mode, bounds) = match window.window_bounds() {
        WindowBounds::Fullscreen(bounds) => (FULLSCREEN, bounds),
        WindowBounds::Maximized(bounds) => (MAXIMIZED, bounds),
        WindowBounds::Windowed(bounds) => (if window.is_maximized() { MAXIMIZED } else { NORMAL }, bounds),
    };
    let display = window.display(cx).map(|display| u64::from(display.id()));
    let points = |pixels: Pixels| f64::from(f32::from(pixels));
    UiState { mode, display, x: points(bounds.origin.x), y: points(bounds.origin.y), width: points(bounds.size.width), height: points(bounds.size.height), minimized: false }
}

/// `isMinimized`; none on GPUI's test platform.
pub fn is_minimized(window: &Window) -> bool {
    #[cfg(target_os = "macos")]
    return crate::dialog::app_kit_window(window).is_some_and(|window| window.isMiniaturized());
    // shortcut: GPUI has no minimized state on Linux, upgrade when gpui_linux exposes one.
    #[cfg(not(target_os = "macos"))]
    {
        let _ = window;
        false
    }
}

/// `validateWindowState`: the display and bounds to open a restored window with, none when its display is gone or
/// the bounds lie off it.
pub fn window_bounds(ui: UiState, cx: &App) -> Option<(DisplayId, WindowBounds)> {
    let display = cx.find_display(DisplayId::new(ui.display?))?;
    let bounds = Bounds::new(point(px(ui.x as f32), px(ui.y as f32)), size(px(ui.width as f32), px(ui.height as f32)));
    if !Bounds::new(point(px(0.), px(0.)), display.bounds().size).intersects(&bounds) {
        return None;
    }
    let window_bounds = match ui.mode {
        MAXIMIZED => WindowBounds::Maximized(bounds),
        FULLSCREEN => WindowBounds::Fullscreen(bounds),
        _ => WindowBounds::Windowed(bounds),
    };
    Some((display.id(), window_bounds))
}

pub(crate) fn storage_file(user_data: &Path) -> PathBuf {
    user_data.join("User/globalStorage/storage.json")
}

fn parse(window: &Value) -> WindowState {
    let path = |uri: &Value| uri.as_str().and_then(|uri| Uri::parse(uri).ok()).filter(|uri| uri.scheme == "file").map(|uri| PathBuf::from(uri.fs_path()));
    let ui = &window["uiState"];
    let number = |key: &str| ui[key].as_f64();
    let ui = match (number("x"), number("y"), number("width"), number("height")) {
        (Some(x), Some(y), Some(width), Some(height)) => Some(UiState { mode: ui["mode"].as_u64().unwrap_or(NORMAL), display: ui["display"].as_u64(), x, y, width, height, minimized: ui["minimized"] == true }),
        _ => None,
    };
    WindowState { workspace: path(&window["workspaceIdentifier"]["configURIPath"]), folder: path(&window["folder"]), backup_path: window["backupPath"].as_str().map(PathBuf::from), ui }
}

fn serialize(window: &WindowState) -> Value {
    let uri = |path: &PathBuf| json!(Uri::file(&path.to_string_lossy()).to_string(false));
    let mut state = json!({});
    if let Some(workspace) = &window.workspace {
        let id = window.backup_path.as_ref().and_then(|path| path.file_name()).map(|id| id.to_string_lossy().into_owned());
        state["workspaceIdentifier"] = json!({ "id": id, "configURIPath": uri(workspace) });
    }
    if let Some(folder) = &window.folder {
        state["folder"] = uri(folder);
    }
    if let Some(backup_path) = &window.backup_path {
        state["backupPath"] = json!(backup_path.to_string_lossy());
    }
    if let Some(ui) = window.ui {
        state["uiState"] = json!({ "mode": ui.mode, "display": ui.display, "x": ui.x, "y": ui.y, "width": ui.width, "height": ui.height });
        if ui.minimized {
            state["uiState"]["minimized"] = json!(true);
        }
    }
    state
}

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

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("vsrustium-windows-state-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("folder")).unwrap();
        std::fs::create_dir_all(dir.join("User/globalStorage")).unwrap();
        // The `fsPath` of the stored URI: Windows reads the drive letter back in lower case.
        PathBuf::from(Uri::file(&dir.to_string_lossy()).fs_path())
    }

    #[test]
    fn the_state_round_trips_through_storage_json() {
        let dir = scratch("save");
        let ui = Some(UiState { mode: NORMAL, display: Some(1), x: 10., y: 20., width: 1024., height: 768., minimized: true });
        let folder = WindowState { folder: Some(dir.join("folder")), backup_path: Some(dir.join("Backups/abc")), ui, ..WindowState::default() };
        let empty = WindowState { backup_path: Some(dir.join("Backups/123")), ..WindowState::default() };
        save(&dir, Some(&folder), &[empty.clone(), folder.clone()], None);
        assert_eq!(load(&dir), (Some(folder), vec![empty, WindowState { folder: Some(dir.join("folder")), backup_path: Some(dir.join("Backups/abc")), ui, ..WindowState::default() }]));
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn an_exact_restore_applies_to_the_next_start_only() {
        let dir = scratch("exact");
        save(&dir, None, &[], None);
        assert_eq!(take_exact_restore(&dir), None);
        save(&dir, None, &[], Some(ExactRestore { hide_application: true }));
        assert_eq!(take_exact_restore(&dir), Some(ExactRestore { hide_application: true }));
        assert_eq!(take_exact_restore(&dir), None);
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn a_new_window_cascades_off_the_open_ones() {
        let at = |x: f32, y: f32| Bounds::new(point(px(x), px(y)), size(px(1024.), px(768.)));
        assert_eq!(cascade(at(100., 50.), &[]), at(100., 50.));
        assert_eq!(cascade(at(100., 50.), &[at(100., 50.), at(130., 300.)]), at(160., 110.));
    }

    #[test]
    fn restore_follows_window_restore_windows() {
        let dir = scratch("restore");
        let folder = WindowState { folder: Some(dir.join("folder")), ..WindowState::default() };
        let gone = WindowState { folder: Some(dir.join("gone")), ..WindowState::default() };
        let empty = WindowState { backup_path: Some(dir.join("Backups/1")), ..WindowState::default() };
        let opened = vec![folder.clone(), gone, empty.clone()];
        assert_eq!(to_restore("all", Some(folder.clone()), opened.clone()), vec![empty.clone(), folder.clone()], "each window once, the last active last");
        assert_eq!(to_restore("one", Some(empty.clone()), opened.clone()), vec![empty]);
        assert_eq!(to_restore("folders", None, opened.clone()), vec![folder]);
        assert!(to_restore("none", None, opened).is_empty());
        std::fs::remove_dir_all(&dir).unwrap();
    }
}