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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::view::{Layout, View, ViewAlign, ViewBase, ViewInput, ViewTree, Decorator};
use alloc::boxed::Box;
use components_arena::{Arena, Component, ComponentStop, Id, NewtypeComponentId};
use components_arena::with_arena_in_state_part;
use core::any::{Any, TypeId};
use core::fmt::Debug;
use dep_obj::{Change, DepObjId, DepType, DetachedDepObjId, Convenient, DepProp};
use dep_obj::{dep_type, impl_dep_obj, with_builder};
use dep_obj::binding::{Binding1, Bindings, Re, Binding};
use downcast_rs::{Downcast, impl_downcast};
use dyn_context::{State, StateExt, Stop};
use errno_no_std::Errno;
use macro_attr_2018::macro_attr;
use tuifw_screen_base::Screen;

pub trait WidgetBehavior {
    fn init_bindings(&self, widget: Widget, state: &mut dyn State);
    fn drop_bindings(&self, widget: Widget, state: &mut dyn State);
}

pub enum WidgetObjKey { }

pub trait WidgetObj: Downcast + DepType<Id=Widget, DepObjKey=WidgetObjKey> {
    fn behavior(&self) -> &'static dyn WidgetBehavior;
}

impl_downcast!(WidgetObj);

macro_attr! {
    #[derive(NewtypeComponentId!)]
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
    pub struct Widget(Id<WidgetNode>);
}

macro_attr! {
    #[derive(Debug, Component!(stop=WidgetStop))]
    struct WidgetNode {
        view: Option<View>,
        base: WidgetBase,
        obj: Box<dyn WidgetObj>,
    }
}

#[derive(Stop)]
pub struct WidgetTree {
    #[stop]
    widget_arena: Arena<WidgetNode>,
    #[stop]
    view_tree: ViewTree,
}

impl ComponentStop for WidgetStop {
    with_arena_in_state_part!(WidgetTree { .widget_arena });

    fn stop(&self, state: &mut dyn State, id: Id<WidgetNode>) {
        Widget(id).drop_bindings(state);
    }
}

impl State for WidgetTree {
    fn get_raw(&self, ty: TypeId) -> Option<&dyn Any> {
        if ty == TypeId::of::<WidgetTree>() { return Some(self); }
        self.view_tree.get_raw(ty)
    }

    fn get_mut_raw(&mut self, ty: TypeId) -> Option<&mut dyn Any> {
        if ty == TypeId::of::<WidgetTree>() { return Some(self); }
        self.view_tree.get_mut_raw(ty)
    }
}

impl WidgetTree {
    pub fn new(screen: Box<dyn Screen>, bindings: &mut Bindings) -> Self {
        let widget_arena = Arena::new();
        let view_tree = ViewTree::new(screen, bindings);
        WidgetTree {
            widget_arena,
            view_tree,
        }
    }

    pub fn root(&self) -> View {
        self.view_tree.root()
    }

    pub fn quit(state: &mut dyn State) {
        ViewTree::quit(state);
    }

    pub fn update(state: &mut dyn State, wait: bool) -> Result<bool, Errno> {
        ViewTree::update(state, wait)
    }
}

impl_dep_obj!(Widget {
    fn<WidgetBase>() -> (WidgetBase) {
        WidgetTree { .widget_arena } | .base
    }

    fn<WidgetObjKey>() -> dyn(WidgetObj) {
        WidgetTree { .widget_arena } | .obj
    }
});

impl Widget {
    with_builder!();

    pub fn new<O: WidgetObj>(
        state: &mut dyn State,
        obj: O,
    ) -> Widget {
        let behavior = obj.behavior();
        let widget = {
            let tree: &mut WidgetTree = state.get_mut();
            tree.widget_arena.insert(|widget| (WidgetNode {
                view: None,
                base: WidgetBase::new_priv(),
                obj: Box::new(obj),
            }, Widget(widget)))
        };
        let drop_old_view = Binding1::new(state, (), |(), change: Option<Change<Option<View>>>|
            change.and_then(|change| change.old)
        );
        drop_old_view.set_target_fn(state, (), |state, (), view: View| view.drop_view(state));
        widget.add_binding::<WidgetBase, _>(state, drop_old_view);
        drop_old_view.set_source_1(state, &mut WidgetBase::VIEW.change_final_source(widget));
        behavior.init_bindings(widget, state);
        widget
    }

    pub fn drop_widget(self, state: &mut dyn State) {
        self.drop_bindings(state);
        {
            let tree: &mut WidgetTree = state.get_mut();
            let node = tree.widget_arena.remove(self.0);
            assert!(node.view.is_none(), "Loaded widget dropped");
        }
    }

    fn drop_bindings(self, state: &mut dyn State) {
        let tree: &WidgetTree = state.get();
        let node = &tree.widget_arena[self.0];
        let behavior = node.obj.behavior();
        behavior.drop_bindings(self, state);
        self.drop_bindings_priv(state);
    }

