2021-05-02 02:40:35 +08:00
|
|
|
#include <functional> // for function
|
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
|
2022-08-31 00:52:33 +08:00
|
|
|
#include "ftxui/component/component.hpp" // for Make, Checkbox
|
|
|
|
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
|
|
|
|
#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/event.hpp" // for Event, Event::Return
|
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
2022-08-31 00:52:33 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, focus, nothing, select
|
2021-07-10 19:20:43 +08:00
|
|
|
#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 {
|
2023-06-25 23:22:05 +08:00
|
|
|
class CheckboxBase : public ComponentBase, public CheckboxOption {
|
2021-07-10 18:02:42 +08:00
|
|
|
public:
|
2023-06-25 23:22:05 +08:00
|
|
|
explicit CheckboxBase(CheckboxOption option)
|
|
|
|
: CheckboxOption(std::move(option)) {}
|
2021-07-10 18:02:42 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Component implementation.
|
|
|
|
Element Render() override {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_focused = Focused();
|
|
|
|
const bool is_active = Active();
|
2021-09-08 15:36:37 +08:00
|
|
|
auto focus_management = is_focused ? focus : is_active ? select : nothing;
|
2023-06-25 23:22:05 +08:00
|
|
|
auto entry_state = EntryState{
|
|
|
|
*label,
|
|
|
|
*checked,
|
2022-03-14 01:51:46 +08:00
|
|
|
is_active,
|
|
|
|
is_focused || hovered_,
|
|
|
|
};
|
2023-06-25 23:22:05 +08:00
|
|
|
auto element = (transform ? transform : CheckboxOption::Simple().transform)(
|
|
|
|
entry_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) {
|
2023-06-25 23:22:05 +08:00
|
|
|
*checked = !*checked;
|
|
|
|
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) {
|
2023-06-25 23:22:05 +08:00
|
|
|
*checked = !*checked;
|
|
|
|
on_change();
|
2021-07-10 18:02:42 +08:00
|
|
|
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-09-08 15:36:37 +08:00
|
|
|
bool hovered_ = false;
|
|
|
|
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
|
|
|
|
/// ```
|
2023-06-25 23:22:05 +08:00
|
|
|
// NOLINTNEXTLINE
|
|
|
|
Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
|
|
|
|
option.label = label;
|
|
|
|
option.checked = checked;
|
|
|
|
return Make<CheckboxBase>(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.
|