Файли
openidea/src-tauri/src/windows.rs
T
f49302d365 fix(windows): stop menu items doing nothing in diff and other auxiliary windows (#8112)
Auxiliary windows (diff, comparison, editor, Sync with Deployed) boot a fixed set of features, so application actions they never register looked enabled in the menu but did nothing, some key equivalents were dropped because the page appeared to decline them, and Sync with Deployed had no menu bridge. Application actions now run in the project window (raised when it shows their result), Toggle Full Screen stays in the focused window, and menu enablement is cached per window so parent-routed items follow the destination window's state.

Supersedes #7280.

Co-authored-by: albince-mi <[email protected]>
Co-authored-by: arookieofc-ai <[email protected]>
2026-12-05 02:38:47 +08:00

4226 рядки
177 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! Project windows: one project per window, labelled `main` or `project-<n>`.
//!
//! A new window is built from the `main` window config (overlay title bar,
//! traffic lights, min size, background colour). What it should open is kept
//! here as a pending [`Launch`] that its frontend takes on startup
//! (`window_take_launch`), and re-taken whenever [`LAUNCH_EVENT`] fires (a
//! second app instance asking an already-loaded window to open a path).
//!
//! Auxiliary windows ([`AuxKind`]: Settings, Compare with Local, Diff, Sync with Deployed, New Project, Get from VCS, Remote Development) belong to a project window and share its project (see
//! [`AppState::project`]). Their labels name the kind and the parent
//! (`settings-main`, `comparison-project-2-3`, `diff-main-0`); their [`Launch`] says what to show.
mod cli;
#[cfg(windows)]
mod single_instance;
#[cfg(windows)]
pub use single_instance::allow_external_launches;
use crate::remote_dev::RemoteTarget;
use crate::state::AppState;
use crate::theme_frame::builtin_frame;
use ide_core::settings::AppearanceSettings;
use ide_core::{IdeError, IdeResult};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use tauri::utils::config::{Color, WindowConfig};
use tauri::{AppHandle, Emitter, Manager, Theme, WebviewWindow, WebviewWindowBuilder};
/// Tells a window that a pending launch is waiting for it.
pub const LAUNCH_EVENT: &str = "openidea://launch";
const MAIN: &str = "main";
const PREFIX: &str = "project-";
const CASCADE: f64 = 28.0;
const SETTINGS_SIZE: (f64, f64) = (980.0, 720.0);
const SETTINGS_MIN_SIZE: (f64, f64) = (900.0, 700.0);
const COMPARISON_SIZE: (f64, f64) = (1100.0, 720.0);
const COMPARISON_MIN_SIZE: (f64, f64) = (640.0, 360.0);
const DIFF_SIZE: (f64, f64) = (1100.0, 720.0);
const DIFF_MIN_SIZE: (f64, f64) = (640.0, 360.0);
const EDITOR_SIZE: (f64, f64) = (1100.0, 720.0);
const EDITOR_MIN_SIZE: (f64, f64) = (640.0, 360.0);
const DEPLOYMENT_SIZE: (f64, f64) = (1400.0, 900.0);
const DEPLOYMENT_MIN_SIZE: (f64, f64) = (800.0, 500.0);
const NEW_PROJECT_SIZE: (f64, f64) = (860.0, 620.0);
const NEW_PROJECT_MIN_SIZE: (f64, f64) = (720.0, 480.0);
const CLONE_SIZE: (f64, f64) = (920.0, 620.0);
const CLONE_MIN_SIZE: (f64, f64) = (640.0, 420.0);
const CONNECT_SIZE: (f64, f64) = (600.0, 460.0);
const CONNECT_MIN_SIZE: (f64, f64) = (520.0, 380.0);
/// What a freshly opened window should do once its frontend is up.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct Launch {
/// Project folder to open.
pub project: Option<ide_core::IpcPath>,
/// Files to open in the editor: in `project` once it is open, else in this
/// window's project or (without one) in LightEdit.
pub files: Vec<ide_core::IpcPath>,
/// Remote project to open (Remote Development).
pub remote: Option<RemoteLaunch>,
/// Settings windows: the page to show.
pub settings_page: Option<String>,
/// Settings windows: edit the global layer only ("Settings for New Projects").
pub new_projects: bool,
/// Where to place the caret in some of `files` (`openidea --line 12 --column 5 <file>`).
#[serde(default)]
pub positions: Vec<FilePosition>,
/// Comparison windows: the revisions to compare (boxed: most launches have none).
#[serde(default)]
pub comparison: Option<Box<ComparisonLaunch>>,
/// Diff windows: the diff to show (boxed: most launches have none).
#[serde(default)]
pub diff_window: Option<Box<DiffLaunch>>,
/// `openidea diff a.txt b.txt`: the two files to open in the diff viewer (boxed: most launches have none).
#[serde(default)]
pub diff: Option<Box<DiffFilesLaunch>>,
/// Files requested after the pending diff take focus after it opens.
#[serde(default)]
pub diff_before_files: bool,
/// Deployment windows: the server and local selection to compare.
#[serde(default)]
pub deployment: Option<DeploymentLaunch>,
/// Editor windows: the file to show (Open in New Window → lightweight aux editor).
#[serde(default)]
pub editor: Option<Box<EditorLaunch>>,
/// Get from VCS windows: the repository URL to fill in.
#[serde(default)]
pub clone_url: Option<String>,
}
/// What a lightweight editor window shows: a file, with optional caret / selection (0-based), or
/// the tab of another editor kind.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct EditorLaunch {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<ide_core::IpcPath>,
/// The tab's editor input, as JSON.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub input: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub column: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end_line: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end_column: Option<u32>,
}
/// Local files to compare with a deployment server (Sync with Deployed).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct DeploymentLaunch {
/// Deployment server id.
pub server: String,
pub paths: Vec<ide_core::IpcPath>,
}
/// A Git revision and how the comparison window names it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct ComparisonRevision {
pub rev: String,
pub label: String,
}
/// Limits a comparison to some paths (Compare with Branch on a selection).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct ComparisonScope {
pub paths: Vec<ide_core::IpcPath>,
pub repositories: Option<Vec<ide_core::IpcPath>>,
}
/// Files that differ between `from` and `to` (the working tree when `None`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct ComparisonLaunch {
pub from: ComparisonRevision,
pub to: Option<ComparisonRevision>,
pub scope: Option<ComparisonScope>,
pub repo: Option<ide_core::IpcPath>,
}
/// One side of a diff window's comparison (the UI's `DiffSide`, `diffInput.ts`).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum DiffSide {
/// File content at a Git revision (`HEAD`, `:0` = index, a commit id).
Rev {
rev: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
label: Option<String>,
},
/// The working file (unsaved editor buffer included).
Worktree {
#[serde(default, skip_serializing_if = "Option::is_none")]
label: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
label_detail: Option<String>,
},
/// Another file (Compare With…, Compare Files).
File {
path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
label: Option<String>,
},
/// Literal text, such as clipboard contents or an editor selection.
Text {
text: String,
label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
label_detail: Option<String>,
},
/// A Local History revision.
History { id: String, label: String },
}
/// The diff a diff window shows (IDEA's "Show Diff in New Window"), from the UI's `DiffData`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct DiffLaunch {
/// Absolute path of the file (the right side's path).
pub path: String,
pub left: DiffSide,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub middle: Option<DiffSide>,
pub right: DiffSide,
/// 0-based line to reveal.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line_side: Option<DiffLineSide>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub col: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub chain: Option<Vec<DiffLaunch>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum DiffLineSide {
Left,
Middle,
Right,
}
/// The two files of a command-line diff (`openidea diff a.txt b.txt`), both sides editable.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
pub struct DiffFilesLaunch {
pub left: ide_core::IpcPath,
pub right: ide_core::IpcPath,
}
/// A caret position for a file opened from the command line: 0-based line and column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct FilePosition {
pub path: ide_core::IpcPath,
pub line: u32,
pub column: u32,
}
impl Launch {
fn add_positions(&mut self, positions: &[FilePosition]) {
for position in positions.iter().filter(|p| self.files.contains(&p.path)) {
self.positions.retain(|p| p.path != position.path);
self.positions.push(position.clone());
}
}
}
/// A project folder on an SSH host, in a WSL distribution or in a dev container.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct RemoteLaunch {
pub target: RemoteTarget,
pub path: String,
}
/// The remote launch for a folder of the `\\wsl.localhost\<distro>` share, if it is one.
pub fn wsl_launch(path: &Path) -> Option<RemoteLaunch> {
let w = ide_core::wsl::parse(path)?;
Some(RemoteLaunch { target: RemoteTarget::Wsl { distro: w.distro }, path: w.path })
}
/// A saved project path that is a remote location: `ssh://`, `wsl://`, `devcontainer://`,
/// or a `\\wsl.localhost` share remembered before those locations existed.
pub(crate) fn saved_remote(path: &Path) -> Option<RemoteLaunch> {
if let Some(text) = path.to_str()
&& let Some((target, root)) = crate::remote_dev::parse_remote_location(text)
{
return Some(RemoteLaunch { target, path: root });
}
wsl_launch(path)
}
/// What reopening `path` should launch. A remote location is recognised before any stat,
/// so a WSL share that is down still comes back as a remote project.
pub(crate) fn saved_project_launch(path: &Path) -> Option<Launch> {
if let Some(remote) = saved_remote(path) {
return Some(Launch { remote: Some(remote), ..Default::default() });
}
path.is_dir().then(|| Launch { project: Some(path.to_path_buf().into()), ..Default::default() })
}
/// The same project, including a WSL share and the `wsl://` location of that folder.
pub(crate) fn same_saved_project(a: &Path, b: &Path) -> bool {
if a == b {
return true;
}
match (saved_remote(a), saved_remote(b)) {
(Some(left), Some(right)) => {
left.target == right.target && left.path.trim_end_matches('/') == right.path.trim_end_matches('/')
}
_ => false,
}
}
/// The project `label` has open: a remote location (`wsl://` and the rest), else its local root.
pub(crate) fn window_project_path(app: &AppHandle, label: &str) -> Option<PathBuf> {
let state = app.try_state::<AppState>()?;
if let Some(c) = state.remote_dev.get(label) {
return Some(PathBuf::from(crate::remote_dev::remote_location(&c.target, &c.root)));
}
state.project(label).ok().filter(|p| !p.light).map(|p| p.root.clone())
}
/// Reopens a project saved at quit. Remote locations become a remote launch; a missing
/// local folder is skipped. An already connected window is focused instead of duplicated.
pub(crate) fn reopen_saved_window(app: &AppHandle, path: &Path) -> IdeResult<Option<WebviewWindow>> {
let Some(launch) = saved_project_launch(path) else {
return Ok(None);
};
if let Some(remote) = launch.remote.clone() {
let open = app.try_state::<AppState>().and_then(|s| s.remote_dev.window_of(&remote.target, &remote.path));
if let Some(w) = open.and_then(|label| app.get_webview_window(&label)) {
return Ok(Some(w));
}
let title = remote
.path
.trim_end_matches('/')
.rsplit('/')
.next()
.filter(|n| !n.is_empty() && *n != "~")
.map(str::to_owned);
return Ok(Some(create_window_for(app, title, launch, true, None)?));
}
if let Some(w) = find_project_window(app, path) {
return Ok(Some(w));
}
Ok(Some(create_restored_window(app, path.to_path_buf())?))
}
#[derive(Default)]
pub struct WindowsState {
pub groups: Mutex<ide_workspace::window_groups::WindowGroups>,
pub groups_restored: AtomicBool,
/// Serialises writes of the saved session. Holds the previous run's session once read: it is
/// read before this run first overwrites the file, so restoring still sees it.
pub groups_persist: tokio::sync::Mutex<Option<Vec<ide_workspace::window_groups::SavedWindows>>>,
launches: Mutex<HashMap<String, Launch>>,
/// A taken launch still owns its window until the frontend finishes opening it.
opening: Mutex<HashMap<String, Launch>>,
cli: Mutex<cli::Queue>,
/// Most recently focused window label (menu events go there).
last_focused: Mutex<Option<String>>,
/// Set by Quit while windows close in parallel.
quitting: AtomicBool,
/// Keep the macOS app alive in the Dock after the last window closes (`closeWithoutWelcome`
/// or active local agent automations/runs when `keepAliveInBackground` is enabled).
#[cfg(target_os = "macos")]
pub keep_alive: AtomicBool,
/// Coordinates parallel window/tab-group closes so clean tabs hide together in one frame
/// and interactive prompts still run one at a time.
close: Mutex<CloseState>,
pub close_notify: Arc<tokio::sync::Notify>,
/// Project tabs closing in parallel (Close Other Tabs and friends).
pub tab_closes: Mutex<Option<crate::window_groups::TabCloses>>,
/// Relaunch once the quit completes (after installing an update).
restart: AtomicBool,
/// Numbers windows of kinds that can open several times per project window.
aux_serial: AtomicU64,
/// Auxiliary windows being built: commands run in parallel, so a burst of the same request
/// (Settings pressed repeatedly) would otherwise pass the already-open check together.
creating: Mutex<HashSet<String>>,
/// Logical sizes remembered per [`AuxKind`] on macOS (`auxiliary-windows.json`), avoiding
/// `tauri-plugin-window-state`'s physical-pixel scale-factor drift across mixed-DPI displays.
#[cfg(target_os = "macos")]
aux_geometry: Mutex<Option<HashMap<String, AuxGeometry>>>,
}
#[cfg(target_os = "macos")]
const AUX_STATE_FILE: &str = "auxiliary-windows.json";
/// How long an auxiliary window waits for the main thread to fit it over its parent.
#[cfg(target_os = "macos")]
const FIT_ON_MAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
#[cfg(any(target_os = "macos", test))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub(crate) struct AuxGeometry {
pub width: f64,
pub height: f64,
#[serde(default)]
pub maximized: bool,
}
#[derive(Default)]
struct CloseState {
/// Windows asked to close whose guards have not finished. AppKit closes a whole tab group at
/// once, so its members ask together; Quit and Close Other Tabs also close members in parallel.
pending: Vec<String>,
/// Members that passed their close guard and are waiting at the batch barrier before destroying.
ready: HashSet<String>,
/// Members whose batch was cancelled while they waited; they stay open.
aborted: HashSet<String>,
/// Windows already hidden as part of their closing cohort so hiding only runs once per batch.
hidden: HashSet<String>,
/// Window currently showing an interactive close dialog (at most one across a batch).
prompt_holder: Option<String>,
}
impl WindowsState {
pub fn close_requested(&self, label: &str) {
let mut close = self.close.lock();
close.aborted.remove(label);
close.ready.remove(label);
close.hidden.remove(label);
if !close.pending.iter().any(|l| l == label) {
close.pending.push(label.to_owned());
}
}
pub fn close_settled(&self, label: &str) {
{
let mut close = self.close.lock();
close.pending.retain(|l| l != label);
close.ready.remove(label);
close.aborted.remove(label);
close.hidden.remove(label);
if close.prompt_holder.as_deref() == Some(label) {
close.prompt_holder = None;
}
}
self.close_notify.notify_waiters();
}
pub fn pending_closes(&self) -> Vec<String> {
self.close.lock().pending.clone()
}
pub fn is_closing(&self, label: &str) -> bool {
let in_close = {
let close = self.close.lock();
close.pending.iter().any(|l| l == label) || close.ready.contains(label)
};
in_close || self.in_tab_close_batch(label)
}
pub fn has_other_pending_closes(&self, exclude: &str) -> bool {
let other_pending = self.close.lock().pending.iter().any(|l| l != exclude);
other_pending || self.tab_closes.lock().as_ref().is_some_and(|b| b.has_other(exclude))
}
pub fn in_tab_close_batch(&self, label: &str) -> bool {
self.tab_closes.lock().as_ref().is_some_and(|b| b.contains(label))
}
/// Marks `label` ready to destroy. Returns `false` if its batch was already cancelled.
pub fn mark_ready(&self, label: &str) -> bool {
let ok = {
let mut close = self.close.lock();
if close.aborted.remove(label) {
close.pending.retain(|l| l != label);
close.ready.remove(label);
close.hidden.remove(label);
if close.prompt_holder.as_deref() == Some(label) {
close.prompt_holder = None;
}
false
} else {
if close.prompt_holder.as_deref() == Some(label) {
close.prompt_holder = None;
}
close.ready.insert(label.to_owned());
true
}
};
self.close_notify.notify_waiters();
ok
}
pub fn is_ready(&self, label: &str) -> bool {
self.close.lock().ready.contains(label)
}
pub fn take_aborted(&self, label: &str) -> bool {
let mut close = self.close.lock();
if close.aborted.remove(label) {
close.pending.retain(|l| l != label);
close.ready.remove(label);
close.hidden.remove(label);
true
} else {
false
}
}
/// Marks `labels` as hidden once and returns those newly claimed for hiding.
pub fn claim_hide(&self, labels: &[String]) -> Vec<String> {
let mut close = self.close.lock();
labels.iter().filter(|l| close.hidden.insert((*l).clone())).cloned().collect()
}
/// Marks `labels` as aborted so peers waiting at the close barrier or prompt turn stay open.
pub fn abort_batch(&self, labels: impl IntoIterator<Item = String>) {
{
let mut close = self.close.lock();
for label in labels {
close.ready.remove(&label);
close.hidden.remove(&label);
close.pending.retain(|l| l != &label);
close.aborted.insert(label);
}
close.prompt_holder = None;
}
self.close_notify.notify_waiters();
}
/// Aborts every in-flight close except `initiator` (used when cancelling Quit).
pub fn abort_all_closing(&self, initiator: Option<&str>) {
{
let mut close = self.close.lock();
let mut targets: Vec<String> = std::mem::take(&mut close.pending);
for label in close.ready.drain() {
if !targets.iter().any(|l| l == &label) {
targets.push(label);
}
}
close.hidden.clear();
close.prompt_holder = None;
for label in targets {
if initiator != Some(label.as_str()) {
close.aborted.insert(label);
}
}
}
self.close_notify.notify_waiters();
}
/// Tries to acquire the interactive prompt turn. `Err(())` means this close was aborted.
pub fn try_acquire_prompt(&self, label: &str) -> Result<bool, ()> {
let mut close = self.close.lock();
if close.aborted.contains(label) {
return Err(());
}
if close.prompt_holder.is_none() || close.prompt_holder.as_deref() == Some(label) {
close.prompt_holder = Some(label.to_owned());
Ok(true)
} else {
Ok(false)
}
}
pub fn release_prompt(&self, label: &str) {
let released = {
let mut close = self.close.lock();
if close.prompt_holder.as_deref() == Some(label) {
close.prompt_holder = None;
true
} else {
false
}
};
if released {
self.close_notify.notify_waiters();
}
}
pub fn has_prompt_holder(&self) -> bool {
self.close.lock().prompt_holder.is_some()
}
pub fn set_launch(&self, label: &str, launch: Launch) {
self.launches.lock().insert(label.to_owned(), launch);
}
/// Whether the window `label` is created to open a project (local or remote).
#[cfg(target_os = "macos")]
pub fn opens_project(&self, label: &str) -> bool {
self.pending(label).is_some_and(|l| l.project.is_some() || l.remote.is_some())
}
pub fn take_launch(&self, label: &str) -> Option<Launch> {
let mut launches = self.launches.lock();
// Lightweight windows need their launch again when their webview reloads.
if let Some(launch) = launches.get(label)
&& launch.editor.is_some()
{
return Some(launch.clone());
}
let launch = launches.remove(label)?;
if launch.project.is_some() || launch.remote.is_some() || !launch.files.is_empty() || launch.diff.is_some() {
self.opening.lock().insert(label.to_owned(), launch.clone());
}
Some(launch)
}
pub fn complete_launch(&self, label: &str) {
self.opening.lock().remove(label);
}
/// Adds `files` to `label`'s pending launch, so files sent before its frontend took the last batch aren't lost.
fn add_files_at(&self, label: &str, files: Vec<PathBuf>, positions: &[FilePosition]) {
let mut launches = self.launches.lock();
let launch = launches.entry(label.to_owned()).or_default();
if !files.is_empty() && launch.diff.is_some() {
launch.diff_before_files = true;
}
launch.files.extend(files.into_iter().map(Into::into));
launch.add_positions(positions);
}
fn add_diff(&self, label: &str, diff: DiffFilesLaunch) {
let mut launches = self.launches.lock();
let launch = launches.entry(label.to_owned()).or_default();
launch.diff = Some(Box::new(diff));
launch.diff_before_files = false;
}
pub(crate) fn pending(&self, label: &str) -> Option<Launch> {
let launches = self.launches.lock();
let opening = self.opening.lock();
opening.get(label).or_else(|| launches.get(label)).cloned()
}
pub fn focused(&self, label: &str) {
*self.last_focused.lock() = Some(label.to_owned());
}
/// Claims the creation of window `label`; `None` while another request is already building it.
fn begin_creating(&self, label: &str) -> Option<CreatingWindow<'_>> {
self.creating.lock().insert(label.to_owned()).then(|| CreatingWindow { state: self, label: label.to_owned() })
}
pub fn forget(&self, label: &str) {
self.close_settled(label);
{
let mut launches = self.launches.lock();
let mut opening = self.opening.lock();
launches.remove(label);
opening.remove(label);
}
let mut last = self.last_focused.lock();
if last.as_deref() == Some(label) {
*last = None;
}
}
pub fn last_focused(&self) -> Option<String> {
self.last_focused.lock().clone()
}
pub fn set_quitting(&self, on: bool) {
self.quitting.store(on, Ordering::SeqCst);
if on {
*self.tab_closes.lock() = None;
} else {
self.restart.store(false, Ordering::SeqCst);
}
}
pub fn set_restart(&self) {
self.restart.store(true, Ordering::SeqCst);
}
pub fn restart_requested(&self) -> bool {
self.restart.load(Ordering::SeqCst)
}
pub fn quitting(&self) -> bool {
self.quitting.load(Ordering::SeqCst)
}
}
/// A window that belongs to a project window (its parent) and shares its project.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuxKind {
Settings,
Comparison,
Diff,
Editor,
Deployment,
NewProject,
Clone,
Connect,
}
impl AuxKind {
const ALL: [AuxKind; 8] = [
AuxKind::Settings,
AuxKind::Comparison,
AuxKind::Diff,
AuxKind::Editor,
AuxKind::Deployment,
AuxKind::NewProject,
AuxKind::Clone,
AuxKind::Connect,
];
/// Label prefix, and the key of the one remembered geometry shared by every window of the kind.
pub fn name(self) -> &'static str {
match self {
AuxKind::Settings => "settings",
AuxKind::Comparison => "comparison",
AuxKind::Diff => "diff",
AuxKind::Editor => "editor",
AuxKind::Deployment => "deployment",
AuxKind::NewProject => "new-project",
AuxKind::Clone => "clone",
AuxKind::Connect => "connect",
}
}
/// Several per project window (`<kind>-<parent>-<n>`), else one (`<kind>-<parent>`).
fn multiple(self) -> bool {
matches!(self, AuxKind::Comparison | AuxKind::Diff | AuxKind::Editor)
}
/// Shows the project's documents: gets its parent's events and edits made in sibling windows,
/// and asks about its own unsaved changes before the parent closes it.
pub fn shares_documents(self) -> bool {
!self.dialog()
}
/// Has no actions of its own: menu items run in its project window, which owns the menu.
pub fn runs_menu_in_parent(self) -> bool {
self.dialog()
}
/// Needs its parent to have a project open.
fn needs_project(self) -> bool {
!self.dialog()
}
/// A dialog in its own window (Settings, New Project, Get from VCS, Remote Development):
/// works without the parent's project or documents, and leaves the menu to the parent.
fn dialog(self) -> bool {
matches!(self, AuxKind::Settings | AuxKind::NewProject | AuxKind::Clone | AuxKind::Connect)
}
pub fn min_size(self) -> (f64, f64) {
match self {
AuxKind::Settings => SETTINGS_MIN_SIZE,
AuxKind::Comparison => COMPARISON_MIN_SIZE,
AuxKind::Diff => DIFF_MIN_SIZE,
AuxKind::Editor => EDITOR_MIN_SIZE,
AuxKind::Deployment => DEPLOYMENT_MIN_SIZE,
AuxKind::NewProject => NEW_PROJECT_MIN_SIZE,
AuxKind::Clone => CLONE_MIN_SIZE,
AuxKind::Connect => CONNECT_MIN_SIZE,
}
}
pub fn default_size(self) -> (f64, f64) {
match self {
AuxKind::Settings => SETTINGS_SIZE,
AuxKind::Comparison => COMPARISON_SIZE,
AuxKind::Diff => DIFF_SIZE,
AuxKind::Editor => EDITOR_SIZE,
AuxKind::Deployment => DEPLOYMENT_SIZE,
AuxKind::NewProject => NEW_PROJECT_SIZE,
AuxKind::Clone => CLONE_SIZE,
AuxKind::Connect => CONNECT_SIZE,
}
}
}
/// The kind and parent of an auxiliary window (`comparison-project-2-3` → `Comparison`, `project-2`).
pub fn auxiliary(label: &str) -> Option<(AuxKind, &str)> {
AuxKind::ALL.into_iter().find_map(|kind| {
let rest = label.strip_prefix(kind.name())?.strip_prefix('-')?;
let parent = if kind.multiple() {
let (parent, n) = rest.rsplit_once('-')?;
n.parse::<u64>().ok()?;
parent
} else {
rest
};
(!parent.is_empty()).then_some((kind, parent))
})
}
/// Where a menu item chosen while window `label` is focused runs.
#[derive(Debug, PartialEq, Eq)]
pub enum MenuDestination<'a> {
/// The window's own frontend.
Own,
/// The project window `label`; `raise` brings it to the front so what the action shows is seen.
Parent { label: &'a str, raise: bool },
/// The window itself is the tab: Close Tab closes it.
Close,
/// Next / Previous Window ([`cycle`]), run here from whichever window is focused: an auxiliary
/// window's page may not have the action, and a dialog's parent would cycle from itself.
CycleWindows { forward: bool },
}
/// Actions an auxiliary window always leaves to its project window, which stays where it is: they
/// open their own window or change every window.
const PARENT_ACTIONS: [&str; 6] = [
"app.quit",
"file.settings",
"file.projectStructure",
"file.newProjectsSettings",
"view.toggleTheme",
"file.togglePowerSave",
];
/// Application actions no auxiliary window's page registers. Chosen there they run in the project
/// window, brought to the front to show what they open (About, a file chooser, a popup).
const APP_ACTIONS: [&str; 30] = [
"app.about",
"app.checkForUpdates",
"app.installCli",
"app.uninstallCli",
"file.newProject",
"file.newUntitled",
"file.openProject",
"file.openFile",
"file.openInNewWindow",
crate::menu::OPEN_RECENT_ACTION,
"file.renameProject",
"file.exportSettings",
"file.importSettings",
"file.importJetBrainsSettings",
"file.importJetBrainsRecentProjects",
"file.syncSettings",
"file.invalidateCaches",
"help.keymapReference",
"help.showLog",
"project.attach",
"vcs.clone",
"vcs.cloneProject",
"view.quickSwitchScheme",
"view.notifications",
"welcome.recentProjects",
"window.new",
"window.mergeAllWindows",
"workspace.create",
"workspace.manage",
"workspace.open",
];
/// Where menu item `action` runs while window `label` is focused. As in IDEA, the menu stays usable
/// with Settings open: it runs in the project window, whose enablement (no focused editor) the menu shows.
pub fn menu_destination<'a>(label: &'a str, action: &str) -> MenuDestination<'a> {
match auxiliary(label) {
None => MenuDestination::Own,
Some((_, parent)) if PARENT_ACTIONS.contains(&action) => {
MenuDestination::Parent { label: parent, raise: false }
}
Some(_) if action == "window.toggleFullScreen" => MenuDestination::Own,
Some(_) if action == "window.next" => MenuDestination::CycleWindows { forward: true },
Some(_) if action == "window.previous" => MenuDestination::CycleWindows { forward: false },
Some((kind, parent)) if kind.runs_menu_in_parent() || APP_ACTIONS.contains(&action) => {
MenuDestination::Parent { label: parent, raise: true }
}
Some(_) if action == "editor.closeTab" => MenuDestination::Close,
Some(_) => MenuDestination::Own,
}
}
/// The disabled action ids the shared menu should show while `label` is focused. Items routed to
/// a parent window follow that destination window's enablement, not the focused auxiliary page's.
pub fn effective_menu_disabled(
label: &str,
by_window: &std::collections::HashMap<String, std::collections::HashSet<String>>,
) -> std::collections::HashSet<String> {
let all: std::collections::HashSet<String> = by_window.values().flatten().cloned().collect();
all.into_iter()
.filter(|action| {
let source = match menu_destination(label, action) {
MenuDestination::Own => label,
MenuDestination::Parent { label: parent, .. } => parent,
MenuDestination::Close | MenuDestination::CycleWindows { .. } => return false,
};
by_window.get(source).is_some_and(|disabled| disabled.contains(action))
})
.collect()
}
/// Whether the key equivalent of menu item `action`, pressed in window `label`, runs the item. A
/// page declines the keys it has no action for, and for an item that runs in that page the keymap
/// has spoken; items that never run there (window cycling, application actions from an auxiliary
/// window) still need the key. Only the macOS menu fires key equivalents (`on_menu_event`).
#[cfg(any(target_os = "macos", test))]
pub fn key_equivalent_runs(label: &str, action: &str) -> bool {
match menu_destination(label, action) {
MenuDestination::CycleWindows { .. } => true,
MenuDestination::Parent { .. } => PARENT_ACTIONS.contains(&action) || APP_ACTIONS.contains(&action),
MenuDestination::Own | MenuDestination::Close => false,
}
}
/// The project window an auxiliary window belongs to (`settings-main` → `main`).
pub fn auxiliary_parent(label: &str) -> Option<&str> {
auxiliary(label).map(|(_, parent)| parent)
}
pub fn settings_label(parent: &str) -> String {
format!("{}-{parent}", AuxKind::Settings.name())
}
/// Held from choosing a project window's label until the window exists.
static CREATING: Mutex<()> = Mutex::new(());
/// Windows being built to join a window group, which takes their frame from the group.
#[cfg(target_os = "macos")]
static JOINING_GROUP: Mutex<Vec<String>> = Mutex::new(Vec::new());
/// Whether the window-state plugin restores and tracks `label`'s geometry. A window joining a
/// group takes the group's frame instead; auxiliary windows store logical sizes in
/// `auxiliary-windows.json` and place themselves over their parent, avoiding physical-pixel
/// scale drift and async restore races across mixed-DPI displays.
#[cfg(any(target_os = "macos", test))]
pub fn keeps_window_state(label: &str) -> bool {
#[cfg(target_os = "macos")]
if JOINING_GROUP.lock().iter().any(|l| l == label) {
return false;
}
auxiliary_parent(label).is_none() && !AuxKind::ALL.iter().any(|k| k.name() == label)
}
/// The window state key: one geometry per auxiliary kind, else the label itself.
pub fn state_key(label: &str) -> &str {
auxiliary(label).map_or(label, |(kind, _)| kind.name())
}
/// `parent`'s auxiliary windows that show its project's documents.
pub fn document_windows(app: &AppHandle, parent: &str) -> Vec<String> {
app.webview_windows()
.into_keys()
.filter(|label| auxiliary(label).is_some_and(|(kind, p)| kind.shares_documents() && p == parent))
.collect()
}
/// Closes `parent`'s auxiliary windows: they show (and would apply to) the project it is leaving.
/// Windows with documents are destroyed: the parent's leave guard has already settled their
/// unsaved changes (`closeAuxiliaryWindows` in the UI).
pub fn close_auxiliary_windows(app: &AppHandle, parent: &str) {
for (label, window) in app.webview_windows() {
match auxiliary(&label) {
Some((kind, p)) if p == parent && kind.shares_documents() => {
tracing::debug!(label, parent, kind = kind.name(), "destroying auxiliary window");
drop(window.destroy());
}
Some((kind, p)) if p == parent => {
tracing::debug!(label, parent, kind = kind.name(), "closing auxiliary window");
drop(window.close());
}
_ => {}
}
}
}
/// Project windows, excluding auxiliary windows, for counting and cycling.
pub fn project_window_labels(app: &AppHandle) -> Vec<String> {
sorted_labels(app.webview_windows().keys().map(String::as_str).filter(|l| auxiliary_parent(l).is_none()))
}
/// Sort key: `main` first, then `project-<n>` by n, then anything else.
fn order_key(label: &str) -> (u8, u64, String) {
if label == MAIN {
(0, 0, String::new())
} else if let Some(n) = label.strip_prefix(PREFIX).and_then(|n| n.parse().ok()) {
(1, n, String::new())
} else {
(2, 0, label.to_owned())
}
}
pub fn sorted_labels<'a>(labels: impl IntoIterator<Item = &'a str>) -> Vec<String> {
let mut v: Vec<String> = labels.into_iter().map(str::to_owned).collect();
v.sort_by_key(|l| order_key(l));
v
}
/// Lowest free `project-<n>` label.
pub fn next_label<'a>(existing: impl IntoIterator<Item = &'a str>) -> String {
let used: std::collections::HashSet<&str> = existing.into_iter().collect();
(1..).map(|n| format!("{PREFIX}{n}")).find(|l| !used.contains(l.as_str())).expect("unbounded")
}
/// The window after (or before) `current` in label order, wrapping around.
pub fn cycle_target(sorted: &[String], current: &str, forward: bool) -> Option<String> {
if sorted.len() < 2 {
return None;
}
let i = sorted.iter().position(|l| l == current).unwrap_or(0);
let n = sorted.len();
let j = if forward { (i + 1) % n } else { (i + n - 1) % n };
Some(sorted[j].clone())
}
/// The first path, for ordinary opening and the Windows recent-project shortcut.
pub fn cli_path(args: &[String], cwd: &Path) -> Option<PathBuf> {
cli::parse(args, cwd).ok().and_then(|r| r.path.or(r.file))
}
/// A positional folder plus `--file` opens that file in its project. Without a folder,
/// ordinary files retain LightEdit behavior; an existing project window can claim them.
pub fn cli_launch(args: &[String], cwd: &Path) -> IdeResult<Option<Launch>> {
if let Some(paths) = cli::diff_paths(args, cwd) {
return Ok(match paths.as_slice() {
[left, right, ..] => Some(Launch {
diff: Some(Box::new(DiffFilesLaunch { left: left.clone().into(), right: right.clone().into() })),
..Default::default()
}),
[one] => {
let mut file_args = args.to_vec();
file_args.remove(1);
let position = cli::parse(&file_args, cwd).map_err(IdeError::invalid)?.position;
Some(position_launch(path_launch(one.clone()), position))
}
[] => None,
});
}
let request = cli::parse(args, cwd).map_err(IdeError::invalid)?;
let canonical = |path: &Path| {
dunce::canonicalize(path).map_err(|e| IdeError::invalid(format!("Cannot open {}: {e}", path.display())))
};
let launch = if let Some(file) = request.file {
let file = canonical(&file)?;
if !file.is_file() {
return Err(IdeError::invalid("--file must name an existing file"));
}
let project = request.path.map(|p| canonical(&p)).transpose()?;
if project.as_ref().is_some_and(|p| !p.is_dir()) {
return Err(IdeError::invalid("The positional path before --file must be a project folder"));
}
if project.as_ref().is_some_and(|p| wsl_launch(p).is_some()) {
return Err(IdeError::invalid("External file locations require a local project folder"));
}
path_launch_of(project, vec![file])
} else if let Some(path) = request.path {
path_launch(canonical(&path)?)
} else {
return Ok(None);
};
Ok(Some(position_launch(launch, request.position)))
}
fn position_launch(mut launch: Launch, position: Option<(u32, u32)>) -> Launch {
if let (Some((line, column)), Some(file)) = (position, launch.files.first()) {
launch.positions.push(FilePosition { path: file.clone(), line, column });
}
launch
}
/// CLI failures also need to be visible before the webview has subscribed to notifications.
pub fn report_cli_error(app: &AppHandle, error: IdeError) {
use tauri_plugin_dialog::DialogExt;
tracing::warn!("external open failed: {}", error.message);
app.dialog()
.message(error.message)
.title("OpenIDEA")
.kind(tauri_plugin_dialog::MessageDialogKind::Error)
.show(|_| {});
}
pub fn focus(window: &WebviewWindow) {
if crate::window_groups::select(window.app_handle(), window.label(), window.label()).is_ok() {
return;
}
let _ = window.unminimize();
let _ = window.show();
let _ = window.set_focus();
}
pub fn hide_windows(windows: Vec<WebviewWindow>) {
#[cfg(target_os = "macos")]
crate::macos::hide_windows(windows);
#[cfg(not(target_os = "macos"))]
for window in windows {
let _ = window.hide();
}
}
/// Closes every project window in parallel without flipping focus across clean tabs.
/// Windows that need user confirmation acquire the prompt turn one at a time via `begin_prompt`.
pub fn start_quit(app: &AppHandle) {
let state = app.state::<WindowsState>();
let labels = project_window_labels(app);
if labels.is_empty() {
app.exit(0);
return;
}
for label in &labels {
state.close_requested(label);
}
let mut any_sent = false;
for label in labels {
if let Some(window) = app.get_webview_window(&label)
&& window.close().is_ok()
{
any_sent = true;
continue;
}
state.close_settled(&label);
}
if !any_sent {
if project_window_labels(app).is_empty() {
app.exit(0);
} else {
state.set_quitting(false);
}
}
}
/// Exits once every project window has finished closing during Quit.
pub fn continue_quit(app: &AppHandle, closed: Option<&str>) {
let state = app.state::<WindowsState>();
if !state.quitting() {
return;
}
let remaining: Vec<String> =
project_window_labels(app).into_iter().filter(|label| Some(label.as_str()) != closed).collect();
if remaining.is_empty() {
app.exit(0);
return;
}
// Close any window that appeared after Quit began and was not yet asked to close.
for label in remaining {
if !state.is_closing(&label)
&& let Some(window) = app.get_webview_window(&label)
{
state.close_requested(&label);
if window.close().is_err() {
state.close_settled(&label);
}
}
}
}
/// The window whose open project is `root` (already canonical).
pub fn find_project_window(app: &AppHandle, root: &Path) -> Option<WebviewWindow> {
let state = app.try_state::<AppState>()?;
app.webview_windows().into_iter().filter(|(label, _)| auxiliary_parent(label).is_none()).find_map(|(label, w)| {
let project = state.project(&label).ok()?;
(project.root == root && !project.light).then_some(w)
})
}
/// Hands `files` to the already loaded `window` (its frontend takes them on [`LAUNCH_EVENT`]) and focuses it.
fn send_files(app: &AppHandle, window: &WebviewWindow, files: Vec<PathBuf>) {
send_files_at(app, window, files, &[]);
}
fn send_files_at(app: &AppHandle, window: &WebviewWindow, files: Vec<PathBuf>, positions: &[FilePosition]) {
app.state::<WindowsState>().add_files_at(window.label(), files, positions);
let _ = app.emit_to(window.label(), LAUNCH_EVENT, ());
focus(window);
}
/// The focused window, else the last focused, else any.
pub fn target_window(app: &AppHandle) -> Option<WebviewWindow> {
let windows = app.webview_windows();
if let Some(w) = windows.values().find(|w| w.is_focused().unwrap_or(false)) {
return Some(w.clone());
}
let last = app.state::<WindowsState>().last_focused();
if let Some(w) = last.and_then(|l| windows.get(&l)) {
return Some(w.clone());
}
sorted_labels(windows.keys().map(String::as_str)).first().and_then(|l| windows.get(l)).cloned()
}
fn window_config(app: &AppHandle) -> IdeResult<WindowConfig> {
app.config()
.app
.windows
.iter()
.find(|w| w.label == MAIN)
.or_else(|| app.config().app.windows.first())
.cloned()
.ok_or_else(|| IdeError::internal("no window configuration"))
}
/// The theme's `--bg-frame` (tokens.css), mirroring `resolveThemeId` in theme.ts.
pub fn frame_color(appearance: &AppearanceSettings, os_dark: bool) -> Color {
let sync = appearance.sync_with_os || appearance.theme == "system";
let theme = match (sync, os_dark) {
(false, _) => &appearance.theme,
(true, true) => &appearance.preferred_dark_theme,
(true, false) => &appearance.preferred_light_theme,
};
builtin_frame(theme)
.or_else(|| builtin_frame(if sync && os_dark { "dark" } else { "light" }))
.unwrap_or(Color(0xe9, 0xea, 0xee, 0xff))
}
/// A `#rgb` / `#rrggbb` / `#rrggbbaa` colour sent by the UI (Tauri's own parser).
fn hex_color(hex: &str) -> Option<Color> {
serde_json::from_value(serde_json::Value::String(hex.to_owned())).ok()
}
/// Builds a project window painted in the saved theme's frame colour, so it
/// doesn't flash the light default before its page (and splash) loads.
fn build_project_window(app: &AppHandle, mut config: WindowConfig) -> IdeResult<WebviewWindow> {
let visible = cfg!(target_os = "macos") && config.visible;
let focused = config.focus;
if visible {
config.visible = false;
}
let state = app.try_state::<AppState>();
if let Some(state) = &state {
state.plugins.reopen_client(&config.label);
}
let appearance = state.as_ref().map(|s| s.global_settings.effective().appearance);
// A plugin theme replaces the built-in one in either OS mode; the UI records its resolved frame.
let plugin_frame = state.as_ref().and_then(|s| s.plugins.startup_frame()).and_then(|c| hex_color(&c));
if let Some(color) = plugin_frame {
config.background_color = Some(color);
} else if let Some(a) = &appearance {
config.background_color = Some(frame_color(a, false));
}
#[cfg(target_os = "linux")]
let show_after_build = crate::linux_window_state::prepare(app, &mut config);
// Windows opens a project window at the configured size (window-state restores only the
// maximized flag there), which Restore Down returns to, on the display last worked on.
#[cfg(windows)]
if let Some(monitor) = crate::window_display::place(app, &mut config) {
let area = monitor.work_area().size.to_logical::<f64>(monitor.scale_factor());
let min = (config.min_width.unwrap_or(0.0), config.min_height.unwrap_or(0.0));
let (size, min) = fit_new_window((config.width, config.height), min, (area.width, area.height));
(config.width, config.height) = size;
(config.min_width, config.min_height) = (Some(min.0), Some(min.1));
}
let window = WebviewWindowBuilder::from_config(app, &config)
.and_then(|b| {
macos_webview_configuration(app, webview_profile(app, b)).on_new_window(open_popup_in_browser).build()
})
.map_err(|e| IdeError::internal(format!("cannot create window: {e}")))?;
#[cfg(target_os = "linux")]
decorate_csd(&window);
#[cfg(target_os = "macos")]
{
suppress_window_writing_tools_affordance(&window);
crate::macos::install_webview_key_handlers(&window);
crate::page_swipe::install(&window);
}
apply_zoom(app, &window);
#[cfg(target_os = "macos")]
align_titlebar(app, &window, app.try_state::<WindowsState>().is_some_and(|w| w.opens_project(window.label())));
// "Sync with OS" needs the OS mode, known once the window exists.
if let Some(a) = appearance.filter(|a| plugin_frame.is_none() && (a.sync_with_os || a.theme == "system"))
&& window.theme().is_ok_and(|t| t == Theme::Dark)
{
let _ = window.set_background_color(Some(frame_color(&a, true)));
}
#[cfg(target_os = "linux")]
if show_after_build {
window.show().map_err(|e| IdeError::internal(format!("cannot show window: {e}")))?;
}
if visible {
window.show().map_err(|e| IdeError::internal(format!("cannot show window: {e}")))?;
if focused {
let _ = window.set_focus();
}
}
Ok(window)
}
/// Wayland: tao already makes frameless windows client-side decorated (an empty GTK title bar, so
/// the compositor adds no frame), but undecorated GTK windows get no shadow or resize margin.
/// Decorating keeps that empty title bar, so GTK adds the theme's shadow and resize edges and
/// nothing else. X11 has no such title bar: decorating there would bring back the WM frame.
#[cfg(target_os = "linux")]
fn decorate_csd(window: &WebviewWindow) {
if crate::linux_window_state::is_wayland() {
let _ = window.set_decorations(true);
}
}
/// Sets the saved IDE zoom before the page loads; applied later by the frontend,
/// it would visibly scale up the splash.
fn apply_zoom(app: &AppHandle, window: &WebviewWindow) {
let Some(zoom) = app.try_state::<AppState>().map(|s| s.global_settings.effective().appearance.zoom) else {
return;
};
if zoom != 100 {
let _ = window.set_zoom(f64::from(zoom) / 100.0);
}
}
/// Aligns the native window buttons before the page reports its title bar height,
/// so they don't visibly move once it loads.
#[cfg(target_os = "macos")]
fn align_titlebar(app: &AppHandle, window: &WebviewWindow, opens_project: bool) {
let Some(a) = app.try_state::<AppState>().map(|s| s.global_settings.effective().appearance) else {
return;
};
let _ = crate::titlebar::set_height(window, initial_titlebar_height(&a, opens_project));
}
/// The title bar height (AppKit points) the page will draw. Only a project window hides its
/// toolbar for View › Appearance › Toolbar, and it is known to be one when it opens a project.
#[cfg(any(target_os = "macos", test))]
fn initial_titlebar_height(a: &AppearanceSettings, opens_project: bool) -> f64 {
// `--h-titlebar` and `--h-compact-header` in tokens.css.
const HEIGHT: f64 = 40.0;
const COMPACT_HEIGHT: f64 = 32.0;
const COMPACT_HEADER_HEIGHT: f64 = 30.0;
let height = if opens_project && !a.show_main_toolbar {
COMPACT_HEADER_HEIGHT
} else if a.compact_mode {
COMPACT_HEIGHT
} else {
HEIGHT
};
height * f64::from(a.zoom.clamp(50, 300)) / 100.0
}
/// The first window (`create: false` in tauri.conf.json so it can take the theme's colour).
pub fn create_main_window(app: &AppHandle) -> IdeResult<WebviewWindow> {
build_project_window(app, window_config(app)?)
}
/// Creates a new project window; `project` is opened by its frontend on load.
pub fn create_window(app: &AppHandle, project: Option<PathBuf>) -> IdeResult<WebviewWindow> {
create_window_with(app, project, Vec::new(), false, None)
}
/// [`create_window`] that also opens `files` (without `project`: in LightEdit).
fn create_window_with(
app: &AppHandle,
project: Option<PathBuf>,
files: Vec<PathBuf>,
restoring: bool,
tab_anchor: Option<&str>,
) -> IdeResult<WebviewWindow> {
let title = project.as_deref().or(files.first().map(PathBuf::as_path)).and_then(Path::file_name);
let title = title.map(|n| n.to_string_lossy().into_owned());
let files = files.into_iter().map(Into::into).collect();
create_window_for(
app,
title,
Launch { project: project.map(Into::into), files, ..Default::default() },
restoring,
tab_anchor,
)
}
/// Restoring a saved group preserves its membership and selected tab.
pub fn create_restored_window(app: &AppHandle, project: PathBuf) -> IdeResult<WebviewWindow> {
create_window_with(app, Some(project), Vec::new(), true, None)
}
fn create_window_for(
app: &AppHandle,
title: Option<String>,
launch: Launch,
restoring: bool,
tab_anchor: Option<&str>,
) -> IdeResult<WebviewWindow> {
let explicit_tab = if restoring { None } else { tab_anchor.and_then(|label| app.get_webview_window(label)) };
#[cfg(target_os = "macos")]
let tab_target = if restoring {
None
} else if explicit_tab.is_some() {
explicit_tab
} else {
crate::macos::new_project_tab_target(app, &launch)?
};
#[cfg(not(target_os = "macos"))]
let tab_target = explicit_tab;
// The label is free only until the window exists, so concurrent opens take turns.
let creating = CREATING.lock();
let windows = app.webview_windows();
let label = next_label(windows.keys().map(String::as_str));
let mut config = window_config(app)?;
config.label = label.clone();
if let Some(title) = title {
config.title = title;
}
// Cascade from the window the user is looking at; window-state overrides
// this with the remembered geometry when it has one for the label.
if let Some(anchor) = target_window(app)
&& let (Ok(pos), Ok(scale)) = (anchor.outer_position(), anchor.scale_factor())
{
let pos = pos.to_logical::<f64>(scale);
config.x = Some(pos.x + CASCADE);
config.y = Some(pos.y + CASCADE);
config.center = false;
}
app.state::<WindowsState>().set_launch(&label, launch);
if tab_target.is_some() {
#[cfg(target_os = "macos")]
JOINING_GROUP.lock().push(label.clone());
// Selecting it in the group shows it; shown before, a window opened over a fullscreen
// group lands on the desktop and takes the screen there, and an explicit New Tab would
// briefly flash as a standalone window before joining the group.
config.visible = false;
}
let window = build_project_window(app, config);
drop(creating);
// Plugins hear of the window from the main thread's queue; forget it after them.
#[cfg(target_os = "macos")]
if tab_target.is_some() {
let joined = label.clone();
let _ = app.run_on_main_thread(move || JOINING_GROUP.lock().retain(|l| *l != joined));
}
let window = window?;
if let Some(target) = tab_target {
match crate::window_groups::join(app, target.label(), window.label()) {
Ok(()) => {
tracing::debug!(target = target.label(), label = window.label(), "joined window group");
return Ok(window);
}
Err(error) => {
tracing::debug!(
target = target.label(),
label = window.label(),
error = %error.message,
"cannot join window group"
);
// The window was created hidden for tabbing; show it standalone unless it is gone too.
if window.show().is_err() {
return Err(error);
}
if tab_anchor.is_some() {
focus(&window);
return Err(error);
}
}
}
}
focus(&window);
Ok(window)
}
/// `create_window` off the event-loop thread (building a window from a
/// main-thread callback can deadlock on Windows).
pub fn spawn_window(app: &AppHandle, project: Option<PathBuf>) {
spawn_window_with(app, project, Vec::new());
}
fn spawn_window_with(app: &AppHandle, project: Option<PathBuf>, files: Vec<PathBuf>) {
let title = project.as_deref().or(files.first().map(PathBuf::as_path)).and_then(Path::file_name);
let title = title.map(|n| n.to_string_lossy().into_owned());
let _ = open_launch_window(app, title, path_launch_of(project, files), false);
}
fn open_launch_window(app: &AppHandle, title: Option<String>, launch: Launch, wait: bool) -> IdeResult<()> {
if wait {
return create_window_for(app, title, launch, false, None).map(|_| ());
}
let app = app.clone();
tauri::async_runtime::spawn(async move {
if let Err(e) = create_window_for(&app, title, launch, false, None) {
tracing::warn!("cannot open window: {}", e.message);
}
});
Ok(())
}
/// How `open_project_window` satisfied the request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum OpenOutcome {
/// Already open in `label`; that window was focused.
Focused { label: String },
/// A new window `label` was created and opens the project itself.
Opened { label: String },
/// The caller should open the project in its own window.
Current,
}
/// Opens folder `path` as a project and then `files` in its editor; see [`OpenOutcome`]
/// (`Current`: the caller opens both itself). When `tab_anchor` is set, the opened or
/// already-open project window joins that window's tab group before it is shown.
pub fn open_project_window(
app: &AppHandle,
path: &Path,
new_window: bool,
files: Vec<PathBuf>,
tab_anchor: Option<&str>,
) -> IdeResult<OpenOutcome> {
// Before touching the share: a stat on `\\wsl.localhost` is what makes it slow.
if let Some(remote) = wsl_launch(path) {
return open_remote_window(app, &remote.target, &remote.path, new_window, tab_anchor);
}
let root = dunce::canonicalize(path).map_err(|e| IdeError::not_found(format!("{}: {e}", path.display())))?;
tracing::info!("open_project_window {} new_window={new_window} tab_anchor={tab_anchor:?}", root.display());
if let Some(w) = find_project_window(app, &root) {
if let Some(anchor) = tab_anchor
&& w.label() != anchor
{
crate::window_groups::join(app, anchor, w.label())?;
if !files.is_empty() {
send_files(app, &w, files);
}
} else if files.is_empty() {
focus(&w);
} else {
send_files(app, &w, files);
}
return Ok(OpenOutcome::Focused { label: w.label().to_owned() });
}
if !new_window {
return Ok(OpenOutcome::Current);
}
let window = create_window_with(app, Some(root), files, false, tab_anchor)?;
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
}
/// Opens `file` in a forced new window: `project` (None: LightEdit) plus the
/// single file, always creating a window even when the project is already open
/// in another one (unlike [`open_project_window`], which focuses it). Used to
/// detach an editor tab out of its window.
pub fn open_file_in_new_window(app: &AppHandle, project: Option<PathBuf>, file: PathBuf) -> IdeResult<OpenOutcome> {
tracing::info!("open_file_in_new_window {} project={:?}", file.display(), project);
let window = create_window_with(app, project, vec![file], false, None)?;
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
}
/// Loose local `files` asked for by window `caller` (File | Open File, a drop on the Welcome
/// screen), routed by [`plan_open`]; returns the files `caller` opens itself.
pub fn open_files_window(app: &AppHandle, caller: &str, files: Vec<PathBuf>) -> Vec<ide_core::IpcPath> {
let files = files.into_iter().map(|f| dunce::canonicalize(&f).unwrap_or(f)).collect();
let (steps, here) = plan_open_files(caller, files, &window_holds(app, Some(caller)));
run_open(app, steps);
here.into_iter().map(Into::into).collect()
}
/// [`open_project_window`] for folder `path` of `target` (Remote Development).
pub fn open_remote_window(
app: &AppHandle,
target: &RemoteTarget,
path: &str,
new_window: bool,
tab_anchor: Option<&str>,
) -> IdeResult<OpenOutcome> {
tracing::info!("open_remote_window {target:?}:{path} new_window={new_window} tab_anchor={tab_anchor:?}");
let windows = app.webview_windows();
let open = app.try_state::<AppState>().and_then(|s| s.remote_dev.window_of(target, path));
if let Some(w) = open.and_then(|label| windows.get(&label)) {
if let Some(anchor) = tab_anchor
&& w.label() != anchor
{
crate::window_groups::join(app, anchor, w.label())?;
} else {
focus(w);
}
return Ok(OpenOutcome::Focused { label: w.label().to_owned() });
}
if !new_window {
return Ok(OpenOutcome::Current);
}
let title = path.trim_end_matches('/').rsplit('/').next().filter(|n| !n.is_empty() && *n != "~").map(str::to_owned);
let remote = RemoteLaunch { target: target.clone(), path: path.to_owned() };
let window =
create_window_for(app, title, Launch { remote: Some(remote), ..Default::default() }, false, tab_anchor)?;
Ok(OpenOutcome::Opened { label: window.label().to_owned() })
}
/// What a window holds, for routing paths opened from outside (command line, Finder).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Holds {
/// Nothing yet (Welcome screen): free to take a launch.
Nothing,
LightEdit,
Project(PathBuf),
/// A project inside a WSL distribution (`path` is the Linux path).
Wsl {
distro: String,
path: String,
},
/// An SSH or dev container remote project.
Other,
}
impl Holds {
fn of_remote(remote: &RemoteLaunch) -> Self {
match &remote.target {
RemoteTarget::Wsl { distro } => Holds::Wsl { distro: distro.clone(), path: remote.path.clone() },
RemoteTarget::Ssh { .. } | RemoteTarget::DevContainer { .. } => Holds::Other,
}
}
fn is_wsl_folder(&self, remote: &RemoteLaunch) -> bool {
match (self, &remote.target) {
(Holds::Wsl { distro, path }, RemoteTarget::Wsl { distro: d }) => {
distro.eq_ignore_ascii_case(d) && path.trim_end_matches('/') == remote.path.trim_end_matches('/')
}
_ => false,
}
}
}
/// What window `label` holds: its open project, else what its pending launch is about to open.
fn holds(app: &AppHandle, label: &str) -> Holds {
let state = app.try_state::<AppState>();
if let Some(project) = state.as_ref().and_then(|s| s.project(label).ok()) {
return if project.light { Holds::LightEdit } else { Holds::Project(project.root.clone()) };
}
if let Some(c) = state.as_ref().and_then(|s| s.remote_dev.get(label)) {
return Holds::of_remote(&RemoteLaunch { target: c.target.clone(), path: c.root.clone() });
}
pending_holds(app.state::<WindowsState>().pending(label).as_ref())
}
fn pending_holds(launch: Option<&Launch>) -> Holds {
match launch {
Some(Launch { project: Some(root), .. }) => Holds::Project(root.to_path_buf()),
Some(Launch { remote: Some(remote), .. }) => Holds::of_remote(remote),
Some(l) if !l.files.is_empty() || l.diff.is_some() => Holds::LightEdit,
_ => Holds::Nothing,
}
}
/// One step of opening paths from outside; see [`plan_open`].
#[derive(Debug, PartialEq, Eq)]
pub enum OpenStep {
/// The window already has the folder open.
Focus(String),
/// Files open in this window: in its project, or join its LightEdit session.
Files(String, Vec<PathBuf>),
/// A free window opens this (boxed: the enum stays small).
Launch(String, Box<Launch>),
/// A new window opens the project folder or (without one) the files in LightEdit.
New(Option<PathBuf>, Vec<PathBuf>),
/// A new window connects to a WSL distribution.
NewRemote(RemoteLaunch),
}
/// Where a file inside a project goes: a window with it open, or the step opening it.
enum ProjectTarget {
Window(String),
Step(usize),
}
/// Routes canonical `paths` (with whether each is a folder) over `windows` (the preferred
/// one first). A folder focuses the window that has it open, else takes a free window,
/// else a new one. A file inside a local project being opened (the deepest root if nested)
/// opens in that project's window; the others join the preferred window when it holds a local
/// project or LightEdit, else the LightEdit window, else a free window or a new one switches to
/// LightEdit for them.
pub fn plan_open(paths: Vec<(PathBuf, bool)>, windows: &[(String, Holds)]) -> Vec<OpenStep> {
let mut free = windows.iter().filter(|(_, h)| *h == Holds::Nothing).map(|(l, _)| l.clone());
let (mut dirs, mut files) = (Vec::new(), Vec::new());
for (path, is_dir) in paths {
let list = if is_dir { &mut dirs } else { &mut files };
if !list.contains(&path) {
list.push(path);
}
}
let mut steps = Vec::new();
let mut roots: Vec<(PathBuf, ProjectTarget)> = windows
.iter()
.filter_map(|(label, h)| match h {
Holds::Project(root) => Some((root.clone(), ProjectTarget::Window(label.clone()))),
_ => None,
})
.collect();
for dir in dirs {
// A folder of a WSL distro opens as a WSL project, not as a network folder.
if let Some(remote) = wsl_launch(&dir) {
steps.push(match windows.iter().find(|(_, h)| h.is_wsl_folder(&remote)) {
Some((label, _)) => OpenStep::Focus(label.clone()),
None => match free.next() {
Some(label) => {
OpenStep::Launch(label, Box::new(Launch { remote: Some(remote), ..Default::default() }))
}
None => OpenStep::NewRemote(remote),
},
});
continue;
}
if let Some((label, _)) = windows.iter().find(|(_, h)| matches!(h, Holds::Project(root) if *root == dir)) {
steps.push(OpenStep::Focus(label.clone()));
continue;
}
roots.push((dir.clone(), ProjectTarget::Step(steps.len())));
steps.push(match free.next() {
Some(label) => OpenStep::Launch(label, Box::new(path_launch_of(Some(dir), Vec::new()))),
None => OpenStep::New(Some(dir), Vec::new()),
});
}
let mut in_windows: Vec<(String, Vec<PathBuf>)> = Vec::new();
let mut loose = Vec::new();
for file in files {
let deepest =
roots.iter().filter(|(root, _)| file.starts_with(root)).max_by_key(|(root, _)| root.components().count());
match deepest.map(|(_, target)| target) {
Some(ProjectTarget::Window(label)) => match in_windows.iter_mut().find(|(l, _)| l == label) {
Some((_, list)) => list.push(file),
None => in_windows.push((label.clone(), vec![file])),
},
Some(ProjectTarget::Step(i)) => match &mut steps[*i] {
OpenStep::Launch(_, launch) => launch.files.push(file.into()),
OpenStep::New(_, list) => list.push(file),
OpenStep::Focus(_) | OpenStep::Files(..) | OpenStep::NewRemote(_) => {
unreachable!("folders opened by the plan launch or open")
}
},
None => loose.push(file),
}
}
let preferred = windows.first().filter(|(_, h)| matches!(h, Holds::Project(_) | Holds::LightEdit));
let light = windows.iter().find(|(_, h)| *h == Holds::LightEdit);
match preferred.or(light) {
Some((label, _)) if !loose.is_empty() => {
let files = std::mem::take(&mut loose);
match in_windows.iter_mut().find(|(l, _)| l == label) {
Some((_, list)) => list.extend(files),
None => in_windows.push((label.clone(), files)),
}
}
_ => {}
}
steps.extend(in_windows.into_iter().map(|(label, files)| OpenStep::Files(label, files)));
if !loose.is_empty() {
steps.push(match free.next() {
Some(label) => OpenStep::Launch(label, Box::new(path_launch_of(None, loose))),
None => OpenStep::New(None, loose),
});
}
steps
}
fn path_launch_of(project: Option<PathBuf>, files: Vec<PathBuf>) -> Launch {
Launch {
project: project.map(Into::into),
files: files.into_iter().map(Into::into).collect(),
..Default::default()
}
}
/// [`plan_open`] for loose `files` asked for by window `caller` (listed first in `windows`):
/// no other free window takes them. Returns the steps for other windows and the files
/// `caller` opens itself (in its LightEdit session, or turning into one).
pub fn plan_open_files(
caller: &str,
files: Vec<PathBuf>,
windows: &[(String, Holds)],
) -> (Vec<OpenStep>, Vec<PathBuf>) {
let windows: Vec<_> = windows.iter().filter(|(l, h)| l == caller || *h != Holds::Nothing).cloned().collect();
let mut here = Vec::new();
let mut steps = Vec::new();
for step in plan_open(files.into_iter().map(|f| (f, false)).collect(), &windows) {
match step {
OpenStep::Files(label, files) if label == caller => here.extend(files),
OpenStep::Launch(label, launch) if label == caller => here.extend(launch.files.into_iter().map(|f| f.0)),
step => steps.push(step),
}
}
(steps, here)
}
/// Canonical existing `paths` with whether each is a folder; the others are logged and dropped.
fn resolve(paths: Vec<PathBuf>) -> Vec<(PathBuf, bool)> {
paths
.into_iter()
.filter_map(|path| match dunce::canonicalize(&path) {
Ok(p) => {
let is_dir = p.is_dir();
Some((p, is_dir))
}
Err(e) => {
tracing::warn!("cannot open {}: {e}", path.display());
None
}
})
.collect()
}
/// Opens paths from Finder's Open With or the Dock:
/// see [`plan_open`]; the focused window is the preferred free one.
#[cfg(target_os = "macos")]
pub fn open_paths(app: &AppHandle, paths: Vec<PathBuf>) {
open_paths_at(app, paths, &[]);
}
fn open_paths_at(app: &AppHandle, paths: Vec<PathBuf>, positions: &[FilePosition]) {
let (existing, missing): (Vec<_>, Vec<_>) = paths.into_iter().partition(|path| path.exists());
if !missing.is_empty() {
let paths = missing.iter().map(|path| path.display().to_string()).collect::<Vec<_>>().join("\n");
app.state::<AppState>().sink.emit(ide_core::IdeEvent::Notify {
level: ide_core::event::NotifyLevel::Warning,
title: "Cannot open path".into(),
body: Some(format!("The path does not exist:\n{paths}")),
});
}
let preferred = target_window(app).map(|w| w.label().to_owned());
if let Err(error) =
run_open_at(app, plan_open(resolve(existing), &window_holds(app, preferred.as_deref())), positions, false)
{
tracing::warn!("cannot open window: {}", error.message);
}
}
/// Local project roots currently open in desktop windows, focused window first.
pub(crate) fn open_project_roots(app: &AppHandle) -> Vec<PathBuf> {
let preferred = target_window(app).map(|w| w.label().to_owned());
let mut roots = Vec::new();
for (_, held) in window_holds(app, preferred.as_deref()) {
if let Holds::Project(root) = held
&& !roots.contains(&root)
{
roots.push(root);
}
}
roots
}
/// Label of the open window holding `project_root`, preferring the focused window.
pub(crate) fn project_window_label(app: &AppHandle, project_root: &Path) -> Option<String> {
let preferred = target_window(app).map(|w| w.label().to_owned());
window_holds(app, preferred.as_deref()).into_iter().find_map(|(label, held)| match held {
Holds::Project(root) if root == project_root => Some(label),
_ => None,
})
}
/// Brings the window `label` to the foreground (runs on the main thread).
pub(crate) fn focus_window(app: &AppHandle, label: &str) {
let handle = app.clone();
let label = label.to_owned();
let _ = app.run_on_main_thread(move || {
if let Some(w) = handle.get_webview_window(&label) {
focus(&w);
}
});
}
/// Focuses the window holding `project_root` and opens `file` at 1-based `line` / `column`.
pub(crate) fn open_file_in_project_window(
app: &AppHandle,
project_root: &Path,
file: PathBuf,
line: Option<u32>,
column: Option<u32>,
) -> IdeResult<()> {
let label = project_window_label(app, project_root)
.ok_or_else(|| ide_core::IdeError::not_found("Project window is no longer open"))?;
let positions = line.map(|l| FilePosition {
path: ide_core::IpcPath::from(file.clone()),
line: l.saturating_sub(1),
column: column.unwrap_or(1).saturating_sub(1),
});
let slice = positions.as_slice();
match app.get_webview_window(&label) {
Some(w) => send_files_at(app, &w, vec![file], slice),
None => app.state::<WindowsState>().add_files_at(&label, vec![file], slice),
}
Ok(())
}
/// What each window holds, `preferred` first.
fn window_holds(app: &AppHandle, preferred: Option<&str>) -> Vec<(String, Holds)> {
let mut labels = project_window_labels(app);
if let Some(i) = preferred.and_then(|p| labels.iter().position(|l| l == p)) {
let first = labels.remove(i);
labels.insert(0, first);
}
labels.into_iter().map(|l| (l.clone(), holds(app, &l))).collect()
}
/// Opens external paths while the app is starting, before `main` exists: it takes them as its launch.
pub fn open_paths_at_launch(app: &AppHandle, paths: Vec<PathBuf>) {
let main = pending_holds(app.state::<WindowsState>().pending(MAIN).as_ref());
run_open(app, plan_open(resolve(paths), &[(MAIN.to_owned(), main)]));
}
fn run_open(app: &AppHandle, steps: Vec<OpenStep>) {
if let Err(error) = run_open_at(app, steps, &[], false) {
tracing::warn!("cannot open window: {}", error.message);
}
}
/// CLI workers wait for new windows so the next request can route to them.
pub(crate) fn run_open_at(
app: &AppHandle,
steps: Vec<OpenStep>,
positions: &[FilePosition],
wait_for_window: bool,
) -> IdeResult<()> {
let windows = app.state::<WindowsState>();
for step in steps {
match step {
OpenStep::Focus(label) => {
if let Some(w) = app.get_webview_window(&label) {
focus(&w);
}
}
OpenStep::Files(label, files) => match app.get_webview_window(&label) {
Some(w) => send_files_at(app, &w, files, positions),
None => windows.add_files_at(&label, files, positions),
},
OpenStep::Launch(label, mut launch) => {
launch.add_positions(positions);
windows.set_launch(&label, *launch);
if let Some(w) = app.get_webview_window(&label) {
let _ = app.emit_to(&label, LAUNCH_EVENT, ());
focus(&w);
}
}
OpenStep::New(project, files) => {
let title = project.as_deref().or(files.first().map(PathBuf::as_path)).and_then(Path::file_name);
let title = title.map(|n| n.to_string_lossy().into_owned());
let mut launch = path_launch_of(project, files);
launch.add_positions(positions);
open_launch_window(app, title, launch, wait_for_window)?;
}
OpenStep::NewRemote(remote) => {
let title = remote.path.trim_end_matches('/').rsplit('/').next().map(str::to_owned);
let launch = Launch { remote: Some(remote), ..Default::default() };
open_launch_window(app, title, launch, wait_for_window)?;
}
}
}
Ok(())
}
/// Local files and folders of the URLs macOS asks the app to open (Finder's Open With, the Dock).
#[cfg(target_os = "macos")]
pub fn opened_paths(urls: &[tauri::Url]) -> Vec<PathBuf> {
urls.iter().filter_map(|u| u.to_file_path().ok()).collect()
}
/// What a window opens for a command-line path: a folder as its project, a file in LightEdit.
pub fn path_launch(path: PathBuf) -> Launch {
let is_dir = path.is_dir();
launch_for(path, is_dir)
}
/// [`path_launch`] once the path is known to be a folder or not: only a WSL *folder* is a
/// remote project, a file of the share stays a LightEdit file.
fn launch_for(path: PathBuf, is_dir: bool) -> Launch {
if !is_dir {
Launch { files: vec![path.into()], ..Default::default() }
} else if let Some(remote) = wsl_launch(&path) {
Launch { remote: Some(remote), ..Default::default() }
} else {
Launch { project: Some(path.into()), ..Default::default() }
}
}
/// Second instance launched: focus this one and open its path argument.
pub fn on_second_instance(app: &AppHandle, args: Vec<String>, cwd: String) {
if app.state::<WindowsState>().cli.lock().push(args, cwd) {
drain_cli(app);
}
}
fn dispatch_cli(app: &AppHandle, args: Vec<String>, cwd: String) {
if args.iter().any(|arg| arg == crate::jump_list::OPEN_RECENT_ARG)
&& let Some(path) = cli_path(&args, Path::new(&cwd)).filter(|path| path.exists())
{
let target = target_window(app)
.filter(|window| auxiliary_parent(window.label()).is_none())
.or_else(|| project_window_labels(app).into_iter().find_map(|label| app.get_webview_window(&label)));
if let Some(window) = target {
let _ = app.emit_to(
window.label(),
crate::menu::MENU_EVENT,
crate::menu::MenuRoute {
action: crate::menu::OPEN_RECENT_ACTION.into(),
arg: Some(ide_core::ipc_path::to_ipc(&path)),
},
);
focus(&window);
return;
}
}
match cli_launch(&args, Path::new(&cwd)) {
Ok(Some(Launch { diff: Some(diff), .. })) => {
if let Err(error) = open_diff_files_at(app, *diff, true) {
report_cli_error(app, error);
}
}
Ok(Some(launch)) => {
let preferred = target_window(app).map(|w| w.label().to_owned());
let positions = launch.positions.clone();
let plan = plan_cli_launch(launch, &window_holds(app, preferred.as_deref()));
if let Err(error) = run_open_at(app, plan, &positions, true) {
report_cli_error(app, error);
}
}
Ok(None) => focus_any(app),
Err(error) => report_cli_error(app, error),
}
}
/// Focuses the window the user last looked at, else opens a new one.
fn focus_any(app: &AppHandle) {
if let Some(w) = target_window(app) {
focus(&w);
} else {
spawn_window(app, None);
}
}
/// Opens the installed command's targets, carrying each file's position with its launch.
pub fn open_targets(app: &AppHandle, targets: Vec<ide_cli::Target>) {
if targets.is_empty() {
return focus_any(app);
}
let positions = target_positions(&targets);
open_paths_at(app, targets.into_iter().map(|t| t.path).collect(), &positions);
}
fn target_positions(targets: &[ide_cli::Target]) -> Vec<FilePosition> {
targets
.iter()
.filter_map(|t| {
let line = t.line?;
if t.path.is_dir() {
return None;
}
let path = dunce::canonicalize(&t.path).unwrap_or_else(|_| t.path.clone());
Some(FilePosition {
path: path.into(),
line: line.saturating_sub(1),
column: t.column.map_or(0, |c| c.saturating_sub(1)),
})
})
.collect()
}
/// Opens the command's local diff pair in a local project window, never an auxiliary or remote window.
pub(crate) fn open_diff_files(app: &AppHandle, diff: DiffFilesLaunch) {
if let Err(error) = open_diff_files_at(app, diff, false) {
tracing::warn!("cannot open window: {}", error.message);
}
}
fn open_diff_files_at(app: &AppHandle, diff: DiffFilesLaunch, wait_for_window: bool) -> IdeResult<()> {
let local = |window: &WebviewWindow| {
auxiliary_parent(window.label()).is_none()
&& matches!(holds(app, window.label()), Holds::Nothing | Holds::Project(_) | Holds::LightEdit)
};
let target = target_window(app).filter(local).or_else(|| {
project_window_labels(app).into_iter().filter_map(|label| app.get_webview_window(&label)).find(local)
});
if let Some(window) = target {
app.state::<WindowsState>().add_diff(window.label(), diff);
let _ = app.emit_to(window.label(), LAUNCH_EVENT, ());
focus(&window);
Ok(())
} else {
let launch = Launch { diff: Some(Box::new(diff)), ..Default::default() };
open_launch_window(app, None, launch, wait_for_window)
}
}
/// An explicit project owns the requested files, including sources outside its root.
fn plan_cli_launch(launch: Launch, windows: &[(String, Holds)]) -> Vec<OpenStep> {
let files: Vec<_> = launch.files.into_iter().map(|f| f.0).collect();
let Some(project) = launch.project else {
if let Some(remote) = launch.remote {
return vec![match windows.iter().find(|(_, h)| h.is_wsl_folder(&remote)) {
Some((label, _)) => OpenStep::Focus(label.clone()),
None => match windows.iter().find(|(_, h)| *h == Holds::Nothing) {
Some((label, _)) => {
OpenStep::Launch(label.clone(), Box::new(Launch { remote: Some(remote), ..Default::default() }))
}
None => OpenStep::NewRemote(remote),
},
}];
}
return plan_open(files.into_iter().map(|f| (f, false)).collect(), windows);
};
plan_open(vec![(project.0, true)], windows)
.into_iter()
.map(|step| match step {
OpenStep::Focus(label) if !files.is_empty() => OpenStep::Files(label, files.clone()),
OpenStep::Launch(label, mut pending) => {
pending.files.extend(files.iter().cloned().map(Into::into));
OpenStep::Launch(label, pending)
}
OpenStep::New(root, _) => OpenStep::New(root, files.clone()),
step => step,
})
.collect()
}
/// Drain requests received while plugins and the first window were being initialized.
pub fn ready_for_cli(app: &AppHandle) {
if app.state::<WindowsState>().cli.lock().ready() {
drain_cli(app);
}
}
fn drain_cli(app: &AppHandle) {
let app = app.clone();
// Creating a webview must not block the event-loop thread. Finish each creation
// before routing the next request so a not-yet-visible window can be reused.
tauri::async_runtime::spawn_blocking(move || {
loop {
let request = app.state::<WindowsState>().cli.lock().next();
let Some((args, cwd)) = request else { break };
dispatch_cli(&app, args, cwd);
}
});
}
/// Next / Previous Window (⌘` / ⇧⌘` on macOS) on every OS: steps through every window, not just
/// project windows, so a diff opened in a new window can be reached. Project windows count even
/// when hidden (a background tab of a window group: [`focus`] selects it); any other window only
/// while it is shown.
pub fn cycle(app: &AppHandle, current: &str, forward: bool) {
let windows = app.webview_windows();
let order = window_cycle_order(
windows
.iter()
.filter(|(label, w)| auxiliary_parent(label).is_none() || w.is_visible().unwrap_or(false))
.map(|(label, _)| label.as_str()),
);
if let Some(w) = cycle_target(&order, current, forward).and_then(|l| windows.get(&l)) {
focus(w);
}
}
/// The order window cycling steps through: each project window followed by its auxiliary windows
/// (diff, editor, Settings, …). Built from every label it is given and drops none, so a new kind
/// of window is reachable without being listed anywhere: an auxiliary window sorts under its
/// parent's label, anything else as a project window.
pub fn window_cycle_order<'a>(labels: impl IntoIterator<Item = &'a str>) -> Vec<String> {
let mut order: Vec<String> = labels.into_iter().map(str::to_owned).collect();
order.sort_by_key(|label| {
let parent = auxiliary_parent(label);
(order_key(parent.unwrap_or(label)), parent.is_some(), order_key(label))
});
order
}
/// An auxiliary window to open over its parent.
pub struct AuxWindow {
pub kind: AuxKind,
pub title: &'static str,
pub size: (f64, f64),
pub min_size: (f64, f64),
pub launch: Launch,
/// `#rrggbb` painted until the page has loaded (the theme's frame colour).
pub background: Option<String>,
}
/// Opens an auxiliary window of `caller`'s project window, centred over it. A matching window
/// receives the new launch ([`LAUNCH_EVENT`]) and is focused.
/// Releases a [`WindowsState::begin_creating`] claim once the window exists (or failed to).
struct CreatingWindow<'a> {
state: &'a WindowsState,
label: String,
}
impl Drop for CreatingWindow<'_> {
fn drop(&mut self) {
self.state.creating.lock().remove(&self.label);
}
}
pub fn open_auxiliary_window(app: &AppHandle, caller: &WebviewWindow, aux: AuxWindow) -> IdeResult<()> {
// Asked from an auxiliary window itself: open it for that window's parent.
let parent_label = auxiliary_parent(caller.label()).unwrap_or(caller.label()).to_owned();
if aux.kind.needs_project() {
let state = app.state::<AppState>();
if state.project(&parent_label).is_err() && state.remote_dev.get(&parent_label).is_none() {
return Err(IdeError::not_found("no project is open"));
}
}
let windows = app.state::<WindowsState>();
let label = if let Some(diff) = aux.launch.diff_window.as_deref() {
format!("diff-{parent_label}-{}", diff_window_id(diff))
} else if aux.kind.multiple() {
format!("{}-{parent_label}-{}", aux.kind.name(), windows.aux_serial.fetch_add(1, Ordering::Relaxed))
} else {
format!("{}-{parent_label}", aux.kind.name())
};
// Already being built for an earlier request: that window shows and focuses itself. Claimed
// before the check below, so a request arriving as the build finishes finds the window.
let Some(_creating) = windows.begin_creating(&label) else {
if aux.launch != Launch::default() {
windows.set_launch(&label, aux.launch);
}
return Ok(());
};
if let Some(existing) = app.get_webview_window(&label) {
if aux.launch != Launch::default() {
windows.set_launch(&label, aux.launch);
let _ = app.emit_to(label.as_str(), LAUNCH_EVENT, ());
}
focus(&existing);
return Ok(());
}
let parent = app.get_webview_window(&parent_label).ok_or_else(|| IdeError::not_found("no parent window"))?;
let mut config = window_config(app)?;
config.label = label.clone();
config.title = aux.title.into();
// Frameless on Windows and Linux like project windows (tauri.<os>.conf.json): the page draws
// the title bar in the app theme instead of a system caption that follows the OS theme.
#[cfg(target_os = "macos")]
let macos_geom = aux_geometry_for(app, &windows, aux.kind, aux.size, aux.min_size);
#[cfg(target_os = "macos")]
{
(config.width, config.height) = (macos_geom.width, macos_geom.height);
}
#[cfg(not(target_os = "macos"))]
{
(config.width, config.height) = aux.size;
}
(config.min_width, config.min_height) = (Some(aux.min_size.0), Some(aux.min_size.1));
config.center = false;
config.visible = false;
config.maximized = false;
config.fullscreen = false;
config.drag_drop_enabled = false;
// Paint the theme's frame colour before the page loads.
if let Some(color) =
aux.background.as_deref().and_then(hex_color).or_else(|| default_aux_frame_color(app, &parent, &parent_label))
{
config.background_color = Some(color);
}
windows.set_launch(&label, aux.launch);
#[cfg(target_os = "linux")]
crate::linux_window_state::prepare(app, &mut config);
let builder = WebviewWindowBuilder::from_config(app, &config);
// Windows: an owned child (WS_POPUP) of a borderless parent gets no DWM
// shadow, leaving the frameless window flat against its parent.
// Ownership is tracked by the `<kind>-<parent>` label instead
// (parent-close cleanup, menu modality), so only Linux keeps the native
// parent link. AppKit child windows can disappear when dragged to another
// display's Space, so macOS keeps it too.
#[cfg(target_os = "linux")]
let builder = builder.and_then(|b| b.parent(&parent));
let window =
builder.and_then(|b| macos_webview_configuration(app, webview_profile(app, b)).build()).map_err(|e| {
windows.forget(&label);
IdeError::internal(format!("cannot create {} window: {e}", aux.kind.name()))
})?;
#[cfg(target_os = "linux")]
decorate_csd(&window);
#[cfg(target_os = "macos")]
{
suppress_window_writing_tools_affordance(&window);
crate::macos::install_webview_key_handlers(&window);
crate::page_swipe::install(&window);
}
apply_zoom(app, &window);
#[cfg(target_os = "macos")]
align_titlebar(app, &window, false);
// Fit size and position to the parent's current display before showing.
#[cfg(target_os = "macos")]
{
if let Err(e) =
fit_over_parent(&window, &parent, (macos_geom.width, macos_geom.height), aux.min_size, macos_geom.maximized)
{
tracing::warn!("cannot fit {} window: {e}", aux.kind.name());
}
track_auxiliary_geometry(&window, aux.kind, aux.size, aux.min_size);
}
#[cfg(not(target_os = "macos"))]
if let Err(e) = place_over_parent(&window, &parent) {
tracing::warn!("cannot place {} window: {e}", aux.kind.name());
}
focus(&window);
Ok(())
}
/// Effective appearance settings of project window `parent_label` (client layer on remote windows).
fn effective_appearance(app: &AppHandle, parent_label: &str) -> Option<AppearanceSettings> {
let state = app.try_state::<AppState>()?;
if state.remote_dev.get(parent_label).is_some() {
return Some(state.global_settings.effective().appearance);
}
Some(match state.host.project(parent_label) {
Ok(p) => p.settings.effective().appearance,
Err(_) => state.global_settings.effective().appearance,
})
}
fn default_aux_frame_color(app: &AppHandle, parent: &WebviewWindow, parent_label: &str) -> Option<Color> {
let state = app.try_state::<AppState>()?;
if let Some(color) = state.plugins.startup_frame().and_then(|c| hex_color(&c)) {
return Some(color);
}
let appearance = effective_appearance(app, parent_label)?;
let os_dark =
(appearance.sync_with_os || appearance.theme == "system") && parent.theme().is_ok_and(|t| t == Theme::Dark);
Some(frame_color(&appearance, os_dark))
}
/// Whether `parent_label` opens Settings in its own native window.
pub fn settings_in_separate_window(app: &AppHandle, parent_label: &str) -> bool {
effective_appearance(app, parent_label).is_none_or(|a| a.settings_in_separate_window)
}
fn menu_settings_launch<'a>(
target_label: &'a str,
has_pending_launch: bool,
separate_window: bool,
action: &str,
arg: Option<String>,
) -> Option<(&'a str, Option<String>, bool)> {
let parent_label = auxiliary_parent(target_label).unwrap_or(target_label);
if has_pending_launch || !separate_window {
return None;
}
let (page, new_projects) = match action {
"file.settings" => (arg, false),
"file.newProjectsSettings" => (Some(arg.unwrap_or_else(|| "languages.java".into())), true),
_ => return None,
};
Some((parent_label, page, new_projects))
}
/// Opens Settings directly from a native menu click when separate-window mode is enabled,
/// avoiding a round-trip through the parent window's JS thread.
pub fn open_settings_from_menu(app: &AppHandle, target: &WebviewWindow, action: &str, arg: Option<String>) -> bool {
let parent_label = auxiliary_parent(target.label()).unwrap_or(target.label());
let has_pending = app.state::<WindowsState>().pending(parent_label).is_some();
let separate = settings_in_separate_window(app, parent_label);
let Some((parent_label, page, new_projects)) =
menu_settings_launch(target.label(), has_pending, separate, action, arg)
else {
return false;
};
let Some(parent) = app.get_webview_window(parent_label) else {
return false;
};
match open_settings_window(app, &parent, page, new_projects, None) {
Ok(()) => true,
Err(e) => {
tracing::warn!("settings window unavailable from menu, falling back to frontend: {e}");
false
}
}
}
/// Opens (or focuses) the settings window of `parent` showing `page`: a
/// separate window centred over its parent, sized like IDEA's Settings.
pub fn open_settings_window(
app: &AppHandle,
parent: &WebviewWindow,
page: Option<String>,
new_projects: bool,
background: Option<String>,
) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Settings,
title: "Settings",
size: AuxKind::Settings.default_size(),
min_size: AuxKind::Settings.min_size(),
launch: Launch { settings_page: page, new_projects, ..Default::default() },
background,
},
)
}
/// Opens a window listing the files that differ between two revisions (Compare with Local and
/// friends), next to its project window like IDEA's diff windows.
pub fn open_comparison_window(
app: &AppHandle,
parent: &WebviewWindow,
comparison: ComparisonLaunch,
background: Option<String>,
) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Comparison,
title: "Compare",
size: AuxKind::Comparison.default_size(),
min_size: AuxKind::Comparison.min_size(),
launch: Launch { comparison: Some(Box::new(comparison)), ..Default::default() },
background,
},
)
}
fn diff_window_id(diff: &DiffLaunch) -> u64 {
use std::hash::{DefaultHasher, Hash, Hasher};
let mut hasher = DefaultHasher::new();
(&diff.path, &diff.left, &diff.middle, &diff.right).hash(&mut hasher);
hasher.finish()
}
/// Opens a window showing one diff of the project ("Show Diff in New Window"), like IDEA's
/// diff window next to the Commit tool window.
pub fn open_diff_window(
app: &AppHandle,
parent: &WebviewWindow,
diff: DiffLaunch,
background: Option<String>,
) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Diff,
title: "Diff",
size: AuxKind::Diff.default_size(),
min_size: AuxKind::Diff.min_size(),
launch: Launch { diff_window: Some(Box::new(diff)), ..Default::default() },
background,
},
)
}
/// Opens a lightweight editor window for `editor.path` ("Open in New Window"), sharing the
/// parent's project documents and language servers.
pub fn open_editor_window(
app: &AppHandle,
parent: &WebviewWindow,
editor: EditorLaunch,
background: Option<String>,
) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Editor,
title: "Editor",
size: AuxKind::Editor.default_size(),
min_size: AuxKind::Editor.min_size(),
launch: Launch { editor: Some(Box::new(editor)), ..Default::default() },
background,
},
)
}
/// Opens (or focuses and hands over to) the project window's Sync with Deployed window: local
/// files next to their copies on a deployment server, like IDEA's.
pub fn open_deployment_window(
app: &AppHandle,
parent: &WebviewWindow,
server: String,
paths: Vec<PathBuf>,
background: Option<String>,
) -> IdeResult<()> {
let paths = paths.into_iter().map(Into::into).collect();
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Deployment,
title: "Sync with Deployed",
size: AuxKind::Deployment.default_size(),
min_size: AuxKind::Deployment.min_size(),
launch: Launch { deployment: Some(DeploymentLaunch { server, paths }), ..Default::default() },
background,
},
)
}
/// Opens (or focuses) the New Project window of `parent`'s project window: a separate window
/// centred over it, so the wizard can be moved and sized on its own.
pub fn open_new_project_window(app: &AppHandle, parent: &WebviewWindow, background: Option<String>) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::NewProject,
title: "New Project",
size: AuxKind::NewProject.default_size(),
min_size: AuxKind::NewProject.min_size(),
launch: Launch::default(),
background,
},
)
}
/// Opens (or focuses, filling in `url`) the Get from VCS window of `parent`'s project window.
pub fn open_clone_window(
app: &AppHandle,
parent: &WebviewWindow,
url: Option<String>,
background: Option<String>,
) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Clone,
title: "Get from Version Control",
size: AuxKind::Clone.default_size(),
min_size: AuxKind::Clone.min_size(),
launch: Launch { clone_url: url.filter(|u| !u.trim().is_empty()), ..Default::default() },
background,
},
)
}
/// Opens (or focuses) the Remote Development window of `parent`'s project window.
pub fn open_connect_window(app: &AppHandle, parent: &WebviewWindow, background: Option<String>) -> IdeResult<()> {
open_auxiliary_window(
app,
parent,
AuxWindow {
kind: AuxKind::Connect,
title: "Remote Development",
size: AuxKind::Connect.default_size(),
min_size: AuxKind::Connect.min_size(),
launch: Launch::default(),
background,
},
)
}
#[cfg(any(target_os = "macos", test))]
fn resolve_aux_geometry(saved: Option<AuxGeometry>, default_size: (f64, f64), min_size: (f64, f64)) -> AuxGeometry {
match saved.filter(|g| g.width.is_finite() && g.height.is_finite() && g.width > 0.0 && g.height > 0.0) {
Some(g) => {
AuxGeometry { width: g.width.max(min_size.0), height: g.height.max(min_size.1), maximized: g.maximized }
}
None => AuxGeometry {
width: default_size.0.max(min_size.0),
height: default_size.1.max(min_size.1),
maximized: false,
},
}
}
#[cfg(target_os = "macos")]
fn aux_state_path(app: &AppHandle) -> Option<PathBuf> {
ide_host::data_dirs::config_dir(&app.config().identifier).ok().map(|dir| dir.join(AUX_STATE_FILE))
}
#[cfg(target_os = "macos")]
fn load_aux_geometry_map(path: &Path) -> HashMap<String, AuxGeometry> {
std::fs::read(path)
.ok()
.and_then(|bytes| serde_json::from_slice::<HashMap<String, AuxGeometry>>(&bytes).ok())
.unwrap_or_default()
}
#[cfg(target_os = "macos")]
fn aux_geometry_for(
app: &AppHandle,
windows: &WindowsState,
kind: AuxKind,
default_size: (f64, f64),
min_size: (f64, f64),
) -> AuxGeometry {
let mut guard = windows.aux_geometry.lock();
let map = guard.get_or_insert_with(|| aux_state_path(app).map_or_else(HashMap::new, |p| load_aux_geometry_map(&p)));
resolve_aux_geometry(map.get(kind.name()).copied(), default_size, min_size)
}
#[cfg(target_os = "macos")]
pub fn flush_auxiliary_geometry(app: &AppHandle) {
let Some(path) = aux_state_path(app) else { return };
let Some(snapshot) = app.try_state::<WindowsState>().and_then(|w| w.aux_geometry.lock().clone()) else {
return;
};
if let Ok(bytes) = serde_json::to_vec_pretty(&snapshot) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let _ = ide_vfs::write_atomic(&path, &bytes);
}
}
#[cfg(target_os = "macos")]
fn same_cocoa_frame(a: objc2_foundation::NSRect, b: objc2_foundation::NSRect) -> bool {
(a.origin.x - b.origin.x).abs() <= 2.0
&& (a.origin.y - b.origin.y).abs() <= 2.0
&& (a.size.width - b.size.width).abs() <= 2.0
&& (a.size.height - b.size.height).abs() <= 2.0
}
#[cfg(target_os = "macos")]
fn track_auxiliary_geometry(window: &WebviewWindow, kind: AuxKind, default_size: (f64, f64), min_size: (f64, f64)) {
let tracked = window.clone();
window.on_window_event(move |event| match event {
tauri::WindowEvent::Resized(_) => {
if objc2::MainThreadMarker::new().is_none() {
return;
}
let Ok(ptr) = tracked.ns_window() else { return };
let ns_win = unsafe { &*ptr.cast::<objc2_app_kit::NSWindow>() };
if !ns_win.isVisible()
|| ns_win.isMiniaturized()
|| ns_win.styleMask().contains(objc2_app_kit::NSWindowStyleMask::FullScreen)
{
return;
}
let frame = ns_win.frame();
let content = ns_win.contentRectForFrameRect(frame);
let maximized = ns_win.screen().is_some_and(|s| same_cocoa_frame(frame, s.visibleFrame()));
let Some(state) = tracked.try_state::<WindowsState>() else { return };
let mut guard = state.aux_geometry.lock();
let map = guard.get_or_insert_with(|| {
aux_state_path(tracked.app_handle()).map_or_else(HashMap::new, |p| load_aux_geometry_map(&p))
});
let entry = map.entry(kind.name().to_owned()).or_insert(AuxGeometry {
width: default_size.0.max(min_size.0),
height: default_size.1.max(min_size.1),
maximized: false,
});
entry.maximized = maximized;
if !maximized && content.size.width >= min_size.0 && content.size.height >= min_size.1 {
entry.width = content.size.width;
entry.height = content.size.height;
}
}
tauri::WindowEvent::CloseRequested { .. } | tauri::WindowEvent::Destroyed => {
let app = tracked.app_handle().clone();
let Some(path) = aux_state_path(&app) else { return };
let Some(snapshot) = app.try_state::<WindowsState>().and_then(|w| w.aux_geometry.lock().clone()) else {
return;
};
std::mem::drop(tauri::async_runtime::spawn_blocking(move || {
if let Ok(bytes) = serde_json::to_vec_pretty(&snapshot) {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let _ = ide_vfs::write_atomic(&path, &bytes);
}
}));
}
_ => {}
});
}
/// Fit outer bounds in AppKit's global bottom-left point space, centred over `parent` inside
/// `visible` (`NSScreen::visibleFrame`) and clamped to `[min_size, visible]`.
#[cfg(any(target_os = "macos", test))]
fn cocoa_fit_bounds(
parent: (f64, f64, f64, f64),
size: (f64, f64),
min_size: (f64, f64),
visible: (f64, f64, f64, f64),
) -> (f64, f64, f64, f64) {
let parent_td = (parent.0, -(parent.1 + parent.3), parent.2, parent.3);
let work_td = (visible.0, -(visible.1 + visible.3), visible.2, visible.3);
let fitted = fit_bounds(parent_td, size, min_size, work_td);
(fitted.0, -(fitted.1 + fitted.3), fitted.2, fitted.3)
}
/// Synchronously fits and centres `window` over `parent` on `parent`'s `NSScreen` in AppKit
/// logical points, avoiding `tao`'s async main-queue position/size race across mixed-DPI displays.
#[cfg(target_os = "macos")]
fn fit_over_parent(
window: &WebviewWindow,
parent: &WebviewWindow,
desired_size: (f64, f64),
min_size: (f64, f64),
maximized: bool,
) -> tauri::Result<()> {
let target = window.clone();
let owner = parent.clone();
let apply = move || {
let mtm = objc2::MainThreadMarker::new().expect("AppKit main thread");
let (Ok(win_ptr), Ok(parent_ptr)) = (target.ns_window(), owner.ns_window()) else {
return;
};
let win_ns = unsafe { &*win_ptr.cast::<objc2_app_kit::NSWindow>() };
let parent_ns = unsafe { &*parent_ptr.cast::<objc2_app_kit::NSWindow>() };
let Some(screen) = parent_ns.screen().or_else(|| objc2_app_kit::NSScreen::mainScreen(mtm)) else {
return;
};
let visible = screen.visibleFrame();
let parent_frame = parent_ns.frame();
let win_frame = win_ns.frame();
let content = win_ns.contentRectForFrameRect(win_frame);
let frame = (
(win_frame.size.width - content.size.width).max(0.0),
(win_frame.size.height - content.size.height).max(0.0),
);
let bounds = if maximized {
(visible.origin.x, visible.origin.y, visible.size.width, visible.size.height)
} else {
cocoa_fit_bounds(
(parent_frame.origin.x, parent_frame.origin.y, parent_frame.size.width, parent_frame.size.height),
(desired_size.0 + frame.0, desired_size.1 + frame.1),
(min_size.0 + frame.0, min_size.1 + frame.1),
(visible.origin.x, visible.origin.y, visible.size.width, visible.size.height),
)
};
win_ns.setContentMinSize(objc2_foundation::NSSize::new(
min_size.0.min((visible.size.width - frame.0).max(1.0)),
min_size.1.min((visible.size.height - frame.1).max(1.0)),
));
win_ns.setFrame_display(
objc2_foundation::NSRect::new(
objc2_foundation::NSPoint::new(bounds.0, bounds.1),
objc2_foundation::NSSize::new(bounds.2.max(1.0), bounds.3.max(1.0)),
),
false,
);
};
if objc2::MainThreadMarker::new().is_some() {
apply();
return Ok(());
}
let (tx, rx) = std::sync::mpsc::sync_channel(1);
window.run_on_main_thread(move || {
apply();
let _ = tx.send(());
})?;
// A stopped event loop never runs the closure: don't block this thread forever.
rx.recv_timeout(FIT_ON_MAIN_TIMEOUT).map_err(|_| tauri::Error::FailedToReceiveMessage)
}
/// A new window's size and minimum size (logical pixels) shrunk to fit `area`, the work area of
/// the monitor it opens on: the default 1440×900 is taller than a 1920×1080 laptop screen at
/// 150 % scaling once the taskbar is taken off.
#[cfg_attr(not(windows), allow(dead_code))]
fn fit_new_window(size: (f64, f64), min: (f64, f64), area: (f64, f64)) -> ((f64, f64), (f64, f64)) {
if area.0 <= 0.0 || area.1 <= 0.0 {
return (size, min);
}
let size = (size.0.min(area.0), size.1.min(area.1));
(size, (min.0.min(size.0), min.1.min(size.1)))
}
/// Centres the settings window over its owner, inside the owner's monitor work area. The window
/// may still be on another display with a different scale, so its size is converted with its own
/// scale and its position is given in physical pixels.
#[cfg(not(target_os = "macos"))]
pub(super) fn place_over_parent(window: &WebviewWindow, parent: &WebviewWindow) -> tauri::Result<()> {
// Placement setters leave maximized mode. Fit the restored frame, then reapply the saved state.
// A restored fullscreen window already covers its display and must not be fitted to the work area.
if window.is_fullscreen()? {
return Ok(());
}
let Some(monitor) = parent.current_monitor()?.or(parent.primary_monitor()?) else {
return Ok(());
};
let maximized = window.is_maximized()?;
if maximized {
window.unmaximize()?;
}
let scale = monitor.scale_factor();
let area = monitor.work_area();
let origin = area.position.to_logical::<f64>(scale);
let available = area.size.to_logical::<f64>(scale);
let pos = parent.outer_position()?.to_logical::<f64>(scale);
let size = parent.outer_size()?.to_logical::<f64>(scale);
let own_scale = window.scale_factor()?;
let own = window.outer_size()?.to_logical::<f64>(own_scale);
// On Wayland, the frame is GTK's CSD shadow margin, which tao's inner size includes.
#[cfg(target_os = "linux")]
let inner = crate::linux_window_state::content_size(&window.as_ref().window())?;
#[cfg(not(target_os = "linux"))]
let inner = window.inner_size()?;
let inner = inner.to_logical::<f64>(own_scale);
let frame = (own.width - inner.width, own.height - inner.height);
let minimum = auxiliary(window.label()).map_or(SETTINGS_MIN_SIZE, |(kind, _)| kind.min_size());
let bounds = fit_bounds(
(pos.x, pos.y, size.width, size.height),
(own.width, own.height),
(minimum.0 + frame.0, minimum.1 + frame.1),
(origin.x, origin.y, available.width, available.height),
);
window.set_position(tauri::PhysicalPosition::new(
(bounds.0 * scale).round() as i32,
(bounds.1 * scale).round() as i32,
))?;
if (bounds.2, bounds.3) != (own.width, own.height) {
// Sizes are set on the client area, and even an undecorated window has a resize frame.
let (width, height) = ((bounds.2 - frame.0).max(1.0), (bounds.3 - frame.1).max(1.0));
window.set_min_size(Some(tauri::LogicalSize::new(minimum.0.min(width), minimum.1.min(height))))?;
window.set_size(tauri::LogicalSize::new(width, height))?;
}
if maximized {
window.maximize()?;
}
Ok(())
}
fn fit_bounds(
parent: (f64, f64, f64, f64),
size: (f64, f64),
min_size: (f64, f64),
work: (f64, f64, f64, f64),
) -> (f64, f64, f64, f64) {
let width = size.0.max(min_size.0).min(work.2);
let height = size.1.max(min_size.1).min(work.3);
let at = centered(parent.0, parent.1, parent.2, parent.3, width, height);
(
work.0 + (at.0 - work.0).clamp(0.0, (work.2 - width).max(0.0)),
work.1 + (at.1 - work.1).clamp(0.0, (work.3 - height).max(0.0)),
width,
height,
)
}
/// Top-left corner that centres a `w`×`h` window over the parent rectangle
/// (never above the parent's top edge, so the title bar stays reachable).
fn centered(px: f64, py: f64, pw: f64, ph: f64, w: f64, h: f64) -> (f64, f64) {
(px + (pw - w) / 2.0, py + ((ph - h) / 2.0).max(0.0))
}
/// A link in an embedded page that asks for a new window (Flutter DevTools' documentation links)
/// opens in the system browser; the IDE itself never needs such a window.
fn open_popup_in_browser<R: tauri::Runtime>(
url: tauri::Url,
_features: tauri::webview::NewWindowFeatures,
) -> tauri::webview::NewWindowResponse<R> {
if matches!(url.scheme(), "http" | "https")
&& let Err(e) = tauri_plugin_opener::open_url(url.as_str(), None::<&str>)
{
tracing::warn!("cannot open {url}: {e}");
}
tauri::webview::NewWindowResponse::Deny
}
/// On Windows every window keeps WebView2's profile (localStorage, IndexedDB) in the local
/// data directory, so a custom location from Settings › System Settings › Caches holds it too;
/// Tauri would always use the default one. All windows share it: one WebView2 browser process
/// serves a single profile folder.
fn webview_profile<'a>(
app: &AppHandle,
builder: WebviewWindowBuilder<'a, tauri::Wry, AppHandle>,
) -> WebviewWindowBuilder<'a, tauri::Wry, AppHandle> {
#[cfg(windows)]
if let Some(state) = app.try_state::<AppState>() {
return builder.data_directory(state.host.local_data_dir.clone());
}
#[cfg(not(windows))]
let _ = app;
builder
}
/// Configure macOS `WKWebViewConfiguration` before `WKWebView` creation:
/// - Disable WebKit's `PreferPageRenderingUpdatesNear60FPSEnabled` cap so ProMotion and other
/// high-refresh displays (120Hz+) drive scrolling, `requestAnimationFrame`, and viewport updates
/// at native refresh rate instead of throttling page rendering updates to 60fps.
/// - Disable macOS 15+ Apple Intelligence Writing Tools (`writingToolsBehavior = None`) and, on
/// macOS 27 where `WKWebView` ignores `writingToolsBehavior = None` for the Siri selection
/// affordance (WebKit bug 327446), also disable `TextInputClientSelectionUpdatesEnabled` on
/// `WKPreferences` and override `allowsWritingToolsAffordance` on `WryWebView` so the floating
/// affordance never appears over Markdown previews, Quick Documentation, chat, or editors.
/// - In debug builds, optionally reproduce WKWebView's user-font restriction when
/// `OPENIDEA_DEV_DISALLOW_USER_FONTS=1` is set (absent from release builds).
#[cfg(target_os = "macos")]
fn macos_webview_configuration<'a>(
app: &AppHandle,
builder: WebviewWindowBuilder<'a, tauri::Wry, AppHandle>,
) -> WebviewWindowBuilder<'a, tauri::Wry, AppHandle> {
match build_macos_webview_configuration(app) {
Some(config) => builder.with_webview_configuration(config),
None => builder,
}
}
#[cfg(target_os = "macos")]
fn build_macos_webview_configuration(
app: &AppHandle,
) -> Option<objc2::rc::Retained<objc2_web_kit::WKWebViewConfiguration>> {
build_macos_webview_configuration_with(|f| app.run_on_main_thread(f))
}
#[cfg(target_os = "macos")]
fn build_macos_webview_configuration_with(
run_on_main_thread: impl FnOnce(Box<dyn FnOnce() + Send>) -> tauri::Result<()>,
) -> Option<objc2::rc::Retained<objc2_web_kit::WKWebViewConfiguration>> {
const ON_MAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
struct SendConfig(objc2::rc::Retained<objc2_web_kit::WKWebViewConfiguration>);
// SAFETY: Created and configured exclusively on the main thread, then moved untouched into
// `WebviewWindowBuilder::with_webview_configuration`, which transfers it back to the main
// thread when Wry constructs the WKWebView.
unsafe impl Send for SendConfig {}
let make = |mtm: objc2::MainThreadMarker| {
let config = unsafe { objc2_web_kit::WKWebViewConfiguration::new(mtm) };
configure_macos_webview_configuration(&config).then_some(SendConfig(config))
};
if let Some(mtm) = objc2::MainThreadMarker::new() {
return make(mtm).map(|c| c.0);
}
// Secondary project windows (`spawn_window`) and auxiliary windows (`open_auxiliary_window`)
// build off the main thread; create the configuration on the AppKit main thread and fall back
// to the default builder if the event loop cannot service it.
let (tx, rx) = std::sync::mpsc::sync_channel(1);
if let Err(err) = run_on_main_thread(Box::new(move || {
let result = objc2::MainThreadMarker::new().and_then(make);
let _ = tx.send(result);
})) {
tracing::warn!("could not schedule macOS WKWebViewConfiguration on main thread: {err}");
return None;
}
match rx.recv_timeout(ON_MAIN_TIMEOUT) {
Ok(Some(config)) => Some(config.0),
Ok(None) => None,
Err(err) => {
tracing::warn!("timed out waiting for macOS WKWebViewConfiguration on main thread: {err}");
None
}
}
}
#[cfg(target_os = "macos")]
fn configure_macos_webview_configuration(config: &objc2_web_kit::WKWebViewConfiguration) -> bool {
use objc2::{msg_send, sel};
use objc2_app_kit::NSWritingToolsBehavior;
use objc2_foundation::NSObjectProtocol;
let mut configured = false;
let preferences = unsafe { config.preferences() };
if disable_macos_60fps_rendering_cap(&preferences) {
configured = true;
}
if config.respondsToSelector(sel!(setWritingToolsBehavior:)) {
unsafe {
let _: () = msg_send![config, setWritingToolsBehavior: NSWritingToolsBehavior::None];
}
disable_macos_text_input_selection_updates(&preferences);
if let Some(wry_cls) = objc2::runtime::AnyClass::get(c"WryWebView") {
suppress_webview_class_writing_tools_affordance(wry_cls);
}
configured = true;
}
#[cfg(debug_assertions)]
if std::env::var("OPENIDEA_DEV_DISALLOW_USER_FONTS").as_deref() == Ok("1") {
if preferences.respondsToSelector(sel!(_setShouldAllowUserInstalledFonts:)) {
unsafe {
let _: () = msg_send![&preferences, _setShouldAllowUserInstalledFonts: false];
}
let allowed: bool = unsafe { msg_send![&preferences, _shouldAllowUserInstalledFonts] };
tracing::info!(allowed, "Development user-installed fonts override");
configured = true;
} else {
tracing::warn!("WKPreferences user-font development selector unavailable");
}
}
configured
}
/// Toggle a private WebKit preference feature by key. Checks `_features` / `_setEnabled:forFeature:`
/// (macOS 13.3+ / Safari 16.4+) first and falls back to `_experimentalFeatures` /
/// `_setEnabled:forExperimentalFeature:` for earlier ProMotion macOS versions (12.0–13.2).
#[cfg(target_os = "macos")]
fn set_macos_webkit_feature(
preferences: &objc2_web_kit::WKPreferences,
feature_key: &objc2_foundation::NSString,
enabled: bool,
) -> bool {
use objc2::{ClassType, msg_send, rc::Retained, runtime::AnyObject, sel};
use objc2_foundation::{NSArray, NSObjectProtocol, NSString};
use objc2_web_kit::WKPreferences;
fn find_feature(features: &NSArray<AnyObject>, target: &NSString) -> Option<Retained<AnyObject>> {
for i in 0..features.count() {
let feature = features.objectAtIndex(i);
let has_key: bool = unsafe { msg_send![&*feature, respondsToSelector: sel!(key)] };
if !has_key {
continue;
}
let key: Option<Retained<NSString>> = unsafe { msg_send![&*feature, key] };
if key.as_deref() == Some(target) {
return Some(feature);
}
}
None
}
let cls = WKPreferences::class();
if preferences.respondsToSelector(sel!(_setEnabled:forFeature:)) {
let responds: bool = unsafe { msg_send![cls, respondsToSelector: sel!(_features)] };
if responds {
let features: Option<Retained<NSArray<AnyObject>>> = unsafe { msg_send![cls, _features] };
if let Some(features) = features
&& let Some(feature) = find_feature(&features, feature_key)
{
unsafe {
let _: () = msg_send![preferences, _setEnabled: enabled, forFeature: &*feature];
}
return true;
}
}
}
if preferences.respondsToSelector(sel!(_setEnabled:forExperimentalFeature:)) {
let responds: bool = unsafe { msg_send![cls, respondsToSelector: sel!(_experimentalFeatures)] };
if responds {
let features: Option<Retained<NSArray<AnyObject>>> = unsafe { msg_send![cls, _experimentalFeatures] };
if let Some(features) = features
&& let Some(feature) = find_feature(&features, feature_key)
{
unsafe {
let _: () = msg_send![preferences, _setEnabled: enabled, forExperimentalFeature: &*feature];
}
return true;
}
}
}
false
}
/// Disable WebKit's default 60fps page rendering update cap (`PreferPageRenderingUpdatesNear60FPSEnabled`)
/// so ProMotion and high-refresh displays run scrolling and viewport updates at 120Hz+.
#[cfg(target_os = "macos")]
fn disable_macos_60fps_rendering_cap(preferences: &objc2_web_kit::WKPreferences) -> bool {
set_macos_webkit_feature(
preferences,
objc2_foundation::ns_string!("PreferPageRenderingUpdatesNear60FPSEnabled"),
false,
)
}
/// Work around WebKit bug 327446 on macOS 27: `WebViewImpl::selectionDidChange` sends selection
/// updates to `NSTextInputContext` whenever `TextInputClientSelectionUpdatesEnabled` is true
/// without checking `writingToolsBehavior == None`, triggering the Siri selection affordance.
/// Turning this embedder feature off keeps `inputContext()` active for editable content (IME
/// composition, dictation) while skipping non-editable selection notifications.
#[cfg(target_os = "macos")]
fn disable_macos_text_input_selection_updates(preferences: &objc2_web_kit::WKPreferences) -> bool {
set_macos_webkit_feature(preferences, objc2_foundation::ns_string!("TextInputClientSelectionUpdatesEnabled"), false)
}
/// Work around WebKit bug 327446 on macOS 27: `-[WKWebView allowsWritingToolsAffordance]` ignores
/// `writingToolsBehavior == None` and returns `YES` for any ranged or editable selection. Override
/// `allowsWritingToolsAffordance` on the `WKWebView` subclass (`WryWebView`) so AppKit always
/// suppresses the affordance even if `NSTextInputContext` queries the view directly.
#[cfg(target_os = "macos")]
fn suppress_window_writing_tools_affordance(window: &WebviewWindow) {
let _ = crate::macos::with_web_view(window, |webview| unsafe {
let ptr = webview.cast::<objc2::runtime::AnyObject>();
if !ptr.is_null() {
let cls = (*ptr).class();
suppress_webview_class_writing_tools_affordance(cls);
}
});
}
#[cfg(target_os = "macos")]
fn suppress_webview_class_writing_tools_affordance(webview_cls: &objc2::runtime::AnyClass) -> bool {
use objc2::{
runtime::{AnyClass, AnyObject, Bool, Imp, Sel},
sel,
};
type AllowsAffordance = unsafe extern "C-unwind" fn(*mut AnyObject, Sel) -> Bool;
unsafe extern "C-unwind" fn disallow_writing_tools_affordance(_this: *mut AnyObject, _cmd: Sel) -> Bool {
Bool::NO
}
// Only patch subclasses of WKWebView (such as WryWebView), never WKWebView itself.
let Some(wk_cls) = AnyClass::get(c"WKWebView") else {
return false;
};
if std::ptr::eq(webview_cls, wk_cls) {
return false;
}
let Some(super_cls) = webview_cls.superclass() else {
return false;
};
let selector = sel!(allowsWritingToolsAffordance);
if !super_cls.responds_to(selector) {
return false;
}
unsafe {
let imp: Imp = std::mem::transmute(disallow_writing_tools_affordance as AllowsAffordance);
if webview_cls.instance_method(selector).is_some_and(|m| std::ptr::fn_addr_eq(m.implementation(), imp)) {
return true;
}
let cls = (webview_cls as *const AnyClass).cast_mut();
let added = objc2::ffi::class_addMethod(cls, selector, imp, c"B@:".as_ptr());
if !added.as_bool() {
objc2::ffi::class_replaceMethod(cls, selector, imp, c"B@:".as_ptr());
}
}
true
}
#[cfg(not(target_os = "macos"))]
fn macos_webview_configuration<'a>(
_app: &AppHandle,
builder: WebviewWindowBuilder<'a, tauri::Wry, AppHandle>,
) -> WebviewWindowBuilder<'a, tauri::Wry, AppHandle> {
builder
}
#[cfg(test)]
mod tests {
#[test]
fn a_window_being_built_is_claimed_once_until_it_exists() {
let state = WindowsState::default();
let first = state.begin_creating("settings-main").expect("first request builds");
assert!(state.begin_creating("settings-main").is_none(), "a second request waits for the first");
assert!(state.begin_creating("settings-other").is_some(), "other windows are independent");
drop(first);
assert!(state.begin_creating("settings-main").is_some());
}
use super::*;
#[test]
fn diff_window_identity_keeps_revisions_distinct_and_ignores_reveal_line() {
let mut diff = DiffLaunch {
path: "/p/a.txt".into(),
left: DiffSide::Rev { rev: "HEAD".into(), path: None, label: None },
middle: None,
right: DiffSide::Worktree { label: None, label_detail: None },
line: None,
line_side: None,
col: None,
chain: None,
};
let id = diff_window_id(&diff);
diff.line = Some(12);
assert_eq!(diff_window_id(&diff), id);
diff.middle = Some(DiffSide::Rev { rev: ":0".into(), path: None, label: None });
assert_ne!(diff_window_id(&diff), id);
diff.middle = None;
diff.right = DiffSide::Rev { rev: ":0".into(), path: None, label: None };
assert_ne!(diff_window_id(&diff), id);
diff.path = "/p/b.txt".into();
assert_ne!(diff_window_id(&diff), id);
}
#[test]
fn diff_window_keeps_file_and_selection_label_details() {
for side in [
serde_json::json!({"type": "worktree", "label": "file.rs", "labelDetail": "src"}),
serde_json::json!({"type": "text", "text": "let x = 1;", "label": "Selection from file.rs", "labelDetail": ""}),
serde_json::json!({"type": "text", "text": "let x = 2;", "label": "From Clipboard"}),
] {
let parsed = serde_json::from_value::<DiffSide>(side.clone()).unwrap();
assert_eq!(serde_json::to_value(parsed).unwrap(), side);
}
}
#[test]
fn a_project_window_starts_with_the_header_its_page_draws() {
let mut a = AppearanceSettings::default();
assert_eq!(initial_titlebar_height(&a, true), 40.0);
a.compact_mode = true;
assert_eq!(initial_titlebar_height(&a, true), 32.0);
a.show_main_toolbar = false;
assert_eq!(initial_titlebar_height(&a, true), 30.0);
// Welcome, Settings and a window whose project comes later keep the full bar.
assert_eq!(initial_titlebar_height(&a, false), 32.0);
a.zoom = 150;
assert_eq!(initial_titlebar_height(&a, true), 45.0);
}
fn s(v: &[&str]) -> Vec<String> {
v.iter().map(|x| x.to_string()).collect()
}
#[test]
fn frame_color_follows_theme_and_os_mode() {
let mut a = AppearanceSettings::default();
assert_eq!(frame_color(&a, true), Color(0xe9, 0xea, 0xee, 0xff));
a.theme = "dark-hc".into();
assert_eq!(frame_color(&a, false), Color(0x1a, 0x1b, 0x1e, 0xff));
a.sync_with_os = true;
assert_eq!(frame_color(&a, false), Color(0xe9, 0xea, 0xee, 0xff));
assert_eq!(frame_color(&a, true), Color(0x26, 0x28, 0x2c, 0xff));
}
#[test]
fn additional_palettes_keep_native_startup_and_os_preferences_in_sync() {
for (id, color) in
[("solarized-light", Color(0xee, 0xe8, 0xd5, 0xff)), ("solarized-dark", Color(0x07, 0x36, 0x42, 0xff))]
{
let mut a = AppearanceSettings { theme: id.into(), ..Default::default() };
assert_eq!(frame_color(&a, false), color);
assert_eq!(frame_color(&a, true), color);
a.sync_with_os = true;
a.preferred_light_theme = id.into();
a.preferred_dark_theme = id.into();
assert_eq!(frame_color(&a, false), color);
assert_eq!(frame_color(&a, true), color);
}
}
#[cfg(target_os = "macos")]
#[test]
fn macos_webview_configuration_disables_writing_tools() {
use objc2::{ClassType, MainThreadMarker, msg_send, rc::Retained, runtime::AnyObject, sel};
use objc2_app_kit::NSWritingToolsBehavior;
use objc2_foundation::{NSArray, NSObjectProtocol, NSString, ns_string};
use objc2_web_kit::WKPreferences;
let Some(mtm) = MainThreadMarker::new() else {
return;
};
let config = unsafe { objc2_web_kit::WKWebViewConfiguration::new(mtm) };
let configured = configure_macos_webview_configuration(&config);
if config.respondsToSelector(sel!(writingToolsBehavior)) {
assert!(configured);
let behavior: NSWritingToolsBehavior = unsafe { msg_send![&config, writingToolsBehavior] };
assert_eq!(behavior, NSWritingToolsBehavior::None);
// If WebKit exposes the `TextInputClientSelectionUpdatesEnabled` embedder feature
// (macOS 27+), `configure_macos_webview_configuration` must turn it off.
let preferences = unsafe { config.preferences() };
let cls = WKPreferences::class();
let has_features: bool = unsafe { msg_send![cls, respondsToSelector: sel!(_features)] };
if has_features && preferences.respondsToSelector(sel!(_isEnabledForFeature:)) {
let features: Option<Retained<NSArray<AnyObject>>> = unsafe { msg_send![cls, _features] };
let target = ns_string!("TextInputClientSelectionUpdatesEnabled");
if let Some(features) = features {
for i in 0..features.count() {
let feature = features.objectAtIndex(i);
let has_key: bool = unsafe { msg_send![&*feature, respondsToSelector: sel!(key)] };
if !has_key {
continue;
}
let key: Option<Retained<NSString>> = unsafe { msg_send![&*feature, key] };
if key.as_deref() == Some(target) {
let enabled: bool = unsafe { msg_send![&preferences, _isEnabledForFeature: &*feature] };
assert!(!enabled, "TextInputClientSelectionUpdatesEnabled must be disabled");
}
}
}
}
// If `WKWebView` implements `allowsWritingToolsAffordance`, overriding a `WKWebView`
// subclass (`WryWebView`) must replace `allowsWritingToolsAffordance` with `NO` while
// leaving `WKWebView` itself untouched.
if let Some(wk_cls) = objc2::runtime::AnyClass::get(c"WKWebView")
&& wk_cls.responds_to(sel!(allowsWritingToolsAffordance))
{
assert!(
!suppress_webview_class_writing_tools_affordance(wk_cls),
"base WKWebView class must not be modified directly"
);
let sub_cls = objc2::runtime::ClassBuilder::new(c"OpenIdeaTestWebView", wk_cls)
.map(|b| b.register())
.unwrap_or_else(|| {
objc2::runtime::AnyClass::get(c"OpenIdeaTestWebView").expect("OpenIdeaTestWebView should exist")
});
assert!(suppress_webview_class_writing_tools_affordance(sub_cls));
assert!(suppress_webview_class_writing_tools_affordance(sub_cls));
let method = sub_cls
.instance_method(sel!(allowsWritingToolsAffordance))
.expect("subclass should resolve allowsWritingToolsAffordance");
let super_method = wk_cls
.instance_method(sel!(allowsWritingToolsAffordance))
.expect("WKWebView should resolve allowsWritingToolsAffordance");
assert!(
!std::ptr::fn_addr_eq(method.implementation(), super_method.implementation()),
"subclass must override WKWebView's allowsWritingToolsAffordance"
);
type AllowsAffordance =
unsafe extern "C-unwind" fn(*const AnyObject, objc2::runtime::Sel) -> objc2::runtime::Bool;
let imp: AllowsAffordance = unsafe { std::mem::transmute(method.implementation()) };
let allowed = unsafe { imp(std::ptr::null(), sel!(allowsWritingToolsAffordance)) };
assert!(!allowed.as_bool());
}
}
}
#[cfg(target_os = "macos")]
#[test]
fn macos_webview_configuration_disables_60fps_rendering_cap() {
use objc2::{ClassType, MainThreadMarker, msg_send, rc::Retained, runtime::AnyObject, sel};
use objc2_foundation::{NSArray, NSObjectProtocol, NSString, ns_string};
use objc2_web_kit::WKPreferences;
let Some(mtm) = MainThreadMarker::new() else {
return;
};
let config = unsafe { objc2_web_kit::WKWebViewConfiguration::new(mtm) };
let configured = configure_macos_webview_configuration(&config);
let preferences = unsafe { config.preferences() };
let cls = WKPreferences::class();
let target = ns_string!("PreferPageRenderingUpdatesNear60FPSEnabled");
let find_key = |features: &NSArray<AnyObject>| -> Option<Retained<AnyObject>> {
for i in 0..features.count() {
let feature = features.objectAtIndex(i);
let has_key: bool = unsafe { msg_send![&*feature, respondsToSelector: sel!(key)] };
if !has_key {
continue;
}
let key: Option<Retained<NSString>> = unsafe { msg_send![&*feature, key] };
if key.as_deref() == Some(target) {
return Some(feature);
}
}
None
};
let has_features: bool = unsafe { msg_send![cls, respondsToSelector: sel!(_features)] };
if has_features && preferences.respondsToSelector(sel!(_isEnabledForFeature:)) {
let features: Option<Retained<NSArray<AnyObject>>> = unsafe { msg_send![cls, _features] };
if let Some(features) = features
&& let Some(feature) = find_key(&features)
{
assert!(configured);
let enabled: bool = unsafe { msg_send![&preferences, _isEnabledForFeature: &*feature] };
assert!(!enabled, "PreferPageRenderingUpdatesNear60FPSEnabled must be disabled");
return;
}
}
let has_experimental: bool = unsafe { msg_send![cls, respondsToSelector: sel!(_experimentalFeatures)] };
if has_experimental && preferences.respondsToSelector(sel!(_isEnabledForExperimentalFeature:)) {
let features: Option<Retained<NSArray<AnyObject>>> = unsafe { msg_send![cls, _experimentalFeatures] };
if let Some(features) = features
&& let Some(feature) = find_key(&features)
{
assert!(configured);
let enabled: bool = unsafe { msg_send![&preferences, _isEnabledForExperimentalFeature: &*feature] };
assert!(!enabled, "PreferPageRenderingUpdatesNear60FPSEnabled must be disabled");
}
}
}
#[cfg(target_os = "macos")]
#[test]
fn macos_webview_configuration_handles_non_main_thread() {
use objc2::MainThreadMarker;
// Off-thread scheduling failure falls back to None instead of panicking.
let result = std::thread::spawn(|| {
assert!(MainThreadMarker::new().is_none());
build_macos_webview_configuration_with(|_| Err(tauri::Error::FailedToReceiveMessage)).is_none()
})
.join()
.expect("worker thread must not panic");
assert!(result);
// Off-thread callback without a main-thread marker falls back to None instead of panicking.
let result = std::thread::spawn(|| {
assert!(MainThreadMarker::new().is_none());
build_macos_webview_configuration_with(|f| {
f();
Ok(())
})
.is_none()
})
.join()
.expect("worker thread must not panic");
assert!(result);
// When a main thread is available (e.g. nextest or --test-threads=1), dispatching from a
// worker thread constructs and configures WKWebViewConfiguration on the main thread.
if let Some(mtm) = MainThreadMarker::new() {
let probe = unsafe { objc2_web_kit::WKWebViewConfiguration::new(mtm) };
let expects_config = configure_macos_webview_configuration(&probe);
let (work_tx, work_rx) = std::sync::mpsc::sync_channel::<Box<dyn FnOnce() + Send>>(1);
let worker = std::thread::spawn(move || {
assert!(MainThreadMarker::new().is_none());
build_macos_webview_configuration_with(move |f| {
work_tx.send(f).map_err(|_| tauri::Error::FailedToReceiveMessage)
})
.is_some()
});
let work = work_rx.recv().expect("worker should schedule onto main thread");
work();
let got_config = worker.join().expect("worker thread must not panic");
if expects_config {
assert!(got_config);
}
}
}
#[test]
fn frame_colours_from_the_ui_parse_as_hex() {
for (hex, expected) in [
("#eaeaeb", Color(0xea, 0xea, 0xeb, 0xff)),
("#21252b", Color(0x21, 0x25, 0x2b, 0xff)),
("#8040bfcc", Color(0x80, 0x40, 0xbf, 0xcc)),
("#fff", Color(0xff, 0xff, 0xff, 0xff)),
] {
assert_eq!(hex_color(hex), Some(expected), "{hex}");
}
assert_eq!(hex_color("hsl(220, 13%, 15%)"), None);
}
#[test]
fn labels_sort_main_first_then_numerically() {
assert_eq!(
sorted_labels(["project-10", "project-2", "main", "x"]),
s(&["main", "project-2", "project-10", "x"])
);
}
#[test]
fn next_label_fills_gaps() {
assert_eq!(next_label(["main"]), "project-1");
assert_eq!(next_label(["main", "project-1", "project-3"]), "project-2");
}
/// A routed action that the menu doesn't have (renamed, removed) would silently fall back to
/// the auxiliary window's page, where it does nothing.
#[test]
fn actions_routed_to_the_project_window_are_menu_items() {
let mac = crate::menu::action_ids(&crate::menu::spec(crate::menu::Os::Mac));
for action in PARENT_ACTIONS.iter().chain(&APP_ACTIONS) {
assert!(
mac.contains(action) || *action == crate::menu::OPEN_RECENT_ACTION,
"{action} is not in the macOS menu"
);
}
}
#[test]
fn key_equivalents_run_only_items_outside_the_page() {
for (label, action, runs) in [
// The diff window's page has no About: its key goes to the project window.
("diff-main-1", "app.about", true),
("diff-main-1", "file.settings", true),
("settings-main", "help.showLog", true),
("diff-main-1", "window.next", true),
("main", "window.previous", false),
// The keymap already declined these: the page runs them, or nothing does.
("main", "app.about", false),
("diff-main-1", "window.toggleFullScreen", false),
("diff-main-1", "find.inFile", false),
("settings-main", "find.inFile", false),
("comparison-main-0", "editor.closeTab", false),
] {
assert_eq!(key_equivalent_runs(label, action), runs, "{label} {action}");
}
}
#[test]
fn menu_enablement_comes_from_the_destination_window() {
let mut by_window = std::collections::HashMap::new();
by_window.insert("main".to_owned(), std::collections::HashSet::from(["workspace.manage".to_owned()]));
by_window.insert(
"diff-main-1".to_owned(),
std::collections::HashSet::from([
"workspace.manage".to_owned(),
"app.about".to_owned(),
"window.next".to_owned(),
]),
);
assert!(effective_menu_disabled("diff-main-1", &by_window).contains("workspace.manage"));
by_window.get_mut("main").unwrap().remove("workspace.manage");
let effective = effective_menu_disabled("diff-main-1", &by_window);
assert!(!effective.contains("workspace.manage"));
assert!(!effective.contains("window.next"));
}
#[test]
fn window_cycling_reaches_auxiliary_windows() {
let order = window_cycle_order(["project-1", "diff-main-42", "settings-main", "main", "editor-project-1-3"]);
assert_eq!(order, s(&["main", "diff-main-42", "settings-main", "project-1", "editor-project-1-3"]));
// One project window and its diff window: Next Window goes back and forth between them.
let order = window_cycle_order(["main", "diff-main-42"]);
assert_eq!(cycle_target(&order, "main", true).as_deref(), Some("diff-main-42"));
assert_eq!(cycle_target(&order, "diff-main-42", true).as_deref(), Some("main"));
assert_eq!(cycle_target(&order, "diff-main-42", false).as_deref(), Some("main"));
}
/// The diff window was once unreachable because cycling listed project windows only. Every
/// label stays in the order, whatever its kind, so a new kind of window can't be skipped.
#[test]
fn window_cycling_drops_no_window() {
let mut labels = vec!["main", "project-2", "some-future-window", "diff-project-9-7"];
labels.extend(AuxKind::ALL.map(|kind| kind.name()));
let names: Vec<String> = AuxKind::ALL.iter().map(|k| format!("{}-main-1", k.name())).collect();
labels.extend(names.iter().map(String::as_str));
let mut order = window_cycle_order(labels.iter().copied());
order.sort();
let mut expected = s(&labels);
expected.sort();
assert_eq!(order, expected);
}
#[test]
fn cycling_wraps_both_ways() {
let w = s(&["main", "project-1", "project-2"]);
assert_eq!(cycle_target(&w, "main", true).as_deref(), Some("project-1"));
assert_eq!(cycle_target(&w, "project-2", true).as_deref(), Some("main"));
assert_eq!(cycle_target(&w, "main", false).as_deref(), Some("project-2"));
assert_eq!(cycle_target(&s(&["main"]), "main", true), None);
}
#[test]
fn cli_path_skips_flags_and_resolves_relative() {
let cwd = Path::new("/work");
let args = s(&["openidea", "--verbose", "proj"]);
assert_eq!(cli_path(&args, cwd), Some(PathBuf::from("/work/proj")));
assert_eq!(cli_path(&s(&["openidea", "/abs/p"]), cwd), Some(PathBuf::from("/abs/p")));
assert_eq!(cli_path(&s(&["openidea"]), cwd), None);
assert_eq!(cli_path(&s(&["openidea", "-psn_0_123"]), cwd), None);
// A flag's value is not the path.
let args = s(&["openidea", "--line", "120", "--column", "8", "src/Main.java"]);
assert_eq!(cli_path(&args, cwd), Some(PathBuf::from("/work/src/Main.java")));
assert_eq!(
cli_path(&s(&["openidea", "--line=3", "a b/文件.rs"]), cwd),
Some(PathBuf::from("/work/a b/文件.rs"))
);
}
#[test]
fn cli_launch_keeps_diff_pairs_and_single_file_fallback() {
let cwd = Path::new("/work");
for args in
[vec!["openidea", "diff", "a.txt", "b.txt"], vec!["openidea", "diff", "--line", "3", "a.txt", "b.txt"]]
{
let launch = cli_launch(&s(&args), cwd).unwrap().unwrap();
assert_eq!(
launch.diff,
Some(Box::new(DiffFilesLaunch { left: "/work/a.txt".into(), right: "/work/b.txt".into() }))
);
assert!(launch.files.is_empty());
}
let launch = cli_launch(&s(&["openidea", "diff", "/abs/my a.txt", "my b.txt"]), cwd).unwrap().unwrap();
assert_eq!(
launch.diff,
Some(Box::new(DiffFilesLaunch { left: "/abs/my a.txt".into(), right: "/work/my b.txt".into() }))
);
let launch = cli_launch(&s(&["openidea", "diff", "a.txt", "--line=3"]), cwd).unwrap().unwrap();
assert_eq!(launch.files, [ide_core::IpcPath::from("/work/a.txt")]);
assert_eq!(launch.positions, [FilePosition { path: "/work/a.txt".into(), line: 2, column: 0 }]);
assert!(launch.diff.is_none());
assert_eq!(cli_launch(&s(&["openidea", "diff"]), cwd).unwrap(), None);
assert_eq!(cli_path(&s(&["openidea", "proj", "diff", "a.txt"]), cwd), Some(PathBuf::from("/work/proj")));
}
#[test]
fn installed_command_positions_follow_canonical_file_paths() {
let temp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(temp.path()).unwrap();
let first = root.join("first.cs");
let second = root.join("second.cs");
std::fs::write(&first, "").unwrap();
std::fs::write(&second, "").unwrap();
let targets = vec![
ide_cli::Target { path: root.join("./first.cs"), line: Some(12), column: Some(5) },
ide_cli::Target { path: second.clone(), line: Some(3), column: None },
ide_cli::Target { path: root, line: Some(9), column: Some(2) },
ide_cli::Target { path: first.clone(), line: None, column: None },
];
let positions = target_positions(&targets);
assert_eq!(
positions,
[
FilePosition { path: first.clone().into(), line: 11, column: 4 },
FilePosition { path: second.clone().into(), line: 2, column: 0 },
]
);
let state = WindowsState::default();
state.add_files_at("main", vec![first], &positions);
state.add_files_at("project-1", vec![second], &positions);
assert_eq!(state.take_launch("main").unwrap().positions, positions[..1]);
assert_eq!(state.take_launch("project-1").unwrap().positions, positions[1..]);
}
#[test]
fn diff_and_file_requests_keep_each_others_pending_data() {
let state = WindowsState::default();
let diff = DiffFilesLaunch { left: "/a.txt".into(), right: "/b.txt".into() };
let position = FilePosition { path: "/project/a.cs".into(), line: 8, column: 2 };
for diff_first in [true, false] {
state.set_launch("main", path_launch_of(Some(p("/project")), vec![]));
if diff_first {
state.add_diff("main", diff.clone());
}
state.add_files_at("main", vec![p("/project/a.cs")], std::slice::from_ref(&position));
if !diff_first {
state.add_diff("main", diff.clone());
}
let launch = state.take_launch("main").unwrap();
assert_eq!(launch.project, Some("/project".into()));
assert_eq!(launch.files, [ide_core::IpcPath::from("/project/a.cs")]);
assert_eq!(launch.positions, std::slice::from_ref(&position));
assert_eq!(launch.diff, Some(Box::new(diff.clone())));
assert_eq!(launch.diff_before_files, diff_first);
state.complete_launch("main");
}
state.add_diff("main", diff.clone());
state.add_files_at("main", vec![], &[]);
assert!(!state.pending("main").unwrap().diff_before_files);
state.add_files_at("main", vec![p("/project/a.cs")], &[]);
assert!(state.pending("main").unwrap().diff_before_files);
state.add_diff("main", diff);
assert!(!state.take_launch("main").unwrap().diff_before_files);
}
#[test]
fn a_launch_carries_the_positions_of_its_files() {
let st = WindowsState::default();
let file = std::env::current_exe().unwrap();
let other = std::env::temp_dir().join("openidea-not-opened.txt");
st.add_files_at(
"main",
vec![file.clone()],
&[
FilePosition { path: file.clone().into(), line: 11, column: 4 },
FilePosition { path: other.into(), line: 1, column: 0 },
],
);
let launch = st.take_launch("main").unwrap();
assert_eq!(launch.positions, vec![FilePosition { path: file.into(), line: 11, column: 4 }]);
// Taken once: the next launch of that file opens it where it was.
st.add_files_at("main", vec![std::env::current_exe().unwrap()], &[]);
assert!(st.take_launch("main").unwrap().positions.is_empty());
}
#[test]
fn later_positions_replace_an_unconsumed_cold_launch_position() {
let st = WindowsState::default();
let file = std::env::current_exe().unwrap();
st.set_launch(
"main",
Launch {
files: vec![file.clone().into()],
positions: vec![FilePosition { path: file.clone().into(), line: 1, column: 0 }],
..Default::default()
},
);
st.add_files_at("main", vec![file.clone()], &[FilePosition { path: file.clone().into(), line: 9, column: 3 }]);
let launch = st.take_launch("main").unwrap();
assert_eq!(launch.positions, vec![FilePosition { path: file.into(), line: 9, column: 3 }]);
}
#[test]
fn windows_keep_independent_positions_for_the_same_external_file() {
let st = WindowsState::default();
let file = std::env::temp_dir().join("shared-package/Type.cs");
let first = FilePosition { path: file.clone().into(), line: 11, column: 4 };
let second = FilePosition { path: file.clone().into(), line: 33, column: 8 };
st.set_launch(
"main",
Launch {
project: Some("/project-a".into()),
files: vec![file.clone().into()],
positions: vec![first.clone()],
..Default::default()
},
);
st.set_launch("project-1", Launch { project: Some("/project-b".into()), ..Default::default() });
st.add_files_at("project-1", vec![file.clone()], std::slice::from_ref(&second));
let launch = st.take_launch("main").unwrap();
assert_eq!(launch.project, Some("/project-a".into()));
assert_eq!(launch.positions, vec![first]);
let launch = st.take_launch("project-1").unwrap();
assert_eq!(launch.project, Some("/project-b".into()));
assert_eq!(launch.positions, vec![second]);
st.add_files_at("main", vec![file], &[]);
assert!(st.take_launch("main").unwrap().positions.is_empty());
}
#[test]
fn external_open_has_an_explicit_project_and_validates_paths() {
let temp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(temp.path()).unwrap();
let project = root.join("Game with spaces");
std::fs::create_dir_all(project.join("Assets")).unwrap();
let file = project.join("Assets/角色.cs");
std::fs::write(&file, "class Player {}\n").unwrap();
let args = s(&["openidea", project.to_str().unwrap(), "--file", "Assets/角色.cs", "--line=12", "--column=5"]);
let launch = cli_launch(&args, &root).unwrap().unwrap();
assert_eq!(launch.project, Some(project.clone().into()));
assert_eq!(launch.files, [ide_core::IpcPath::from(file.clone())]);
assert_eq!(launch.positions, [FilePosition { path: file.into(), line: 11, column: 4 }]);
assert!(cli_launch(&s(&["openidea", "--file", project.to_str().unwrap()]), &root).is_err());
assert!(cli_launch(&s(&["openidea", "--file", "missing.cs"]), &root).is_err());
assert!(cli_launch(&s(&["openidea", "--file", "--line", "2"]), &root).is_err());
}
#[test]
fn explicit_project_routes_external_files_to_the_requested_window() {
let project = PathBuf::from("/game");
let file = PathBuf::from("/package-cache/Package.cs");
let launch = || path_launch_of(Some(project.clone()), vec![file.clone()]);
let windows = vec![
("main".into(), Holds::Project(PathBuf::from("/package-cache"))),
("project-1".into(), Holds::Project(project.clone())),
];
assert_eq!(plan_cli_launch(launch(), &windows), vec![OpenStep::Files("project-1".into(), vec![file.clone()])]);
assert_eq!(plan_cli_launch(launch(), &[]), vec![OpenStep::New(Some(project.clone()), vec![file.clone()])]);
assert_eq!(
plan_cli_launch(launch(), &[("main".into(), Holds::Nothing)]),
vec![OpenStep::Launch("main".into(), Box::new(launch()))]
);
}
#[test]
fn command_line_folders_open_as_projects_and_files_in_light_edit() {
let dir = std::env::temp_dir();
assert_eq!(path_launch(dir.clone()).project, Some(dir.into()));
let file = std::env::current_exe().unwrap();
let launch = path_launch(file.clone());
assert_eq!((launch.project, launch.files), (None, vec![file.into()]));
}
#[test]
fn first_launch_keeps_wsl_files_apart_from_wsl_folders() {
let share = PathBuf::from(r"\\wsl.localhost\Ubuntu\home\me\app");
let folder = launch_for(share.clone(), true);
let remote =
RemoteLaunch { target: RemoteTarget::Wsl { distro: "Ubuntu".into() }, path: "/home/me/app".into() };
assert_eq!((folder.remote, folder.project, folder.files.len()), (Some(remote.clone()), None, 0));
let file = launch_for(share.join("main.py"), false);
assert_eq!((file.remote, file.project, file.files.len()), (None, None, 1));
}
#[test]
fn a_saved_wsl_project_reopens_as_a_remote_launch_without_the_share() {
let remote =
RemoteLaunch { target: RemoteTarget::Wsl { distro: "Ubuntu".into() }, path: "/home/me/app".into() };
for saved in [r"\\wsl.localhost\Ubuntu\home\me\app", "wsl://Ubuntu/home/me/app", "//wsl$/Ubuntu/home/me/app"] {
let launch = saved_project_launch(Path::new(saved)).expect(saved);
assert_eq!(launch.remote, Some(remote.clone()));
assert!(launch.project.is_none(), "{saved}");
}
assert!(same_saved_project(
Path::new(r"\\wsl.localhost\Ubuntu\home\me\app"),
Path::new("wsl://Ubuntu/home/me/app/")
));
let ssh = saved_project_launch(Path::new("ssh://box/srv/app")).unwrap();
assert_eq!(
ssh.remote,
Some(RemoteLaunch { target: RemoteTarget::Ssh { config: "box".into() }, path: "/srv/app".into() })
);
let container =
saved_project_launch(Path::new("devcontainer://%2Fwork%2Fapp:devcontainer.json/workspaces/app")).unwrap();
assert_eq!(
container.remote,
Some(RemoteLaunch {
target: RemoteTarget::DevContainer { folder: "/work/app".into(), config: "devcontainer.json".into() },
path: "/workspaces/app".into(),
})
);
assert!(saved_project_launch(Path::new("/no/such/local/project-openidea")).is_none());
}
fn p(s: &str) -> PathBuf {
PathBuf::from(s)
}
fn w(label: &str, holds: Holds) -> (String, Holds) {
(label.to_owned(), holds)
}
#[test]
fn opening_at_launch_gives_the_first_window_files_or_folder() {
let main = [w("main", Holds::Nothing)];
let files = plan_open(vec![(p("/x/a.txt"), false), (p("/y/b.rs"), false)], &main);
assert_eq!(
files,
[OpenStep::Launch("main".into(), Box::new(path_launch_of(None, vec![p("/x/a.txt"), p("/y/b.rs")])))]
);
let dir = plan_open(vec![(p("/proj"), true)], &main);
assert_eq!(dir, [OpenStep::Launch("main".into(), Box::new(path_launch_of(Some(p("/proj")), Vec::new())))]);
// Folder and files: the folder takes the first window, the files a new LightEdit one.
let both = plan_open(vec![(p("/x/a.txt"), false), (p("/proj"), true)], &main);
assert_eq!(
both,
[
OpenStep::Launch("main".into(), Box::new(path_launch_of(Some(p("/proj")), Vec::new()))),
OpenStep::New(None, vec![p("/x/a.txt")]),
]
);
}
#[test]
fn files_opened_at_launch_join_files_from_the_command_line() {
let cli = path_launch_of(None, vec![p("/x/a.txt")]);
let main = [w("main", pending_holds(Some(&cli)))];
assert_eq!(
plan_open(vec![(p("/x/b.txt"), false)], &main),
[OpenStep::Files("main".into(), vec![p("/x/b.txt")])]
);
let st = WindowsState::default();
st.set_launch("main", cli);
st.add_files_at("main", vec![p("/x/b.txt")], &[]);
assert_eq!(st.take_launch("main").unwrap().files, [p("/x/a.txt"), p("/x/b.txt")]);
}
#[test]
fn files_inside_an_open_project_open_in_its_window_and_the_others_in_light_edit() {
let windows = [
w("main", Holds::Project(p("/proj"))),
w("project-1", Holds::LightEdit),
w("project-2", Holds::Project(p("/proj/sub"))),
];
let steps = plan_open(
vec![
(p("/proj/src/a.rs"), false),
(p("/x/b.txt"), false),
(p("/proj/sub/c.rs"), false),
(p("/projects/d.rs"), false),
(p("/proj/e.rs"), false),
],
&windows,
);
assert_eq!(
steps,
[
// `/projects` is not inside `/proj`; loose files join the focused project window.
OpenStep::Files(
"main".into(),
vec![p("/proj/src/a.rs"), p("/proj/e.rs"), p("/x/b.txt"), p("/projects/d.rs")],
),
// The deepest root wins.
OpenStep::Files("project-2".into(), vec![p("/proj/sub/c.rs")]),
]
);
}
#[test]
fn files_inside_a_folder_being_opened_open_in_its_project() {
let opening = path_launch_of(Some(p("/a")), Vec::new());
let windows = [w("main", pending_holds(Some(&opening))), w("project-1", Holds::Nothing)];
let steps = plan_open(vec![(p("/a/x.rs"), false), (p("/b"), true), (p("/b/y.rs"), false)], &windows);
assert_eq!(
steps,
[
OpenStep::Launch("project-1".into(), Box::new(path_launch_of(Some(p("/b")), vec![p("/b/y.rs")]))),
OpenStep::Files("main".into(), vec![p("/a/x.rs")]),
]
);
assert_eq!(
plan_open(vec![(p("/c"), true), (p("/c/z.rs"), false)], &[]),
[OpenStep::New(Some(p("/c")), vec![p("/c/z.rs")])]
);
}
#[test]
fn remote_projects_do_not_take_local_files() {
let windows = [w("main", Holds::Other), w("project-1", Holds::LightEdit)];
assert_eq!(
plan_open(vec![(p("/home/me/a.rs"), false)], &windows),
[OpenStep::Files("project-1".into(), vec![p("/home/me/a.rs")])]
);
}
#[test]
fn wsl_folders_open_as_wsl_projects() {
let (distro, linux_path) = ("Debian-12", "/srv/app");
let share = format!(r"\\wsl.localhost\{distro}{}", linux_path.replace('/', r"\"));
let share = share.as_str();
let remote = wsl_launch(&p(share)).unwrap();
assert_eq!(
remote,
RemoteLaunch { target: RemoteTarget::Wsl { distro: distro.into() }, path: linux_path.into() }
);
assert!(wsl_launch(&p(linux_path)).is_none());
let launch = Box::new(Launch { remote: Some(remote.clone()), ..Default::default() });
let folder = vec![(p(share), true)];
// A free window connects, else a new one; the window that has it open is focused.
assert_eq!(plan_open(folder.clone(), &[w("main", Holds::Nothing)]), [OpenStep::Launch("main".into(), launch)]);
assert_eq!(plan_open(folder.clone(), &[w("main", Holds::LightEdit)]), [OpenStep::NewRemote(remote.clone())]);
let open = Holds::Wsl { distro: distro.to_lowercase(), path: format!("{linux_path}/") };
assert_eq!(
plan_open(folder, &[w("main", Holds::Nothing), w("project-1", open)]),
[OpenStep::Focus("project-1".into())]
);
// Another distro's folder is a different project.
let other = Holds::Wsl { distro: format!("{distro}-other"), path: linux_path.into() };
assert!(!other.is_wsl_folder(&remote));
assert_eq!(launch_for(p(share), true).remote, Some(remote));
}
#[test]
fn files_opened_from_a_window_go_to_their_project_window_else_light_edit() {
let welcome_only = [w("main", Holds::Nothing), w("project-2", Holds::Nothing), w("project-3", Holds::Other)];
// With no local project or LightEdit window, the Welcome window turns into LightEdit itself.
let (steps, here) = plan_open_files("main", vec![p("/x/b.txt")], &welcome_only);
assert!(steps.is_empty());
assert_eq!(here, [p("/x/b.txt")]);
// A LightEdit window keeps loose files, even with another one open.
let files = vec![p("/proj/src/a.rs"), p("/x/b.txt")];
let light =
[w("main", Holds::LightEdit), w("project-1", Holds::Project(p("/proj"))), w("project-2", Holds::LightEdit)];
let (steps, here) = plan_open_files("main", files.clone(), &light);
assert_eq!(steps, [OpenStep::Files("project-1".into(), vec![p("/proj/src/a.rs")])]);
assert_eq!(here, [p("/x/b.txt")]);
// An existing LightEdit window takes them from a Welcome window.
let others = [w("main", Holds::Nothing), w("project-2", Holds::LightEdit)];
let (steps, here) = plan_open_files("main", vec![p("/x/b.txt")], &others);
assert_eq!((steps, here), (vec![OpenStep::Files("project-2".into(), vec![p("/x/b.txt")])], vec![]));
// A project window in the background doesn't take them from a Welcome window.
let with_proj = [
w("main", Holds::Nothing),
w("project-1", Holds::Project(p("/proj"))),
w("project-2", Holds::Nothing),
w("project-3", Holds::Other),
];
let (steps, here) = plan_open_files("main", files.clone(), &with_proj);
assert_eq!(steps, [OpenStep::Files("project-1".into(), vec![p("/proj/src/a.rs")])]);
assert_eq!(here, [p("/x/b.txt")]);
// A remote project window gets a new LightEdit window, never a free one or its own.
let remote =
[w("project-3", Holds::Other), w("main", Holds::Nothing), w("project-1", Holds::Project(p("/proj")))];
let (steps, here) = plan_open_files("project-3", files, &remote);
assert_eq!(
steps,
[OpenStep::Files("project-1".into(), vec![p("/proj/src/a.rs")]), OpenStep::New(None, vec![p("/x/b.txt")])]
);
assert!(here.is_empty());
}
#[test]
fn files_opened_while_running_join_the_focused_window_else_a_free_one_else_a_new_one() {
// The focused local project window takes them, even with a LightEdit window open.
let project =
[w("main", Holds::Project(p("/proj"))), w("project-1", Holds::Nothing), w("project-2", Holds::LightEdit)];
assert_eq!(
plan_open(vec![(p("/x/a.txt"), false)], &project),
[OpenStep::Files("main".into(), vec![p("/x/a.txt")])]
);
// A project window in the background doesn't.
let free =
[w("main", Holds::Other), w("project-2", Holds::Project(p("/proj"))), w("project-1", Holds::Nothing)];
assert_eq!(
plan_open(vec![(p("/x/a.txt"), false)], &free),
[OpenStep::Launch("project-1".into(), Box::new(path_launch_of(None, vec![p("/x/a.txt")])))]
);
let busy = [w("main", Holds::Other)];
assert_eq!(plan_open(vec![(p("/x/a.txt"), false)], &busy), [OpenStep::New(None, vec![p("/x/a.txt")])]);
}
#[test]
fn folders_opened_while_running_focus_their_window_else_open_as_projects() {
let windows = [w("main", Holds::LightEdit), w("project-1", Holds::Project(p("/a")))];
let steps = plan_open(vec![(p("/a"), true), (p("/b"), true), (p("/b"), true)], &windows);
assert_eq!(steps, [OpenStep::Focus("project-1".into()), OpenStep::New(Some(p("/b")), Vec::new())]);
// A window still opening the folder (pending launch) counts as having it.
let opening = path_launch_of(Some(p("/a")), Vec::new());
let windows = [w("project-2", pending_holds(Some(&opening)))];
assert_eq!(plan_open(vec![(p("/a"), true)], &windows), [OpenStep::Focus("project-2".into())]);
}
#[test]
fn launches_are_taken_once() {
let st = WindowsState::default();
st.set_launch("project-1", Launch { project: Some("/p".into()), ..Default::default() });
assert_eq!(st.take_launch("project-1").and_then(|l| l.project), Some("/p".into()));
assert_eq!(st.take_launch("project-1"), None);
}
#[test]
fn editor_launch_survives_webview_reload_until_window_closes() {
let st = WindowsState::default();
let launch = Launch {
editor: Some(Box::new(EditorLaunch {
path: None,
input: Some(r#"{"kind":"agent-session","key":"agent:running-task"}"#.into()),
line: None,
column: None,
end_line: None,
end_column: None,
})),
..Default::default()
};
st.set_launch("editor-main-3", launch.clone());
assert_eq!(st.take_launch("editor-main-3"), Some(launch.clone()));
assert_eq!(st.take_launch("editor-main-3"), Some(launch));
st.forget("editor-main-3");
assert!(st.take_launch("editor-main-3").is_none());
}
#[test]
fn a_taken_launch_owns_its_window_until_completion() {
for launch in [
path_launch_of(Some(p("/project")), vec![p("/project/a.cs")]),
path_launch_of(None, vec![p("/loose/a.cs")]),
Launch {
diff: Some(Box::new(DiffFilesLaunch { left: "/a.txt".into(), right: "/b.txt".into() })),
..Default::default()
},
Launch {
remote: Some(RemoteLaunch {
target: RemoteTarget::Wsl { distro: "Ubuntu".into() },
path: "/project".into(),
}),
..Default::default()
},
] {
let st = WindowsState::default();
let expected = pending_holds(Some(&launch));
st.set_launch("main", launch.clone());
assert_eq!(st.take_launch("main"), Some(launch));
assert!(st.take_launch("main").is_none());
assert_eq!(pending_holds(st.pending("main").as_ref()), expected);
let windows = vec![("main".into(), pending_holds(st.pending("main").as_ref()))];
assert_eq!(
plan_open(vec![(p("/other-project"), true)], &windows),
vec![OpenStep::New(Some(p("/other-project")), vec![])]
);
st.complete_launch("main");
assert_eq!(pending_holds(st.pending("main").as_ref()), Holds::Nothing);
}
}
#[test]
fn completing_a_launch_keeps_later_requests_and_forgetting_clears_both() {
let st = WindowsState::default();
st.set_launch("main", path_launch_of(Some(p("/project")), vec![]));
st.take_launch("main").unwrap();
st.add_files_at("main", vec![p("/project/late.cs")], &[]);
assert_eq!(pending_holds(st.pending("main").as_ref()), Holds::Project(p("/project")));
st.complete_launch("main");
let late = st.take_launch("main").unwrap();
assert_eq!(late.files, [ide_core::IpcPath::from(p("/project/late.cs"))]);
st.add_files_at("main", vec![p("/project/later.cs")], &[]);
st.forget("main");
assert!(st.pending("main").is_none());
assert!(st.take_launch("main").is_none());
}
#[test]
fn auxiliary_windows_map_to_their_parent() {
assert_eq!(settings_label("project-2"), "settings-project-2");
assert_eq!(auxiliary("settings-main"), Some((AuxKind::Settings, "main")));
assert_eq!(auxiliary("comparison-project-2-14"), Some((AuxKind::Comparison, "project-2")));
assert_eq!(auxiliary("diff-project-2-3"), Some((AuxKind::Diff, "project-2")));
assert_eq!(auxiliary("editor-project-2-3"), Some((AuxKind::Editor, "project-2")));
assert_eq!(auxiliary("deployment-project-2"), Some((AuxKind::Deployment, "project-2")));
assert_eq!(auxiliary("new-project-project-2"), Some((AuxKind::NewProject, "project-2")));
assert_eq!(auxiliary("new-project-main"), Some((AuxKind::NewProject, "main")));
assert_eq!(auxiliary("clone-project-2"), Some((AuxKind::Clone, "project-2")));
assert_eq!(auxiliary("connect-main"), Some((AuxKind::Connect, "main")));
for label in ["main", "project-1", "comparison-main", "comparison-main-x", "settings-", "settingsmain"] {
assert_eq!(auxiliary(label), None, "{label}");
}
assert_eq!(state_key("comparison-main-3"), "comparison");
assert_eq!(state_key("diff-main-3"), "diff");
assert_eq!(state_key("editor-main-3"), "editor");
assert_eq!(state_key("project-3"), "project-3");
let labels = [
"comparison-main-0",
"diff-main-1",
"editor-main-2",
"project-2",
"main",
"settings-main",
"deployment-project-2",
];
assert_eq!(sorted_labels(labels.into_iter().filter(|l| auxiliary_parent(l).is_none())), ["main", "project-2"]);
}
#[test]
fn menu_items_chosen_in_settings_run_in_its_project_window() {
use MenuDestination::*;
for (label, action, expected) in [
("main", "app.about", Own),
("main", "editor.closeTab", Own),
// #3625: Settings has no actions of its own; the rest of the menu runs in the project window.
("settings-main", "app.checkForUpdates", Parent { label: "main", raise: true }),
("settings-project-2", "file.newFile", Parent { label: "project-2", raise: true }),
("settings-main", "editor.closeTab", Parent { label: "main", raise: true }),
("new-project-main", "file.newFile", Parent { label: "main", raise: true }),
("clone-main", "vcs.clone", Parent { label: "main", raise: true }),
("clone-main", "vcs.cloneProject", Parent { label: "main", raise: true }),
("new-project-project-2", "editor.closeTab", Parent { label: "project-2", raise: true }),
("settings-main", "file.settings", Parent { label: "main", raise: false }),
("settings-main", "app.quit", Parent { label: "main", raise: false }),
("comparison-main-0", "app.quit", Parent { label: "main", raise: false }),
("comparison-main-0", "editor.closeTab", Close),
("diff-main-1", "vcs.diff.next", Own),
// Its page has no About: before, choosing it there did nothing.
("deployment-main", "app.about", Parent { label: "main", raise: true }),
("deployment-main", "vcs.diff.next", Own),
// ⌘` from any window, its page's actions and a dialog's parent notwithstanding.
("main", "window.next", Own),
("diff-main-1", "window.next", CycleWindows { forward: true }),
("diff-main-1", "window.previous", CycleWindows { forward: false }),
("settings-main", "window.next", CycleWindows { forward: true }),
("editor-project-2-4", "window.previous", CycleWindows { forward: false }),
// Application actions no auxiliary page registers run in its project window.
("diff-main-1", "app.about", Parent { label: "main", raise: true }),
("editor-project-2-4", "file.openFile", Parent { label: "project-2", raise: true }),
("deployment-main", "help.showLog", Parent { label: "main", raise: true }),
("comparison-main-0", "file.openRecentProject", Parent { label: "main", raise: true }),
("diff-main-1", "view.toggleTheme", Parent { label: "main", raise: false }),
("main", "app.about", Own),
// Toggle Full Screen is the window's own: every auxiliary page registers it.
("diff-main-1", "window.toggleFullScreen", Own),
("settings-main", "window.toggleFullScreen", Own),
// What concerns the project itself stays with the window (greyed out where unregistered).
("diff-main-1", "file.closeProject", Own),
] {
assert_eq!(menu_destination(label, action), expected, "{label} {action}");
}
}
#[test]
fn a_new_window_fits_the_work_area() {
// 1920×1080 at 150 % with a 48-pixel taskbar (#5750).
let area = (1280.0, 1032.0 / 1.5);
assert_eq!(fit_new_window((1440.0, 900.0), (400.0, 500.0), area), ((1280.0, 688.0), (400.0, 500.0)));
// A minimum larger than the screen gives way too.
assert_eq!(fit_new_window((1440.0, 900.0), (400.0, 800.0), area).1, (400.0, 688.0));
// A large screen keeps the configured size; an unknown work area changes nothing.
let big = ((1440.0, 900.0), (400.0, 500.0));
assert_eq!(fit_new_window(big.0, big.1, (2560.0, 1400.0)), big);
assert_eq!(fit_new_window(big.0, big.1, (0.0, 0.0)), big);
}
#[test]
fn settings_bounds_fit_current_work_area() {
for (parent, size, work, expected) in [
// 1920×1080 at 150% scaling, with a 40-physical-pixel taskbar.
(
(0.0, 0.0, 1280.0, 600.0),
(980.0, 720.0),
(0.0, 0.0, 1280.0, 1040.0 / 1.5),
(150.0, 0.0, 980.0, 1040.0 / 1.5),
),
// 3840×2160 at 250% scaling: an owner taller than the work area must not push it off screen.
((100.0, 60.0, 1440.0, 900.0), (980.0, 720.0), (0.0, 0.0, 1536.0, 816.0), (330.0, 96.0, 980.0, 720.0)),
// Restored geometry from a larger display.
((100.0, 50.0, 1440.0, 900.0), (2400.0, 1600.0), (0.0, 24.0, 1920.0, 1016.0), (0.0, 24.0, 1920.0, 1016.0)),
// Secondary display with a negative origin and reserved top/left space.
(
(-1800.0, 100.0, 1400.0, 900.0),
(980.0, 720.0),
(-1870.0, 24.0, 1870.0, 1016.0),
(-1590.0, 190.0, 980.0, 720.0),
),
// Parent near the lower-right edge.
((1700.0, 900.0, 800.0, 600.0), (980.0, 720.0), (0.0, 0.0, 1920.0, 1040.0), (940.0, 320.0, 980.0, 720.0)),
// Work area smaller than the normal minimum size.
((0.0, 0.0, 800.0, 600.0), (980.0, 720.0), (0.0, 0.0, 600.0, 360.0), (0.0, 0.0, 600.0, 360.0)),
// Restored geometry shrunk below minimum size (e.g. from a lower-DPI monitor) clamps up to min_size.
((100.0, 60.0, 1440.0, 900.0), (350.0, 177.0), (0.0, 0.0, 1728.0, 1080.0), (370.0, 160.0, 900.0, 700.0)),
] {
assert_eq!(fit_bounds(parent, size, SETTINGS_MIN_SIZE, work), expected);
}
}
#[test]
fn macos_auxiliary_windows_bypass_physical_window_state_and_clamp_cocoa_bounds() {
assert!(keeps_window_state("main"));
assert!(keeps_window_state("project-2"));
for label in [
"settings",
"comparison",
"diff",
"deployment",
"settings-main",
"comparison-project-2-0",
"diff-main-9",
"deployment-main",
"new-project-main",
"clone-main",
"connect-main",
] {
assert!(!keeps_window_state(label), "{label} must not use physical window-state");
}
// Undersized or invalid saved auxiliary geometry clamps up to min_size.
assert_eq!(
resolve_aux_geometry(
Some(AuxGeometry { width: 350.0, height: 177.0, maximized: false }),
SETTINGS_SIZE,
SETTINGS_MIN_SIZE,
),
AuxGeometry { width: 900.0, height: 700.0, maximized: false },
);
assert_eq!(
resolve_aux_geometry(None, SETTINGS_SIZE, SETTINGS_MIN_SIZE),
AuxGeometry { width: 980.0, height: 720.0, maximized: false },
);
assert_eq!(
resolve_aux_geometry(
Some(AuxGeometry { width: 1180.0, height: 820.0, maximized: true }),
SETTINGS_SIZE,
SETTINGS_MIN_SIZE,
),
AuxGeometry { width: 1180.0, height: 820.0, maximized: true },
);
// Cocoa bottom-left visibleFrame: y=50..1090 (Dock=50, menu=37 on 1127pt display),
// parent=(100, 140, 1440, 900) -> top=1040, centre=(820, 590).
// Settings (980×720) centres at x=330, y=590 - 360 = 230.
assert_eq!(
cocoa_fit_bounds(
(100.0, 140.0, 1440.0, 900.0),
(980.0, 720.0),
SETTINGS_MIN_SIZE,
(0.0, 50.0, 1728.0, 1040.0)
),
(330.0, 230.0, 980.0, 720.0),
);
// Secondary QHD display at negative x=(-2560, 0, 2560, 1415), undersized 350×177 clamps to 900×700.
assert_eq!(
cocoa_fit_bounds(
(-2200.0, 200.0, 1600.0, 1000.0),
(350.0, 177.0),
SETTINGS_MIN_SIZE,
(-2560.0, 0.0, 2560.0, 1415.0),
),
(-1850.0, 350.0, 900.0, 700.0),
);
// Taller than parent: top edge aligns with parent's top edge (y + h == 200 + 500 = 700 -> y = 0).
assert_eq!(
cocoa_fit_bounds(
(0.0, 200.0, 1000.0, 500.0),
(900.0, 700.0),
SETTINGS_MIN_SIZE,
(0.0, 0.0, 1728.0, 1040.0)
),
(50.0, 0.0, 900.0, 700.0),
);
}
#[test]
fn settings_window_is_centred_over_parent() {
assert_eq!(centered(100.0, 50.0, 1440.0, 900.0, 980.0, 720.0), (330.0, 140.0));
// Taller than the parent: aligned with its top edge.
assert_eq!(centered(0.0, 50.0, 800.0, 500.0, 600.0, 720.0), (100.0, 50.0));
}
#[test]
fn menu_settings_launch_routes_to_parent_and_respects_separate_window_and_pending_launch() {
assert_eq!(menu_settings_launch("main", false, true, "file.settings", None), Some(("main", None, false)),);
assert_eq!(
menu_settings_launch("diff-project-2-42", false, true, "file.settings", Some("keymap".into())),
Some(("project-2", Some("keymap".into()), false)),
);
assert_eq!(
menu_settings_launch("settings-main", false, true, "file.newProjectsSettings", None),
Some(("main", Some("languages.java".into()), true)),
);
// Separate-window off, pending cold launch, or unrelated menu actions fall back to frontend dispatch.
assert_eq!(menu_settings_launch("main", false, false, "file.settings", None), None);
assert_eq!(menu_settings_launch("main", true, true, "file.settings", None), None);
assert_eq!(menu_settings_launch("main", false, true, "file.openProject", None), None);
}
#[test]
fn parallel_close_coordination_tracks_ready_prompt_and_abort() {
let st = WindowsState::default();
st.close_requested("main");
st.close_requested("project-1");
assert!(st.is_closing("main"));
assert!(st.has_other_pending_closes("main"));
// Prompt turn allows only one holder at a time.
assert_eq!(st.try_acquire_prompt("main"), Ok(true));
assert_eq!(st.try_acquire_prompt("project-1"), Ok(false));
assert!(st.has_prompt_holder());
// Marking ready releases the prompt turn.
assert!(st.mark_ready("main"));
assert!(!st.has_prompt_holder());
assert_eq!(st.try_acquire_prompt("project-1"), Ok(true));
// Cancelling the batch aborts waiting ready peers and clears the prompt holder.
st.abort_all_closing(Some("project-1"));
assert!(!st.has_prompt_holder());
assert!(st.take_aborted("main"));
assert!(!st.is_closing("main"));
assert!(!st.take_aborted("project-1"));
// A fresh close clears any stale aborted state and claims hide once per cohort.
st.close_requested("main");
assert_eq!(
st.claim_hide(&["main".into(), "project-1".into()]),
vec!["main".to_string(), "project-1".to_string()]
);
assert!(st.claim_hide(&["main".into(), "project-1".into()]).is_empty());
}
}