2021-05-02 02:40:35 +08:00
|
|
|
#include <functional> // for function
|
|
|
|
#include <memory> // for shared_ptr
|
2021-07-10 19:20:43 +08:00
|
|
|
#include <utility> // for move
|
2020-03-23 05:32:44 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/component.hpp" // for Make, Component, Checkbox
|
2021-07-10 18:02:42 +08:00
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/component_options.hpp" // for CheckboxOption
|
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::Return
|
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, text, Element, hbox, reflect, focus, nothing, select
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
|
2021-07-10 18:02:42 +08:00
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
namespace ftxui {
|
|
|
|
|
2021-07-10 18:02:42 +08:00
|
|
|
namespace {
|
|
|
|
class CheckboxBase : public ComponentBase {
|
|
|
|
public:
|
2021-07-10 19:20:43 +08:00
|
|
|
CheckboxBase(ConstStringRef label, bool* state, Ref<CheckboxOption> option)
|
2022-03-31 08:17:43 +08:00
|
|
|
: label_(std::move(label)), state_(state), option_(std::move(option)) {}
|
2021-07-10 18:02:42 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Component implementation.
|
|
|
|
Element Render() override {
|
|
|
|
bool is_focused = Focused();
|
2021-09-08 15:36:37 +08:00
|
|
|
bool is_active = Active();
|
|
|
|
auto focus_management = is_focused ? focus : is_active ? select : nothing;
|
2022-03-14 01:51:46 +08:00
|
|
|
auto state = EntryState{
|
|
|
|
*label_,
|
|
|
|
*state_,
|
|
|
|
is_active,
|
|
|
|
is_focused || hovered_,
|
|
|
|
};
|
2022-03-31 08:17:43 +08:00
|
|
|
auto element =
|
|
|
|
(option_->transform ? option_->transform
|
|
|
|
: CheckboxOption::Simple().transform)(state);
|
2022-03-14 01:51:46 +08:00
|
|
|
return element | focus_management | reflect(box_);
|
2021-07-10 18:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OnEvent(Event event) override {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!CaptureMouse(event)) {
|
2021-09-16 06:47:31 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-16 06:47:31 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.is_mouse()) {
|
2021-07-10 18:02:42 +08:00
|
|
|
return OnMouseEvent(event);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 18:02:42 +08:00
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
hovered_ = false;
|
2021-07-10 18:02:42 +08:00
|
|
|
if (event == Event::Character(' ') || event == Event::Return) {
|
|
|
|
*state_ = !*state_;
|
|
|
|
option_->on_change();
|
2021-09-16 06:47:31 +08:00
|
|
|
TakeFocus();
|
2021-07-10 18:02:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnMouseEvent(Event event) {
|
2021-09-08 15:36:37 +08:00
|
|
|
hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!CaptureMouse(event)) {
|
2021-07-10 18:02:42 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 18:02:42 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!hovered_) {
|
2021-09-08 15:36:37 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 18:02:42 +08:00
|
|
|
|
|
|
|
if (event.mouse().button == Mouse::Left &&
|
|
|
|
event.mouse().motion == Mouse::Pressed) {
|
|
|
|
*state_ = !*state_;
|
|
|
|
option_->on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-07 02:32:33 +08:00
|
|
|
bool Focusable() const final { return true; }
|
2021-08-06 04:40:40 +08:00
|
|
|
|
2021-07-10 18:02:42 +08:00
|
|
|
ConstStringRef label_;
|
|
|
|
bool* const state_;
|
2021-09-08 15:36:37 +08:00
|
|
|
bool hovered_ = false;
|
2021-07-10 18:29:39 +08:00
|
|
|
Ref<CheckboxOption> option_;
|
2021-09-08 15:36:37 +08:00
|
|
|
Box box_;
|
2021-07-10 18:02:42 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @brief Draw checkable element.
|
|
|
|
/// @param label The label of the checkbox.
|
|
|
|
/// @param checked Whether the checkbox is checked or not.
|
2021-07-10 20:23:46 +08:00
|
|
|
/// @param option Additional optional parameters.
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @ingroup component
|
|
|
|
/// @see CheckboxBase
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::FitComponent();
|
2021-08-09 05:25:20 +08:00
|
|
|
/// std::string label = "Make a sandwidth";
|
2021-05-10 02:32:27 +08:00
|
|
|
/// bool checked = false;
|
|
|
|
/// Component checkbox = Checkbox(&label, &checked);
|
|
|
|
/// screen.Loop(checkbox)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// ☐ Make a sandwitch
|
|
|
|
/// ```
|
2021-07-08 04:37:50 +08:00
|
|
|
Component Checkbox(ConstStringRef label,
|
|
|
|
bool* checked,
|
2021-07-10 18:29:39 +08:00
|
|
|
Ref<CheckboxOption> option) {
|
2022-03-31 08:17:43 +08:00
|
|
|
return Make<CheckboxBase>(std::move(label), checked, std::move(option));
|
2021-05-10 02:32:27 +08:00
|
|
|
}
|
|
|
|
|
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.
|