termux_gui/components/
progress_bar.rs

1//! ProgressBar component
2
3use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8/// A ProgressBar displays progress from 0 to 100
9pub struct ProgressBar {
10    view: View,
11    aid: i64,
12}
13
14impl ProgressBar {
15    /// Create a new ProgressBar
16    pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
17        let mut params = json!({
18            "aid": activity.id()
19        });
20        
21        // Only set parent if explicitly provided
22        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    /// Get the view ID
42    pub fn id(&self) -> i64 {
43        self.view.id()
44    }
45    
46    /// Get the underlying View
47    pub fn view(&self) -> &View {
48        &self.view
49    }
50    
51    /// Set the progress value (0-100)
52    /// 
53    /// The progress must be an integer from 0 to 100 (inclusive).
54    pub fn set_progress(&self, activity: &mut Activity, progress: i32) -> Result<()> {
55        // Clamp progress to valid range
56        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}