diff --git a/examples/component/checkbox_in_frame.cpp b/examples/component/checkbox_in_frame.cpp index 90bd7e3..6b12c0b 100644 --- a/examples/component/checkbox_in_frame.cpp +++ b/examples/component/checkbox_in_frame.cpp @@ -25,7 +25,10 @@ class MyComponent : public Component { for(auto& it : checkbox) { 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: diff --git a/examples/component/gallery.cpp b/examples/component/gallery.cpp index 6bea97b..1278423 100644 --- a/examples/component/gallery.cpp +++ b/examples/component/gallery.cpp @@ -54,7 +54,12 @@ class MyComponent : public 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 { @@ -69,7 +74,7 @@ class MyComponent : public Component { Render(L"radiobox", radiobox), separator(), Render(L"input", input) - ) | border | frame | size(10,10) | border; + ) | border; } }; diff --git a/examples/component/radiobox_in_frame.cpp b/examples/component/radiobox_in_frame.cpp index b72b4bd..3415638 100644 --- a/examples/component/radiobox_in_frame.cpp +++ b/examples/component/radiobox_in_frame.cpp @@ -21,7 +21,7 @@ class MyComponent : public Component { } Element Render() override { - return radiobox.Render() | frame | size(20,10) | border; + return radiobox.Render() | frame | size(HEIGHT, LESS_THAN, 10) | border; } }; diff --git a/examples/component/tab_vertical.cpp b/examples/component/tab_vertical.cpp index 4cce543..ca8d643 100644 --- a/examples/component/tab_vertical.cpp +++ b/examples/component/tab_vertical.cpp @@ -1,5 +1,4 @@ #include -#include #include "ftxui/component/container.hpp" #include "ftxui/component/menu.hpp" diff --git a/examples/dom/package_manager.cpp b/examples/dom/package_manager.cpp index 9cf0d32..3f32822 100644 --- a/examples/dom/package_manager.cpp +++ b/examples/dom/package_manager.cpp @@ -43,7 +43,7 @@ int main(int argc, const char *argv[]) int nb_done = 0; 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) { diff --git a/examples/dom/spinner.cpp b/examples/dom/spinner.cpp index 5e943c5..835a2c9 100644 --- a/examples/dom/spinner.cpp +++ b/examples/dom/spinner.cpp @@ -19,7 +19,8 @@ int main(int argc, const char *argv[]) entries.push_back(separator()); entries.push_back( hbox( - text(to_wstring(i)) | size(5,1), + text(to_wstring(i)) | size(WIDTH, EQUAL, 2), + separator(), spinner(i, index) | bold ) ); diff --git a/ftxui/include/ftxui/dom/elements.hpp b/ftxui/include/ftxui/dom/elements.hpp index 8949f8c..d130edf 100644 --- a/ftxui/include/ftxui/dom/elements.hpp +++ b/ftxui/include/ftxui/dom/elements.hpp @@ -42,7 +42,11 @@ Element dbox(Elements); // container. Element filler(); 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 --- // A frame is a scrollable area. The internal area is potentially larger than diff --git a/ftxui/src/ftxui/component/screen_interactive.cpp b/ftxui/src/ftxui/component/screen_interactive.cpp index 6407a04..c10409d 100644 --- a/ftxui/src/ftxui/component/screen_interactive.cpp +++ b/ftxui/src/ftxui/component/screen_interactive.cpp @@ -123,9 +123,10 @@ void ScreenInteractive::Draw(Component* component) { dimy = Terminal::Size().dimy; break; case Dimension::FitComponent: + auto terminal = Terminal::Size(); document->ComputeRequirement(); - dimx = document->requirement().min.x; - dimy = document->requirement().min.y; + dimx = std::min(document->requirement().min.x, terminal.dimx); + dimy = std::min(document->requirement().min.y, terminal.dimy); break; } diff --git a/ftxui/src/ftxui/dom/size.cpp b/ftxui/src/ftxui/dom/size.cpp index 5d03e59..51633a9 100644 --- a/ftxui/src/ftxui/dom/size.cpp +++ b/ftxui/src/ftxui/dom/size.cpp @@ -1,20 +1,40 @@ -#include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" +#include "ftxui/dom/node.hpp" namespace ftxui { class Size : public Node { public: - Size(Element child, size_t width, size_t height) - : Node(unpack(std::move(child))), width_(width), height_(height) {} + Size(Element child, Direction direction, Constraint constraint, size_t value) + : Node(unpack(std::move(child))), + direction_(direction), + constraint_(constraint), + value_(value) {} + ~Size() override {} + void ComputeRequirement() override { Node::ComputeRequirement(); requirement_ = children[0]->requirement(); - requirement_.min.x = width_; - requirement_.min.y = height_; - requirement_.flex.x = 0; - requirement_.flex.y = 0; + + 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; + else + requirement_.flex.y = 0; } void SetBox(Box box) override { @@ -23,13 +43,14 @@ class Size : public Node { } private: - size_t width_; - size_t height_; + Direction direction_; + Constraint constraint_; + int value_; }; -Decorator size(size_t width, size_t height) { +Decorator size(Direction direction, Constraint constraint, int value) { return [=](Element e) { - return std::make_unique(std::move(e), width, height); + return std::make_unique(std::move(e), direction, constraint, value); }; } diff --git a/ftxui/src/ftxui/screen/string.cpp b/ftxui/src/ftxui/screen/string.cpp index 8569495..0ba974b 100644 --- a/ftxui/src/ftxui/screen/string.cpp +++ b/ftxui/src/ftxui/screen/string.cpp @@ -1,4 +1,4 @@ -#include "ftxui/util/string.hpp" +#include "ftxui/screen/string.hpp" #include #include