termux_gui/components/
spinner.rs

1//! Spinner (dropdown) component
2
3use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8/// A Spinner is a dropdown list
9pub struct Spinner {
10    view: View,
11    aid: i64,
12}
13
14impl Spinner {
15    /// Create a new Spinner
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": "createSpinner",
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(Spinner {
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 list of options
52    pub fn set_list(&self, activity: &mut Activity, items: &[&str]) -> Result<()> {
53        activity.send(&json!({
54            "method": "setList",
55            "params": {
56                "aid": self.aid,
57                "id": self.view.id(),
58                "list": items
59            }
60        }))?;
61        Ok(())
62    }
63    
64    /// Refresh the spinner (needed after setList to ensure display is updated)
65    pub fn refresh(&self, activity: &mut Activity) -> Result<()> {
66        activity.send(&json!({
67            "method": "refreshSpinner",
68            "params": {
69                "aid": self.aid,
70                "id": self.view.id()
71            }
72        }))?;
73        Ok(())
74    }
75}