forked from hchenqi/ViewDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMirroring.cpp
More file actions
114 lines (96 loc) · 3.05 KB
/
Copy pathStateMirroring.cpp
File metadata and controls
114 lines (96 loc) · 3.05 KB
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
// state mirroring: one view updates the state and the other read-only views mirror the state
#include <ViewDesign/view/widget/DefaultWindow.h>
#include <ViewDesign/view/frame/ScrollFrame.h>
#include <ViewDesign/view/frame/ClipFrame.h>
#include <ViewDesign/view/frame/BackgroundFrame.h>
#include <ViewDesign/view/frame/PaddingFrame.h>
#include <ViewDesign/view/frame/BorderFrame.h>
#include <ViewDesign/view/layout/ListLayout.h>
#include <ViewDesign/view/control/TextEditor.h>
#include <ViewDesign/view/control/TextView.h>
#include <ViewDesign/view/wrapper/Background.h>
#include <ViewDesign/view/widget/TextViewAdapter.h>
#include <ViewDesign/messaging/state.h>
#include <ViewDesign/messaging/signal.h>
using namespace ViewDesign;
using ListView = DeferredReflow<ListLayoutVertical<Bounded>>;
class TextInput : public TextEditor {
public:
using Style = TextEditor::Style;
public:
TextInput(const Style& style, u16string text) : TextEditor(style, std::move(text)), text_state(this->text) {}
public:
StateRef<u16string> text_state;
Signal text_update_signal;
private:
virtual void OnTextUpdate() override {
TextEditor::OnTextUpdate();
text_state.Notify();
text_update_signal.Notify();
}
};
class TextLabel : public TextView {
public:
using Style = TextView::Style;
public:
TextLabel(const Style& style, const StateRef<u16string>& text_state) : TextView(style, text_state.Get()), text_state_watcher(text_state, [&](const u16string& text) { Assign(text); }) {}
private:
StateRef<u16string>::Watcher text_state_watcher;
};
struct TextStyleA : TextLabel::Style {
TextStyleA() {
font.size(30.0f).color(ColorCode::Black);
}
};
struct TextStyleB : TextLabel::Style {
TextStyleB() {
font.size(50.0f).color(ColorCode::Blue);
}
};
void App() {
std::unique_ptr<TextInput> text_input = create<TextInput>(TextInput::Style(), u"Type something here...");
TextInput& text_input_ref = *text_input;
ref_ptr<ListView> list_view;
Signal::Listener text_update_listener(text_input_ref.text_update_signal, [&] { list_view->Reflow(); });
desktop.AddWindow(
new DefaultBackground<DefaultWindow>(
DefaultWindow::Style(),
u"StateMirroring",
new ClipFrame<Fixed, Fixed, Top>(
new ScrollFrame<Fixed, Bounded>(
new ClipFrame<Fixed, Auto, Left>(
list_view = new ListView(
5.0f,
create<BackgroundFrame<Bounded, Auto>>(
ColorCode::LightSalmon,
new PaddingFrame(
Padding(20.0f),
TextViewAdapter<Bounded, Auto>(
new TextLabel(TextStyleA(), text_input_ref.text_state)
)
)
),
create<BorderFrame<Bounded, Auto>>(
Border(2.0f, ColorCode::Orange),
new PaddingFrame(
Padding(10.0f),
TextViewAdapter<Bounded, Auto>(
std::move(text_input)
)
)
),
create<BackgroundFrame<Bounded, Auto>>(
ColorCode::LightSkyBlue,
TextViewAdapter<Bounded, Auto>(
create<TextLabel>(TextStyleB(), text_input_ref.text_state)
)
)
)
)
)
)
)
);
text_input_ref.Edit();
desktop.EventLoop();
}