Giter VIP home page Giter VIP logo

Comments (6)

ArthurSonzogni avatar ArthurSonzogni commented on June 9, 2024

Hi,
Here is the implementation of Container::Vertical::OnEvent:
https://github.com/ArthurSonzogni/FTXUI/blob/8058e1af6cf67073575a3056acdadf88e6d5b307/src/ftxui/component/container.cpp#L31C2-L40C4

So, it should redirect events to its active child.

Would you have some example I can try, so that I might understand the issue you are experiencing?

from ftxui.

mingsheng13 avatar mingsheng13 commented on June 9, 2024

When I press arrow up/down the counter will not increment.

#include <iostream>
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"

using namespace ftxui;

Component MakeOne() {
    class Impl : public ComponentBase {
        int counter_up = 0;
        int counter_down = 0;

        Element Render() final {
            return vbox({
                                text("counter_up = " + std::to_string(counter_up)),
                                text("counter_down = " + std::to_string(counter_down)),
                        });
        }

        bool OnEvent(Event event) final {
            if (event == Event::ArrowUp) {
                counter_up++;
                return true;
            }

            if (event == Event::ArrowDown) {
                counter_down++;
                return true;
            }

            return true;
        }
    };

    return Make<Impl>();
}

int main()
{
    auto makeOne = MakeOne();
    auto container = Container::Vertical({makeOne});
    auto screen = ScreenInteractive::TerminalOutput();
    screen.Loop(container);
}

from ftxui.

ArthurSonzogni avatar ArthurSonzogni commented on June 9, 2024

I see! Thanks.

A component needs to define when it can be focused by the user. The default is to delegate this by checking the children. Here, this is a lead component (= no children), so it has to add:

        bool Focusable() const final { return true; }

in order to get events.

Not sure how to improve this ;-)

from ftxui.

mingsheng13 avatar mingsheng13 commented on June 9, 2024

I don't think I'm getting this... why isn't MakeOne2 working in this case?

#include <iostream>
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"

using namespace ftxui;

Component MakeOne() {
    class Impl : public ComponentBase {
        int counter_up = 0;
        int counter_down = 0;

        Element Render() final {
            return vbox({
                                text("counter_up = " + std::to_string(counter_up)),
                                text("counter_down = " + std::to_string(counter_down)),
                        });
        }

        bool OnEvent(Event event) final {
            if (event == Event::ArrowUp) {
                counter_up++;
                return true;
            }

            if (event == Event::ArrowDown) {
                counter_down++;
                return true;
            }

            return false;
        }

        bool Focusable() const final
        {
            return true;
        }
    };
    return Make<Impl>();
}

Component MakeOne2() {
    class Impl : public ComponentBase {
        int counter_left = 0;
        int counter_right = 0;

        Element Render() final {
            return vbox({
                                text("counter_left = " + std::to_string(counter_left)),
                                text("counter_right = " + std::to_string(counter_right)),
                        });
        }

        bool OnEvent(Event event) final {
            if (event == Event::ArrowLeft) {
                counter_left++;
                return true;
            }

            if (event == Event::ArrowRight) {
                counter_right++;
                return true;
            }

            return false;
        }

        bool Focusable() const final
        {
            return true;
        }
    };
    return Make<Impl>();
}
int main()
{
    auto makeOne = MakeOne();
    auto makeOne2 = MakeOne2();
    auto container = Container::Vertical({ makeOne,  makeOne2});
    auto screen = ScreenInteractive::TerminalOutput();
    screen.Loop(container);
}

from ftxui.

ArthurSonzogni avatar ArthurSonzogni commented on June 9, 2024

You have 3 components.

Container::Vertical()
 ├─makeOne()
 └─makeOne2()

To handle events, the Container::Vertical() will delegate event handling toward the currently focused children. If the child do not handle the event (return false), then it will try handling the event itself.

So, when sending Event::ArrowDown(), the child handle the event, so, the parent can't handle it itself.

See this example instead:

#include <iostream>
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"

using namespace ftxui;

Component MakeOne() {
  class Impl : public ComponentBase {
    int counter_up = 0;
    int counter_down = 0;

    Element Render() final {
      auto style = Focused() ? inverted : nothing;
      return vbox({
                 text("counter_up = " + std::to_string(counter_up)),
                 separator(),
                 text("counter_down = " + std::to_string(counter_down)),
             }) |
             border | style;
    }

    bool OnEvent(Event event) final {
      if (event == Event::ArrowUp) {
        counter_up++;
        return true;
      }

      if (event == Event::ArrowDown) {
        counter_down++;
        return true;
      }

      return false;
    }

    bool Focusable() const final { return true; }
  };
  return Make<Impl>();
}

int main() {
  auto makeOne = MakeOne();
  auto makeOne2 = MakeOne();
  auto container = Container::Horizontal({makeOne, makeOne2});
  auto screen = ScreenInteractive::TerminalOutput();
  screen.Loop(container);
}

from ftxui.

mingsheng13 avatar mingsheng13 commented on June 9, 2024

Now I see why! Thank you.

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.