Skip to content

Question : Rendering different UI states : colored buttons on click, #161

@daminetreg

Description

@daminetreg

Hi @ArthurSonzogni,

Here is more of a question than any kind of bug report while working with your library with @Lambourl for it's integration at the CLI UI for tipi.build.

What is supposed to be the way that you've foreseen to have for instance colored components that are updated on events (custom or click) ?

We went for something where we delete from the Component children_ list and from the Element list before rerendering.

I feel we are missing the big picture, how it supposed to be done. I was wondering, why aren't all Components also Elements ?

Is it a design choice or a lack of time ? Because in the solution we have found it seems to require some manual synchronization between Elements and Components vectors.

My use case is : I want to change the color state of the button when I click on it.

The follow works but I don't think it's the way it is supposed to be done ? Or is it ?
image

#include <memory>  // for allocator, __shared_ptr_access, shared_ptr
#include <string>  // for wstring, basic_string
#include <vector>  // for vector

#include "ftxui/component/captured_mouse.hpp"  // for ftxui
#include "ftxui/component/component.hpp"  // for Radiobox, Renderer, Tab, Toggle, Vertical
#include "ftxui/component/component_base.hpp"      // for ComponentBase
#include "ftxui/component/screen_interactive.hpp"  // for ScreenInteractive
#include "ftxui/dom/elements.hpp"  // for Element, separator, operator|, vbox, border

using namespace ftxui;

int main(int argc, const char* argv[]) {

  struct AppMainWindow : public ComponentBase {

      int tab_selected = 0;
      std::vector<std::wstring> tab_1_entries{
          L"Forest",
          L"Water",
          L"I don't know",
      };
      int tab_1_selected = 0;

      std::vector<std::wstring> tab_2_entries{
          L"Hello",
          L"Hi",
          L"Hay",
      };
      int tab_2_selected = 0;

      std::vector<std::wstring> tab_3_entries{
          L"Table",
          L"Nothing",
          L"Is",
          L"Empty",
      };
      int tab_3_selected = 0;

      Component tab_container = Container::Tab(
      {
          Radiobox(&tab_1_entries, &tab_1_selected),
          Radiobox(&tab_2_entries, &tab_2_selected),
          Radiobox(&tab_3_entries, &tab_3_selected),
      },
      &tab_selected);

      Components buttons;
      Elements button_line;
      
      void updateButtonModel() {
          button_line.clear();
          for (auto& button : buttons) {
              auto found = std::find(children_.begin(), children_.end(), button);
              if (found != children_.end()) {
                  children_.erase(found);
              }

          }
          buttons.clear();

          for (int i = 0; i < 3; ++i) {
            auto button = Button( std::wstring(L" [ ⚈ Hey ") + std::to_wstring(i) + std::wstring(L"] "), 
            [this, i] () mutable { 
                tab_selected = i;
                updateButtonModel();
             }, false);
            this->Add(button);
            button_line.push_back(hbox(button->Render()) | color(((tab_selected == i) ? Color::Red1 : Color::Green1)));
            button_line.push_back(separator());
          }
      }

      AppMainWindow() : ComponentBase() {

          this->Add(tab_container);            
          this->updateButtonModel();
      }

      virtual Element Render() override {
        return 
        vbox({
                hbox(
                    button_line
                ),
                hbox({
                   tab_container->Render(),
                    }),
        }) ;
      }
    
  };

  auto screen = ScreenInteractive::TerminalOutput();
  screen.Loop(std::make_shared<AppMainWindow>());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions