FTXUI/src/ftxui/component/button.cpp

102 lines
3.0 KiB
C++
Raw Normal View History

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-08-26 22:26:09 +08:00
2021-07-10 19:20:43 +08:00
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/component.hpp" // for Make, Button
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for ButtonOption
#include "ftxui/component/event.hpp" // for Event, Event::Return
2021-05-02 02:40:35 +08:00
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
2021-07-10 19:20:43 +08:00
#include "ftxui/component/screen_interactive.hpp" // for Component
#include "ftxui/dom/elements.hpp" // for operator|, Element, nothing, reflect, text, border, inverted
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/util/ref.hpp" // for ConstStringRef, Ref
2020-08-26 22:26:09 +08:00
namespace ftxui {
2021-07-10 17:56:40 +08:00
namespace {
/// @brief A button. An action is associated to the click event.
/// @ingroup dom
class ButtonBase : public ComponentBase {
public:
ButtonBase(ConstStringRef label,
std::function<void()> on_click,
2021-07-10 18:29:39 +08:00
Ref<ButtonOption> option)
2021-07-10 17:56:40 +08:00
: label_(label), on_click_(on_click), option_(std::move(option)) {}
~ButtonBase() override = default;
// Component implementation:
Element Render() override {
auto style = Focused() ? inverted : nothing;
auto my_border = option_->border ? border : nothing;
return text(*label_) | my_border | style | reflect(box_);
}
bool OnEvent(Event event) override {
if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
if (!CaptureMouse(event))
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
on_click_();
return true;
}
return false;
}
if (event == Event::Return) {
on_click_();
return true;
}
return false;
}
private:
ConstStringRef label_;
std::function<void()> on_click_;
Box box_;
2021-07-10 18:29:39 +08:00
Ref<ButtonOption> option_;
2021-07-10 17:56:40 +08:00
};
} // namespace
2021-05-10 02:32:27 +08:00
/// @brief Draw a button. Execute a function when clicked.
/// @param label The label of the button.
/// @param on_click The action to execute when clicked.
/// @ingroup component
/// @see ButtonBase
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::FitComponent();
/// std::wstring label = L"Click to quit";
/// Component button = Button(&label, screen.ExitLoopClosure());
/// screen.Loop(button)
/// ```
///
/// ### Output
///
/// ```bash
/// ┌─────────────┐
/// │Click to quit│
/// └─────────────┘
/// ```
Component Button(ConstStringRef label,
std::function<void()> on_click,
2021-07-10 18:29:39 +08:00
Ref<ButtonOption> option) {
2021-07-08 04:23:07 +08:00
return Make<ButtonBase>(label, std::move(on_click), std::move(option));
2021-05-10 02:32:27 +08:00
}
2020-08-26 22:26:09 +08:00
} // namespace ftxui
// 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.