termux_gui/components/
edit_text.rs

1//! EditText component
2
3use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8/// An EditText allows text input
9pub struct EditText {
10    view: View,
11    aid: i64,
12}
13
14impl EditText {
15    /// Create a new EditText (single-line by default)
16    pub fn new(activity: &mut Activity, text: &str, parent: Option<i64>) -> Result<Self> {
17        Self::new_with_options(activity, text, parent, true, "text")
18    }
19    
20    /// Create a new multi-line EditText
21    pub fn new_multiline(activity: &mut Activity, text: &str, parent: Option<i64>) -> Result<Self> {
22        Self::new_with_options(activity, text, parent, false, "textMultiLine")
23    }
24    
25    /// Create a new EditText with full options
26    pub fn new_with_options(
27        activity: &mut Activity,
28        text: &str,
29        parent: Option<i64>,
30        singleline: bool,
31        input_type: &str
32    ) -> Result<Self> {
33        let mut params = json!({
34            "aid": activity.id(),
35            "text": text,
36            "singleline": singleline,
37            "line": true,
38            "blockinput": false,
39            "type": input_type
40        });
41        
42        // Only set parent if explicitly provided
43        if let Some(parent_id) = parent {
44            params["parent"] = json!(parent_id);
45        }
46        
47        let response = activity.send_read(&json!({
48            "method": "createEditText",
49            "params": params
50        }))?;
51        
52        let id = response
53            .as_i64()
54            .ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
55        
56        Ok(EditText {
57            view: View::new(id),
58            aid: activity.id(),
59        })
60    }
61    
62    /// Get the view ID
63    pub fn id(&self) -> i64 {
64        self.view.id()
65    }
66    
67    /// Get the underlying View
68    pub fn view(&self) -> &View {
69        &self.view
70    }
71    
72    /// Set the text content
73    pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()> {
74        activity.send(&json!({
75            "method": "setText",
76            "params": {
77                "aid": self.aid,
78                "id": self.view.id(),
79                "text": text
80            }
81        }))?;
82        Ok(())
83    }
84    
85    /// Set hint text
86    pub fn set_hint(&self, activity: &mut Activity, hint: &str) -> Result<()> {
87        activity.send(&json!({
88            "method": "setHint",
89            "params": {
90                "aid": self.aid,
91                "id": self.view.id(),
92                "hint": hint
93            }
94        }))?;
95        Ok(())
96    }
97    
98    /// Get the text content
99    pub fn get_text(&self, activity: &mut Activity) -> Result<String> {
100        let response = activity.send_read(&json!({
101            "method": "getText",
102            "params": {
103                "aid": self.aid,
104                "id": self.view.id()
105            }
106        }))?;
107        
108        Ok(response
109            .as_str()
110            .unwrap_or("")
111            .to_string())
112    }
113}