forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandle.rs
More file actions
76 lines (67 loc) · 1.99 KB
/
handle.rs
File metadata and controls
76 lines (67 loc) · 1.99 KB
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
use tokio::sync::mpsc;
use super::protocol::ViewerEvent;
/// Handle for sending interaction events from the viewer to the application.
///
/// Cheap to clone and thread-safe.
#[derive(Clone)]
pub struct InteractionHandle {
tx: mpsc::UnboundedSender<ViewerEvent>,
}
impl InteractionHandle {
/// Create a new handle from a channel sender.
pub fn new(tx: mpsc::UnboundedSender<ViewerEvent>) -> Self {
Self { tx }
}
/// Send a click event to the application.
pub fn send_click(
&self,
position: [f32; 3],
entity_path: Option<String>,
view_id: String,
is_2d: bool,
) {
let event = ViewerEvent::Click {
position,
entity_path,
view_id,
timestamp_ms: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
is_2d,
};
if let Err(e) = self.tx.send(event) {
eprintln!("Failed to send click event: {}", e);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handle_send_click() {
let (tx, mut rx) = mpsc::unbounded_channel();
let handle = InteractionHandle::new(tx);
handle.send_click(
[1.0, 2.0, 3.0],
Some("world/robot".to_string()),
"view_123".to_string(),
false,
);
let event = rx.try_recv().unwrap();
match event {
ViewerEvent::Click { position, entity_path, view_id, is_2d, .. } => {
assert_eq!(position, [1.0, 2.0, 3.0]);
assert_eq!(entity_path, Some("world/robot".to_string()));
assert_eq!(view_id, "view_123");
assert!(!is_2d);
}
}
}
#[test]
fn test_handle_is_cloneable() {
let (tx, _rx) = mpsc::unbounded_channel();
let handle1 = InteractionHandle::new(tx);
let _handle2 = handle1.clone();
}
}