polyhorn_android_sys/
view.rs

1use jni::objects::JValue;
2
3use super::{Color, Context, Env, Object, Rect, Reference};
4
5#[derive(Clone)]
6pub struct View {
7    reference: Reference,
8}
9
10impl View {
11    pub fn new(env: &Env, context: impl Into<Context>) -> View {
12        unsafe {
13            let context = context.into();
14
15            let object = env.call_constructor(
16                "com/glacyr/polyhorn/View",
17                "(Landroid/content/Context;)V",
18                &[JValue::Object(context.as_reference().as_object()).into()],
19            );
20
21            View {
22                reference: env.retain(object),
23            }
24        }
25    }
26
27    pub fn set_background_color(&mut self, env: &Env, color: Color) {
28        unsafe {
29            match color {
30                Color::Unmanaged(value) => env.call_method(
31                    self.reference.as_object(),
32                    "setBackgroundColor",
33                    "(I)V",
34                    &[JValue::Int(value)],
35                ),
36                Color::Managed(_) => todo!("Managed colors are not yet available on Android."),
37            };
38        }
39    }
40
41    pub fn set_frame(&mut self, env: &Env, frame: Rect) {
42        unsafe {
43            env.call_method(
44                self.reference.as_object(),
45                "setFrame",
46                "(Lcom/glacyr/polyhorn/Rect;)V",
47                &[JValue::Object(frame.as_reference().as_object())],
48            );
49        }
50    }
51
52    pub fn bounds(&self, env: &Env) -> Rect {
53        unsafe {
54            match env.call_method(
55                self.reference.as_object(),
56                "getBounds",
57                "()Lcom/glacyr/polyhorn/Rect;",
58                &[],
59            ) {
60                JValue::Object(value) => Rect::from_reference(env.retain(value)),
61                _ => unreachable!(),
62            }
63        }
64    }
65
66    pub fn add_view(&mut self, env: &Env, view: &View) {
67        unsafe {
68            env.call_method(
69                self.reference.as_object(),
70                "addView",
71                "(Landroid/view/View;)V",
72                &[JValue::Object(view.as_reference().as_object()).into()],
73            );
74        }
75    }
76}
77
78impl Object for View {
79    fn from_reference(reference: Reference) -> Self {
80        View { reference }
81    }
82
83    fn as_reference(&self) -> &Reference {
84        &self.reference
85    }
86}