2019-01-12 22:25:49 +01:00
|
|
|
#include "ftxui/component/checkbox.hpp"
|
2021-05-01 18:13:56 +02:00
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2020-03-22 22:32:44 +01:00
|
|
|
|
2019-01-12 22:25:49 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
Element CheckBox::Render() {
|
2019-01-19 22:06:05 +01: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),
|
2021-04-18 22:33:41 +02:00
|
|
|
text(label) | style | focus_management) |
|
|
|
|
reflect(box_);
|
2019-01-12 22:25:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckBox::OnEvent(Event event) {
|
2021-04-18 22:33:41 +02:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2019-01-12 22:25:49 +01:00
|
|
|
if (event == Event::Character(' ') || event == Event::Return) {
|
|
|
|
state = !state;
|
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-22 22:32:44 +01:00
|
|
|
return false;
|
2019-01-12 22:25:49 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 22:33:41 +02:00
|
|
|
bool CheckBox::OnMouseEvent(Event event) {
|
2021-05-01 18:13:56 +02:00
|
|
|
if (!event.screen()->CaptureMouse())
|
|
|
|
return false;
|
2021-04-25 15:22:38 +02:00
|
|
|
if (!box_.Contain(event.mouse().x, event.mouse().y))
|
2021-04-18 22:33:41 +02:00
|
|
|
return false;
|
|
|
|
|
2021-04-25 15:22:38 +02:00
|
|
|
TakeFocus();
|
2021-04-18 22:33:41 +02:00
|
|
|
|
2021-04-25 15:22:38 +02:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
2021-04-18 22:33:41 +02:00
|
|
|
state = !state;
|
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:25:49 +01:00
|
|
|
} // namespace ftxui
|
2020-08-16 00:24:18 +02: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.
|