1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use jni::objects::JValue;

use super::{Color, Context, Env, Object, Rect, Reference};

#[derive(Clone)]
pub struct View {
    reference: Reference,
}

impl View {
    pub fn new(env: &Env, context: impl Into<Context>) -> View {
        unsafe {
            let context = context.into();

            let object = env.call_constructor(
                "com/glacyr/polyhorn/View",
                "(Landroid/content/Context;)V",
                &[JValue::Object(context.as_reference().as_object()).into()],
            );

            View {
                reference: env.retain(object),
            }
        }
    }

    pub fn set_background_color(&mut self, env: &Env, color: Color) {
        unsafe {
            match color {
                Color::Unmanaged(value) => env.call_method(
                    self.reference.as_object(),
                    "setBackgroundColor",
                    "(I)V",
                    &[JValue::Int(value)],
                ),
                Color::Managed(_) => todo!("Managed colors are not yet available on Android."),
            };
        }
    }

    pub fn set_frame(&mut self, env: &Env, frame: Rect) {
        unsafe {
            env.call_method(
                self.reference.as_object(),
                "setFrame",
                "(Lcom/glacyr/polyhorn/Rect;)V",
                &[JValue::Object(frame.as_reference().as_object())],
            );
        }
    }

    pub fn bounds(&self, env: &Env) -> Rect {
        unsafe {
            match env.call_method(
                self.reference.as_object(),
                "getBounds",
                "()Lcom/glacyr/polyhorn/Rect;",
                &[],
            ) {
                JValue::Object(value) => Rect::from_reference(env.retain(value)),
                _ => unreachable!(),
            }
        }
    }

    pub fn add_view(&mut self, env: &Env, view: &View) {
        unsafe {
            env.call_method(
                self.reference.as_object(),
                "addView",
                "(Landroid/view/View;)V",
                &[JValue::Object(view.as_reference().as_object()).into()],
            );
        }
    }
}

impl Object for View {
    fn from_reference(reference: Reference) -> Self {
        View { reference }
    }

    fn as_reference(&self) -> &Reference {
        &self.reference
    }
}