mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-20 10:38:09 +08:00
Add Event.
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
#ifndef FTXUI_COMPONENT_EVENT
|
||||
#define FTXUI_COMPONENT_EVENT
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
struct Event{
|
||||
// --- Character ---
|
||||
static Event Character(char);
|
||||
|
||||
// --- Arrow ---
|
||||
static Event Arrow_Left;
|
||||
static Event Arrow_Right;
|
||||
static Event Arrow_Up;
|
||||
static Event Arrow_Down;
|
||||
|
||||
// --- Other ---
|
||||
static Event Backspace;
|
||||
static Event Delete;
|
||||
static Event Escape;
|
||||
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
|
||||
|
||||
// Internal representation.
|
||||
int values [3];
|
||||
Event(int values[3]) : values(values);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_EVENT */
|
@@ -1,8 +1,9 @@
|
||||
#ifndef FTXUI_COMPONENT_COMPONENT_HPP
|
||||
#define FTXUI_COMPONENT_COMPONENT_HPP
|
||||
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/component/delegate.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/event.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
@@ -20,7 +21,7 @@ class Component {
|
||||
virtual dom::Element Render();
|
||||
|
||||
// Handle an event. By default, it calls this function on each children.
|
||||
virtual bool Event(int key);
|
||||
virtual bool OnEvent(Event even);
|
||||
|
||||
// If this component contains children, this indicates which one is active. It
|
||||
// can be none of them.
|
||||
|
@@ -10,12 +10,12 @@ namespace component {
|
||||
class ComponentDirection : public Component {
|
||||
public:
|
||||
ComponentDirection(Delegate* delegate);
|
||||
bool Event(int key) override;
|
||||
bool OnEvent(Event) override;
|
||||
Component* GetActiveChild() override;
|
||||
|
||||
protected:
|
||||
void Focus(Component* child);
|
||||
virtual bool HandleDirection(int key) = 0;
|
||||
virtual bool HandleDirection(Event) = 0;
|
||||
Component* active_child_;
|
||||
};
|
||||
|
||||
|
@@ -10,8 +10,8 @@ namespace component {
|
||||
// It assumes its children are put in the horizontal direction.
|
||||
class ComponentHorizontal : public ComponentDirection {
|
||||
public:
|
||||
ComponentHorizontal(Delegate* delegate);
|
||||
bool HandleDirection(int key) override;
|
||||
ComponentHorizontal(Delegate*);
|
||||
bool HandleDirection(Event) override;
|
||||
};
|
||||
|
||||
} // namespace component
|
||||
|
@@ -10,8 +10,8 @@ namespace component {
|
||||
// It assumes its children are put in the vertical direction.
|
||||
class ComponentVertical : public ComponentDirection {
|
||||
public:
|
||||
ComponentVertical(Delegate* delegate);
|
||||
bool HandleDirection(int key) override;
|
||||
ComponentVertical(Delegate*);
|
||||
bool HandleDirection(Event) override;
|
||||
};
|
||||
|
||||
} // namespace component
|
||||
|
35
ftxui/include/ftxui/component/input.hpp
Normal file
35
ftxui/include/ftxui/component/input.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef FTXUI_COMPONENT_INPUT_H_
|
||||
#define FTXUI_COMPONENT_INPUT_H_
|
||||
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
class Input : public Component {
|
||||
public:
|
||||
// Constructor.
|
||||
Input(Delegate*);
|
||||
~Input() override;
|
||||
|
||||
// State.
|
||||
std::wstring content = L"input";
|
||||
std::wstring placeholder = L"placeholder";
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = [](){};
|
||||
std::function<void()> on_enter = [](){};
|
||||
|
||||
// Component implementation.
|
||||
dom::Element Render() override;
|
||||
bool OnEvent(Event) override;
|
||||
|
||||
private:
|
||||
int cursor_position = 0;
|
||||
};
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_INPUT_H_ */
|
@@ -22,7 +22,7 @@ class Menu : public Component {
|
||||
|
||||
// Component implementation.
|
||||
dom::Element Render() override;
|
||||
bool Event(int key) override;
|
||||
bool OnEvent(Event) override;
|
||||
};
|
||||
|
||||
} // namespace component
|
||||
|
@@ -22,7 +22,7 @@ class Toggle : public Component {
|
||||
|
||||
// Component implementation.
|
||||
dom::Element Render() override;
|
||||
bool Event(int key) override;
|
||||
bool OnEvent(Event) override;
|
||||
};
|
||||
|
||||
} // namespace component
|
||||
|
37
ftxui/include/ftxui/event.hpp
Normal file
37
ftxui/include/ftxui/event.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef FTXUI_EVENT_H_
|
||||
#define FTXUI_EVENT_H_
|
||||
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
struct Event{
|
||||
public:
|
||||
// --- Character ---
|
||||
static Event Character(char);
|
||||
|
||||
// --- Arrow ---
|
||||
static Event ArrowLeft;
|
||||
static Event ArrowRight;
|
||||
static Event ArrowUp;
|
||||
static Event ArrowDown;
|
||||
|
||||
// --- Other ---
|
||||
static Event Backspace;
|
||||
static Event Delete;
|
||||
static Event Return;
|
||||
static Event Escape;
|
||||
static Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
|
||||
|
||||
bool operator==(const Event& other) { return values == other.values; }
|
||||
|
||||
// Internal representation.
|
||||
std::array<char, 5> values = {0, 0, 0, 0, 0};
|
||||
};
|
||||
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
|
||||
#endif /* end of include guard: FTXUI_EVENT_H_ */
|
Reference in New Issue
Block a user