Merge pull request #8 from bresilla/master

using TAB key event
This commit is contained in:
ArthurSonzogni 2019-11-01 20:03:35 +01:00 committed by GitHub
commit 283f0fed08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 55 deletions

View File

@ -33,6 +33,8 @@ struct Event {
static Event Delete; static Event Delete;
static Event Return; static Event Return;
static Event Escape; static Event Escape;
static Event Tab;
static Event TabReverse;
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12; static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
// --- Custom --- // --- Custom ---

View File

@ -44,45 +44,33 @@ Component* Container::ActiveChild() {
} }
bool Container::VerticalEvent(Event event) { bool Container::VerticalEvent(Event event) {
selected_ %= children_.size(); int old_selected = selected_;
// Left pressed ? if (event == Event::ArrowUp || event == Event::Character('k'))
if (event == Event::ArrowUp || event == Event::Character('k')) {
if (selected_ != 0) {
selected_--; selected_--;
return true; if (event == Event::ArrowDown || event == Event::Character('j'))
}
}
// Left pressed ?
if (event == Event::ArrowDown || event == Event::Character('j')) {
if (selected_ != int(children_.size()) - 1) {
selected_++; selected_++;
return true; if (event == Event::Tab && children_.size())
} selected_ = (selected_ + 1) % children_.size();
} if (event == Event::TabReverse && children_.size())
selected_ = (selected_ + children_.size() - 1) % children_.size();
return false; selected_ = std::max(0, std::min(int(children_.size()) - 1, selected_));
return old_selected != selected_;
} }
bool Container::HorizontalEvent(Event event) { bool Container::HorizontalEvent(Event event) {
selected_ %= children_.size(); int old_selected = selected_;
// Left pressed ? if (event == Event::ArrowLeft || event == Event::Character('h'))
if (event == Event::ArrowLeft || event == Event::Character('h')) {
if (selected_ != 0) {
selected_--; selected_--;
return true; if (event == Event::ArrowRight || event == Event::Character('l'))
}
}
// Left pressed ?
if (event == Event::ArrowRight || event == Event::Character('l')) {
if (selected_ != int(children_.size()) - 1) {
selected_++; selected_++;
return true; if (event == Event::Tab && children_.size())
} selected_ = (selected_ + 1) % children_.size();
} if (event == Event::TabReverse && children_.size())
selected_ = (selected_ + children_.size() - 1) % children_.size();
return false; selected_ = std::max(0, std::min(int(children_.size()) - 1, selected_));
return old_selected != selected_;
} }
Element Container::Render() { Element Container::Render() {
@ -91,7 +79,7 @@ Element Container::Render() {
Element Container::VerticalRender() { Element Container::VerticalRender() {
Elements elements; Elements elements;
for(auto& it : children_) for (auto& it : children_)
elements.push_back(it->Render()); elements.push_back(it->Render());
if (elements.size() == 0) if (elements.size() == 0)
return text(L"Empty container"); return text(L"Empty container");
@ -100,7 +88,7 @@ Element Container::VerticalRender() {
Element Container::HorizontalRender() { Element Container::HorizontalRender() {
Elements elements; Elements elements;
for(auto& it : children_) for (auto& it : children_)
elements.push_back(it->Render()); elements.push_back(it->Render());
if (elements.size() == 0) if (elements.size() == 0)
return text(L"Empty container"); return text(L"Empty container");

View File

@ -137,6 +137,8 @@ Event Event::Backspace = Event::Special({127});
Event Event::Delete = Event::Special("\e[3~"); Event Event::Delete = Event::Special("\e[3~");
Event Event::Escape = Event::Special("\e"); Event Event::Escape = Event::Special("\e");
Event Event::Return = Event::Special({10}); Event Event::Return = Event::Special({10});
Event Event::Tab = Event::Special({9});
Event Event::TabReverse = Event::Special({27, 91, 90});
Event Event::F1 = Event::Special("\e[OP"); Event Event::F1 = Event::Special("\e[OP");
Event Event::F2 = Event::Special("\e[OQ"); Event Event::F2 = Event::Special("\e[OQ");
Event Event::F3 = Event::Special("\e[OR"); Event Event::F3 = Event::Special("\e[OR");

View File

@ -22,15 +22,19 @@ bool Menu::OnEvent(Event event) {
if (!Focused()) if (!Focused())
return false; return false;
int new_selected = selected; int old_selected = selected;
if (event == Event::ArrowUp || event == Event::Character('k')) if (event == Event::ArrowUp || event == Event::Character('k'))
new_selected--; selected--;
if (event == Event::ArrowDown || event == Event::Character('j')) if (event == Event::ArrowDown || event == Event::Character('j'))
new_selected++; selected++;
new_selected = std::max(0, std::min(int(entries.size()) - 1, new_selected)); if (event == Event::Tab && entries.size())
selected = (selected + 1) % entries.size();
if (event == Event::TabReverse && entries.size())
selected = (selected + entries.size() - 1) % entries.size();
if (selected != new_selected) { selected = std::max(0, std::min(int(entries.size()) - 1, selected));
selected = new_selected;
if (selected != old_selected) {
on_change(); on_change();
return true; return true;
} }

View File

@ -28,7 +28,13 @@ bool RadioBox::OnEvent(Event event) {
new_focused--; new_focused--;
if (event == Event::ArrowDown || event == Event::Character('j')) if (event == Event::ArrowDown || event == Event::Character('j'))
new_focused++; new_focused++;
if (event == Event::Tab && entries.size())
new_focused = (new_focused + 1) % entries.size();
if (event == Event::TabReverse && entries.size())
new_focused = (new_focused + entries.size() - 1) % entries.size();
new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused)); new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused));
if (focused != new_focused) { if (focused != new_focused) {
focused = new_focused; focused = new_focused;
return true; return true;

View File

@ -6,7 +6,7 @@ Element Toggle::Render() {
bool is_focused = Focused(); bool is_focused = Focused();
Elements children; Elements children;
for(size_t i = 0; i<entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
// Separator. // Separator.
if (i != 0) if (i != 0)
children.push_back(separator()); children.push_back(separator());
@ -22,21 +22,18 @@ Element Toggle::Render() {
} }
bool Toggle::OnEvent(Event event) { bool Toggle::OnEvent(Event event) {
if (selected > 0 && int old_selected = selected;
(event == Event::ArrowLeft || event == Event::Character('h'))) { if (event == Event::ArrowLeft || event == Event::Character('h'))
selected--; selected--;
on_change(); if (event == Event::ArrowRight || event == Event::Character('l'))
return true;
}
if (selected < int(entries.size()) - 1 &&
(event == Event::ArrowRight || event == Event::Character('l'))) {
selected++; selected++;
on_change(); if (event == Event::Tab && entries.size())
return true; selected = (selected + 1) % entries.size();
} if (event == Event::TabReverse && entries.size())
selected = (selected + entries.size() - 1) % entries.size();
return false; selected = std::max(0, std::min(int(entries.size()) - 1, selected));
return old_selected != selected;
} }
} // namespace ftxui } // namespace ftxui