termux_gui/
view.rs

1//! Base View type and common view operations
2
3use serde_json::json;
4use crate::activity::Activity;
5use crate::error::Result;
6
7/// Special dimension constants for Android layouts
8pub const MATCH_PARENT: i32 = -1;
9pub const WRAP_CONTENT: i32 = -2;
10
11/// Base View structure
12pub struct View {
13    id: i64,
14}
15
16impl View {
17    /// Create a new View with the given ID
18    pub fn new(id: i64) -> Self {
19        View { id }
20    }
21    
22    /// Get the view ID
23    pub fn id(&self) -> i64 {
24        self.id
25    }
26    
27    /// Set view width
28    pub fn set_width(&self, activity: &mut Activity, width: i32) -> Result<()> {
29        activity.send(&json!({
30            "method": "setWidth",
31            "params": {
32                "aid": activity.id(),
33                "id": self.id,
34                "width": width
35            }
36        }))?;
37        Ok(())
38    }
39    
40    /// Set view height
41    pub fn set_height(&self, activity: &mut Activity, height: i32) -> Result<()> {
42        activity.send(&json!({
43            "method": "setHeight",
44            "params": {
45                "aid": activity.id(),
46                "id": self.id,
47                "height": height
48            }
49        }))?;
50        Ok(())
51    }
52    
53    /// Set view width and height
54    pub fn set_dimensions(&self, activity: &mut Activity, width: i32, height: i32) -> Result<()> {
55        // Call both methods since there's no combined setDimensions
56        self.set_width(activity, width)?;
57        self.set_height(activity, height)?;
58        Ok(())
59    }
60    
61    /// Set view margin
62    pub fn set_margin(&self, activity: &mut Activity, margin: i32) -> Result<()> {
63        activity.send(&json!({
64            "method": "setMargin",
65            "params": {
66                "aid": activity.id(),
67                "id": self.id,
68                "margin": margin
69            }
70        }))?;
71        Ok(())
72    }
73    
74    /// Set view width to WRAP_CONTENT
75    pub fn set_width_wrap_content(&self, activity: &mut Activity) -> Result<()> {
76        self.set_width(activity, WRAP_CONTENT)
77    }
78    
79    /// Set view height to WRAP_CONTENT
80    pub fn set_height_wrap_content(&self, activity: &mut Activity) -> Result<()> {
81        self.set_height(activity, WRAP_CONTENT)
82    }
83    
84    /// Set view width to MATCH_PARENT
85    pub fn set_width_match_parent(&self, activity: &mut Activity) -> Result<()> {
86        self.set_width(activity, MATCH_PARENT)
87    }
88    
89    /// Set view height to MATCH_PARENT
90    pub fn set_height_match_parent(&self, activity: &mut Activity) -> Result<()> {
91        self.set_height(activity, MATCH_PARENT)
92    }
93    
94    /// Set LinearLayout parameters for this view
95    /// 
96    /// # Arguments
97    /// * `activity` - The activity
98    /// * `weight` - Layout weight (higher weight = more space)
99    /// * `position` - Optional position index in the layout
100    pub fn set_linear_layout_params(&self, activity: &mut Activity, weight: i32, position: Option<i32>) -> Result<()> {
101        let mut params = json!({
102            "aid": activity.id(),
103            "id": self.id,
104            "weight": weight
105        });
106        
107        if let Some(pos) = position {
108            params["position"] = json!(pos);
109        }
110        
111        activity.send(&json!({
112            "method": "setLinearLayoutParams",
113            "params": params
114        }))?;
115        Ok(())
116    }
117}