2019-01-19 05:41:33 +08:00
|
|
|
#include "ftxui/component/radiobox.hpp"
|
2021-05-02 00:13:56 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
2020-03-24 04:26:00 +08:00
|
|
|
#include <functional>
|
2019-01-19 05:41:33 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
Element RadioBox::Render() {
|
|
|
|
std::vector<Element> elements;
|
|
|
|
bool is_focused = Focused();
|
2021-04-19 04:33:41 +08:00
|
|
|
boxes_.resize(entries.size());
|
2019-01-19 05:41:33 +08:00
|
|
|
for (size_t i = 0; i < entries.size(); ++i) {
|
|
|
|
auto style =
|
|
|
|
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
|
2019-01-20 05:06:05 +08:00
|
|
|
auto focus_management =
|
|
|
|
(focused != int(i)) ? nothing : is_focused ? focus : select;
|
|
|
|
|
2019-01-19 05:41:33 +08:00
|
|
|
const std::wstring& symbol = selected == int(i) ? checked : unchecked;
|
2019-01-20 05:06:05 +08:00
|
|
|
elements.push_back(hbox(text(symbol), text(entries[i]) | style) |
|
2021-04-19 04:33:41 +08:00
|
|
|
focus_management | reflect(boxes_[i]));
|
2019-01-19 05:41:33 +08:00
|
|
|
}
|
|
|
|
return vbox(std::move(elements));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RadioBox::OnEvent(Event event) {
|
2021-05-02 00:13:56 +08:00
|
|
|
if (!event.screen()->CaptureMouse())
|
|
|
|
return false;
|
2021-04-19 04:33:41 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2019-01-19 05:41:33 +08:00
|
|
|
if (!Focused())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int new_focused = focused;
|
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
|
|
|
new_focused--;
|
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
|
|
|
new_focused++;
|
2019-11-02 02:52:41 +08:00
|
|
|
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();
|
|
|
|
|
2019-01-19 05:41:33 +08:00
|
|
|
new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused));
|
2019-11-02 02:52:41 +08:00
|
|
|
|
2019-01-19 05:41:33 +08:00
|
|
|
if (focused != new_focused) {
|
|
|
|
focused = new_focused;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:26:00 +08:00
|
|
|
if (event == Event::Character(' ') || event == Event::Return) {
|
2019-01-19 05:41:33 +08:00
|
|
|
selected = focused;
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-19 04:33:41 +08:00
|
|
|
bool RadioBox::OnMouseEvent(Event event) {
|
2021-05-02 00:13:56 +08:00
|
|
|
if (!event.screen()->CaptureMouse())
|
|
|
|
return false;
|
2021-04-19 04:33:41 +08:00
|
|
|
for (int i = 0; i < boxes_.size(); ++i) {
|
2021-04-25 21:22:38 +08:00
|
|
|
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
|
2021-04-19 04:33:41 +08:00
|
|
|
continue;
|
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
focused = i;
|
|
|
|
TakeFocus();
|
2021-04-19 04:33:41 +08:00
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
2021-04-19 04:33:41 +08:00
|
|
|
cursor_position = i;
|
|
|
|
TakeFocus();
|
|
|
|
if (selected != i) {
|
|
|
|
selected = i;
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-19 05:41:33 +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.
|