termux_gui/components/
text_view.rs1use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8pub struct TextView {
10 view: View,
11 aid: i64,
12}
13
14impl TextView {
15 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 });
21
22 if let Some(parent_id) = parent {
24 params["parent"] = json!(parent_id);
25 }
26
27 eprintln!("[DEBUG] TextView::new() - sending createTextView...");
28 let response = activity.send_read(&json!({
29 "method": "createTextView",
30 "params": params
31 }))?;
32 eprintln!("[DEBUG] TextView::new() - got response: {:?}", response);
33
34 let id = response
35 .as_i64()
36 .ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
37
38 Ok(TextView {
39 view: View::new(id),
40 aid: activity.id(),
41 })
42 }
43
44 pub fn id(&self) -> i64 {
46 self.view.id()
47 }
48
49 pub fn view(&self) -> &View {
51 &self.view
52 }
53
54 pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()> {
56 activity.send(&json!({
57 "method": "setText",
58 "params": {
59 "aid": self.aid,
60 "id": self.view.id(),
61 "text": text
62 }
63 }))?;
64 Ok(())
65 }
66
67 pub fn get_text(&self, activity: &mut Activity) -> Result<String> {
69 let response = activity.send_read(&json!({
70 "method": "getText",
71 "params": {
72 "aid": self.aid,
73 "id": self.view.id()
74 }
75 }))?;
76
77 Ok(response.as_str().unwrap_or("").to_string())
78 }
79
80 pub fn set_text_size(&self, activity: &mut Activity, size: i32) -> Result<()> {
82 activity.send(&json!({
83 "method": "setTextSize",
84 "params": {
85 "aid": self.aid,
86 "id": self.view.id(),
87 "size": size
88 }
89 }))?;
90 Ok(())
91 }
92
93 pub fn set_text_color(&self, activity: &mut Activity, color: i32) -> Result<()> {
95 activity.send(&json!({
96 "method": "setTextColor",
97 "params": {
98 "aid": self.aid,
99 "id": self.view.id(),
100 "color": color
101 }
102 }))?;
103 Ok(())
104 }
105}