FTXUI/src/ftxui/component/checkbox.cpp

52 lines
1.2 KiB
C++
Raw Normal View History

2019-01-13 05:25:49 +08:00
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/screen_interactive.hpp"
2020-03-23 05:32:44 +08:00
2019-01-13 05:25:49 +08:00
#include <functional>
namespace ftxui {
Element CheckBox::Render() {
2019-01-20 05:06:05 +08:00
bool is_focused = Focused();
auto style = is_focused ? focused_style : unfocused_style;
auto focus_management = is_focused ? focus : state ? select : nothing;
return hbox(text(state ? checked : unchecked),
text(label) | style | focus_management) |
reflect(box_);
2019-01-13 05:25:49 +08:00
}
bool CheckBox::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
2019-01-13 05:25:49 +08:00
if (event == Event::Character(' ') || event == Event::Return) {
state = !state;
on_change();
return true;
}
2020-03-23 05:32:44 +08:00
return false;
2019-01-13 05:25:49 +08:00
}
bool CheckBox::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
2021-04-25 21:22:38 +08:00
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;
2021-04-25 21:22:38 +08:00
TakeFocus();
2021-04-25 21:22:38 +08:00
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
state = !state;
on_change();
return true;
}
return false;
}
2019-01-13 05:25:49 +08:00
} // 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.