box_helper.cpp
ftxui/dom/box_helper.hpp
algorithm
vector
ftxui/dom/requirement.hpp
ftxui/screen/box.hpp
ftxui/screen/color.hpp
ftxui/screen/image.hpp
ftxui/screen/pixel.hpp
ftxui/screen/screen.hpp
ftxui/screen/terminal.hpp
ftxui/util/autoreset.hpp
src/ftxui/dom/box_helper.cpp
ftxui/dom/box_helper.hpp
algorithm
cstdint
functional
string
utility
vector
ftxui
ftxui::box_helper

    //Copyright2021ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include"ftxui/dom/box_helper.hpp"

    #include<algorithm>//formax
    #include<vector>//forvector

    namespaceftxui::box_helper{

    namespace{
    //Calledwhenthesizeallowedisgreaterthantherequestedsize.This
    //distributestheextraspacestowardtheflexibleelements,inrelative
    //proportions.
    voidComputeGrow(std::vector<Element>*elements,
    intextra_space,
    intflex_grow_sum){
    for(Element&element:*elements){
    constintadded_space=
    extra_space*element.flex_grow/std::max(flex_grow_sum,1);
    extra_space-=added_space;
    flex_grow_sum-=element.flex_grow;
    element.size=element.min_size+added_space;
    }
    }

    //Calledwhenthesizeallowedislowerthantherequestedsize,andthe
    //shrinkableelementcanabsorbethe(negative)extra_space.Thisdistribute
    //theextra_spacetowardthose.
    voidComputeShrinkEasy(std::vector<Element>*elements,
    intextra_space,
    intflex_shrink_sum){
    for(Element&element:*elements){
    constintadded_space=extra_space*element.min_size*
    element.flex_shrink/std::max(flex_shrink_sum,1);
    extra_space-=added_space;
    flex_shrink_sum-=element.flex_shrink*element.min_size;
    element.size=element.min_size+added_space;
    }
    }

    //Calledwhenthesizeallowedislowerthantherequestedsize,andthe
    //shrinkableelementcannotabsorbethe(negative)extra_space.Thisassign
    //zerotoshrinkableelementsanddistributetheremaining(negative)
    //extra_spacetowardtheothernonshrinkableelements.
    voidComputeShrinkHard(std::vector<Element>*elements,
    intextra_space,
    intsize){
    for(Element&element:*elements){
    if(element.flex_shrink!=0){
    element.size=0;
    continue;
    }

    constintadded_space=extra_space*element.min_size/std::max(1,size);
    extra_space-=added_space;
    size-=element.min_size;

    element.size=element.min_size+added_space;
    }
    }

    }//namespace

    voidCompute(std::vector<Element>*elements,inttarget_size){
    intsize=0;
    intflex_grow_sum=0;
    intflex_shrink_sum=0;
    intflex_shrink_size=0;

    for(auto&element:*elements){
    flex_grow_sum+=element.flex_grow;
    flex_shrink_sum+=element.min_size*element.flex_shrink;
    if(element.flex_shrink!=0){
    flex_shrink_size+=element.min_size;
    }
    size+=element.min_size;
    }

    constintextra_space=target_size-size;
    if(extra_space>=0){
    ComputeGrow(elements,extra_space,flex_grow_sum);
    }elseif(flex_shrink_size+extra_space>=0){
    ComputeShrinkEasy(elements,extra_space,flex_shrink_sum);

    }else{
    ComputeShrinkHard(elements,extra_space+flex_shrink_size,
    size-flex_shrink_size);
    }
    }

    }//namespaceftxui::box_helper