Skip to content

Commit

Permalink
vaev-driver: Implement orientation and margins print settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 12, 2024
1 parent 82c3ded commit 1c6eaea
Show file tree
Hide file tree
Showing 24 changed files with 621 additions and 480 deletions.
6 changes: 4 additions & 2 deletions src/libs/karm-io/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ struct Formatter<Cased<T>> {
}

Res<usize> format(Io::TextWriter &writer, Cased<T> val) {
auto result = try$(changeCase(val._inner, val._case));
return writer.writeStr(result);
Io::StringWriter sw;
try$(_innerFmt.format(sw, val._inner));
String result = try$(changeCase(sw.str(), val._case));
return writer.writeStr(result.str());
}
};

Expand Down
132 changes: 104 additions & 28 deletions src/libs/karm-kira/print-dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <karm-app/form-factor.h>
#include <karm-print/paper.h>
#include <karm-ui/layout.h>
#include <karm-ui/popover.h>
#include <karm-ui/reducer.h>
#include <karm-ui/scroll.h>

Expand All @@ -20,9 +21,55 @@ struct State {
Vec<Strong<Scene::Page>> pages = preview(settings);
};

using Action = Union<None>;
struct ChangePaper {
Print::PaperStock paper;
};

struct ChangeOrientation {
Print::Orientation orientation;
};

struct ChangeMargin {
Print::Margins margins;
};

struct ToggleHeaderFooter {};

struct ToggleBackgroundGraphics {};

using Action = Union<
ChangePaper,
ChangeOrientation,
ChangeMargin,
ToggleHeaderFooter,
ToggleBackgroundGraphics>;

