From 33a970530bdc1cb3b7a084d0a15498d234b6453e Mon Sep 17 00:00:00 2001 From: vedantparanjape <22630228+VedantParanjape@users.noreply.github.com> Date: Tue, 4 Aug 2020 22:24:30 +0530 Subject: [PATCH] Added on_enter() method to toggle component --- include/ftxui/component/toggle.hpp | 3 ++- src/ftxui/component/toggle.cpp | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/ftxui/component/toggle.hpp b/include/ftxui/component/toggle.hpp index 9747d6c..f0a2a65 100644 --- a/include/ftxui/component/toggle.hpp +++ b/include/ftxui/component/toggle.hpp @@ -27,7 +27,8 @@ class Toggle : public Component { // Callback. std::function on_change = []() {}; - + std::function on_enter = []() {}; + // Component implementation. Element Render() override; bool OnEvent(Event) override; diff --git a/src/ftxui/component/toggle.cpp b/src/ftxui/component/toggle.cpp index 16311d0..59786a2 100644 --- a/src/ftxui/component/toggle.cpp +++ b/src/ftxui/component/toggle.cpp @@ -40,10 +40,17 @@ bool Toggle::OnEvent(Event event) { selected = std::max(0, std::min(int(entries.size()) - 1, selected)); - if (old_selected != selected) + if (old_selected != selected) { on_change(); + return true; + } - return old_selected != selected; + if (event == Event::Return) { + on_enter(); + return true; + } + + return false; } } // namespace ftxui