Introduce CatchEvent (#104)

This commit is contained in:
Arthur Sonzogni 2021-05-23 12:53:20 +02:00 committed by GitHub
parent ffb6dcef9a
commit aacb677e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 37 deletions

View File

@ -16,7 +16,7 @@ jobs:
mkdir build; mkdir build;
cd build; cd build;
cmake .. cmake ..
-DCMAKE_CXX_COMPILER=clang++-9 -DCMAKE_CXX_COMPILER=clang++
-DFTXUI_BUILD_TESTS=ON; -DFTXUI_BUILD_TESTS=ON;
cmake --build . --config Release; cmake --build . --config Release;

View File

@ -91,6 +91,7 @@ add_library(component STATIC
include/ftxui/component/screen_interactive.hpp include/ftxui/component/screen_interactive.hpp
include/ftxui/component/toggle.hpp include/ftxui/component/toggle.hpp
src/ftxui/component/button.cpp src/ftxui/component/button.cpp
src/ftxui/component/catch_event.cpp
src/ftxui/component/checkbox.cpp src/ftxui/component/checkbox.cpp
src/ftxui/component/component.cpp src/ftxui/component/component.cpp
src/ftxui/component/container.cpp src/ftxui/component/container.cpp

View File

@ -14,8 +14,10 @@ int main(int argc, const char* argv[]) {
// The tree of components. This defines how to navigate using the keyboard. // The tree of components. This defines how to navigate using the keyboard.
auto buttons = Container::Horizontal({ auto buttons = Container::Horizontal({
Button("Decrease", [&] { value--; }), Button(
Button("Increase", [&] { value++; }), "[Decrease]", [&] { value--; }, false),
Button(
"[Increase]", [&] { value++; }, false),
}); });
// Modify the way to render them on screen: // Modify the way to render them on screen:

View File

@ -4,17 +4,17 @@
#include <stddef.h> // for size_t #include <stddef.h> // for size_t
#include <algorithm> // for max #include <algorithm> // for max
#include <ftxui/component/component.hpp> // for Make #include <memory> // for shared_ptr
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive #include <string> // for allocator, char_traits, operator+, wstring, basic_string, to_wstring, string
#include <string> // for allocator, operator+, wstring, char_traits, to_wstring, string
#include <utility> // for move #include <utility> // for move
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component.hpp" // for CatchEvent, Renderer
#include "ftxui/component/event.hpp" // for Event #include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Middle, Mouse::None, Mouse::Pressed, Mouse::Released, Mouse::Right, Mouse::WheelDown, Mouse::WheelUp #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Middle, Mouse::None, Mouse::Pressed, Mouse::Released, Mouse::Right, Mouse::WheelDown, Mouse::WheelUp
#include "ftxui/dom/elements.hpp" // for text, vbox, window, Elements, Element #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, vbox, window, Element, Elements
using namespace ftxui; using namespace ftxui;
@ -72,28 +72,22 @@ std::wstring Stringify(Event event) {
return out; return out;
} }
class DrawKey : public ComponentBase {
public:
~DrawKey() override = default;
Element Render() override {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(Stringify(keys[i])));
}
return window(text(L"keys"), vbox(std::move(children)));
}
bool OnEvent(Event event) override {
keys.push_back(event);
return true;
}
private:
std::vector<Event> keys;
};
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.Loop(Make<DrawKey>());
std::vector<Event> keys;
auto component = Renderer([&] {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i)
children.push_back(text(Stringify(keys[i])));
return window(text(L"keys"), vbox(std::move(children)));
});
component = CatchEvent(component, [&](Event event) {
keys.push_back(event);
return true;
});
screen.Loop(component);
} }

View File

@ -13,6 +13,7 @@
namespace ftxui { namespace ftxui {
class ComponentBase; class ComponentBase;
struct Event;
using Component = std::shared_ptr<ComponentBase>; using Component = std::shared_ptr<ComponentBase>;
using Components = std::vector<Component>; using Components = std::vector<Component>;
@ -30,10 +31,11 @@ Component Input(StringRef content, ConstStringRef placeholder);
Component Menu(const std::vector<std::wstring>* entries, int* selected_); Component Menu(const std::vector<std::wstring>* entries, int* selected_);
Component Radiobox(const std::vector<std::wstring>* entries, int* selected_); Component Radiobox(const std::vector<std::wstring>* entries, int* selected_);
Component Toggle(const std::vector<std::wstring>* entries, int* selected); Component Toggle(const std::vector<std::wstring>* entries, int* selected);
Component Renderer(Component child, std::function<Element()>);
Component Renderer(std::function<Element()>);
template <class T> // T = {int, float} template <class T> // T = {int, float}
Component Slider(StringRef label, T* value, T min, T max, T increment); Component Slider(StringRef label, T* value, T min, T max, T increment);
Component Renderer(Component child, std::function<Element()>);
Component Renderer(std::function<Element()>);
Component CatchEvent(Component child, std::function<bool(Event)>);
namespace Container { namespace Container {
Component Vertical(Components children); Component Vertical(Components children);

View File

@ -35,7 +35,6 @@ class Node {
// Step 3: Draw this element. // Step 3: Draw this element.
virtual void Render(Screen& screen); virtual void Render(Screen& screen);
protected: protected:
std::vector<Element> children_; std::vector<Element> children_;
Requirement requirement_; Requirement requirement_;

View File

@ -0,0 +1,57 @@
#include <functional> // for function
#include <memory> // for __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Component, Make, CatchEvent
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/event.hpp" // for Event
namespace ftxui {
/// @brief A component executing a provided function for catching events.
/// @ingroup component.
class CatchEventBase : public ComponentBase {
public:
// Constructor.
CatchEventBase(std::function<bool(Event)> on_event)
: on_event_(std::move(on_event)) {}
~CatchEventBase() override = default;
// Component implementation.
bool OnEvent(Event event) override {
if (on_event_(event))
return true;
else
return ComponentBase::OnEvent(event);
}
protected:
std::function<bool(Event)> on_event_;
};
/// @brief Return a component, using |on_event| to catch events. This function
/// must returns true when the event has been handled, false otherwise.
/// @param on_event The function drawing the interface.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer = Renderer([] {
/// return text(L"My interface");
/// });
/// screen.Loop(renderer);
/// ```
Component CatchEvent(Component child,
std::function<bool(Event event)> on_event) {
auto out = Make<CatchEventBase>(std::move(on_event));
out->Add(std::move(child));
return out;
}
} // namespace ftxui
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

View File

@ -1,7 +1,8 @@
#include "ftxui/screen/string.hpp" #include "ftxui/screen/string.hpp"
#include <codecvt> #include <codecvt> // for codecvt_utf8_utf16
#include <locale> #include <locale> // for wstring_convert
#include <utility> // for move
namespace ftxui { namespace ftxui {
#ifdef _MSC_VER #ifdef _MSC_VER