radiobox.cpp
functional
utility
vector
ftxui/component/component.hpp
ftxui/component/component_base.hpp
ftxui/component/component_options.hpp
ftxui/component/event.hpp
ftxui/component/mouse.hpp
ftxui/component/screen_interactive.hpp
ftxui/dom/elements.hpp
ftxui/screen/box.hpp
ftxui/screen/util.hpp
ftxui/util/ref.hpp
ftxui/component/animation.hpp
ftxui/component/captured_mouse.hpp
ftxui/component/component.hpp
ftxui/component/component_base.hpp
ftxui/component/component_options.hpp
ftxui/component/event.hpp
ftxui/component/mouse.hpp
ftxui/component/receiver.hpp
ftxui/component/screen_interactive.hpp
ftxui/component/task.hpp
ftxui/dom/canvas.hpp
ftxui/dom/deprecated.hpp
ftxui/dom/direction.hpp
ftxui/dom/elements.hpp
ftxui/dom/flexbox_config.hpp
ftxui/dom/linear_gradient.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/string.hpp
ftxui/screen/terminal.hpp
ftxui/util/autoreset.hpp
ftxui/util/ref.hpp
src/ftxui/component/radiobox.cpp
ftxui/screen/util.hpp
algorithm
atomic
chrono
condition_variable
cstddef
cstdint
ftxui/dom/take_any_args.hpp
functional
memory
mutex
optional
queue
sstream
string
thread
unordered_map
utility
variant
vector
ftxui

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<functional>//forfunction
    #include<utility>//formove
    #include<vector>//forvector

    #include"ftxui/component/component.hpp"//forMake,Radiobox
    #include"ftxui/component/component_base.hpp"//forComponentBase
    #include"ftxui/component/component_options.hpp"//forRadioboxOption,EntryState
    #include"ftxui/component/event.hpp"//forEvent,Event::ArrowDown,Event::ArrowUp,Event::End,Event::Home,Event::PageDown,Event::PageUp,Event::Return,Event::Tab,Event::TabReverse
    #include"ftxui/component/mouse.hpp"//forMouse,Mouse::WheelDown,Mouse::WheelUp,Mouse::Left,Mouse::Released
    #include"ftxui/component/screen_interactive.hpp"//forComponent
    #include"ftxui/dom/elements.hpp"//foroperator|,reflect,Element,vbox,Elements,focus,nothing,select
    #include"ftxui/screen/box.hpp"//forBox
    #include"ftxui/screen/util.hpp"//forclamp
    #include"ftxui/util/ref.hpp"//forRef,ConstStringListRef

    namespaceftxui{

    namespace{
    ///@briefAlistofselectableelement.Oneandonlyonecanbeselectedat
    ///thesametime.
    ///@ingroupcomponent
    classRadioboxBase:publicComponentBase,publicRadioboxOption{
    public:
    explicitRadioboxBase(constRadioboxOption&option)
    :RadioboxOption(option){}

    private:
    ElementOnRender()override{
    Clamp();
    Elementselements;
    constboolis_menu_focused=Focused();
    elements.reserve(size());
    for(inti=0;i<size();++i){
    constboolis_focused=(focused_entry()==i)&&is_menu_focused;
    constboolis_selected=(hovered_==i);
    autostate=EntryState{
    entries[i],selected()==i,is_selected,is_focused,i,
    };
    autoelement=
    (transform?transform:RadioboxOption::Simple().transform)(state);
    if(is_selected){
    element|=focus;
    }
    elements.push_back(element|reflect(boxes_[i]));
    }
    returnvbox(std::move(elements),hovered_)|reflect(box_);
    }

    //NOLINTNEXTLINE(readability-function-cognitive-complexity)
    boolOnEvent(Eventevent)override{
    Clamp();
    if(!CaptureMouse(event)){
    returnfalse;
    }

    if(event.is_mouse()){
    returnOnMouseEvent(event);
    }

    if(Focused()){
    constintold_hovered=hovered_;
    if(event==Event::ArrowUp||event==Event::Character('k')){
    (hovered_)--;
    }
    if(event==Event::ArrowDown||event==Event::Character('j')){
    (hovered_)++;
    }
    if(event==Event::PageUp){
    (hovered_)-=box_.y_max-box_.y_min;
    }
    if(event==Event::PageDown){
    (hovered_)+=box_.y_max-box_.y_min;
    }
    if(event==Event::Home){
    (hovered_)=0;
    }
    if(event==Event::End){
    (hovered_)=size()-1;
    }
    if(event==Event::Tab&&size()){
    hovered_=(hovered_+1)%size();
    }
    if(event==Event::TabReverse&&size()){
    hovered_=(hovered_+size()-1)%size();
    }

    hovered_=util::clamp(hovered_,0,size()-1);

    if(hovered_!=old_hovered){
    focused_entry()=hovered_;
    on_change();
    returntrue;
    }
    }

    if(event==Event::Character('')||event==Event::Return){
    selected()=hovered_;
    on_change();
    returntrue;
    }

    returnfalse;
    }

    boolOnMouseEvent(Eventevent){
    if(event.mouse().button==Mouse::WheelDown||
    event.mouse().button==Mouse::WheelUp){
    returnOnMouseWheel(event);
    }

    for(inti=0;i<size();++i){
    if(!boxes_[i].Contain(event.mouse().x,event.mouse().y)){
    continue;
    }

    TakeFocus();
    focused_entry()=i;
    if(event.mouse().button==Mouse::Left&&
    event.mouse().motion==Mouse::Pressed){
    if(selected()!=i){
    selected()=i;
    on_change();
    }

    returntrue;
    }
    }
    returnfalse;
    }

    boolOnMouseWheel(Eventevent){
    if(!box_.Contain(event.mouse().x,event.mouse().y)){
    returnfalse;
    }

    constintold_hovered=hovered_;

    if(event.mouse().button==Mouse::WheelUp){
    (hovered_)--;
    }
    if(event.mouse().button==Mouse::WheelDown){
    (hovered_)++;
    }

    hovered_=util::clamp(hovered_,0,size()-1);

    if(hovered_!=old_hovered){
    on_change();
    }

    returntrue;
    }

    voidClamp(){
    boxes_.resize(size());
    selected()=util::clamp(selected(),0,size()-1);
    focused_entry()=util::clamp(focused_entry(),0,size()-1);
    hovered_=util::clamp(hovered_,0,size()-1);
    }

    boolFocusable()constfinal{returnentries.size();}
    intsize()const{returnint(entries.size());}

    inthovered_=selected();
    std::vector<Box>boxes_;
    Boxbox_;
    };

    }//namespace

    ///@briefAlistofelement,whereonlyonecanbeselected.
    ///@paramoptionTheparameters
    ///@ingroupcomponent
    ///@seeRadioboxBase
    ///
    ///###Example
    ///
    ///```cpp
    ///autoscreen=ScreenInteractive::TerminalOutput();
    ///std::vector<std::string>entries={
    ///"entry1",
    ///"entry2",
    ///"entry3",
    ///};
    ///intselected=0;
    ///automenu=Radiobox({
    ///.entries=entries,
    ///.selected=&selected,
    ///});
    ///screen.Loop(menu);
    ///```
    ///
    ///###Output
    ///
    ///```bash
    ///◉entry1
    ///○entry2
    ///○entry3
    ///```
    ///NOLINTNEXTLINE
    ComponentRadiobox(RadioboxOptionoption){
    returnMake<RadioboxBase>(std::move(option));
    }

    ///@briefAlistofelement,whereonlyonecanbeselected.
    ///@paramentriesThelistofentriesinthelist.
    ///@paramselectedTheindexofthecurrentlyselectedelement.
    ///@paramoptionAdditionaloptionalparameters.
    ///@ingroupcomponent
    ///@seeRadioboxBase
    ///
    ///###Example
    ///
    ///```cpp
    ///autoscreen=ScreenInteractive::TerminalOutput();
    ///std::vector<std::string>entries={
    ///"entry1",
    ///"entry2",
    ///"entry3",
    ///};
    ///intselected=0;
    ///automenu=Radiobox(&entries,&selected);
    ///screen.Loop(menu);
    ///```
    ///
    ///###Output
    ///
    ///```bash
    ///◉entry1
    ///○entry2
    ///○entry3
    ///```
    ComponentRadiobox(ConstStringListRefentries,
    int*selected,
    RadioboxOptionoption){
    option.entries=std::move(entries);
    option.selected=selected;
    returnMake<RadioboxBase>(std::move(option));
    }

    }//namespaceftxui