termux_gui/components/
spinner.rs1use serde_json::json;
4use crate::activity::Activity;
5use crate::view::View;
6use crate::error::Result;
7
8pub struct Spinner {
10 view: View,
11 aid: i64,
12}
13
14impl Spinner {
15 pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
17 let mut params = json!({
18 "aid": activity.id()
19 });
20
21 if let Some(parent_id) = parent {
23 params["parent"] = json!(parent_id);
24 }
25
26 let response = activity.send_read(&json!({
27 "method": "createSpinner",
28 "params": params
29 }))?;
30
31 let id = response
32 .as_i64()
33 .ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
34
35 Ok(Spinner {
36 view: View::new(id),
37 aid: activity.id(),
38 })
39 }
40
41 pub fn id(&self) -> i64 {
43 self.view.id()
44 }
45
46 pub fn view(&self) -> &View {
48 &self.view
49 }
50
51 pub fn set_list(&self, activity: &mut Activity, items: &[&str]) -> Result<()> {
53 activity.send(&json!({
54 "method": "setList",
55 "params": {
56 "aid": self.aid,
57 "id": self.view.id(),
58 "list": items
59 }
60 }))?;
61 Ok(())
62 }
63
64 pub fn refresh(&self, activity: &mut Activity) -> Result<()> {
66 activity.send(&json!({
67 "method": "refreshSpinner",
68 "params": {
69 "aid": self.aid,
70 "id": self.view.id()
71 }
72 }))?;
73 Ok(())
74 }
75}