use serde_json::json;
use crate::activity::Activity;
use crate::view::View;
use crate::error::Result;
pub struct LinearLayout {
view: View,
#[allow(dead_code)]
aid: i64,
}
impl LinearLayout {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
Self::new_with_orientation(activity, parent, true)
}
pub fn new_with_orientation(activity: &mut Activity, parent: Option<i64>, vertical: bool) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"vertical": vertical
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createLinearLayout",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(LinearLayout {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
}
pub struct NestedScrollView {
view: View,
#[allow(dead_code)]
aid: i64,
}
impl NestedScrollView {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"nobar": false,
"snapping": false
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createNestedScrollView",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(NestedScrollView {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
}
pub struct FrameLayout {
view: View,
#[allow(dead_code)]
aid: i64,
}
impl FrameLayout {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id()
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createFrameLayout",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(FrameLayout {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
}
pub struct GridLayout {
view: View,
#[allow(dead_code)]
aid: i64,
#[allow(dead_code)]
rows: i32,
#[allow(dead_code)]
cols: i32,
}
impl GridLayout {
pub fn new(activity: &mut Activity, rows: i32, cols: i32, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"rows": rows,
"cols": cols
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createGridLayout",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(GridLayout {
view: View::new(id),
aid: activity.id(),
rows,
cols,
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
}
pub struct HorizontalScrollView {
view: View,
#[allow(dead_code)]
aid: i64,
}
impl HorizontalScrollView {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"nobar": false,
"snapping": false,
"fillviewport": true });
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createHorizontalScrollView",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(HorizontalScrollView {
view: View::new(id),
aid: activity.id(),
})
}
pub fn new_with_params(activity: &mut Activity, parent: Option<i64>,
fillviewport: bool, snapping: bool, nobar: bool) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"nobar": nobar,
"snapping": snapping,
"fillviewport": fillviewport
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createHorizontalScrollView",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(HorizontalScrollView {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
pub fn get_scroll_position(&self, activity: &mut Activity) -> Result<(i32, i32)> {
let response = activity.send_read(&json!({
"method": "getScrollPosition",
"params": {
"aid": self.aid,
"id": self.view.id()
}
}))?;
if let Some(arr) = response.as_array() {
let x = arr.get(0).and_then(|v| v.as_i64()).unwrap_or(0) as i32;
let y = arr.get(1).and_then(|v| v.as_i64()).unwrap_or(0) as i32;
Ok((x, y))
} else {
Ok((0, 0))
}
}
pub fn set_scroll_position(&self, activity: &mut Activity, x: i32, y: i32, smooth: bool) -> Result<()> {
activity.send(&json!({
"method": "setScrollPosition",
"params": {
"aid": self.aid,
"id": self.view.id(),
"x": x,
"y": y,
"soft": smooth
}
}))?;
Ok(())
}
}
pub struct SwipeRefreshLayout {
view: View,
aid: i64,
}
impl SwipeRefreshLayout {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id()
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createSwipeRefreshLayout",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(SwipeRefreshLayout {
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_refreshing(&self, activity: &mut Activity, refreshing: bool) -> Result<()> {
activity.send(&json!({
"method": "setRefreshing",
"params": {
"aid": self.aid,
"id": self.view.id(),
"refresh": refreshing
}
}))?;
Ok(())
}
}
pub struct TabLayout {
view: View,
aid: i64,
}
impl TabLayout {
pub fn new(activity: &mut Activity, parent: Option<i64>) -> Result<Self> {
let mut params = json!({
"aid": activity.id()
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createTabLayout",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(TabLayout {
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_list(&self, activity: &mut Activity, tabs: &[&str]) -> Result<()> {
activity.send(&json!({
"method": "setList",
"params": {
"aid": self.aid,
"id": self.view.id(),
"list": tabs
}
}))?;
Ok(())
}
pub fn select_tab(&self, activity: &mut Activity, index: usize) -> Result<()> {
activity.send(&json!({
"method": "selectTab",
"params": {
"aid": self.aid,
"id": self.view.id(),
"tab": index
}
}))?;
Ok(())
}
}