2020-04-20 03:00:37 +08: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.
|
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <stddef.h> // for size_t
|
|
|
|
#include <algorithm> // for max
|
2021-05-10 02:32:27 +08:00
|
|
|
#include <ftxui/component/component.hpp> // for Make
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
|
2021-05-10 02:32:27 +08:00
|
|
|
#include <string> // for allocator, operator+, wstring, char_traits, to_wstring, string
|
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
|
|
#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/dom/elements.hpp" // for text, vbox, window, Elements, Element
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
using namespace ftxui;
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
std::wstring Stringify(Event event) {
|
|
|
|
std::wstring out;
|
|
|
|
for (auto& it : event.input())
|
|
|
|
out += L" " + std::to_wstring((unsigned int)it);
|
|
|
|
|
|
|
|
out = L"(" + out + L" ) -> ";
|
|
|
|
if (event.is_character()) {
|
|
|
|
out += std::wstring(L"character(") + event.character() + L")";
|
|
|
|
} else if (event.is_mouse()) {
|
|
|
|
out += L"mouse";
|
|
|
|
switch (event.mouse().button) {
|
|
|
|
case Mouse::Left:
|
|
|
|
out += L"_left";
|
|
|
|
break;
|
|
|
|
case Mouse::Middle:
|
|
|
|
out += L"_middle";
|
|
|
|
break;
|
|
|
|
case Mouse::Right:
|
|
|
|
out += L"_right";
|
|
|
|
break;
|
|
|
|
case Mouse::None:
|
|
|
|
out += L"_none";
|
|
|
|
break;
|
|
|
|
case Mouse::WheelUp:
|
|
|
|
out += L"_wheel_up";
|
|
|
|
break;
|
|
|
|
case Mouse::WheelDown:
|
|
|
|
out += L"_wheel_down";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (event.mouse().motion) {
|
|
|
|
case Mouse::Pressed:
|
|
|
|
out += L"_pressed";
|
|
|
|
break;
|
|
|
|
case Mouse::Released:
|
|
|
|
out += L"_released";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (event.mouse().control)
|
|
|
|
out += L"_control";
|
|
|
|
if (event.mouse().shift)
|
|
|
|
out += L"_shift";
|
|
|
|
if (event.mouse().meta)
|
|
|
|
out += L"_meta";
|
|
|
|
|
|
|
|
out += L"(" + //
|
2021-05-02 02:40:35 +08:00
|
|
|
std::to_wstring(event.mouse().x) + L"," +
|
|
|
|
std::to_wstring(event.mouse().y) + L")";
|
2021-04-25 21:22:38 +08:00
|
|
|
} else {
|
|
|
|
out += L"(special)";
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
class DrawKey : public ComponentBase {
|
2018-10-10 01:06:03 +08:00
|
|
|
public:
|
2019-01-13 01:24:46 +08:00
|
|
|
~DrawKey() override = default;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
Element Render() override {
|
2019-01-13 01:24:46 +08:00
|
|
|
Elements children;
|
2021-04-25 00:16:13 +08:00
|
|
|
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
|
2021-04-25 21:22:38 +08:00
|
|
|
children.push_back(text(Stringify(keys[i])));
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
2020-05-21 02:50:40 +08:00
|
|
|
return window(text(L"keys"), vbox(std::move(children)));
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
bool OnEvent(Event event) override {
|
2018-10-19 04:58:38 +08:00
|
|
|
keys.push_back(event);
|
2018-10-10 01:06:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-07 00:10:35 +08:00
|
|
|
std::vector<Event> keys;
|
2018-10-10 01:06:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2020-05-21 02:50:40 +08:00
|
|
|
auto screen = ScreenInteractive::TerminalOutput();
|
2021-05-10 02:32:27 +08:00
|
|
|
screen.Loop(Make<DrawKey>());
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|