FTXUI/src/ftxui/component/toggle.cpp

106 lines
3.2 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <stddef.h> // for size_t
#include <algorithm> // for max, min
2021-05-10 02:32:27 +08:00
#include <memory> // for shared_ptr, allocator_traits<>::value_type
2021-05-02 02:40:35 +08:00
#include <utility> // for move
2021-05-10 02:32:27 +08:00
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Return, Event::Tab, Event::TabReverse
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
2021-05-02 02:40:35 +08:00
#include "ftxui/component/toggle.hpp"
namespace ftxui {
2021-05-10 02:32:27 +08:00
Component Toggle(const std::vector<std::wstring>* entries, int* selected) {
return Make<ToggleBase>(entries, selected);
}
// static
ToggleBase* ToggleBase::From(Component component) {
return static_cast<ToggleBase*>(component.get());
}
ToggleBase::ToggleBase(const std::vector<std::wstring>* entries, int* selected)
: entries_(entries), selected_(selected) {}
Element ToggleBase::Render() {
2019-01-13 01:24:46 +08:00
Elements children;
bool is_toggle_focused = Focused();
2021-05-10 02:32:27 +08:00
boxes_.resize(entries_->size());
for (size_t i = 0; i < entries_->size(); ++i) {
2019-01-03 07:35:59 +08:00
// Separator.
if (i != 0)
children.push_back(separator());
2019-01-03 07:35:59 +08:00
bool is_focused = (focused == int(i)) && is_toggle_focused;
2021-05-10 02:32:27 +08:00
bool is_selected = (*selected_ == int(i));
auto style = is_selected
? (is_focused ? selected_focused_style : selected_style)
: (is_focused ? focused_style : normal_style);
auto focus_management = !is_selected ? nothing
: is_toggle_focused ? focus
: select;
2021-05-10 02:32:27 +08:00
children.push_back(text(entries_->at(i)) | style | focus_management |
reflect(boxes_[i]));
}
return hbox(std::move(children));
}
2021-05-10 02:32:27 +08:00
bool ToggleBase::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
2021-05-10 02:32:27 +08:00
int old_selected = *selected_;
if (event == Event::ArrowLeft || event == Event::Character('h'))
2021-05-10 02:32:27 +08:00
(*selected_)--;
if (event == Event::ArrowRight || event == Event::Character('l'))
2021-05-10 02:32:27 +08:00
(*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();
2021-05-10 02:32:27 +08:00
*selected_ = std::max(0, std::min(int(entries_->size()) - 1, *selected_));
2021-05-10 02:32:27 +08:00
if (old_selected != *selected_) {
focused = *selected_;
on_change();
return true;
}
if (event == Event::Return) {
on_enter();
return true;
}
return false;
}
2021-05-10 02:32:27 +08:00
bool ToggleBase::OnMouseEvent(Event event) {
2021-05-02 02:40:35 +08:00
if (!CaptureMouse(event))
return false;
2021-05-16 15:38:24 +08:00
for (int i = 0; i < int(boxes_.size()); ++i) {
2021-04-25 21:22:38 +08:00
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
TakeFocus();
focused = i;
2021-04-25 21:22:38 +08:00
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
TakeFocus();
2021-05-10 02:32:27 +08:00
if (*selected_ != i) {
*selected_ = i;
on_change();
}
return true;
}
}
return false;
}
} // namespace ftxui
// 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.