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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use polyhorn_core::CommandBuffer;
use polyhorn_ios_sys::coregraphics::CGRect;
use polyhorn_ios_sys::foundation::NSNumber;
use polyhorn_ios_sys::polykit::{PLYCallback, PLYKeyboardAvoidingView, PLYView};
use polyhorn_ui::geometry::{ByEdge, Dimension};
use polyhorn_ui::layout::LayoutAxisY;
use polyhorn_ui::styles::{Position, Relative, ViewStyle};
use std::sync::Arc;
use crate::prelude::*;
use crate::raw::{Builtin, Container, ContainerID, OpaqueContainer};
use crate::{Key, Reference};
#[derive(Default)]
pub struct LayoutAdjustment {
margin: ByEdge<Dimension<f32>>,
}
impl LayoutAdjustment {
pub fn new() -> LayoutAdjustment {
Default::default()
}
pub fn margin_bottom(self, bottom: Dimension<f32>) -> LayoutAdjustment {
LayoutAdjustment {
margin: ByEdge {
vertical: LayoutAxisY {
bottom,
..self.margin.vertical
},
..self.margin
},
..self
}
}
}
#[derive(Default)]
pub struct KeyboardAvoidingView {
pub transform: Option<Arc<dyn Fn(f32) -> LayoutAdjustment + Send + Sync>>,
}
impl Container for PLYKeyboardAvoidingView {
fn mount(&mut self, child: &mut OpaqueContainer) {
if let Some(view) = child.container().to_view() {
PLYKeyboardAvoidingView::to_view(self).add_subview(&view)
}
}
fn unmount(&mut self) {
PLYKeyboardAvoidingView::to_view(self).remove_from_superview();
}
fn to_view(&self) -> Option<PLYView> {
Some(PLYKeyboardAvoidingView::to_view(self))
}
}
impl Component for KeyboardAvoidingView {
fn render(&self, manager: &mut Manager) -> Element {
let view_ref: Reference<Option<ContainerID>> = use_reference!(manager, None);
let transform = self.transform.clone();
use_layout_effect!(manager, move |link, buffer| {
let id = match view_ref.apply(link, |id| id.to_owned()) {
Some(id) => id,
None => return,
};
buffer.mutate(&[id], move |containers, _| {
let container = &mut containers[0];
let layout = match container.layout() {
Some(layout) => layout.clone(),
None => return,
};
if let Some(view) = container.downcast_mut::<PLYKeyboardAvoidingView>() {
{
let layout = layout.clone();
view.set_on_keyboard(PLYCallback::new(move |height: NSNumber| {
if let Some(transform) = transform.as_ref() {
let adjustment = transform(height.float_value());
layout.set_style(ViewStyle {
position: Position::Relative(Relative {
flex_grow: 1.0,
..Default::default()
}),
margin: ByEdge {
vertical: LayoutAxisY {
bottom: adjustment.margin.vertical.bottom,
..Default::default()
},
..Default::default()
},
..Default::default()
});
layout.layouter().write().unwrap().recompute_roots();
}
}));
}
view.to_view().set_layout(move || {
let current = layout.current();
CGRect::new(
current.origin.x as _,
current.origin.y as _,
current.size.width as _,
current.size.height as _,
)
});
}
});
});
Element::builtin(
Key::new(()),
Builtin::KeyboardAvoidingView,
manager.children(),
Some(view_ref.weak(manager)),
)
}
}