termux_gui/components/
progress_bar.rs1use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8pub struct ProgressBar {
10 view: View,
11 aid: i64,
12}
13
14impl ProgressBar {
15 pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
17 let mut params = json!({
18 "aid": activity.id()
19 });
20
21 if let Some(parent_id) = parent {
23 params["parent"] = json!(parent_id);
24 }
25
26 let response = activity.send_read(&json!({
27 "method": "createProgressBar",
28 "params": params
29 }))?;
30
31 let id = response
32 .as_i64()
33 .ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
34
35 Ok(ProgressBar {
36 view: View::new(id),
37 aid: activity.id(),
38 })
39 }
40
41 pub fn id(&self) -> i64 {
43 self.view.id()
44 }
45
46 pub fn view(&self) -> &View {
48 &self.view
49 }
50
51 pub fn set_progress(&self, activity: &mut Activity, progress: i32) -> Result<()> {
55 let progress = progress.clamp(0, 100);
57
58 activity.send(&json!({
59 "method": "setProgress",
60 "params": {
61 "aid": self.aid,
62 "id": self.view.id(),
63 "progress": progress
64 }
65 }))?;
66 Ok(())
67 }
68}