1
1
#include < karm-app/form-factor.h>
2
+ #include < karm-print/paper.h>
2
3
#include < karm-ui/layout.h>
4
+ #include < karm-ui/reducer.h>
3
5
#include < karm-ui/scroll.h>
4
6
5
7
#include " checkbox.h"
10
12
11
13
namespace Karm ::Kira {
12
14
13
- Ui::Child _printSelect (usize index) {
15
+ // MARK: Model -----------------------------------------------------------------
16
+
17
+ struct State {
18
+ PrintPreview preview;
19
+ Print::Settings settings = {};
20
+ Vec<Strong<Scene::Page>> pages = preview(settings);
21
+ };
22
+
23
+ using Action = Union<None>;
24
+
25
+ void reduce (State &, Action) {
26
+ }
27
+
28
+ using Model = Ui::Model<State, Action, reduce>;
29
+
30
+ // MARK: Dialog ----------------------------------------------------------------
31
+
32
+ Ui::Child _printSelect (State const &s, usize index) {
14
33
return Ui::hflow (
15
34
8 ,
16
35
Math::Align::CENTER,
17
36
checkbox (true , NONE),
18
- Ui::labelMedium (" Page {} of 3 " , index + 1 )
37
+ Ui::labelMedium (" Page {} of {} " , index + 1 , s. pages . len () )
19
38
) |
20
39
Ui::box ({
21
40
.margin = 8 ,
@@ -27,56 +46,61 @@ Ui::Child _printSelect(usize index) {
27
46
});
28
47
}
29
48
30
- Ui::Child _printPaper (usize index) {
49
+ Ui::Child _printPaper (State const &s, usize index) {
31
50
auto scale = 1 .;
32
51
auto isMobile = App::useFormFactor () == App::FormFactor::MOBILE;
33
52
if (isMobile) {
34
53
scale = 0.5 ;
35
54
}
36
55
37
56
return Ui::stack (
38
- Ui::empty (Math::Vec2f{320 * scale, 452 * scale}.cast <isize>()) |
39
- Ui::box ({
40
- .borderRadii = 6 ,
41
- .borderWidth = 1 ,
42
- .borderFill = Gfx::BLACK.withOpacity (0.1 ),
43
- .backgroundFill = Gfx::WHITE,
44
- }),
45
- _printSelect (index ) | Ui::align (Math::Align::BOTTOM_END)
46
- );
57
+ Ui::canvas (s.pages [index ]) |
58
+ Ui::box ({
59
+ .borderRadii = 6 ,
60
+ .borderWidth = 1 ,
61
+ .borderFill = Ui::GRAY50.withOpacity (0.1 ),
62
+ .backgroundFill = Gfx::WHITE,
63
+ }),
64
+ _printSelect (s, index ) | Ui::align (Math::Align::BOTTOM_END)
65
+ ) |
66
+ Ui::pinSize (Math::Vec2f{320 * scale, 452 * scale}.cast <isize>());
47
67
}
48
68
49
- Ui::Child _printPreviewMobile () {
69
+ Ui::Child _printPreviewMobile (State const &s) {
70
+ Ui::Children pages;
71
+ for (usize i = 0 ; i < s.pages .len (); ++i) {
72
+ pages.pushBack (_printPaper (s, i));
73
+ }
74
+
50
75
return Ui::hflow (
51
76
8 ,
52
77
Math::Align::CENTER,
53
- _printPaper (0 ),
54
- _printPaper (1 ),
55
- _printPaper (2 )
78
+ std::move (pages)
56
79
) |
57
80
Ui::insets (32 ) |
58
81
Ui::hscroll () |
59
82
Ui::box (
60
83
{
61
- .borderRadii = {0 , 0 , 0 , 12 },
62
84
.backgroundFill = Ui::GRAY950,
63
85
}
64
86
);
65
87
}
66
88
67
- Ui::Child _printPreview () {
89
+ Ui::Child _printPreview (State const &s) {
90
+ Ui::Children pages;
91
+ for (usize i = 0 ; i < s.pages .len (); ++i) {
92
+ pages.pushBack (_printPaper (s, i));
93
+ }
94
+
68
95
return Ui::vflow (
69
96
8 ,
70
97
Math::Align::CENTER,
71
- _printPaper (0 ),
72
- _printPaper (1 ),
73
- _printPaper (2 )
98
+ std::move (pages)
74
99
) |
75
100
Ui::insets (32 ) |
76
101
Ui::vscroll () |
77
102
Ui::box (
78
103
{
79
- .borderRadii = {0 , 0 , 0 , 12 },
80
104
.backgroundFill = Ui::GRAY950,
81
105
}
82
106
);
@@ -106,6 +130,24 @@ Ui::Child _destinationSelect() {
106
130
);
107
131
}
108
132
133
+ Ui::Child _paperSelect () {
134
+ return select (selectValue (" A4" s), [] -> Ui::Children {
135
+ Vec<Ui::Child> groups;
136
+
137
+ for (auto &serie : Print::SERIES) {
138
+ Vec<Ui::Child> items;
139
+ items.pushBack (selectLabel (serie.name ));
140
+ for (auto const &stock : serie.stocks ) {
141
+ items.pushBack (selectItem (Ui::NOP, stock.name ));
142
+ }
143
+
144
+ groups.pushBack (selectGroup (std::move (items)));
145
+ }
146
+
147
+ return groups;
148
+ });
149
+ }
150
+
109
151
Ui::Child _printSettings () {
110
152
return Ui::vflow (
111
153
rowContent (
@@ -158,18 +200,11 @@ Ui::Child _printSettings() {
158
200
NONE,
159
201
Karm::Ui::Slots{[] -> Ui::Children {
160
202
return {
161
- selectRow (
162
- selectValue (" A4" s),
163
- [] -> Ui::Children {
164
- return {
165
- selectItem (Ui::NOP, " A3" s),
166
- selectItem (Ui::NOP, " A4" s),
167
- selectItem (Ui::NOP, " A5" s),
168
- selectItem (Ui::NOP, " Letter" s),
169
- selectItem (Ui::NOP, " Legal" s),
170
- };
171
- },
172
- " Paper size" s
203
+ rowContent (
204
+ NONE,
205
+ " Paper" s,
206
+ NONE,
207
+ _paperSelect ()
173
208
),
174
209
selectRow (
175
210
selectValue (" 1" s),
@@ -213,11 +248,11 @@ Ui::Child _printControls() {
213
248
Ui::minSize ({320 , Ui::UNCONSTRAINED});
214
249
}
215
250
216
- Ui::Child _printDialog () {
251
+ Ui::Child _printDialog (State const &s ) {
217
252
return dialogContent ({
218
253
dialogTitleBar (" Print" s),
219
254
Ui::hflow (
220
- _printPreview (),
255
+ _printPreview (s ),
221
256
_printControls () | Ui::grow ()
222
257
) | Ui::maxSize ({Ui::UNCONSTRAINED, 500 }) |
223
258
Ui::grow (),
@@ -229,16 +264,17 @@ Ui::Child _printDialog() {
229
264
});
230
265
}
231
266
232
- Ui::Child _printDialogMobile () {
267
+ Ui::Child _printDialogMobile (State const &s ) {
233
268
return dialogContent ({
234
269
dialogTitleBar (" Print" s),
235
270
Ui::separator (),
236
271
Ui::vflow (
237
- _printPreviewMobile (),
272
+ _printPreviewMobile (s ),
238
273
Ui::separator (),
239
274
titleRow (" Settings" s),
240
275
_printSettings ()
241
- ) | Ui::vscroll () |
276
+ ) | Ui::minSize (500 ) |
277
+ Ui::vscroll () |
242
278
Ui::grow (),
243
279
Ui::separator (),
244
280
dialogFooter ({
@@ -248,13 +284,13 @@ Ui::Child _printDialogMobile() {
248
284
});
249
285
}
250
286
251
- Ui::Child printDialog () {
252
- auto isMobile = App::useFormFactor () == App::FormFactor::MOBILE;
253
-
254
- if (isMobile) {
255
- return _printDialogMobile ();
256
- }
257
- return _printDialog ( );
287
+ Ui::Child printDialog (PrintPreview preview ) {
288
+ return Ui::reducer<Model>({preview}, [](State const &s) {
289
+ auto isMobile = App::useFormFactor () == App::FormFactor::MOBILE;
290
+ if (isMobile)
291
+ return _printDialogMobile (s );
292
+ return _printDialog (s);
293
+ } );
258
294
}
259
295
260
296
} // namespace Karm::Kira
0 commit comments