Improve add_child and Button APIs

This commit is contained in:
Luke Street
2026-04-30 23:36:30 -06:00
parent 39298f8d8f
commit e0c449f28e
9 changed files with 551 additions and 275 deletions
+15 -14
View File
@@ -18,20 +18,6 @@ Rml::Element* createRoot(Rml::Element* parent, const Rml::String& tagName) {
Button::Button(Rml::Element* parent, ButtonProps props, const Rml::String& tagName)
: Component(createRoot(parent, tagName)) {
update_props(std::move(props));
listen(mRoot, Rml::EventId::Click, [this](Rml::Event& event) {
if (mProps.onPressed) {
mProps.onPressed(event);
}
});
listen(mRoot, Rml::EventId::Keydown, [this](Rml::Event& event) {
const auto cmd = map_nav_event(event);
if (cmd == NavCommand::Confirm) {
if (mProps.onPressed) {
mProps.onPressed(event);
}
event.StopPropagation();
}
});
}
void Button::set_text(const Rml::String& text) {
@@ -48,6 +34,21 @@ void Button::set_selected(bool selected) {
}
}
Button& Button::on_pressed(ButtonCallback callback) {
if (!callback) {
return *this;
}
listen(mRoot, Rml::EventId::Click, [callback](Rml::Event&) { callback(); });
listen(mRoot, Rml::EventId::Keydown, [callback = std::move(callback)](Rml::Event& event) {
const auto cmd = map_nav_event(event);
if (cmd == NavCommand::Confirm) {
callback();
event.StopPropagation();
}
});
return *this;
}
void Button::update_props(Props props) {
set_text(props.text);
set_selected(props.selected);