mirror of
https://github.com/BetterCorp/BetterFrame.git
synced 2026-05-26 19:06:34 +00:00
fix: use std::sync::mpsc + timeout_add_local for thread→UI messaging
This commit is contained in:
parent
df231344a8
commit
76af07de61
1 changed files with 9 additions and 10 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
|
||||||
use gtk4::prelude::*;
|
use gtk4::prelude::*;
|
||||||
use gtk4::{self as gtk, Application, ApplicationWindow, Box as GtkBox, Grid, Label, Orientation, Picture};
|
use gtk4::{self as gtk, Application, ApplicationWindow, Box as GtkBox, Grid, Label, Orientation, Picture};
|
||||||
|
|
@ -32,14 +33,11 @@ fn activate(app: &Application) {
|
||||||
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Start with logo
|
|
||||||
show_logo(&window);
|
show_logo(&window);
|
||||||
window.present();
|
window.present();
|
||||||
|
|
||||||
// Channel to send results from worker thread to UI
|
let (tx, rx) = mpsc::channel::<WorkerMsg>();
|
||||||
let (tx, rx) = gtk::glib::MainContext::channel::<WorkerMsg>(gtk::glib::Priority::DEFAULT);
|
|
||||||
|
|
||||||
// Worker thread — blocking HTTP calls
|
|
||||||
let server_url = std::env::args().nth(1);
|
let server_url = std::env::args().nth(1);
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let server = server::discover_server(server_url.as_deref());
|
let server = server::discover_server(server_url.as_deref());
|
||||||
|
|
@ -62,19 +60,20 @@ fn activate(app: &Application) {
|
||||||
info!("bundle: {} cameras, {} layouts", bundle.cameras.len(), bundle.layouts.len());
|
info!("bundle: {} cameras, {} layouts", bundle.cameras.len(), bundle.layouts.len());
|
||||||
let _ = tx.send(WorkerMsg::RenderBundle(bundle));
|
let _ = tx.send(WorkerMsg::RenderBundle(bundle));
|
||||||
|
|
||||||
// Heartbeat loop
|
|
||||||
loop {
|
loop {
|
||||||
std::thread::sleep(std::time::Duration::from_secs(60));
|
std::thread::sleep(std::time::Duration::from_secs(60));
|
||||||
server::heartbeat(&server, &key);
|
server::heartbeat(&server, &key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Receive messages on UI thread
|
// Poll channel from UI thread via timeout
|
||||||
let window_clone = window.clone();
|
let window_clone = window.clone();
|
||||||
rx.attach(None, move |msg| {
|
gtk::glib::timeout_add_local(std::time::Duration::from_millis(100), move || {
|
||||||
match msg {
|
while let Ok(msg) = rx.try_recv() {
|
||||||
WorkerMsg::ShowPairingCode(code) => show_pairing_code(&window_clone, &code),
|
match msg {
|
||||||
WorkerMsg::RenderBundle(bundle) => render_bundle(&window_clone, bundle),
|
WorkerMsg::ShowPairingCode(code) => show_pairing_code(&window_clone, &code),
|
||||||
|
WorkerMsg::RenderBundle(bundle) => render_bundle(&window_clone, bundle),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
gtk::glib::ControlFlow::Continue
|
gtk::glib::ControlFlow::Continue
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue