From e968fd4de389125674b733d020a8bf22e3e9c1c7 Mon Sep 17 00:00:00 2001 From: "Vedant R. Nimje" Date: Sat, 9 Sep 2023 17:37:39 +0530 Subject: [PATCH] [Feature] Adding color options for Toggle and Dropdown (#11) Signed-off-by: Vedant --- examples/menu_toggle_dropdown.qf | 6 +++--- include/quick-ftxui.hpp | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/examples/menu_toggle_dropdown.qf b/examples/menu_toggle_dropdown.qf index 4f3c20f..ee3e650 100644 --- a/examples/menu_toggle_dropdown.qf +++ b/examples/menu_toggle_dropdown.qf @@ -1,7 +1,7 @@ Vertical { int x - Blue Menu{ + Red Menu{ [ "Physics", "Maths", "Chemistry", "Biology",], HorizontalAnimated, x @@ -14,11 +14,11 @@ Vertical } int y int z - Toggle { + Cyan Toggle { [ "On", "Off", ], y } - Dropdown { + BlueLight Dropdown { [ "Zain", "Mahesh", "Alqama", "Vaidic", "Mundane", "Advait", ], z } diff --git a/include/quick-ftxui.hpp b/include/quick-ftxui.hpp index badf98e..9073a5f 100644 --- a/include/quick-ftxui.hpp +++ b/include/quick-ftxui.hpp @@ -143,11 +143,13 @@ struct menu { }; struct toggle { + colours color = colours::Default; std::vector entries; std::string selected; }; struct dropdown { + colours color = colours::Default; std::vector entries; std::string selected; }; @@ -239,11 +241,13 @@ BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::menu, ) BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::toggle, + (quick_ftxui_ast::colours, color) (std::vector , entries) (std::string, selected) ) BOOST_FUSION_ADAPT_STRUCT(quick_ftxui_ast::dropdown, + (quick_ftxui_ast::colours, color) (std::vector , entries) (std::string, selected) ) @@ -525,10 +529,14 @@ struct node_printer : boost::static_visitor<> { void operator()(quick_ftxui_ast::toggle const &text) const { // tab(indent + tabsize); + + ftxui::Color toggle_clr = quick_ftxui_ast::resolveColour(text.color); + if (auto It = quick_ftxui_ast::numbers.find(std::string(text.selected)); It != quick_ftxui_ast::numbers.end()) { data->components.push_back( - ftxui::Toggle(&text.entries, (int *)(&It->second))); + ftxui::Toggle(&text.entries, (int *)(&It->second)) | + ftxui::color(toggle_clr)); } else { throw std::runtime_error("Variable " + text.selected + " not found"); } @@ -536,10 +544,14 @@ struct node_printer : boost::static_visitor<> { void operator()(quick_ftxui_ast::dropdown const &text) const { // tab(indent + tabsize); + + ftxui::Color drpdwn_clr = quick_ftxui_ast::resolveColour(text.color); + if (auto It = quick_ftxui_ast::numbers.find(std::string(text.selected)); It != quick_ftxui_ast::numbers.end()) { data->components.push_back( - ftxui::Dropdown(&text.entries, (int *)(&It->second))); + ftxui::Dropdown(&text.entries, (int *)(&It->second)) | + ftxui::color(drpdwn_clr)); } else { throw std::runtime_error("Variable " + text.selected + " not found"); } @@ -696,10 +708,10 @@ struct parser +(quoted_string >> ',') >> ']' >> -(',' >> menuopt_kw) >> ',' >> identifier >> '}'; - toggle_comp %= qi::lit("Toggle") >> '{' >> '[' >> +(quoted_string >> ',') >> - ']' >> ',' >> identifier >> '}'; + toggle_comp %= -(color_kw) >> qi::lit("Toggle") >> '{' >> '[' >> + +(quoted_string >> ',') >> ']' >> ',' >> identifier >> '}'; - drpdwn_comp %= qi::lit("Dropdown") >> '{' >> '[' >> + drpdwn_comp %= -(color_kw) >> qi::lit("Dropdown") >> '{' >> '[' >> +(quoted_string >> ',') >> ']' >> ',' >> identifier >> '}'; int_var_decl %= qi::lit("int") >> identifier >> -('=' > qi::int_);