Add Event.

This commit is contained in:
Arthur Sonzogni
2018-10-18 22:58:38 +02:00
parent 1a4b2c98b2
commit f94b63fafb
25 changed files with 273 additions and 76 deletions

View File

@@ -1,6 +1,7 @@
add_subdirectory(color)
add_subdirectory(frame)
add_subdirectory(gauge)
add_subdirectory(input)
add_subdirectory(menu)
add_subdirectory(menu2)
add_subdirectory(print_key_press)

View File

@@ -0,0 +1,4 @@
add_executable(input_main
main.cpp
)
target_link_libraries(input_main PRIVATE ftxui)

47
examples/input/main.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen_interactive.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/component_vertical.hpp"
#include "ftxui/util/string.hpp"
using namespace ftxui::component;
using namespace ftxui::dom;
class MyComponent : ComponentVertical {
public:
MyComponent(ftxui::component::Delegate* delegate)
: ComponentVertical(delegate),
input_1(delegate->NewChild()),
input_2(delegate->NewChild()),
input_3(delegate->NewChild()) {
Focus(&input_1);
}
std::function<void()> on_enter = []() {};
private:
Input input_1;
Input input_2;
Input input_3;
Element Render() override {
return
frame(
vbox(
hbox(text(L" input_1 : "), input_1.Render()),
hbox(text(L" input_2 : "), input_2.Render()),
hbox(text(L" input_3 : "), input_3.Render())
)
);
}
};
int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(60, 17);
MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure();
screen.Loop();
}

View File

@@ -15,26 +15,30 @@ class DrawKey : public ftxui::component::Component {
using namespace ftxui::dom;
Children children;
for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) {
std::string code = "";
for(size_t j = 0; j<5; ++j)
code += " " + std::to_string(keys[i].values[j]);
try {
std::string line = std::to_string(i) + " -> " + std::to_string(keys[i]) +
" (" + char(keys[i]) + ")";
std::string line = code + " -> " + std::to_string(keys[i].values[0]) + " (" +
char(keys[i].values[0]) + ")";
children.push_back(text(to_wstring(line)));
} catch (...) {
std::string line = std::to_string(i) + " -> " + std::to_string(keys[i]) +
" (undefined)";
std::string line =
code + " -> " + std::to_string(keys[i].values[0]) + " (undefined)";
children.push_back(text(to_wstring(line)));
}
}
return vbox(std::move(children));
}
bool Event(int key) override {
keys.push_back(key);
bool OnEvent(ftxui::Event event) override {
keys.push_back(event);
return true;
}
private:
std::vector<int> keys;
std::vector<ftxui::Event> keys;
};
int main(int argc, const char* argv[]) {

View File

@@ -8,6 +8,7 @@
#include "ftxui/component/component_vertical.hpp"
#include "ftxui/util/string.hpp"
using namespace ftxui;
using namespace ftxui::component;
using namespace ftxui::dom;
@@ -48,11 +49,11 @@ class MyComponent : ComponentVertical {
);
}
bool Event(int key) override {
if (ComponentVertical::Event(key))
bool OnEvent(Event event) override {
if (ComponentVertical::OnEvent(event))
return true;
if (key == 10) {
if (event == Event::Return) {
on_enter();
return true;
}