Skip to content

Commit d88e086

Browse files
committed
Fix torrents not opening on mac unless the app is already running
Issue #305
1 parent 95fd434 commit d88e086

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

‎src-tauri/src/ipc.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ pub struct Ipc {
382382
stop_signal: Option<oneshot::Sender<()>>,
383383
args_sem: Arc<Semaphore>,
384384
args_lock: Option<OwnedSemaphorePermit>,
385+
pending: Vec<String>,
385386
}
386387

387388
impl Ipc {
@@ -392,9 +393,19 @@ impl Ipc {
392393
stop_signal: Default::default(),
393394
args_sem: Semaphore::new(1).into(),
394395
args_lock: None,
396+
pending: Vec::new(),
395397
}
396398
}
397399

400+
#[cfg(target_os = "macos")]
401+
pub fn push_pending(&mut self, uris: Vec<String>) {
402+
self.pending.extend(uris);
403+
}
404+
405+
pub fn take_pending(&mut self) -> Vec<String> {
406+
std::mem::take(&mut self.pending)
407+
}
408+
398409
pub async fn init(&mut self) {
399410
self.args_lock = self.args_sem.clone().acquire_owned().await.ok();
400411

‎src-tauri/src/macos.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ pub fn listen_url() {
157157
listen_apple_event(GURL_EVENT_CLASS, EVENT_GET_URL);
158158
}
159159

160-
// Call this after app is initialised
160+
// Call this in app setup hook
161161
pub fn listen_open_documents() {
162162
listen_apple_event(CORE_EVENT_CLASS, EVENT_OPEN_DOCUMENTS);
163163
}
164164

165-
// Call this after app is initialised
165+
// Call this in app setup hook
166166
pub fn listen_reopen_app() {
167167
listen_apple_event(CORE_EVENT_CLASS, EVENT_REOPEN_APP);
168168
}

‎src-tauri/src/main.rs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ struct ListenerHandle(Arc<RwLock<ipc::Ipc>>);
4545

4646
#[cfg(target_os = "macos")]
4747
fn handle_uris(app: &AppHandle, uris: Vec<String>) {
48+
if uris.is_empty() {
49+
return;
50+
}
4851
let listener_state: State<ListenerHandle> = app.state();
4952
let listener_lock = listener_state.0.clone();
5053
let app_handle = app.clone();
5154
async_runtime::spawn(async move {
55+
{
56+
let mut listener = listener_lock.write().await;
57+
if !listener.listening {
58+
listener.push_pending(uris);
59+
return;
60+
}
61+
}
5262
let listener = listener_lock.read().await;
5363
if let Err(e) = listener.send(&uris, app_handle).await {
5464
println!("Unable to send args to listener: {:?}", e);
@@ -65,6 +75,8 @@ fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
6575
})
6676
.expect("Unable to set apple event handler");
6777
macos::listen_url();
78+
macos::listen_open_documents();
79+
macos::listen_reopen_app();
6880
}
6981

7082
let mut torrents: Vec<String> = vec![];
@@ -129,16 +141,11 @@ fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
129141

130142
let app_clone = app.clone();
131143
async_runtime::spawn(async move {
132-
let listener = listener_lock.read().await;
144+
let mut listener = listener_lock.write().await;
145+
torrents.extend(listener.take_pending());
133146
if let Err(e) = listener.send(&torrents, app_clone).await {
134147
println!("Unable to send args to listener: {e}");
135148
}
136-
137-
#[cfg(target_os = "macos")]
138-
{
139-
macos::listen_open_documents();
140-
macos::listen_reopen_app();
141-
}
142149
});
143150

144151
let app_clone = app.clone();

0 commit comments

Comments
 (0)