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