Expand description
One instance of an app: a later launch hands its command line to the instance already running,
and quits. An app’s own pattern rather than the platform’s, as Electron’s
requestSingleInstanceLock and Tauri’s single-instance plugin are, and opt-in.
A later launch’s arguments arrive as the strings they are, unread, with the folder it was started in: which of them are files is the app’s to say, as it says for its own launch’s.
Each system has its own way: Linux’s session bus, where the first instance owns the app’s id as its name and a later launch calls it; Windows’ named mutex, which tells a later launch it is second, and a named pipe it writes its arguments to; macOS keeps one instance itself, handing a later open to the running app as an open request, so every launch there is the first, as on the phones and the web.
An app reads its own launch’s command line, hears the later launches’ through
FirstInstance::launches, and hands the files it decides they name to
App::open_requested, where the platform’s own open requests also arrive
(App::set_open_requested_handler): those bring the files the system itself hands over, and on
macOS every later open. All of them reach the same code:
use std::ffi::OsString;
use std::path::Path;
use std::rc::Rc;
use waft::ui::{OpenRequest, PickedFile};
use waft::{App, run_app};
use waft_single_instance::{SingleInstance, single_instance};
#[waft::main]
fn main(app: &mut App) {
let first = match single_instance("com.example.notes") {
Ok(SingleInstance::First(first)) => Some(first),
// The first instance has this launch's command line.
Ok(SingleInstance::Second) => std::process::exit(0),
Err(_) => None,
};
app.set_open_requested_handler(Some(Rc::new(open)));
let current_dir = std::env::current_dir().unwrap_or_default();
let launch = files_named(std::env::args_os().skip(1), ¤t_dir);
if !launch.is_empty() {
app.open_requested(launch);
}
if let Some(first) = first {
let mut launches = first.launches();
app.spawn(async move |cx| {
loop {
let launch = launches.next_launch().await;
let request = files_named(launch.arguments, &launch.current_dir);
cx.update(|app| app.open_requested(request));
}
});
}
run_app(app, home());
}
/// The files this app's command line names: each of its arguments, in the folder it was
/// started in.
fn files_named(arguments: impl IntoIterator<Item = OsString>, current_dir: &Path) -> OpenRequest {
let files = arguments.into_iter().map(|argument| PickedFile::Path(current_dir.join(argument)));
OpenRequest { files: files.collect(), links: Vec::new() }
}
/// Shows the request's files and links; an empty request asks the app to show itself.
fn open(app: &mut App, request: OpenRequest) {
// ...
}The invariant: the first instance keeps its claim as long as its stream lives, so a launch while it runs is always second.
Structs§
- First
Instance - The app’s only instance, holding its claim.
- Later
Launches - The launches after the first instance’s, each passed on to it.
- Launch
- A later launch: its arguments, without the program’s name, as the strings the system gave it, and the folder it was started in, which relative paths are in. A launch with no arguments asks the app to show itself again.
Enums§
- Single
Instance - Whether this launch is the app’s only instance.
Functions§
- single_
instance - Claims to be the one instance of the app
id, a reverse-DNS name such ascom.example.notes, or, where another instance holds the claim, hands it this launch’s arguments. An error leaves the app to run as though alone.