polyhorn_android/components/
view.rs

1use polyhorn_android_sys::Rect;
2use polyhorn_core::CommandBuffer;
3use polyhorn_ui::geometry::Size;
4use polyhorn_ui::layout::LayoutGuide;
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8use crate::prelude::*;
9use crate::raw::{Builtin, Container, Convert, Environment, OpaqueContainer, Platform};
10use crate::{Component, Key};
11
12pub struct AnimationHandle;
13
14impl std::future::Future for AnimationHandle {
15    type Output = ();
16
17    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
18        todo!()
19    }
20}
21
22impl polyhorn_ui::animation::AnimationHandle for AnimationHandle {}
23
24pub struct Animator;
25
26impl polyhorn_ui::animation::Animator for Animator {
27    type AnimationHandle = AnimationHandle;
28
29    fn start(&mut self, _animation: polyhorn_ui::animation::Animation) -> Self::AnimationHandle {
30        todo!()
31    }
32}
33
34pub struct ViewHandle;
35
36impl polyhorn_ui::animation::Animatable for ViewHandle {
37    type Animator = Animator;
38    type CommandBuffer = ();
39
40    fn animate<F>(&mut self, _animations: F)
41    where
42        F: FnOnce(&mut Self::Animator) + Send + 'static,
43    {
44        todo!()
45    }
46
47    fn animate_with_buffer<F>(&mut self, _buffer: &mut Self::CommandBuffer, _animations: F)
48    where
49        F: FnOnce(&mut Self::Animator) + Send + 'static,
50    {
51        todo!()
52    }
53}
54
55impl polyhorn_ui::handles::ViewHandle for ViewHandle {
56    fn layout_guide(&self) -> LayoutGuide<f32> {
57        todo!()
58    }
59
60    fn size<F>(&self, _callback: F)
61    where
62        F: FnOnce(Size<f32>) + Send + 'static,
63    {
64        todo!()
65    }
66
67    fn size_with_buffer<F>(&self, _buffer: &mut Self::CommandBuffer, _callback: F)
68    where
69        F: FnOnce(Size<f32>) + Send + 'static,
70    {
71        todo!()
72    }
73}
74
75impl Container for polyhorn_android_sys::View {
76    fn mount(&mut self, child: &mut OpaqueContainer, environment: &mut Environment) {
77        if let Some(view) = child.container().to_view() {
78            self.add_view(environment.env(), &view)
79        }
80    }
81
82    fn unmount(&mut self) {}
83
84    fn to_view(&self) -> Option<polyhorn_android_sys::View> {
85        Some(self.clone())
86    }
87}
88
89pub type View = polyhorn_ui::components::View<Platform, ViewHandle>;
90
91impl Component for View {
92    fn render(&self, manager: &mut Manager) -> Element {
93        let reference = use_reference!(manager, None);
94
95        let style = self.style;
96
97        use_layout_effect!(manager, move |link, buffer| {
98            if let Some(view) = reference.apply(link, |&mut id| id) {
99                buffer.mutate(&[view], move |views, environment| {
100                    let layout = views[0].layout().unwrap().current();
101
102                    if let Some(view) = views[0].downcast_mut::<polyhorn_android_sys::View>() {
103                        view.set_background_color(
104                            environment.env(),
105                            style.background_color.convert(environment.env()),
106                        );
107                        view.set_frame(
108                            environment.env(),
109                            Rect::new(
110                                environment.env(),
111                                layout.origin.x,
112                                layout.origin.y,
113                                layout.size.width,
114                                layout.size.height,
115                            ),
116                        );
117                    }
118                });
119            }
120        });
121
122        Element::builtin(
123            Key::new(()),
124            Builtin::View(self.style),
125            manager.children(),
126            Some(reference.weak(manager)),
127        )
128    }
129}