static void reduce(State &s, Action a) {
bool shouldUpdatePreview = false;

if (auto changePaper = a.is<ChangePaper>()) {
s.settings.paper = changePaper->paper;
shouldUpdatePreview = true;
} else if (auto changeOrientation = a.is<ChangeOrientation>()) {
s.settings.orientation = changeOrientation->orientation;
shouldUpdatePreview = true;
} else if (auto changeMargin = a.is<ChangeMargin>()) {
s.settings.margins = changeMargin->margins;
shouldUpdatePreview = true;
} else if (a.is<ToggleHeaderFooter>()) {
s.settings.headerFooter = not s.settings.headerFooter;
shouldUpdatePreview = true;
} else if (a.is<ToggleBackgroundGraphics>()) {
s.settings.backgroundGraphics = not s.settings.backgroundGraphics;
shouldUpdatePreview = true;
}

void reduce(State &, Action) {
if (shouldUpdatePreview) {
auto settings = s.settings;
if (settings.orientation == Print::Orientation::LANDSCAPE)
settings.paper = s.settings.paper.landscape();
s.pages = s.preview(settings);
}
}

using Model = Ui::Model<State, Action, reduce>;
Expand All @@ -48,21 +95,34 @@ Ui::Child _printSelect(State const &s, usize index) {

Ui::Child _printPaper(State const &s, usize index) {
auto scale = 1.;

auto paper = s.settings.paper;
if (s.settings.orientation == Print::Orientation::LANDSCAPE)
paper = paper.landscape();

auto isMobile = App::useFormFactor() == App::FormFactor::MOBILE;
if (isMobile) {
if (isMobile)
scale = 0.5;
}

Math::Vec2f previewSize{
320 * scale,
320 * (1. / paper.aspect()) * scale,
};

return Ui::stack(
Ui::canvas(s.pages[index]) |
Ui::box({
Ui::canvas(
s.pages[index],
{
.showBackgroundGraphics = s.settings.backgroundGraphics,
}
) | Ui::box({
.borderWidth = 1,
.borderFill = Ui::GRAY50.withOpacity(0.1),
.backgroundFill = Gfx::WHITE,
}),
_printSelect(s, index) | Ui::align(Math::Align::BOTTOM_END)
) |
Ui::pinSize(Math::Vec2f{320 * scale, 452 * scale}.cast<isize>());
Ui::pinSize(previewSize.cast<isize>());
}

Ui::Child _printPreviewMobile(State const &s) {
Expand Down Expand Up @@ -129,16 +189,16 @@ Ui::Child _destinationSelect() {
);
}

Ui::Child _paperSelect() {
return select(selectValue("A4"s), [] -> Ui::Children {
Ui::Child _paperSelect(State const &s) {
return select(selectValue(s.settings.paper.name), [] -> Ui::Children {
Vec<Ui::Child> groups;

bool first = false;
for (auto &serie : Print::SERIES) {
Vec<Ui::Child> items;
items.pushBack(selectLabel(serie.name));
for (auto const &stock : serie.stocks) {
items.pushBack(selectItem(Ui::NOP, stock.name));
items.pushBack(selectItem(Model::bind<ChangePaper>(stock), stock.name));
}

if (not first)
Expand All @@ -152,7 +212,7 @@ Ui::Child _paperSelect() {
});
}

Ui::Child _printSettings() {
Ui::Child _printSettings(State const &s) {
return Ui::vflow(
rowContent(
NONE,
Expand Down Expand Up @@ -187,11 +247,15 @@ Ui::Child _printSettings() {
"Pages"s
),
selectRow(
selectValue("Portrait"s),
selectValue(
s.settings.orientation == Print::Orientation::PORTRAIT
? "Portrait"s
: "Landscape"s
),
[] -> Ui::Children {
return {
selectItem(Ui::NOP, "Portrait"s),
selectItem(Ui::NOP, "Landscape"s),
selectItem(Model::bind<ChangeOrientation>(Print::Orientation::PORTRAIT), "Portrait"s),
selectItem(Model::bind<ChangeOrientation>(Print::Orientation::LANDSCAPE), "Landscape"s),

};
},
Expand All @@ -202,13 +266,13 @@ Ui::Child _printSettings() {
NONE,
"More settings"s,
NONE,
Karm::Ui::Slots{[] -> Ui::Children {
Karm::Ui::Slots{[&] -> Ui::Children {
return {
rowContent(
NONE,
"Paper"s,
NONE,
_paperSelect()
_paperSelect(s)
),
selectRow(
selectValue("1"s),
Expand All @@ -225,28 +289,40 @@ Ui::Child _printSettings() {
"Page per sheet"s
),
selectRow(
selectValue("Default"s),
selectValue(Io::format("{}", Io::cased(s.settings.margins, Io::Case::CAPITAL)).unwrap()),
[] -> Ui::Children {
return {
selectItem(Ui::NOP, "None"s),
selectItem(Ui::NOP, "Small"s),
selectItem(Ui::NOP, "Default"s),
selectItem(Ui::NOP, "Large"s),
selectItem(Model::bind<ChangeMargin>(Print::Margins::NONE), "None"s),
selectItem(Model::bind<ChangeMargin>(Print::Margins::MINIMUM), "Minimum"s),
selectItem(Model::bind<ChangeMargin>(Print::Margins::DEFAULT), "Default"s),
selectItem(Model::bind<ChangeMargin>(Print::Margins::CUSTOM), "Custom"s),
};
},
"Margins"s
),

checkboxRow(true, NONE, "Header and footers"s),
checkboxRow(false, NONE, "Background graphics"s),
checkboxRow(
s.settings.headerFooter,
[&](auto &n, ...) {
Model::bubble<ToggleHeaderFooter>(n);
},
"Header and footers"s
),
checkboxRow(
s.settings.backgroundGraphics,
[&](auto &n, ...) {
Model::bubble<ToggleBackgroundGraphics>(n);
},
"Background graphics"s
),
};
}}
)
);
}

Ui::Child _printControls() {
return _printSettings() |
Ui::Child _printControls(State const &s) {
return _printSettings(s) |
Ui::vscroll() |
Ui::grow() |
Ui::minSize({320, Ui::UNCONSTRAINED});
Expand All @@ -257,7 +333,7 @@ Ui::Child _printDialog(State const &s) {
dialogTitleBar("Print"s),
Ui::hflow(
_printPreview(s),
_printControls() | Ui::grow()
_printControls(s) | Ui::grow()
) | Ui::maxSize({Ui::UNCONSTRAINED, 500}) |
Ui::grow(),
Ui::separator(),
Expand All @@ -276,7 +352,7 @@ Ui::Child _printDialogMobile(State const &s) {
_printPreviewMobile(s),
Ui::separator(),
titleRow("Settings"s),
_printSettings()
_printSettings(s)
) | Ui::minSize(500) |
Ui::vscroll() |
Ui::grow(),
Expand All @@ -293,7 +369,7 @@ Ui::Child printDialog(PrintPreview preview) {
auto isMobile = App::useFormFactor() == App::FormFactor::MOBILE;
if (isMobile)
return _printDialogMobile(s);
return _printDialog(s);
return _printDialog(s) | Ui::popoverLayer();
});
}

Expand Down
14 changes: 13 additions & 1 deletion src/libs/karm-print/paper.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ struct Margins {
NONE,
MINIMUM,
CUSTOM,

_LEN
};
using enum _Named;
_Named named;
Math::Insetsf custom;
Math::Insetsf custom = 20 * UNIT;

Margins(_Named named) : named(named) {}

Expand All @@ -142,11 +144,21 @@ struct Margins {
bool operator==(_Named named) const {
return this->named == named;
}

void repr(Io::Emit &e) const {
e("{}", named);
}
};

enum struct Orientation {
PORTRAIT,
LANDSCAPE
};

struct Settings {
PaperStock paper = Print::A4;
Margins margins = Margins::DEFAULT;
Orientation orientation = Orientation::PORTRAIT;

double scale = 1.;
bool headerFooter = true;
Expand Down
Loading

0 comments on commit 1c6eaea

Please sign in to comment.