Skip to content

Commit

Permalink
meta: Update from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 13, 2024
1 parent cd2876a commit e969f7b
Show file tree
Hide file tree
Showing 77 changed files with 2,346 additions and 973 deletions.
1 change: 1 addition & 0 deletions src/libs/karm-base/_prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Darwin is poluting the global namespace with unwanted macros.
#undef UNDERFLOW
#undef DOMAIN
#undef OVERFLOW
#endif

#include <string.h>
Expand Down
7 changes: 7 additions & 0 deletions src/libs/karm-base/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ struct Array {
T _buf[N];

always_inline static constexpr Array from(Sliceable<T> auto slice) {

Array res{};
for (usize i = 0; i < N; i++)
res[i] = slice[i];
return res;
}

always_inline static constexpr Array fill(auto f) {
return [&]<usize... Is>(std::index_sequence<Is...>) {
return Array{f(Is)...};
}(std::make_index_sequence<N>());
}

always_inline constexpr T &operator[](usize i) {
if (i >= N) [[unlikely]]
panic("index out of range");
Expand Down
10 changes: 10 additions & 0 deletions src/libs/karm-cli/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,23 @@ struct Command {
if (_usage) {
Io::Emit e{Sys::out()};
_showUsage(e);
e.newline();
co_try$(e.flush());
co_return Ok();
}

_invoked = true;
if (callbackAsync)
co_trya$(callbackAsync.unwrap()(ctx));

if (any(_commands) and c.ended()) {
Io::Emit e{Sys::out()};
_showUsage(e);
e.newline();
co_try$(e.flush());
co_return Error::invalidInput("expected subcommand");
}

if (not c.ended()) {
if (c->kind != Token::OPERAND)
co_return Error::invalidInput("expected subcommand");
Expand Down
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
10 changes: 10 additions & 0 deletions src/libs/karm-math/insets.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ struct Insets {
return top + bottom;
}

template <typename U>
Insets<U> cast() const {
return {
static_cast<U>(start),
static_cast<U>(top),
static_cast<U>(end),
static_cast<U>(bottom),
};
}

void repr(Io::Emit &e) const {
e("(insets {} {} {} {})", start, top, end, bottom);
}
Expand Down
66 changes: 0 additions & 66 deletions src/libs/karm-print/bmp.h

This file was deleted.

Loading

0 comments on commit e969f7b

Please sign in to comment.