Comments (2)
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.
Thank you so much Arthur! That really helped.
from ftxui.
Related Issues (20)
- 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
- Is there a way to change the Slider 's color? HOT 1
- [doc-bug] Move Documentation Comments From `.cpp` To `.hpp` For Better LSP Support 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.