    pub fn load<X: Convenient>(
        self,
        state: &mut dyn State,
        parent: View,
        prev: Option<View>,
        init: impl FnOnce(&mut dyn State, View),
    ) -> Re<X> {
        let view = View::new(state, parent, prev);
        view.set_tag(state, self);
        {
            let tree: &mut WidgetTree = state.get_mut();
            assert!(
                tree.widget_arena[self.0].view.replace(view).is_none(),
                "Widget already loaded"
            );
        }
        init(state, view);

        let input_binding = Binding1::new(state, (), |(), input: Option<ViewInput>| input);
        input_binding.dispatch(state, self, |state, widget, input|
            WidgetBase::VIEW_INPUT.raise(state, widget, input)
        );
        self.add_binding::<WidgetBase, _>(state, input_binding);
        input_binding.set_source_1(state, &mut ViewBase::INPUT.source(view));

        WidgetBase::VIEW.set(state, self, Some(view))
    }

    pub fn unload<X: Convenient>(self, state: &mut dyn State) -> Re<X> {
        {
            let tree: &mut WidgetTree = state.get_mut();
            assert!(
                tree.widget_arena[self.0].view.take().is_some(),
                "Widget is not loaded"
            );
        }
        WidgetBase::VIEW.set(state, self, None)
    }

    pub fn view(self, tree: &WidgetTree) -> Option<View> {
        tree.widget_arena[self.0].view
    }

    pub fn focus(self, state: &mut dyn State) {
        let tree: &WidgetTree = state.get();
        self.view(tree).map(|view| view.focus(state));
    }

    fn bind_view<O: WidgetObj, M: Clone + 'static, P: Clone + 'static, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        widget_prop: DepProp<O, T>,
        map_param: M,
        map: fn(M, T) -> U,
        param: P,
        bind: fn(&mut dyn State, P, Binding<U>)
    ) {
        let binding = Binding1::new(state, (map_param, map), |(map_param, map), value: T|
            Some(map(map_param, value))
        );
        bind(state, param, binding.into());
        binding.set_source_1(state, &mut widget_prop.value_source(self));
    }
}

impl DetachedDepObjId for Widget { }

pub trait ViewWidgetExt {
    fn bind_base_to_widget_option<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_base_prop: DepProp<ViewBase, U>,
        widget: Widget,
        widget_prop: DepProp<O, Option<T>>,
        map: fn(T) -> U,
    );

    fn bind_base_to_widget<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_base_prop: DepProp<ViewBase, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    );

    fn bind_align_to_widget<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_align_prop: DepProp<ViewAlign, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    );

    fn bind_layout_to_widget<O: WidgetObj, L: Layout, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        layout_prop: DepProp<L, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    );

    fn bind_decorator_to_widget<O: WidgetObj, D: Decorator, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        decorator_prop: DepProp<D, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    );
}

impl ViewWidgetExt for View {
    fn bind_base_to_widget_option<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_base_prop: DepProp<ViewBase, U>,
        widget: Widget,
        widget_prop: DepProp<O, Option<T>>,
        map: fn(T) -> U,
    ) {
        widget.bind_view(state, widget_prop, map, |map, x| x.map(map), (view_base_prop, self),
            |state, (view_base_prop, view), binding| {
                binding.dispatch(state, (view_base_prop, view), |state, (view_base_prop, view), value| {
                    if let Some(value) = value {
                        view_base_prop.set(state, view, value)
                    } else {
                        view_base_prop.unset(state, view)
                    }
                });
                view.add_binding::<ViewBase, _>(state, binding);
            }
        );
    }

    fn bind_base_to_widget<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_base_prop: DepProp<ViewBase, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U
    ) {
        widget.bind_view(state, widget_prop, map, |map, x| map(x), (view_base_prop, self),
            |state, (view_base_prop, view), binding|
                view_base_prop.bind(state, view, binding)
        );
    }

    fn bind_align_to_widget<O: WidgetObj, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        view_align_prop: DepProp<ViewAlign, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    ) {
        widget.bind_view(state, widget_prop, map, |map, x| map(x), (view_align_prop, self),
            |state, (view_align_prop, view), binding|
                view_align_prop.bind(state, view, binding)
        );
    }

    fn bind_layout_to_widget<O: WidgetObj, L: Layout, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        layout_prop: DepProp<L, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    ) {
        widget.bind_view(state, widget_prop, map, |map, x| map(x), (layout_prop, self),
            |state, (layout_prop, view), binding|
                layout_prop.bind(state, view, binding)
        );
    }

    fn bind_decorator_to_widget<O: WidgetObj, D: Decorator, T: Convenient, U: Convenient>(
        self,
        state: &mut dyn State,
        decorator_prop: DepProp<D, U>,
        widget: Widget,
        widget_prop: DepProp<O, T>,
        map: fn(T) -> U,
    ) {
        widget.bind_view(state, widget_prop, map, |map, x| map(x),
            (decorator_prop, self), |state, (decorator_prop, view), binding|
                decorator_prop.bind(state, view, binding)
        );
    }
}

dep_type! {
    #[derive(Debug)]
    pub struct WidgetBase = Widget[WidgetBase] {
        view: Option<View> = None,
        view_input yield ViewInput,
    }
}