Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage inactive container selection #970

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/ftxui/component/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ContainerBase : public ComponentBase {
public:
ContainerBase(Components children, int* selector)
: selector_(selector ? selector : &selected_) {
selectorState_ = selector_;
for (Component& child : children) {
Add(std::move(child));
}
Expand All @@ -34,6 +35,8 @@ class ContainerBase : public ComponentBase {
return false;
}

ManageSelector();

if (ActiveChild() && ActiveChild()->OnEvent(event)) {
return true;
}
Expand All @@ -42,14 +45,15 @@ class ContainerBase : public ComponentBase {
}

Component ActiveChild() override {
if (children_.empty()) {
if (children_.empty() || !selector_) {
return nullptr;
}

return children_[static_cast<size_t>(*selector_) % children_.size()];
}

void SetActiveChild(ComponentBase* child) override {
selector_ = selectorState_; // in case of a mouse event
for (size_t i = 0; i < children_.size(); ++i) {
if (children_[i].get() == child) {
*selector_ = static_cast<int>(i);
Expand All @@ -67,6 +71,7 @@ class ContainerBase : public ComponentBase {
}

int selected_ = 0;
int* selectorState_ = nullptr;
int* selector_ = nullptr;

void MoveSelector(int dir) {
Expand All @@ -92,13 +97,25 @@ class ContainerBase : public ComponentBase {
}
}
}

void ManageSelector() {
if (selector_) {
selectorState_ = selector_;
}
if (!Active()) {
selector_ = nullptr;
return;
}
selector_ = selectorState_;
}
};

class VerticalContainer : public ContainerBase {
public:
using ContainerBase::ContainerBase;

Element Render() override {
ManageSelector();
Elements elements;
elements.reserve(children_.size());
for (auto& it : children_) {
Expand Down Expand Up @@ -182,6 +199,7 @@ class HorizontalContainer : public ContainerBase {
using ContainerBase::ContainerBase;

Element Render() override {
ManageSelector();
Elements elements;
elements.reserve(children_.size());
for (auto& it : children_) {
Expand Down Expand Up @@ -333,7 +351,7 @@ Component Vertical(Components children) {
/// children_2,
/// children_3,
/// children_4,
/// });
/// }, &selected_children);
/// ```
Component Vertical(Components children, int* selector) {
return std::make_shared<VerticalContainer>(std::move(children), selector);
Expand All @@ -354,7 +372,7 @@ Component Vertical(Components children, int* selector) {
/// children_2,
/// children_3,
/// children_4,
/// }, &selected_children);
/// });
/// ```
Component Horizontal(Components children) {
return Horizontal(std::move(children), nullptr);
Expand Down