Skip to content

Commit 2fafbbc

Browse files
committed
karm-kira: Added select component.
1 parent bac6305 commit 2fafbbc

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/apps/hideo-zoo/app.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "page-radio.h"
1818
#include "page-resizable.h"
1919
#include "page-rows.h"
20+
#include "page-select.h"
2021
#include "page-side-nav.h"
2122
#include "page-side-panel.h"
2223
#include "page-slider.h"
@@ -39,6 +40,7 @@ static Array PAGES = {
3940
&PAGE_RADIO,
4041
&PAGE_RESIZABLE,
4142
&PAGE_ROWS,
43+
&PAGE_SELECT,
4244
&PAGE_SIDE_PANEL,
4345
&PAGE_SIDENAV,
4446
&PAGE_SLIDER,

src/apps/hideo-zoo/page-select.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <karm-kira/select.h>
4+
#include <karm-ui/layout.h>
5+
#include <mdi/form-select.h>
6+
7+
#include "model.h"
8+
9+
namespace Hideo::Zoo {
10+
11+
static inline Page PAGE_SELECT{
12+
Mdi::FORM_SELECT,
13+
"Select",
14+
"A control that allows the user to toggle between checked and not checked.",
15+
[] {
16+
return Kr::select(
17+
Kr::selectValue("Pick something to eat"s),
18+
[] -> Ui::Children {
19+
return {
20+
Kr::selectGroup({
21+
Kr::selectLabel("Fruits"s),
22+
Kr::selectItem(Ui::NOP, "Apple"s),
23+
Kr::selectItem(Ui::NOP, "Banana"s),
24+
Kr::selectItem(Ui::NOP, "Cherry"s),
25+
}),
26+
Ui::separator(),
27+
Kr::selectGroup({
28+
Kr::selectLabel("Vegetables"s),
29+
Kr::selectItem(Ui::NOP, "Carrot"s),
30+
Kr::selectItem(Ui::NOP, "Cucumber"s),
31+
Kr::selectItem(Ui::NOP, "Tomato"s),
32+
}),
33+
Ui::separator(),
34+
Kr::selectGroup({
35+
Kr::selectLabel("Meat"s),
36+
Kr::selectItem(Ui::NOP, "Beef"s),
37+
Kr::selectItem(Ui::NOP, "Chicken"s),
38+
Kr::selectItem(Ui::NOP, "Pork"s),
39+
}),
40+
};
41+
}
42+
) |
43+
Ui::center();
44+
},
45+
};
46+
47+
} // namespace Hideo::Zoo

src/libs/karm-kira/select.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <karm-ui/anim.h>
2+
#include <karm-ui/layout.h>
3+
#include <karm-ui/popover.h>
4+
#include <karm-ui/scroll.h>
5+
#include <mdi/chevron-down.h>
6+
7+
#include "select.h"
8+
9+
namespace Karm::Kira {
10+
11+
Ui::Child select(Ui::Child value, Ui::Slots slots) {
12+
return Ui::button(
13+
[slots = std::move(slots)](auto &n) {
14+
Ui::showPopover(
15+
n,
16+
n.bound().bottomStart(),
17+
Ui::vflow(
18+
slots()
19+
) |
20+
Ui::vscroll() |
21+
Ui::sizing({n.bound().width, Ui::UNCONSTRAINED}, {Ui::UNCONSTRAINED, 160}) |
22+
Ui::box({
23+
.borderRadii = 6,
24+
.borderWidth = 1,
25+
.borderFill = Ui::GRAY800,
26+
.backgroundFill = Ui::GRAY900,
27+
.shadowStyle = Gfx::BoxShadow::elevated(4),
28+
}) |
29+
Ui::scaleIn()
30+
);
31+
},
32+
Ui::ButtonStyle::outline(),
33+
Ui::hflow(
34+
8,
35+
Math::Align::CENTER,
36+
value,
37+
Ui::icon(Mdi::CHEVRON_DOWN)
38+
) | Ui::insets({6, 12, 6, 16}) |
39+
Ui::minSize({Ui::UNCONSTRAINED, 36})
40+
);
41+
}
42+
43+
Ui::Child selectValue(String text) {
44+
return Ui::labelMedium(text);
45+
}
46+
47+
Ui::Child selectLabel(String text) {
48+
return Ui::labelMedium(Ui::GRAY400, text) |
49+
Ui::insets({12, 6, 3, 14});
50+
}
51+
52+
Ui::Child selectItem(Ui::OnPress onPress, String t) {
53+
return Ui::hflow(
54+
12,
55+
Math::Align::CENTER,
56+
Ui::text(t)
57+
) |
58+
Ui::insets({6, 6, 6, 10}) |
59+
Ui::minSize({Ui::UNCONSTRAINED, 28}) |
60+
Ui::button(
61+
[onPress = std::move(onPress)](auto &n) {
62+
onPress(n);
63+
Ui::closePopover(n);
64+
},
65+
Ui::ButtonStyle::subtle()
66+
) |
67+
Ui::insets(4);
68+
}
69+
70+
Ui::Child selectGroup(Ui::Children children) {
71+
return Ui::vflow(children);
72+
}
73+
74+
} // namespace Karm::Kira

src/libs/karm-kira/select.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <karm-ui/input.h>
4+
5+
#include "_prelude.h"
6+
7+
namespace Karm::Kira {
8+
9+
Ui::Child select(Ui::Child value, Ui::Slots options);
10+
11+
Ui::Child selectValue(String text);
12+
13+
Ui::Child selectLabel(String text);
14+
15+
Ui::Child selectItem(Ui::OnPress onPress, String t);
16+
17+
Ui::Child selectGroup(Ui::Children children);
18+
19+
} // namespace Karm::Kira

0 commit comments

Comments
 (0)