Comments (6)
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.
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.
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.
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.
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.
Now I see why! Thank you.
from ftxui.
Related Issues (20)
- how to get a mdal dialog window (Stacked)? HOT 2
- the decoration of stacked window not work correctly HOT 1
- Mouse input does not work correctly on urxvt: Moving the cursor always claims the mouse button is clicked. HOT 4
- Cursor state (blinking, block, underline, ...) is not reset after exiting a program HOT 2
- No definition for `Component Checkbox(CheckboxOption options)` method HOT 4
- Combine canvas and other elements HOT 6
- Checkbox does not `TakeFocus()` when clicked HOT 3
- ftxui_set_options.cmake: unable to compile on windows. HOT 2
- ButtonBase::OnClick ordering HOT 1
- Should Table::SelectRectangle() wrap in the way it does? HOT 1
- input.cpp field length. HOT 2
- Console doesn't clear previous renderer text after new renderer HOT 3
- Canvas background
- Introductory example fails to build HOT 2
- Custom event question HOT 2
- Add example to use system ftxui HOT 3
- "Bug or feature" HOT 3
- fatal error: string_view: No such file or directory HOT 4
- How to check if current terminal supports TUI? HOT 2
- include could not find requested file: .../lib/ftxui/cmake/ftxui-targets.cmake HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ftxui.