feat: disconnect devices on app background mobile

This commit is contained in:
Lucas
2026-11-04 11:08:22 +01:00
parent be713620ef
commit ddb7a907a1
15 changed files with 264 additions and 37 deletions
+7
View File
@@ -69,8 +69,15 @@ pub fn create_dsp408(
let id = *next_id;
*next_id += 1;
#[cfg(mobile)]
let mut device = DSPDevice { dsp };
#[cfg(not(mobile))]
let device = DSPDevice { dsp };
#[cfg(mobile)]
device.dsp.set_auto_reconnect(false);
let mut devices = state.devices.lock().map_err(|e| e.to_string())?;
devices.insert(id, Arc::new(Mutex::new(device)));
+21 -4
View File
@@ -4,11 +4,26 @@ mod state;
use state::AppState;
use tauri::Manager;
#[cfg(mobile)]
fn handle_run_event(app: &tauri::AppHandle, event: &tauri::RunEvent) {
if let tauri::RunEvent::WindowEvent {
event: tauri::WindowEvent::Focused(false),
..
} = event
{
app.state::<AppState>().disconnect_all();
}
}
#[cfg(not(mobile))]
fn handle_run_event(_app: &tauri::AppHandle, _event: &tauri::RunEvent) {}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
let app = tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_os::init()) // added by `tauri add os`
.manage(AppState::new())
.invoke_handler(tauri::generate_handler![
commands::create_dsp408,
@@ -51,6 +66,8 @@ pub fn run() {
state.shutdown();
}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
.build(tauri::generate_context!())
.expect("error while building tauri application");
app.run(|app, event| handle_run_event(app, &event));
}
+18
View File
@@ -30,4 +30,22 @@ impl AppState {
}
}
}
pub fn disconnect_all(&self) {
// clone the Arcs so the map lock isn't held while waiting on devices
let devices: Vec<Arc<Mutex<DSPDevice>>> = {
let map = self.devices.lock().unwrap_or_else(|e| e.into_inner());
map.values().cloned().collect()
};
// one thread per device so a stuck device doesn't delay the others
for device in devices {
std::thread::spawn(move || {
let mut d = device.lock().unwrap_or_else(|e| e.into_inner());
if d.dsp.connected() {
let _ = d.dsp.disconnect();
}
});
}
}
}