Add mouse implementation of most components.

This commit is contained in:
ArthurSonzogni
2021-04-18 22:33:41 +02:00
parent d685a8655e
commit 890a41a64c
20 changed files with 239 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ namespace ftxui {
Element RadioBox::Render() {
std::vector<Element> elements;
bool is_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto style =
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
@@ -16,12 +17,15 @@ Element RadioBox::Render() {
const std::wstring& symbol = selected == int(i) ? checked : unchecked;
elements.push_back(hbox(text(symbol), text(entries[i]) | style) |
focus_management);
focus_management | reflect(boxes_[i]));
}
return vbox(std::move(elements));
}
bool RadioBox::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused())
return false;
@@ -50,6 +54,30 @@ bool RadioBox::OnEvent(Event event) {
return false;
}
bool RadioBox::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_move()) {
focused = i;
TakeFocus();
return true;
}
if (event.is_mouse_left_down()) {
cursor_position = i;
TakeFocus();
if (selected != i) {
selected = i;
on_change();
}
return true;
}
}
return false;
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.