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

@ -6,7 +6,7 @@ Element Toggle::Render() {
bool is_focused = Focused();
Elements children;
for(size_t i = 0; i<entries.size(); ++i) {
for (size_t i = 0; i < entries.size(); ++i) {
// Separator.
if (i != 0)
children.push_back(separator());
@ -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