SpreadSheet Like Scrolling #829
Replies: 4 comments 2 replies
-
Hello! What about:
Example: #include <memory>
#include <string>
#include <utility>
#include <vector>
#include <ftxui/component/captured_mouse.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/component/component_base.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <ftxui/dom/table.hpp>
using namespace ftxui;
Component Grid(const std::vector<Components>& elements) {
class Impl : public ComponentBase {
private:
std::vector<Components> rows_;
int selector_x_ = 0;
int selector_y_ = 0;
public:
Impl(std::vector<Components> rows) : rows_(std::move(rows)) {
Components components;
for (const auto& line : rows_) {
components.push_back(Container::Horizontal(line, &selector_x_));
}
Add(Container::Vertical(components, &selector_y_));
}
Element Render() override {
// Render every cells:
std::vector<Elements> elements;
for(auto& row : rows_) {
elements.emplace_back();
for(auto& element : row) {
elements.back().push_back(element->Render());
}
}
auto table = Table(std::move(elements));
table.SelectAll().Separator(LIGHT);
table.SelectAll().Border(DOUBLE);
auto element = table.Render();
// Wrap it into a frame so that it becomes scrollable.
element |= vscroll_indicator;
element |= hscroll_indicator;
element |= frame;
element |= center;
element |= flex;
return element;
}
};
return Make<Impl>(elements);
}
Component Dummy() {
return Renderer([](bool focused) {
Element element = text("Dummy");
if (focused) {
element |= focus;
element |= inverted;
}
return element;
});
}
int main() {
auto component = Grid({
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
{ Dummy(), Dummy(), Dummy(), Dummy(), Dummy(),Dummy(), Dummy()},
});
auto screen = ScreenInteractive::Fullscreen();
screen.Loop(component);
return 0;
} test-2024-03-20_16.54.31.mp4 |
Beta Was this translation helpful? Give feedback.
-
This is great! Is there a way of the render for Dummy() to know which cell location it is rendering? I maintain a matrix of data and when the dummy() is called return the right number. |
Beta Was this translation helpful? Give feedback.
-
Also, does the table only render what's visible on the window. The spreadsheet is 2000x25 rows, so lot's of live data being updated on a different thread? |
Beta Was this translation helpful? Give feedback.
-
Hi, I've got a working prototype. It works but the behavior of the scrolling is strange. When it first start the selector in on a single cell and I can scroll horizontal. But when I go past bottom of the #include #include <ftxui/component/captured_mouse.hpp> using namespace ftxui; Component Grid(const std::vector& elements) {
}; return Make(elements); Component Cell(int row,int col) {
}); int main() { std::vector elements; for(int ri=0;ri<1000;ri++) auto component = Grid(elements); auto screen = ScreenInteractive::Fullscreen(); |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for writing such a neat library. I'm trying to use it to create a live updating spreadsheet for monitoring stocks. I've got the table component working but I don't think that the correct approach.
What I'd like to do is to have a component that renders each cell via a call back function. It'd also like to to be able to scroll vertically and horizontally like a spreadsheet keeping the row and column headers locked. Any pointers on how to implement this in your library?
Beta Was this translation helpful? Give feedback.
All reactions