Skip to content

Commit ac3d7a2

Browse files
committed
karm-ui: Use karm-async events instead of whatever karm-ui was using.
1 parent 0969d7b commit ac3d7a2

File tree

36 files changed

+554
-587
lines changed

36 files changed

+554
-587
lines changed

src/apps/hideo-about/main.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ Ui::Child app() {
1212
"LICENSE");
1313

1414
auto closeBtn = Ui::button(
15-
[](Ui::Node &n) {
16-
Events::ExitEvent e{Ok()};
17-
n.bubble(e);
18-
},
15+
Ui::bindBubble<Events::ExitEvent>(Ok()),
1916
Ui::ButtonStyle::primary(),
2017
"OK");
2118

src/apps/hideo-colorpicker/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct HsvPicker : public Ui::View<HsvPicker> {
6767
g.restore();
6868
}
6969

70-
void event(Events::Event &e) override {
70+
void event(Async::Event &e) override {
7171
_mouseListener.listen(*this, e);
7272

7373
if (_mouseListener.isPress() and e.is<Events::MouseEvent>()) {

src/apps/hideo-image-viewer/reduce.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace ImageViewer {
55
void reduce(State &s, Action a) {
66
a.visit(Visitor{
77
[&](Refresh) {
8-
debug("Refresh");
98
},
109
[&](ToggleEditor) {
1110
s.filter = Gfx::Unfiltered{};
@@ -15,10 +14,8 @@ void reduce(State &s, Action a) {
1514
s.filter = f.filter;
1615
},
1716
[&](ApplyFilter) {
18-
debug("Apply filter");
1917
},
2018
[&](SaveImage) {
21-
debug("Save image");
2219
},
2320
});
2421
}

src/apps/hideo-spreadsheet/table.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct Table : public Ui::View<Table> {
5252

5353
/* --- Events --- */
5454

55-
void event(Events::Event &e) override {
55+
void event(Async::Event &e) override {
5656
e.handle<Events::MouseEvent>([&](Events::MouseEvent const &m) {
5757
auto pos = m.pos - bound().topStart();
5858
if (bound().contains(m.pos)) {

src/impls/impl-efi/ui.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ struct EfiHost :
3838
Efi::Key key = {};
3939
_stip->readKeyStroke(_stip, &key).unwrap();
4040
auto e = key.toKeyEvent();
41-
event(e);
41+
event<Events::KeyboardEvent>(*this, e);
4242
}
4343

4444
void wait(TimeSpan) override {
4545
}
4646

47-
void bubble(Events::Event &e) override {
47+
void bubble(Async::Event &e) override {
4848
Ui::Host::bubble(e);
4949
}
5050
};

src/impls/impl-sdl/ui.cpp

+74-68
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,20 @@ struct SdlHost :
5353
break;
5454

5555
case SDL_KEYDOWN: {
56-
Events::KeyboardEvent uiEvent{
57-
.type = Events::KeyboardEvent::PRESS,
58-
};
59-
event(uiEvent);
56+
event<Events::TypedEvent>(*this, Events::KeyboardEvent::PRESS);
6057
break;
6158
}
6259

6360
case SDL_KEYUP: {
64-
Events::KeyboardEvent uiEvent{
65-
.type = Events::KeyboardEvent::RELEASE,
66-
};
67-
event(uiEvent);
61+
event<Events::TypedEvent>(*this, Events::KeyboardEvent::RELEASE);
6862
break;
6963
}
7064

7165
case SDL_TEXTINPUT: {
7266
Str text = sdlEvent.text.text;
7367
for (u8 c : iterRunes(text)) {
74-
Events::TypedEvent uiEvent{
75-
.codepoint = c,
76-
};
7768
logInfo("typed: {c}", c);
78-
event(uiEvent);
69+
event<Events::TypedEvent>(*this, c);
7970
}
8071
break;
8172
}
@@ -88,21 +79,24 @@ struct SdlHost :
8879
Math::Vec2<i32> screenPos = {};
8980
SDL_GetGlobalMouseState(&screenPos.x, &screenPos.y);
9081

91-
Events::MouseEvent uiEvent{
92-
.type = Events::MouseEvent::MOVE,
82+
Events::Button buttons = Events::Button::NONE;
83+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
84+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
85+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
9386

94-
.pos = {sdlEvent.motion.x, sdlEvent.motion.y},
95-
.delta = screenPos - _lastScreenMousePos,
96-
};
87+
_lastMousePos = {sdlEvent.motion.x, sdlEvent.motion.y};
9788

98-
uiEvent.buttons |= (sdlEvent.motion.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
99-
uiEvent.buttons |= (sdlEvent.motion.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
100-
uiEvent.buttons |= (sdlEvent.motion.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
89+
event<Events::MouseEvent>(
90+
*this,
91+
Events::MouseEvent{
92+
.type = Events::MouseEvent::MOVE,
93+
.pos = _lastMousePos,
94+
.delta = screenPos - _lastScreenMousePos,
95+
.buttons = buttons,
96+
});
10197

102-
_lastMousePos = uiEvent.pos;
10398
_lastScreenMousePos = screenPos.cast<isize>();
10499

105-
event(uiEvent);
106100
break;
107101
}
108102

@@ -111,20 +105,28 @@ struct SdlHost :
111105
return;
112106
}
113107

114-
Events::MouseEvent uiEvent{
115-
.type = Events::MouseEvent::RELEASE,
116-
.pos = _lastMousePos,
117-
};
118-
119-
uiEvent.button = (sdlEvent.button.which == SDL_BUTTON_LEFT) ? Events::Button::LEFT : Events::Button::NONE;
120-
uiEvent.button = (sdlEvent.button.which == SDL_BUTTON_RIGHT) ? Events::Button::MIDDLE : Events::Button::NONE;
121-
uiEvent.button = (sdlEvent.button.which == SDL_BUTTON_MIDDLE) ? Events::Button::RIGHT : Events::Button::NONE;
122-
123-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
124-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
125-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
108+
Events::Button buttons = Events::Button::NONE;
109+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
110+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
111+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
112+
113+
Events::Button button = Events::Button::NONE;
114+
if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
115+
button = Events::Button::LEFT;
116+
} else if (sdlEvent.button.button == SDL_BUTTON_RIGHT) {
117+
button = Events::Button::RIGHT;
118+
} else if (sdlEvent.button.button == SDL_BUTTON_MIDDLE) {
119+
button = Events::Button::MIDDLE;
120+
}
126121

127-
event(uiEvent);
122+
event<Events::MouseEvent>(
123+
*this,
124+
Events::MouseEvent{
125+
.type = Events::MouseEvent::RELEASE,
126+
.pos = _lastMousePos,
127+
.buttons = buttons,
128+
.button = button,
129+
});
128130
break;
129131
}
130132

@@ -133,20 +135,28 @@ struct SdlHost :
133135
return;
134136
}
135137

136-
Events::MouseEvent uiEvent{
137-
.type = Events::MouseEvent::PRESS,
138-
.pos = _lastMousePos,
139-
};
140-
141-
uiEvent.button = (sdlEvent.button.button == SDL_BUTTON_LEFT) ? Events::Button::LEFT : Events::Button::NONE;
142-
uiEvent.button = (sdlEvent.button.button == SDL_BUTTON_RIGHT) ? Events::Button::MIDDLE : Events::Button::NONE;
143-
uiEvent.button = (sdlEvent.button.button == SDL_BUTTON_MIDDLE) ? Events::Button::RIGHT : Events::Button::NONE;
144-
145-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
146-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
147-
uiEvent.buttons |= (sdlEvent.button.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
138+
Events::Button buttons = Events::Button::NONE;
139+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_LMASK) ? Events::Button::LEFT : Events::Button::NONE;
140+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_MMASK) ? Events::Button::MIDDLE : Events::Button::NONE;
141+
buttons |= (sdlEvent.motion.state & SDL_BUTTON_RMASK) ? Events::Button::RIGHT : Events::Button::NONE;
142+
143+
Events::Button button = Events::Button::NONE;
144+
if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
145+
button = Events::Button::LEFT;
146+
} else if (sdlEvent.button.button == SDL_BUTTON_RIGHT) {
147+
button = Events::Button::RIGHT;
148+
} else if (sdlEvent.button.button == SDL_BUTTON_MIDDLE) {
149+
button = Events::Button::MIDDLE;
150+
}
148151

149-
event(uiEvent);
152+
event<Events::MouseEvent>(
153+
*this,
154+
Events::MouseEvent{
155+
.type = Events::MouseEvent::PRESS,
156+
.pos = _lastMousePos,
157+
.buttons = buttons,
158+
.button = button,
159+
});
150160
break;
151161
}
152162

@@ -155,32 +165,27 @@ struct SdlHost :
155165
return;
156166
}
157167

158-
Events::MouseEvent uiEvent{
159-
.type = Events::MouseEvent::SCROLL,
160-
.pos = _lastMousePos,
161-
.scrollLines = {
162-
sdlEvent.wheel.x,
163-
sdlEvent.wheel.y,
164-
},
165-
.scrollPrecise = {
168+
event<Events::MouseEvent>(
169+
*this,
170+
Events::MouseEvent{
171+
.type = Events::MouseEvent::SCROLL,
172+
.pos = _lastMousePos,
173+
.scroll = {
166174
#if SDL_VERSION_ATLEAST(2, 0, 18)
167-
sdlEvent.wheel.preciseX,
168-
sdlEvent.wheel.preciseY,
175+
sdlEvent.wheel.preciseX,
176+
sdlEvent.wheel.preciseY,
169177
#else
170-
(f64)sdlEvent.wheel.x,
171-
(f64)sdlEvent.wheel.y,
178+
(f64)sdlEvent.wheel.x,
179+
(f64)sdlEvent.wheel.y,
172180
#endif
173-
},
174-
};
175-
176-
event(uiEvent);
181+
},
182+
});
177183

178184
break;
179185
}
180186

181187
case SDL_QUIT: {
182-
Events::ExitEvent uiEvent{Ok()};
183-
bubble(uiEvent);
188+
bubble<Events::ExitEvent>(*this, Ok());
184189
break;
185190
}
186191

@@ -189,7 +194,8 @@ struct SdlHost :
189194
}
190195
}
191196

192-
void pump() override {
197+
void
198+
pump() override {
193199
SDL_Event e{};
194200

195201
while (SDL_PollEvent(&e) != 0 and alive()) {
@@ -201,7 +207,7 @@ struct SdlHost :
201207
SDL_WaitEventTimeout(nullptr, span.toMSecs());
202208
}
203209

204-
void bubble(Events::Event &e) override {
210+
void bubble(Async::Event &e) override {
205211
if (e.is<Ui::DragEvent>()) {
206212
auto &dragEvent = e.unwrap<Ui::DragEvent>();
207213
if (dragEvent.type == Ui::DragEvent::START) {

src/impls/impl-skift/ui.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Host :
3131
void wait(TimeSpan) override {
3232
}
3333

34-
void bubble(Events::Event &e) override {
34+
void bubble(Async::Event &e) override {
3535
Ui::Host::bubble(e);
3636
}
3737
};

src/kernel/loader/menu.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ Ui::Child alert(String title, String subtitle) {
112112
Ui::spacing(64);
113113
}
114114

115-
void intent(Ui::Node &n, Events::Event &e) {
115+
void intent(Ui::Node &n, Async::Event &e) {
116116
if (auto *k = e.is<Events::KeyboardEvent>()) {
117117
if (k->key == Events::Key::LEFT) {
118-
Ui::dispatchAction<Action>(n, MoveSelectionAction{-1});
118+
Ui::bubble<Action>(n, MoveSelectionAction{-1});
119119
e.accept();
120120
} else if (k->key == Events::Key::RIGHT) {
121-
Ui::dispatchAction<Action>(n, MoveSelectionAction{1});
121+
Ui::bubble<Action>(n, MoveSelectionAction{1});
122122
e.accept();
123123
} else if (k->key == Events::Key::ENTER) {
124-
Ui::dispatchAction<Action>(n, SelectAction{});
124+
Ui::bubble<Action>(n, SelectAction{});
125125
e.accept();
126126
}
127127
}

src/libs/karm-async/async.cpp

+9-27
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,34 @@ namespace Karm::Async {
77
/* --- Sink --- */
88

99
void Loop::_post(Sink &sink, Box<Event> event) {
10-
_queued.emplaceBack(sink, std::move(event));
10+
_queued.emplaceBack(&sink, std::move(event));
1111
}
1212

13-
void Loop::_move(Sink &from, Sink &to) {
13+
void Loop::_move(Sink &from, Sink *to) {
1414
for (auto &q : _queued) {
1515
if (q.sink == &from) {
16-
q.sink = &to;
16+
q.sink = to;
1717
}
1818
}
1919

2020
for (auto &s : _sources) {
2121
if (s.sink == &from) {
22-
s.sink = &to;
22+
s.sink = to;
2323
}
2424
}
2525
}
2626

27-
void Loop::_dtor(Sink &sink) {
28-
for (auto &q : _queued) {
29-
if (q.sink == &sink)
30-
q.sink = nullptr;
31-
}
32-
33-
for (auto &s : _sources) {
34-
if (s.sink == &sink)
35-
s.sink = nullptr;
36-
}
37-
}
38-
3927
/* --- Source --- */
4028

4129
void Loop::_bind(Source &source, Sink &sink) {
42-
_sources.emplaceBack(source, sink);
43-
}
44-
45-
void Loop::_move(Source &from, Source &to) {
46-
for (auto &s : _sources) {
47-
if (s.source == &from)
48-
s.source = &to;
49-
}
30+
_sources.emplaceBack(&source, &sink);
5031
}
5132

52-
void Loop::_dtor(Source *source) {
33+
void Loop::_move(Source &from, Source *to) {
5334
for (auto &s : _sources) {
54-
if (s.source == source)
55-
s.source = nullptr;
35+
if (s.source == &from) {
36+
s.source = to;
37+
}
5638
}
5739
}
5840

0 commit comments

Comments
 (0)