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
use crate::base::*;
use crate::view::{View, ViewBase};
use crate::view::decorators::TextDecorator;
use dep_obj::{Builder, Change, DepObjId, dep_type, ext_builder};
use dep_obj::binding::Binding1;
use dyn_context::State;
use alloc::borrow::Cow;
use tuifw_screen_base::*;

dep_type! {
    #[derive(Debug)]
    pub struct StaticText = Widget[WidgetObjKey] {
        text: Cow<'static, str> = Cow::Borrowed(""),
        bg: Option<Option<Color>> = None,
        fg: Option<Color> = None,
        attr: Option<Attr> = None,
    }
}

ext_builder!(<'a> Builder<'a, Widget> as BuilderWidgetStaticTextExt[Widget] {
    fn static_text() -> (StaticText);
});

struct StaticTextBehavior;

impl WidgetBehavior for StaticTextBehavior {
    fn init_bindings(&self, widget: Widget, state: &mut dyn State) {
        let init_new_view = Binding1::new(state, (), |(), change: Option<Change<Option<View>>>|
            change.and_then(|change| change.new)
        );
        init_new_view.set_target_fn(state, widget, |state, widget, view: View| {
            TextDecorator::new(state, view);
            view.bind_base_to_widget_option(state, ViewBase::BG, widget, StaticText::BG, |x| x);
            view.bind_base_to_widget_option(state, ViewBase::FG, widget, StaticText::FG, |x| x);
            view.bind_base_to_widget_option(state, ViewBase::ATTR, widget, StaticText::ATTR, |x| x);
            view.bind_decorator_to_widget(state, TextDecorator::TEXT, widget, StaticText::TEXT, |x| x);
        });
        widget.add_binding::<StaticText, _>(state, init_new_view);
        init_new_view.set_source_1(state, &mut WidgetBase::VIEW.change_initial_source(widget));
    }

    fn drop_bindings(&self, _widget: Widget, _state: &mut dyn State) { }
}

impl StaticText {
    const BEHAVIOR: StaticTextBehavior = StaticTextBehavior;

    #[allow(clippy::new_ret_no_self)]
    pub fn new(state: &mut dyn State) -> Widget {
        Widget::new(state, StaticText::new_priv())
    }
}

impl WidgetObj for StaticText {
    fn behavior(&self) -> &'static dyn WidgetBehavior { &Self::BEHAVIOR }
}