use serde_json::json;
use crate::activity::Activity;
use crate::view::View;
use crate::error::Result;
pub struct Button {
view: View,
aid: i64,
}
impl Button {
pub fn new(activity: &mut Activity, text: &str, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"text": text,
"allcaps": false
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
eprintln!("[DEBUG] Button::new() - sending createButton...");
let response = activity.send_read(&json!({
"method": "createButton",
"params": params
}))?;
eprintln!("[DEBUG] Button::new() - got response: {:?}", response);
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(Button {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()> {
activity.send(&json!({
"method": "setText",
"params": {
"aid": self.aid,
"id": self.view.id(),
"text": text
}
}))?;
Ok(())
}
}