Giter VIP home page Giter VIP logo

Comments (2)

ArthurSonzogni avatar ArthurSonzogni commented on June 8, 2024

Hi @bqle!

I spotted several issues with the current code, and then provided an example based on yours.

Error 1

    bool OnEvent(Event event) final {
      return true;
    }

Don't handle every events (e.g. returning true)!. This prevents the parent component: Container::Vertical() to handle the up/down keys.

Error 2

    Element Render() final {
      return hbox({
          text("Hello!")
      });
    }

You should use the active and select element to decorate the currently Active() or Focused() component. This is what will cause the frame element to position the active element in the center of the scrolled area.

Error 3

    bool Focusable() const final { return true; }

This should be used to indicate the parent element not to skip it when pressing up/down.

Fixed code:

#include <memory>  // for allocator, __shared_ptr_access, shared_ptr
#include <string>  // for to_string, operator+

#include "ftxui/component/captured_mouse.hpp"  // for ftxui
#include "ftxui/component/component.hpp"       // for Button, Renderer, Vertical
#include "ftxui/component/component_base.hpp"  // for ComponentBase
#include "ftxui/component/component_options.hpp"   // for ButtonOption
#include "ftxui/component/screen_interactive.hpp"  // for ScreenInteractive
#include "ftxui/dom/elements.hpp"  // for operator|, text, Element, hbox, separator, size, vbox, border, frame, vscroll_indicator, HEIGHT, LESS_THAN
#include "ftxui/dom/table.hpp"     // for Table
#include "ftxui/screen/color.hpp"  // for Color, Color::Default, Color::GrayDark, Color::White

using namespace ftxui;

Component MakeOne() {
  class Impl : public ComponentBase {
    Element Render() final {
      const bool active = Active();
      const bool focused = Focused();
      auto focus_management = focused  ? ftxui::focus
                              : active ? ftxui::select
                                       : ftxui::nothing;
      auto style = focused ? bold : nothing;
      return text("Hello") | style | focus_management;
    }

    bool Focusable() const final { return true; }
  };

  return Make<Impl>();
}

int main() {
  auto container = Container::Vertical({});
  for (int i = 0; i < 30; i++) {
    container->Add(MakeOne());
  }

  auto screen = ScreenInteractive::FitComponent();

  auto renderer = Renderer(container, [&] {
    return vbox({
               container->Render() | vscroll_indicator | frame |
                   size(HEIGHT, LESS_THAN, 20),
           }) |
           border;
  });

  auto component = CatchEvent(renderer, [&](Event event) {
    if (event == Event::Character('q')) {
      screen.ExitLoopClosure()();
      return true;
    }
    return false;
  });

  screen.Loop(renderer);

  return 0;
}

from ftxui.

bqle avatar bqle commented on June 8, 2024

Thank you so much Arthur! That really helped.

from ftxui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.