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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::{
    makepad_draw::*,
    view::*,
    widget::*,
};

live_design!{
    KeyboardViewBase = {{KeyboardView}} {}
}


#[derive(Live)]
pub struct KeyboardView {
    #[deref] view: View,
    #[rust] area: Area,
    #[live] outer_layout: Layout,
    #[live] outer_walk: Walk,
    #[live] keyboard_walk: Walk,
    #[live] keyboard_min_shift: f64,
    #[rust] next_frame: NextFrame,
    
    #[rust] keyboard_shift: f64,
    #[rust(AnimState::Closed)] anim_state: AnimState,
    #[rust] draw_state: DrawStateWrap<Walk>,
}

enum AnimState{
    Closed,
    Opening{duration:f64, start_time:f64, ease:Ease, height:f64},
    Open,
    Closing{duration:f64, start_time:f64,  ease:Ease, height:f64}
}

impl LiveHook for KeyboardView {
    fn before_live_design(cx:&mut Cx){
        register_widget!(cx, KeyboardView)
    }
}

impl KeyboardView {

    fn compute_max_height(&self, height:f64, cx:&Cx)->f64{
        let self_rect = self.area.get_rect(cx);
        let ime_rect = cx.get_ime_area_rect();
        let av_height = self_rect.size.y - height;
        let ime_height = ime_rect.size.y + ime_rect.pos.y + self.keyboard_min_shift;
        if  ime_height > av_height {
            return ime_height - av_height 
        }
        0.0
    }
    
    fn begin(&mut self, cx: &mut Cx2d, walk: Walk) { 
        cx.begin_turtle(walk, self.outer_layout.with_scroll(dvec2(0.,self.keyboard_shift)));
    }
    
    fn end(&mut self, cx: &mut Cx2d) {
        cx.end_turtle_with_area(&mut self.area);
    }
}

impl Widget for KeyboardView {
    fn redraw(&mut self, cx: &mut Cx) {
        self.area.redraw(cx);
    }
    
    fn handle_widget_event_with(&mut self, cx: &mut Cx, event: &Event, dispatch_action: &mut dyn FnMut(&mut Cx, WidgetActionItem)) {
        if let Some(e) = self.next_frame.is_event(event){
            match &self.anim_state{
                AnimState::Opening{duration, start_time, ease, height}=>{
                    if e.time - start_time < *duration{
                        self.keyboard_shift = ease.map((e.time - start_time)/duration) * height;
                        self.next_frame = cx.new_next_frame();
                    }
                    else{
                        self.keyboard_shift = *height;
                        self.anim_state = AnimState::Open;
                    }
                    self.redraw(cx);
                }
                AnimState::Closing{duration, start_time, ease, height}=>{
                    if e.time - start_time < *duration{
                        self.keyboard_shift = (1.0-ease.map((e.time - start_time)/duration)) * height;
                        self.next_frame = cx.new_next_frame();
                    }
                    else{ 
                        self.keyboard_shift = 0.0;
                        self.anim_state = AnimState::Closed;
                    }
                    self.redraw(cx);
                }
                _=>()
            }
        }
        match event{
            Event::VirtualKeyboard(vk)=>{
                match vk{
                    VirtualKeyboardEvent::WillShow{time, height, ease, duration}=>{
                        // ok so lets run an animation with next
                        let height = self.compute_max_height(*height, cx);
                        self.anim_state = AnimState::Opening{
                            duration: *duration,
                            start_time: *time,
                            ease: *ease,
                            height: height
                        };
                        self.next_frame = cx.new_next_frame();
                    }
                    VirtualKeyboardEvent::WillHide{time, height:_, ease, duration}=>{
                        self.anim_state = AnimState::Closing{
                            height: self.keyboard_shift,
                            duration: *duration,
                            start_time: *time,
                            ease: *ease,
                        };
                        self.next_frame = cx.new_next_frame();
                    }
                    VirtualKeyboardEvent::DidShow{time:_, height}=>{
                        if let AnimState::Closed = self.anim_state{
                            self.keyboard_shift = self.compute_max_height(*height, cx);
                        }
                        self.anim_state = AnimState::Open;
                        self.redraw(cx);
                    }
                    VirtualKeyboardEvent::DidHide{time:_}=>{
                        self.anim_state = AnimState::Closed;
                        self.keyboard_shift = 0.0;
                        self.redraw(cx);
                    }
                }
            }
            _=>()
        }
        self.view.handle_widget_event_with(cx, event, dispatch_action);
    }
    
    fn walk(&mut self, _cx:&mut Cx) -> Walk {
        self.outer_walk
    }
    
    fn draw_walk_widget(&mut self, cx: &mut Cx2d, walk: Walk) -> WidgetDraw {
        if self.draw_state.begin_with(cx, &(), |cx,_|{
            self.view.walk(cx)
        }){
            self.begin(cx, walk);
        }
        if let Some(walk) = self.draw_state.get() {
            self.view.draw_walk_widget(cx, walk)?;
        }
        self.end(cx);
        WidgetDraw::done()
    }

    fn find_widgets(&mut self, path: &[LiveId], cached: WidgetCache, results: &mut WidgetSet) {
        self.view.find_widgets(path, cached, results);
    }
}