All apps: a second launch hands its files to the running copy

R4.12 (user report). The copy holding the instance lock opens a local
door guarded by a random token in its data folder; a second copy sends
its file paths there, lets it take the foreground, and exits 0 instead
of showing "already running". Files join the ely_ui::on_open queue every
app already uses; the main window comes to the front. Also records the
R4.10 round 1 verdict.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-14 19:02:38 -04:00
1 parent d5ceaf9aac
commit 5bf2056281
10 files changed
+436 -41

No files matched your search

+50 -19
View File
@@ -1,41 +1,55 @@
//! Files the system hands the app: arguments, Finder.
//! Files handed to the app: arguments, Finder, second copies.
use std::path::PathBuf;
use std::{io, path::PathBuf};
use ely_platform::instance::Door;
use futures::{StreamExt, channel::mpsc};
use gpui::{App, Application, Global};
struct Asked(Option<mpsc::UnboundedReceiver<PathBuf>>);
/// A request to the running app.
pub(crate) enum Ask {
Open(PathBuf),
/// A second copy started: come to the front.
Raise,
}
struct Asked(Option<mpsc::UnboundedReceiver<Ask>>);
impl Global for Asked {}
/// Queues launch arguments and every later open request.
pub(crate) fn listen(application: &Application) -> mpsc::UnboundedReceiver<PathBuf> {
/// Launch arguments, relative to where the app was started.
pub(crate) fn launch_args() -> io::Result<Vec<PathBuf>> {
std::env::args_os().skip(1).map(std::path::absolute).collect()
}
/// Queues `files`, then files the system or copies ask for.
pub(crate) fn listen(application: &Application, files: Vec<PathBuf>, door: Door) -> io::Result<mpsc::UnboundedReceiver<Ask>> {
let (sender, asked) = mpsc::unbounded();
// Relative to where the app was started.
for path in std::env::args_os().skip(1) {
match std::path::absolute(&path) {
Ok(path) => {
sender.unbounded_send(path).ok();
}
Err(error) => log::error!("run: a launch argument has no place: {error}"),
}
for path in files {
sender.unbounded_send(Ask::Open(path)).ok();
}
let handed = sender.clone();
door.serve(move |files| {
for path in files {
handed.unbounded_send(Ask::Open(path)).ok();
}
handed.unbounded_send(Ask::Raise).ok();
})?;
application.on_open_urls(move |urls| {
for url in urls {
match file_path(&url) {
Some(path) => {
log::info!("run: the system asks to open a file");
sender.unbounded_send(path).ok();
sender.unbounded_send(Ask::Open(path)).ok();
}
None => log::warn!("run: not a file URL, ignored"),
}
}
});
asked
Ok(asked)
}
pub(crate) fn keep(asked: mpsc::UnboundedReceiver<PathBuf>, cx: &mut App) {
pub(crate) fn keep(asked: mpsc::UnboundedReceiver<Ask>, cx: &mut App) {
cx.set_global(Asked(Some(asked)));
}
@@ -49,7 +63,9 @@ pub fn install_no_launch_files(cx: &mut App) {
pub fn launch_files(cx: &mut App) -> Vec<PathBuf> {
let asked = cx.global_mut::<Asked>().0.as_mut();
let asked = asked.expect("launch_files comes before on_open");
std::iter::from_fn(|| asked.try_recv().ok()).collect()
let pending = std::iter::from_fn(|| asked.try_recv().ok());
// The window opens in front anyway.
pending.filter_map(|ask| if let Ask::Open(path) = ask { Some(path) } else { None }).collect()
}
/// Hands each file asked from now on to `open`.
@@ -60,13 +76,28 @@ pub fn on_open(cx: &mut App, mut open: impl FnMut(PathBuf, &mut App) + 'static)
.take()
.expect("on_open runs once per app");
cx.spawn(async move |cx| {
while let Some(path) = asked.next().await {
cx.update(|cx| open(path, cx));
while let Some(ask) = asked.next().await {
cx.update(|cx| match ask {
Ask::Open(path) => open(path, cx),
Ask::Raise => raise(cx),
});
}
})
.detach();
}
/// Brings the main window, the first opened, to the front.
fn raise(cx: &mut App) {
cx.activate(true);
let Some(window) = cx.windows().into_iter().next() else {
log::warn!("run: no window to bring to the front");
return;
};
if let Err(error) = window.update(cx, |_, window, _| window.activate_window()) {
log::warn!("run: the window could not come to the front: {error}");
}
}
/// A `file://` URL's path, percent escapes decoded.
fn file_path(url: &str) -> Option<PathBuf> {
let rest = url.strip_prefix("file://")?;
+8 -5
View File
@@ -2,7 +2,7 @@ use std::time::Duration;
use ely_gpui_component::Assets;
use ely_gpui_component::theme::ActiveTheme;
use ely_platform::{AppId, env, graphics, logging, startup};
use ely_platform::{AppId, env, graphics, instance::Start, logging, startup};
use gpui::{
App, Bounds, Entity, Render, TitlebarOptions, Window, WindowBounds, WindowDecorations,
WindowOptions, px, size,
@@ -19,10 +19,13 @@ pub fn run<V: Render>(
if let Some(code) = crate::updates::installer(app) {
std::process::exit(code);
}
let files = crate::opening::launch_args().unwrap_or_else(|error| startup::fail(app, &format!("a launch argument has no place: {error}")));
// Before the log: a second copy must not trim it.
if let Err(error) = ely_platform::instance::hold(app) {
startup::fail(app, &format!("{error}"));
}
let door = match ely_platform::instance::hold(app, &files) {
Ok(Start::First(door)) => door,
Ok(Start::Handed) => std::process::exit(0),
Err(error) => startup::fail(app, &format!("{error}")),
};
if let Err(error) = logging::init(app) {
startup::fail(app, &format!("logging did not start: {error:#}"));
}
@@ -33,7 +36,7 @@ pub fn run<V: Render>(
startup::no_window_restore();
let smoke = Smoke::from_env();
let application = gpui_platform::application().with_assets(Assets);
let asked = crate::opening::listen(&application);
let asked = crate::opening::listen(&application, files, door).unwrap_or_else(|error| startup::fail(app, &format!("the door for other copies did not open: {error}")));
application.run(move |cx: &mut App| {
crate::opening::keep(asked, cx);
open(app, smoke, root, cx)