Added single-instance, where second instance raises the current running instance instead of creating a second conflicting instance
This commit is contained in:
1 parent
6425cfaacf
commit
4dc4401871
4 files changed
+190
-2
No files matched your search
@@ -0,0 +1,87 @@
|
||||
//! Single-instance election and activation hand-off.
|
||||
//!
|
||||
//! `instance-handoff` takes a per-user lock before any window, tray or hotkey
|
||||
//! exists. Later launches hand over their request and exit; the owner keeps a
|
||||
//! local IPC listener alive for its lifetime. The OS releases the lock when
|
||||
//! the owner exits or crashes, so a stale lock cannot block a future launch.
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use iced::futures::{SinkExt, Stream};
|
||||
use iced::{Subscription, stream};
|
||||
use instance_handoff::{AppId, Builder, Owner, Request, Started, State};
|
||||
use tokio::sync::mpsc::{UnboundedReceiver, unbounded_channel};
|
||||
|
||||
const APP_ID: AppId = AppId::windowed("dev.moorly.Moorly");
|
||||
|
||||
static OWNER: Mutex<Option<Owner>> = Mutex::new(None);
|
||||
static EVENTS: Mutex<Option<UnboundedReceiver<()>>> = Mutex::new(None);
|
||||
|
||||
/// Elects the primary instance. Returns `false` when this launch handed its
|
||||
/// activation request to the already-running instance and should exit.
|
||||
pub fn acquire() -> bool {
|
||||
let state = crate::config_path()
|
||||
.and_then(|path| path.parent().map(|dir| State::dir(dir.to_path_buf())))
|
||||
.unwrap_or(State::none());
|
||||
|
||||
match Builder::new(APP_ID, state).start(&()) {
|
||||
Started::Owner(claim) => {
|
||||
let (sender, receiver) = unbounded_channel();
|
||||
|
||||
if let Ok(mut events) = EVENTS.lock() {
|
||||
*events = Some(receiver);
|
||||
}
|
||||
|
||||
let owner = claim.serve(move |_request: Request<()>| sender.send(()).is_ok());
|
||||
|
||||
if let Ok(mut slot) = OWNER.lock() {
|
||||
*slot = Some(owner);
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
Started::HandedOff(_) => false,
|
||||
Started::Alone(reason) => {
|
||||
eprintln!("Moorly could not become the single instance: {reason}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Activation requests from later launches, delivered into the iced update
|
||||
/// loop without blocking the GUI thread.
|
||||
pub fn subscription() -> Subscription<crate::Message> {
|
||||
Subscription::run(events)
|
||||
}
|
||||
|
||||
fn events() -> impl Stream<Item = crate::Message> {
|
||||
stream::channel(
|
||||
8,
|
||||
|mut output: iced::futures::channel::mpsc::Sender<crate::Message>| async move {
|
||||
let receiver = EVENTS.lock().ok().and_then(|mut slot| slot.take());
|
||||
|
||||
let Some(mut receiver) = receiver else {
|
||||
return;
|
||||
};
|
||||
|
||||
while receiver.recv().await.is_some() {
|
||||
if output
|
||||
.send(crate::Message::SecondInstanceActivated)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Stops the IPC listener and gives up the lock before the process exits.
|
||||
pub fn shutdown() {
|
||||
if let Ok(mut slot) = OWNER.lock()
|
||||
&& let Some(owner) = slot.take()
|
||||
{
|
||||
owner.release();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user