node.cpp
ftxui/screen/box.hpp
string
utility
cstddef
ftxui/dom/node.hpp
ftxui/dom/selection.hpp
ftxui/screen/screen.hpp
ftxui/dom/node.hpp
ftxui/dom/requirement.hpp
ftxui/dom/selection.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/node.cpp
cstddef
cstdint
functional
memory
sstream
string
utility
vector
ftxui

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<ftxui/screen/box.hpp>//forBox
    #include<string>
    #include<utility>//formove

    #include<cstddef>
    #include"ftxui/dom/node.hpp"
    #include"ftxui/dom/selection.hpp"//forSelection
    #include"ftxui/screen/screen.hpp"//forScreen

    namespaceftxui{

    Node::Node()=default;
    Node::Node(Elementschildren):children_(std::move(children)){}
    Node::~Node()=default;

    ///@briefComputehowmuchspaceanelementsneeds.
    ///@ingroupdom
    voidNode::ComputeRequirement(){
    if(children_.empty()){
    return;
    }
    for(auto&child:children_){
    child->ComputeRequirement();
    }

    //Bydefault,therequirementistheoneofthefirstchild.
    requirement_=children_[0]->requirement();

    //Propagatethefocusedrequirement.
    for(size_ti=1;i<children_.size();++i){
    if(!requirement_.focused.enabled&&
    children_[i]->requirement().focused.enabled){
    requirement_.focused=children_[i]->requirement().focused;
    }
    }
    }

    ///@briefAssignapositionandadimensiontoanelementfordrawing.
    ///@ingroupdom
    voidNode::SetBox(Boxbox){
    box_=box;
    }

    ///@briefComputetheselectionofanelement.
    ///@ingroupdom
    voidNode::Select(Selection&selection){
    //IfthisNodebox_doesn'tintersectwiththeselection,thennoselection.
    if(Box::Intersection(selection.GetBox(),box_).IsEmpty()){
    return;
    }

    //Bydefaultwedefertheselectiontothechildren.
    for(auto&child:children_){
    child->Select(selection);
    }
    }

    ///@briefDisplayanelementonaftxui::Screen.
    ///@ingroupdom
    voidNode::Render(Screen&screen){
    for(auto&child:children_){
    child->Render(screen);
    }
    }

    voidNode::Check(Status*status){
    for(auto&child:children_){
    child->Check(status);
    }
    status->need_iteration|=(status->iteration==0);
    }

    std::stringNode::GetSelectedContent(Selection&selection){
    std::stringcontent;

    for(auto&child:children_){
    content+=child->GetSelectedContent(selection);
    }

    returncontent;
    }

    ///@briefDisplayanelementonaftxui::Screen.
    ///@ingroupdom
    voidRender(Screen&screen,constElement&element){
    Selectionselection;
    Render(screen,element.get(),selection);
    }

    ///@briefDisplayanelementonaftxui::Screen.
    ///@ingroupdom
    voidRender(Screen&screen,Node*node){
    Selectionselection;
    Render(screen,node,selection);
    }

    voidRender(Screen&screen,Node*node,Selection&selection){
    Boxbox;
    box.x_min=0;
    box.y_min=0;
    box.x_max=screen.dimx()-1;
    box.y_max=screen.dimy()-1;

    Node::Statusstatus;
    node->Check(&status);
    constintmax_iterations=20;
    while(status.need_iteration&&status.iteration<max_iterations){
    //Step1:Findwhatdimensionthiselementswantstobe.
    node->ComputeRequirement();

    //Step2:Assignadimensiontotheelement.
    node->SetBox(box);

    //Checkiftheelementneedsanotheriterationofthelayoutalgorithm.
    status.need_iteration=false;
    status.iteration++;
    node->Check(&status);
    }

    //Step3:Selection
    if(!selection.IsEmpty()){
    node->Select(selection);
    }

    if(node->requirement().focused.enabled
    #ifdefined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
    //SettingthecursortotherightpositionallowfolksusingCJK(China,
    //Japanese,Korean,...)characterstoseetheir[inputmethodeditor]
    //displayedattherightlocation.See[issue].
    //
    //[inputmethodeditor]:
    //https://en.wikipedia.org/wiki/Input_method
    //
    //[issue]:
    //https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-505282355
    //
    //Unfortunately,Microsoftterminaldonothandleproperlyhidingthe
    //cursor.Insteadthecharacterunderthecursorishidden,whichisa
    //bigproblem.Asaresult,wecan'tenablesettingcursortotheright
    //location.Itwillbedisplayedatthebottomrightcorner.
    //See:
    //https://github.com/microsoft/terminal/issues/1203
    //https://github.com/microsoft/terminal/issues/3093
    &&
    node->requirement().focused.cursor_shape!=Screen::Cursor::Shape::Hidden
    #endif
    ){
    screen.SetCursor(Screen::Cursor{
    node->requirement().focused.node->box_.x_max,
    node->requirement().focused.node->box_.y_max,
    node->requirement().focused.cursor_shape,
    });
    }else{
    screen.SetCursor(Screen::Cursor{
    screen.dimx()-1,
    screen.dimy()-1,
    Screen::Cursor::Shape::Hidden,
    });
    }

    //Step4:Drawtheelement.
    screen.stencil=box;
    node->Render(screen);

    //Step5:Applyshaders
    screen.ApplyShader();
    }

    std::stringGetNodeSelectedContent(Screen&screen,
    Node*node,
    Selection&selection){
    Boxbox;
    box.x_min=0;
    box.y_min=0;
    box.x_max=screen.dimx()-1;
    box.y_max=screen.dimy()-1;

    Node::Statusstatus;
    node->Check(&status);
    constintmax_iterations=20;
    while(status.need_iteration&&status.iteration<max_iterations){
    //Step1:Findwhatdimensionthiselementswantstobe.
    node->ComputeRequirement();

    //Step2:Assignadimensiontotheelement.
    node->SetBox(box);

    //Checkiftheelementneedsanotheriterationofthelayoutalgorithm.
    status.need_iteration=false;
    status.iteration++;
    node->Check(&status);
    }

    //Step3:Selection
    node->Select(selection);

    //Step4:gettheselectedcontent.
    returnnode->GetSelectedContent(selection);
    }

    }//namespaceftxui