1use serde_json::json;
4use crate::activity::Activity;
5use crate::error::Result;
6
7pub const MATCH_PARENT: i32 = -1;
9pub const WRAP_CONTENT: i32 = -2;
10
11pub struct View {
13 id: i64,
14}
15
16impl View {
17 pub fn new(id: i64) -> Self {
19 View { id }
20 }
21
22 pub fn id(&self) -> i64 {
24 self.id
25 }
26
27 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 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 pub fn set_dimensions(&self, activity: &mut Activity, width: i32, height: i32) -> Result<()> {
55 self.set_width(activity, width)?;
57 self.set_height(activity, height)?;
58 Ok(())
59 }
60
61 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 pub fn set_width_wrap_content(&self, activity: &mut Activity) -> Result<()> {
76 self.set_width(activity, WRAP_CONTENT)
77 }
78
79 pub fn set_height_wrap_content(&self, activity: &mut Activity) -> Result<()> {
81 self.set_height(activity, WRAP_CONTENT)
82 }
83
84 pub fn set_width_match_parent(&self, activity: &mut Activity) -> Result<()> {
86 self.set_width(activity, MATCH_PARENT)
87 }
88
89 pub fn set_height_match_parent(&self, activity: &mut Activity) -> Result<()> {
91 self.set_height(activity, MATCH_PARENT)
92 }
93
94 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}