Add size(direction, constraint, value).

For example:
============

element
  | size(WIDTH, EQUAL 10);

element
  | size(HEIGHT, GREATER_THAN, 10);

element
  | size(WIDTH, EQUAL, 10)
  | size(HEIGHT, EQUAL, 10)
This commit is contained in:
Arthur Sonzogni 2019-01-20 23:04:10 +01:00
parent fddcbdea65
commit 456ede70fd
10 changed files with 56 additions and 22 deletions

View File

@ -25,7 +25,10 @@ class MyComponent : public Component {
for(auto& it : checkbox) { for(auto& it : checkbox) {
content.push_back(it.Render()); content.push_back(it.Render());
} }
return vbox(std::move(content)) | frame | size(20, 10) | border; return vbox(std::move(content))
| frame
| size(HEIGHT, LESS_THAN, 10)
| border;
} }
private: private:

View File

@ -54,7 +54,12 @@ class MyComponent : public Component {
} }
Element Render(std::wstring name, Component& component) { Element Render(std::wstring name, Component& component) {
return hbox(text(name) | size(8,1), separator(), component.Render()); return
hbox(
text(name) | size(WIDTH, EQUAL, 8),
separator(),
component.Render()
);
} }
Element Render() override { Element Render() override {
@ -69,7 +74,7 @@ class MyComponent : public Component {
Render(L"radiobox", radiobox), Render(L"radiobox", radiobox),
separator(), separator(),
Render(L"input", input) Render(L"input", input)
) | border | frame | size(10,10) | border; ) | border;
} }
}; };

View File

@ -21,7 +21,7 @@ class MyComponent : public Component {
} }
Element Render() override { Element Render() override {
return radiobox.Render() | frame | size(20,10) | border; return radiobox.Render() | frame | size(HEIGHT, LESS_THAN, 10) | border;
} }
}; };

View File

@ -1,5 +1,4 @@
#include <iostream> #include <iostream>
#include <thread>
#include "ftxui/component/container.hpp" #include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp" #include "ftxui/component/menu.hpp"

View File

@ -43,7 +43,7 @@ int main(int argc, const char *argv[])
int nb_done = 0; int nb_done = 0;
auto to_text = [](int number) { auto to_text = [](int number) {
return text(to_wstring(number)) | size(3,1); return text(to_wstring(number)) | size(WIDTH, EQUAL, 3);
}; };
auto renderTask = [&](const Task& task) { auto renderTask = [&](const Task& task) {

View File

@ -19,7 +19,8 @@ int main(int argc, const char *argv[])
entries.push_back(separator()); entries.push_back(separator());
entries.push_back( entries.push_back(
hbox( hbox(
text(to_wstring(i)) | size(5,1), text(to_wstring(i)) | size(WIDTH, EQUAL, 2),
separator(),
spinner(i, index) | bold spinner(i, index) | bold
) )
); );

View File

@ -42,7 +42,11 @@ Element dbox(Elements);
// container. // container.
Element filler(); Element filler();
Element flex(Element); Element flex(Element);
Decorator size(size_t width, size_t height);
// -- Size override;
enum Direction { WIDTH, HEIGHT };
enum Constraint { LESS_THAN, EQUAL, GREATER_THAN };
Decorator size(Direction, Constraint, int value);
// --- Frame --- // --- Frame ---
// A frame is a scrollable area. The internal area is potentially larger than // A frame is a scrollable area. The internal area is potentially larger than

View File

@ -123,9 +123,10 @@ void ScreenInteractive::Draw(Component* component) {
dimy = Terminal::Size().dimy; dimy = Terminal::Size().dimy;
break; break;
case Dimension::FitComponent: case Dimension::FitComponent:
auto terminal = Terminal::Size();
document->ComputeRequirement(); document->ComputeRequirement();
dimx = document->requirement().min.x; dimx = std::min(document->requirement().min.x, terminal.dimx);
dimy = document->requirement().min.y; dimy = std::min(document->requirement().min.y, terminal.dimy);
break; break;
} }

View File

@ -1,19 +1,39 @@
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
namespace ftxui { namespace ftxui {
class Size : public Node { class Size : public Node {
public: public:
Size(Element child, size_t width, size_t height) Size(Element child, Direction direction, Constraint constraint, size_t value)
: Node(unpack(std::move(child))), width_(width), height_(height) {} : Node(unpack(std::move(child))),
direction_(direction),
constraint_(constraint),
value_(value) {}
~Size() override {} ~Size() override {}
void ComputeRequirement() override { void ComputeRequirement() override {
Node::ComputeRequirement(); Node::ComputeRequirement();
requirement_ = children[0]->requirement(); requirement_ = children[0]->requirement();
requirement_.min.x = width_;
requirement_.min.y = height_; auto& value = direction_ == WIDTH ? requirement_.min.x : requirement_.min.y;
switch (constraint_) {
case LESS_THAN:
value = std::min(value, value_);
break;
case EQUAL:
value = value_;
break;
case GREATER_THAN:
value = std::max(value, value_);
break;
}
if (direction_ == WIDTH)
requirement_.flex.x = 0; requirement_.flex.x = 0;
else
requirement_.flex.y = 0; requirement_.flex.y = 0;
} }
@ -23,13 +43,14 @@ class Size : public Node {
} }
private: private:
size_t width_; Direction direction_;
size_t height_; Constraint constraint_;
int value_;
}; };
Decorator size(size_t width, size_t height) { Decorator size(Direction direction, Constraint constraint, int value) {
return [=](Element e) { return [=](Element e) {
return std::make_unique<Size>(std::move(e), width, height); return std::make_unique<Size>(std::move(e), direction, constraint, value);
}; };
} }

View File

@ -1,4 +1,4 @@
#include "ftxui/util/string.hpp" #include "ftxui/screen/string.hpp"
#include <codecvt> #include <codecvt>
#include <locale> #include <locale>