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
//! ImageView component
use serde_json::json;
use crate::activity::Activity;
use crate::view::View;
use crate::error::Result;
/// An ImageView displays images
pub struct ImageView {
view: View,
aid: i64,
}
impl ImageView {
/// Create a new ImageView
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id()
});
// Only set parent if explicitly provided
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createImageView",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(ImageView {
view: View::new(id),
aid: activity.id(),
})
}
/// Get the view ID
pub fn id(&self) -> i64 {
self.view.id()
}
/// Get the underlying View
pub fn view(&self) -> &View {
&self.view
}
/// Set image from base64 encoded string
///
/// The image should be base64 encoded PNG or JPEG data.
/// You can use the `base64` crate to encode image bytes.
pub fn set_image(&self, activity: &mut Activity, img_base64: &str) -> Result<()> {
activity.send(&json!({
"method": "setImage",
"params": {
"aid": self.aid,
"id": self.view.id(),
"img": img_base64
}
}))?;
Ok(())
}
/// Refresh the ImageView
///
/// This redraws the ImageView. Needed when using shared buffers.
pub fn refresh(&self, activity: &mut Activity) -> Result<()> {
activity.send(&json!({
"method": "refreshImageView",
"params": {
"aid": self.aid,
"id": self.view.id()
}
}))?;
Ok(())
}
}