[fix](trx-frontend-appkit): run AppKit event loop on calling thread

Extract the AppKit event loop from FrontendSpawner::spawn_frontend into
a new public run_appkit_main_thread() function that blocks on the
calling thread. This allows the process main thread (thread 0) to drive
the UI, which is required for MainThreadMarker::new() to succeed.

The FrontendSpawner impl now only spawns the async state watcher task.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Stanislaw Grams <stanislawgrams@gmail.com>
This commit is contained in:
2026-02-07 13:36:32 +01:00
parent a7582f17f2
commit e7ddaa7300
2 changed files with 57 additions and 42 deletions
@@ -11,6 +11,9 @@ pub mod server;
#[cfg(all(target_os = "macos", feature = "appkit"))] #[cfg(all(target_os = "macos", feature = "appkit"))]
pub mod ui; pub mod ui;
#[cfg(all(target_os = "macos", feature = "appkit"))]
pub use server::run_appkit_main_thread;
#[cfg(all(target_os = "macos", feature = "appkit"))] #[cfg(all(target_os = "macos", feature = "appkit"))]
pub fn register_frontend() { pub fn register_frontend() {
use trx_frontend::FrontendSpawner; use trx_frontend::FrontendSpawner;
@@ -9,7 +9,6 @@
//! thread via a std::sync::mpsc channel. //! thread via a std::sync::mpsc channel.
use std::net::SocketAddr; use std::net::SocketAddr;
use std::thread;
use objc2::MainThreadMarker; use objc2::MainThreadMarker;
use objc2_app_kit::NSApplication; use objc2_app_kit::NSApplication;
@@ -30,65 +29,78 @@ pub struct AppKitFrontend;
impl FrontendSpawner for AppKitFrontend { impl FrontendSpawner for AppKitFrontend {
fn spawn_frontend( fn spawn_frontend(
state_rx: watch::Receiver<RigState>, state_rx: watch::Receiver<RigState>,
rig_tx: mpsc::Sender<RigRequest>, _rig_tx: mpsc::Sender<RigRequest>,
_callsign: Option<String>, _callsign: Option<String>,
listen_addr: SocketAddr, listen_addr: SocketAddr,
) -> JoinHandle<()> { ) -> JoinHandle<()> {
// Channel for state updates: async watcher -> AppKit thread. let (state_update_tx, _state_update_rx) = std::sync::mpsc::channel::<RigState>();
let (state_update_tx, state_update_rx) = std::sync::mpsc::channel::<RigState>();
// Channel for button actions: UI buttons -> AppKit thread main loop.
let (action_tx, action_rx) = std::sync::mpsc::channel::<ButtonAction>();
// Spawn async state watcher that forwards state changes. // Spawn async state watcher that forwards state changes.
let handle = tokio::spawn(async move { // The actual AppKit event loop is driven by `run_appkit_main_thread`
// called from main() on the process main thread.
tokio::spawn(async move {
info!("AppKit frontend starting (addr hint: {})", listen_addr); info!("AppKit frontend starting (addr hint: {})", listen_addr);
run_state_watcher(state_rx, state_update_tx).await; run_state_watcher(state_rx, state_update_tx).await;
}); })
}
}
// Spawn the AppKit main thread. /// Run the AppKit event loop on the calling thread (must be the process main
thread::spawn(move || { /// thread, i.e. thread 0). This function **blocks forever**.
let mtm = match MainThreadMarker::new() { ///
Some(m) => m, /// It creates the NSApplication, builds the UI window, and enters a polling
None => { /// loop that drains AppKit events, applies rig state updates, and dispatches
warn!("AppKit frontend: could not obtain MainThreadMarker"); /// button actions.
return; pub fn run_appkit_main_thread(
} state_rx: watch::Receiver<RigState>,
}; rig_tx: mpsc::Sender<RigRequest>,
) {
// Channel for state updates: async watcher -> main thread.
let (state_update_tx, state_update_rx) = std::sync::mpsc::channel::<RigState>();
let app = NSApplication::sharedApplication(mtm); // Channel for button actions: UI buttons -> main thread loop.
let (action_tx, action_rx) = std::sync::mpsc::channel::<ButtonAction>();
let (window, ui_elements) = ui::build_window(mtm, action_tx); // Spawn async state watcher onto the tokio runtime (running on a
// background thread).
tokio::spawn(async move {
run_state_watcher(state_rx, state_update_tx).await;
});
// Keep window alive for the process lifetime. let mtm = MainThreadMarker::new()
std::mem::forget(window); .expect("run_appkit_main_thread must be called from the process main thread");
let mut model = RigStateModel::default(); let app = NSApplication::sharedApplication(mtm);
// Run a polling loop instead of NSApplication::run() so we can let (window, ui_elements) = ui::build_window(mtm, action_tx);
// process state updates and button actions between event cycles.
loop {
// Process pending AppKit events.
drain_appkit_events(&app);
// Process state updates from the async watcher. // Keep window alive for the process lifetime.
while let Ok(state) = state_update_rx.try_recv() { std::mem::forget(window);
if model.update(&state) {
ui_elements.refresh(&model);
}
}
// Process button actions. let mut model = RigStateModel::default();
while let Ok(action) = action_rx.try_recv() {
handle_action(action, &ui_elements, &rig_tx, &model);
}
// Sleep briefly to avoid busy-waiting. info!("AppKit frontend: entering main run loop");
std::thread::sleep(std::time::Duration::from_millis(16));
// Run a polling loop instead of NSApplication::run() so we can
// process state updates and button actions between event cycles.
loop {
// Process pending AppKit events.
drain_appkit_events(&app);
// Process state updates from the async watcher.
while let Ok(state) = state_update_rx.try_recv() {
if model.update(&state) {
ui_elements.refresh(&model);
} }
}); }
handle // Process button actions.
while let Ok(action) = action_rx.try_recv() {
handle_action(action, &ui_elements, &rig_tx, &model);
}
// Sleep briefly to avoid busy-waiting.
std::thread::sleep(std::time::Duration::from_millis(16));
} }
} }