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 Return;
static Event Escape;
static Event Tab;
static Event TabReverse;
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
// --- Custom ---

View File

@ -44,45 +44,33 @@ Component* Container::ActiveChild() {
}
bool Container::VerticalEvent(Event event) {
selected_ %= children_.size();
// Left pressed ?
if (event == Event::ArrowUp || event == Event::Character('k')) {
if (selected_ != 0) {
int old_selected = selected_;
if (event == Event::ArrowUp || event == Event::Character('k'))
selected_--;
return true;
}
}
// Left pressed ?
if (event == Event::ArrowDown || event == Event::Character('j')) {
if (selected_ != int(children_.size()) - 1) {
if (event == Event::ArrowDown || event == Event::Character('j'))
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) {
selected_ %= children_.size();
// Left pressed ?
if (event == Event::ArrowLeft || event == Event::Character('h')) {
if (selected_ != 0) {
int old_selected = selected_;
if (event == Event::ArrowLeft || event == Event::Character('h'))
selected_--;
return true;
}
}
// Left pressed ?
if (event == Event::ArrowRight || event == Event::Character('l')) {
if (selected_ != int(children_.size()) - 1) {
if (event == Event::ArrowRight || event == Event::Character('l'))
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() {

View File

@ -137,6 +137,8 @@ Event Event::Backspace = Event::Special({127});
Event Event::Delete = Event::Special("\e[3~");
Event Event::Escape = Event::Special("\e");
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::F2 = Event::Special("\e[OQ");
Event Event::F3 = Event::Special("\e[OR");

View File

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

View File

@ -28,7 +28,13 @@ bool RadioBox::OnEvent(Event event) {
new_focused--;
if (event == Event::ArrowDown || event == Event::Character('j'))
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));
if (focused != new_focused) {
focused = new_focused;
return true;

View File

@ -22,21 +22,18 @@ Element Toggle::Render() {
}
bool Toggle::OnEvent(Event event) {
if (selected > 0 &&
(event == Event::ArrowLeft || event == Event::Character('h'))) {
int old_selected = selected;
if (event == Event::ArrowLeft || event == Event::Character('h'))
selected--;
on_change();
return true;
}
if (selected < int(entries.size()) - 1 &&
(event == Event::ArrowRight || event == Event::Character('l'))) {
if (event == Event::ArrowRight || event == Event::Character('l'))
selected++;
on_change();
return true;
}
if (event == Event::Tab && entries.size())
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