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