2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/toggle.hpp"
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
Element Toggle::Render() {
|
2019-01-20 05:06:05 +08:00
|
|
|
bool is_focused = Focused();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-04-19 04:33:41 +08:00
|
|
|
boxes_.resize(entries.size());
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
Elements children;
|
2019-11-02 02:52:41 +08:00
|
|
|
for (size_t i = 0; i < entries.size(); ++i) {
|
2019-01-03 07:35:59 +08:00
|
|
|
// Separator.
|
|
|
|
if (i != 0)
|
2019-01-05 09:03:49 +08:00
|
|
|
children.push_back(separator());
|
2019-01-03 07:35:59 +08:00
|
|
|
|
|
|
|
// Entry.
|
2021-04-19 04:33:41 +08:00
|
|
|
auto style = (selected != int(i)) ? normal_style
|
|
|
|
: is_focused ? focused_style
|
|
|
|
: selected_style;
|
2019-01-20 05:06:05 +08:00
|
|
|
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select;
|
2021-04-19 04:33:41 +08:00
|
|
|
children.push_back(text(entries[i]) | style | focused | reflect(boxes_[i]));
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return hbox(std::move(children));
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Toggle::OnEvent(Event event) {
|
2021-04-19 04:33:41 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2019-11-02 02:52:41 +08:00
|
|
|
int old_selected = selected;
|
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h'))
|
2019-01-13 05:25:49 +08:00
|
|
|
selected--;
|
2019-11-02 02:52:41 +08:00
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l'))
|
2019-01-13 05:25:49 +08:00
|
|
|
selected++;
|
2019-11-02 02:52:41 +08:00
|
|
|
if (event == Event::Tab && entries.size())
|
|
|
|
selected = (selected + 1) % entries.size();
|
|
|
|
if (event == Event::TabReverse && entries.size())
|
|
|
|
selected = (selected + entries.size() - 1) % entries.size();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-11-02 02:52:41 +08:00
|
|
|
selected = std::max(0, std::min(int(entries.size()) - 1, selected));
|
2020-08-03 04:11:05 +08:00
|
|
|
|
2020-08-05 00:54:30 +08:00
|
|
|
if (old_selected != selected) {
|
2020-08-03 04:11:05 +08:00
|
|
|
on_change();
|
2020-08-05 00:54:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::Return) {
|
|
|
|
on_enter();
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-03 04:11:05 +08:00
|
|
|
|
2020-08-05 00:54:30 +08:00
|
|
|
return false;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 04:33:41 +08:00
|
|
|
bool Toggle::OnMouseEvent(Event event) {
|
|
|
|
for (int i = 0; i < boxes_.size(); ++i) {
|
|
|
|
if (!boxes_[i].Contain(event.mouse_x(), event.mouse_y()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (event.is_mouse_left_down()) {
|
|
|
|
if (selected != i) {
|
|
|
|
selected = i;
|
|
|
|
TakeFocus();
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|