A single instance and deep links
An application that should run once per user (a photo library, a
player, a chat) makes a second launch hand its arguments to the running
one, which comes to the front and opens what it was given: files,
myapp:// links. egui-pop-platform does it with a local socket named
after the application; the facade re-exports it as
egui_pop::platform with the platform feature (or
platform-instance alone):
egui-pop = { version = "0.3", features = ["platform-instance"] }
At the top of main
instance::single runs before any window. The first launch listens and
gets Single::First; a later one connects, hands over its arguments,
its working directory and its activation token, gets Single::Handed
and exits:
use egui_pop::platform::instance::{self, InstancePlugin, Single};
use egui_pop::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let first = match instance::single("org.example.Viewer")? {
Single::Handed => return Ok(()),
Single::First(first) => first,
};
App::builder()
.plugins(DefaultPlugins)
.plugin(
InstancePlugin::new(first)
.scheme("viewer")
.links(CommandId::from_static("viewer.open_link")),
)
.run_native("Viewer", Default::default())?;
Ok(())
}
The socket is an abstract Unix socket on Linux (a connection from
another user is refused), a file in $XDG_RUNTIME_DIR/app/<id> inside a
Flatpak sandbox (the directory the application’s sandboxes share, since
each may have a network namespace of its own), a named pipe on Windows,
and a socket in /tmp on the other Unix systems; its name holds the
application’s id and the user’s name. Nothing else is written.
Without the application
instance::single does not need App. An application without it takes
the later launches itself from the Launches of Single::First
(Instance::launches): Launches::set_waker with its egui context, so
a launch handed over repaints the window, and Launches::take in each
frame, which drains the launches handed over since the last call:
use egui_pop::egui;
use egui_pop::platform::instance::{Instance, Launch};
let first = Instance::detached(Launch::default()); // `Single::First(first)`
let launches = first.launches();
let ctx = egui::Context::default();
launches.set_waker(Some(ctx.clone()));
launches.deliver(Launch { args: vec!["photo.jpg".into()], ..Launch::default() });
// Each frame:
for launch in launches.take() {
egui_pop::platform::window::raise(&ctx);
assert_eq!(launch.args, ["photo.jpg"]); // open what it names
}
Links in the application’s scheme are in Launch::links(&["viewer".into()]);
what to do with them, and who asked (a link from a launch is not the
user’s gesture, below), is the application’s call.
The steps
| Step | Arguments | Default |
|---|---|---|
instance.second_launch | args, cwd, links (the arguments in one of the plugin’s schemes) | the main window comes to the front, then each link runs deeplink.open |
deeplink.open | url, origin | the plugin’s links command runs with { "url" }, for origin; without one the step fails and says so |
A click on a link in one of the plugin’s schemes inside the
application (the links service) opens it in-process, with
no round trip through the system: the plugin adds its schemes to the
links policy at start, and its hidden command deeplink.open (url)
runs the same step.
deeplink.open also runs at start for the links the first launch was
given (viewer://album/3 on its command line). A plugin vetoes
instance.second_launch to stay in the background, replaces it to open
the files a launch names, or follows deeplink.open to log links:
use egui_pop::egui;
use egui_pop::platform::instance::{
Instance, InstancePlugin, LAUNCHES, Launch, STEP_SECOND_LAUNCH,
};
use egui_pop::prelude::*;
struct Viewer;
impl Plugin for Viewer {
fn id(&self) -> PluginId {
PluginId::from_static("viewer")
}
fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
ctx.command(
CommandSpec::new(CommandId::from_static("viewer.open_link"), "Open a link", "Viewer"),
|call| Ok(call.args["url"].clone()),
)?;
// Files named by a second launch open here; links still go to
// `deeplink.open`.
ctx.after(STEP_SECOND_LAUNCH, |app, args, _| {
for file in args["args"].as_array().into_iter().flatten() {
app.publish("viewer", None, "viewer.file", file.clone());
}
});
Ok(())
}
}
let ctx = egui::Context::default();
let mut app = App::builder()
.plugin(Viewer)
.plugin(
InstancePlugin::new(Instance::detached(Launch::default()))
.scheme("viewer")
.links(CommandId::from_static("viewer.open_link")),
)
.build(&ctx)
.unwrap();
// A launch handed over in-process, as the socket would.
app.handle().service(&LAUNCHES).unwrap().deliver(Launch {
args: vec!["viewer://album/3".into()],
..Launch::default()
});
app.logic(&ctx);
app.logic(&ctx);
let ran: Vec<_> = app.handle().history().into_iter().map(|r| r.id.to_string()).collect();
assert_eq!(ran, ["viewer.open_link"]);
Instance::detached is an instance listening to nothing, for tests and
for applications that are single some other way; Launches::deliver
hands a launch over in-process.
Who opens a link
A link that comes with a launch is not the user’s gesture: any program
of the user’s can start the application with one (a web page in a
browser, a script, another application), and a later launch reaches the
running one through the single-instance socket. So the links command
runs for Origin::Remote("deeplink") (instance::deeplink_origin,
remote:deeplink on the journal and in the step’s origin), not for
the user:
- the handler sees it in
Call::origin; - the commands it starts with
AppHandle::runrun for it too, and it may not run one as the user (command.refusedon the journal); - the values it writes are written as
remote:deeplink, held to their declared writers.
A handler that changes or deletes anything on a link asks the user
first, in a dialog: the user’s answer runs its command as the user. A
link of the application’s own scheme clicked inside the application
(the deeplink.open command, run by the links service) runs for the
click’s origin, the user’s.
Platforms
| Single instance | Coming to the front | |
|---|---|---|
| Linux, X11 | yes | yes |
| Linux, Wayland | yes | the window asks for attention: the compositor gives the focus only with an activation token, which egui does not pass on to the window |
| Windows | yes (named pipe) | yes, within the focus-stealing rules of Windows (the taskbar button flashes otherwise) |
| macOS | yes | yes; macOS itself usually keeps a bundle single and sends links as Apple events, which winit does not hand to egui yet: links reach a bundled application only on its command line |
| Web | no (each tab is its own) | no |
Registering the viewer: scheme with the system (a desktop file’s
MimeType=x-scheme-handler/viewer, a registry key, the bundle’s
CFBundleURLTypes) is the package’s part: see
Packaging.