2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/toggle.hpp"
|
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
namespace component {
|
|
|
|
|
|
|
|
Toggle::Toggle(Delegate* delegate) : Component(delegate) {}
|
|
|
|
|
|
|
|
dom::Element Toggle::Render() {
|
|
|
|
using namespace dom;
|
|
|
|
auto highlight = Focused() ? inverted : bold;
|
|
|
|
|
|
|
|
Children children;
|
|
|
|
children.push_back(text(L"["));
|
|
|
|
if (activated) {
|
|
|
|
children.push_back(highlight(text(on)));
|
|
|
|
children.push_back(text(L"|"));
|
|
|
|
children.push_back(dim(text(off)));
|
|
|
|
} else {
|
|
|
|
children.push_back(dim(text(on)));
|
|
|
|
children.push_back(text(L"|"));
|
|
|
|
children.push_back(highlight(text(off)));
|
|
|
|
}
|
|
|
|
children.push_back(text(L"]"));
|
|
|
|
return hbox(std::move(children));
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Toggle::OnEvent(Event event) {
|
2018-10-10 01:06:03 +08:00
|
|
|
if (activated) {
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l')) {
|
2018-10-10 01:06:03 +08:00
|
|
|
activated = false;
|
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h')) {
|
2018-10-10 01:06:03 +08:00
|
|
|
activated = true;
|
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace component
|
|
|
|
} // namespace ftxui
|