use serde_json::json;
use crate::activity::Activity;
use crate::error::Result;
pub const MATCH_PARENT: i32 = -1;
pub const WRAP_CONTENT: i32 = -2;
pub struct View {
id: i64,
}
impl View {
pub fn new(id: i64) -> Self {
View { id }
}
pub fn id(&self) -> i64 {
self.id
}
pub fn set_width(&self, activity: &mut Activity, width: i32) -> Result<()> {
activity.send(&json!({
"method": "setWidth",
"params": {
"aid": activity.id(),
"id": self.id,
"width": width
}
}))?;
Ok(())
}
pub fn set_width_px(&self, activity: &mut Activity, width: i32) -> Result<()> {
activity.send(&json!({
"method": "setWidth",
"params": {
"aid": activity.id(),
"id": self.id,
"width": width,
"px": true
}
}))?;
Ok(())
}
pub fn set_height(&self, activity: &mut Activity, height: i32) -> Result<()> {
activity.send(&json!({
"method": "setHeight",
"params": {
"aid": activity.id(),
"id": self.id,
"height": height
}
}))?;
Ok(())
}
pub fn set_height_px(&self, activity: &mut Activity, height: i32) -> Result<()> {
activity.send(&json!({
"method": "setHeight",
"params": {
"aid": activity.id(),
"id": self.id,
"height": height,
"px": true
}
}))?;
Ok(())
}
pub fn set_dimensions(&self, activity: &mut Activity, width: i32, height: i32) -> Result<()> {
self.set_width(activity, width)?;
self.set_height(activity, height)?;
Ok(())
}
pub fn set_margin(&self, activity: &mut Activity, margin: i32) -> Result<()> {
activity.send(&json!({
"method": "setMargin",
"params": {
"aid": activity.id(),
"id": self.id,
"margin": margin
}
}))?;
Ok(())
}
pub fn set_width_wrap_content(&self, activity: &mut Activity) -> Result<()> {
self.set_width(activity, WRAP_CONTENT)
}
pub fn set_height_wrap_content(&self, activity: &mut Activity) -> Result<()> {
self.set_height(activity, WRAP_CONTENT)
}
pub fn set_width_match_parent(&self, activity: &mut Activity) -> Result<()> {
self.set_width(activity, MATCH_PARENT)
}
pub fn set_height_match_parent(&self, activity: &mut Activity) -> Result<()> {
self.set_height(activity, MATCH_PARENT)
}
pub fn set_linear_layout_params(&self, activity: &mut Activity, weight: i32, position: Option<i32>) -> Result<()> {
let mut params = json!({
"aid": activity.id(),
"id": self.id,
"weight": weight
});
if let Some(pos) = position {
params["position"] = json!(pos);
}
activity.send(&json!({
"method": "setLinearLayoutParams",
"params": params
}))?;
Ok(())
}
pub fn set_grid_layout_params(&self, activity: &mut Activity,
row: i32, col: i32,
row_size: i32, col_size: i32,
alignment_row: &str, alignment_col: &str) -> Result<()> {
activity.send(&json!({
"method": "setGridLayoutParams",
"params": {
"aid": activity.id(),
"id": self.id,
"row": row,
"col": col,
"rowsize": row_size,
"colsize": col_size,
"alignmentrow": alignment_row,
"alignmentcol": alignment_col
}
}))?;
Ok(())
}
pub fn get_dimensions(&self, activity: &mut Activity) -> Result<(i32, i32)> {
let response = activity.send_read(&json!({
"method": "getDimensions",
"params": {
"aid": activity.id(),
"id": self.id
}
}))?;
if let Some(arr) = response.as_array() {
let width = arr.get(0).and_then(|v| v.as_i64()).unwrap_or(0) as i32;
let height = arr.get(1).and_then(|v| v.as_i64()).unwrap_or(0) as i32;
Ok((width, height))
} else {
Ok((0, 0))
}
}
pub fn set_background_color(&self, activity: &mut Activity, color: i32) -> Result<()> {
activity.send(&json!({
"method": "setBackgroundColor",
"params": {
"aid": activity.id(),
"id": self.id,
"color": color
}
}))?;
Ok(())
}
}