-
Notifications
You must be signed in to change notification settings - Fork 251
/
remote_viewer_app.rs
82 lines (71 loc) · 2.55 KB
/
remote_viewer_app.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::App;
/// Connects to a server over `WebSockets`.
#[derive(Default)]
pub struct RemoteViewerApp {
url: String,
app: Option<(re_ws_comms::Connection, App)>,
}
impl RemoteViewerApp {
/// url to rerun server
pub fn new(
egui_ctx: &egui::Context,
storage: Option<&dyn eframe::Storage>,
url: String,
) -> Self {
let mut slf = Self { url, app: None };
slf.connect(egui_ctx, storage);
slf
}
fn connect(&mut self, egui_ctx: &egui::Context, storage: Option<&dyn eframe::Storage>) {
let (tx, rx) = std::sync::mpsc::channel();
let egui_ctx_clone = egui_ctx.clone();
let connection = re_ws_comms::Connection::viewer_to_server(
self.url.clone(),
move |data_msg: re_log_types::LogMsg| {
if tx.send(data_msg).is_ok() {
egui_ctx_clone.request_repaint(); // Wake up UI thread
std::ops::ControlFlow::Continue(())
} else {
re_log::info!("Failed to send log message to viewer - closing");
std::ops::ControlFlow::Break(())
}
},
)
.unwrap(); // TODO(emilk): handle error
let app = crate::App::from_receiver(egui_ctx, storage, rx);
self.app = Some((connection, app));
}
#[cfg(all(feature = "puffin", not(target_arch = "wasm32")))]
pub fn set_profiler(&mut self, profiler: crate::Profiler) {
if let Some((_, app)) = &mut self.app {
app.set_profiler(profiler);
}
}
}
impl eframe::App for RemoteViewerApp {
fn save(&mut self, storage: &mut dyn eframe::Storage) {
if let Some((_, app)) = &mut self.app {
app.save(storage);
}
}
fn update(&mut self, egui_ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::TopBottomPanel::top("server").show(egui_ctx, |ui| {
ui.horizontal(|ui| {
ui.label("URL:");
if ui.text_edit_singleline(&mut self.url).lost_focus()
&& ui.input().key_pressed(egui::Key::Enter)
{
if let Some(storage) = frame.storage_mut() {
if let Some((_, mut app)) = self.app.take() {
app.save(storage);
}
}
self.connect(egui_ctx, frame.storage());
}
});
});
if let Some((_, app)) = &mut self.app {
app.update(egui_ctx, frame);
}
}
}