termux_gui/components/
button.rs

1//! Button component
2
3use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8/// A Button that can be clicked
9pub struct Button {
10    view: View,
11    aid: i64,
12}
13
14impl Button {
15    /// Create a new Button
16    pub fn new(activity: &mut Activity, text: &str, parent: Option<i64>) -> Result<Self> {
17        let mut params = json!({
18            "aid": activity.id(),
19            "text": text,
20            "allcaps": false
21        });
22        
23        // Only set parent if explicitly provided
24        if let Some(parent_id) = parent {
25            params["parent"] = json!(parent_id);
26        }
27        
28        eprintln!("[DEBUG] Button::new() - sending createButton...");
29        let response = activity.send_read(&json!({
30            "method": "createButton",
31            "params": params
32        }))?;
33        eprintln!("[DEBUG] Button::new() - got response: {:?}", response);
34        
35        let id = response
36            .as_i64()
37            .ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
38        
39        Ok(Button {
40            view: View::new(id),
41            aid: activity.id(),
42        })
43    }
44    
45    /// Get the view ID
46    pub fn id(&self) -> i64 {
47        self.view.id()
48    }
49    
50    /// Get the underlying View
51    pub fn view(&self) -> &View {
52        &self.view
53    }
54    
55    /// Set the button text
56    pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()> {
57        activity.send(&json!({
58            "method": "setText",
59            "params": {
60                "aid": self.aid,
61                "id": self.view.id(),
62                "text": text
63            }
64        }))?;
65        Ok(())
66    }
67}