Merge pull request #85 from ArthurSonzogni/feature/mouse-support

Add mouse support.
This commit is contained in:
Arthur Sonzogni 2021-05-01 23:52:22 +02:00 committed by GitHub
commit 1f050e3fa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
124 changed files with 1776 additions and 404 deletions

View File

@ -16,6 +16,5 @@ jobs:
run: > run: >
mkdir build; mkdir build;
cd build; cd build;
emcmake cmake .. emcmake cmake ..;
-DFTXUI_BUILD_TESTS=ON;
cmake --build . --config Release; cmake --build . --config Release;

View File

@ -15,7 +15,7 @@ endif()
project(ftxui project(ftxui
LANGUAGES CXX LANGUAGES CXX
VERSION 0.3.${git_version} VERSION 0.4.${git_version}
) )
option(FTXUI_BUILD_EXAMPLES "Set to ON to build examples" ON) option(FTXUI_BUILD_EXAMPLES "Set to ON to build examples" ON)
@ -65,6 +65,7 @@ add_library(dom
src/ftxui/dom/node.cpp src/ftxui/dom/node.cpp
src/ftxui/dom/node_decorator.cpp src/ftxui/dom/node_decorator.cpp
src/ftxui/dom/paragraph.cpp src/ftxui/dom/paragraph.cpp
src/ftxui/dom/reflect.cpp
src/ftxui/dom/separator.cpp src/ftxui/dom/separator.cpp
src/ftxui/dom/size.cpp src/ftxui/dom/size.cpp
src/ftxui/dom/spinner.cpp src/ftxui/dom/spinner.cpp
@ -76,15 +77,18 @@ add_library(dom
add_library(component add_library(component
include/ftxui/component/button.hpp include/ftxui/component/button.hpp
include/ftxui/component/captured_mouse.hpp
include/ftxui/component/checkbox.hpp include/ftxui/component/checkbox.hpp
include/ftxui/component/component.hpp include/ftxui/component/component.hpp
include/ftxui/component/container.hpp include/ftxui/component/container.hpp
include/ftxui/component/event.hpp include/ftxui/component/event.hpp
include/ftxui/component/input.hpp include/ftxui/component/input.hpp
include/ftxui/component/menu.hpp include/ftxui/component/menu.hpp
include/ftxui/component/mouse.hpp
include/ftxui/component/radiobox.hpp include/ftxui/component/radiobox.hpp
include/ftxui/component/receiver.hpp include/ftxui/component/receiver.hpp
include/ftxui/component/screen_interactive.hpp include/ftxui/component/screen_interactive.hpp
include/ftxui/component/slider.hpp
include/ftxui/component/toggle.hpp include/ftxui/component/toggle.hpp
src/ftxui/component/button.cpp src/ftxui/component/button.cpp
src/ftxui/component/checkbox.cpp src/ftxui/component/checkbox.cpp
@ -96,9 +100,10 @@ add_library(component
src/ftxui/component/radiobox.cpp src/ftxui/component/radiobox.cpp
src/ftxui/component/radiobox.cpp src/ftxui/component/radiobox.cpp
src/ftxui/component/screen_interactive.cpp src/ftxui/component/screen_interactive.cpp
src/ftxui/component/toggle.cpp src/ftxui/component/slider.cpp
src/ftxui/component/terminal_input_parser.cpp src/ftxui/component/terminal_input_parser.cpp
src/ftxui/component/terminal_input_parser.hpp src/ftxui/component/terminal_input_parser.hpp
src/ftxui/component/toggle.cpp
) )
add_library(ftxui::screen ALIAS screen) add_library(ftxui::screen ALIAS screen)
@ -107,6 +112,14 @@ add_library(ftxui::component ALIAS component)
target_link_libraries(dom PUBLIC screen) target_link_libraries(dom PUBLIC screen)
target_link_libraries(component PUBLIC dom Threads::Threads) target_link_libraries(component PUBLIC dom Threads::Threads)
find_program(iwyu_path NAMES include-what-you-use iwyu)
if(iwyu_path)
set_property(TARGET ${lib}
PROPERTY ${iwyu_path} -Xiwyu
--mapping_file ${CMAKE_CURRENT_SOURCE_DIR}/iwyu.impl
)
endif()
foreach(lib screen dom component) foreach(lib screen dom component)
target_include_directories(${lib} target_include_directories(${lib}
@ -199,7 +212,7 @@ if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4")
FetchContent_Declare( googletest FetchContent_Declare( googletest
GIT_REPOSITORY "https://github.com/google/googletest" GIT_REPOSITORY "https://github.com/google/googletest"
GIT_TAG release-1.10.0 GIT_TAG 23ef29555ef4789f555f1ba8c51b4c52975f0907
) )
FetchContent_GetProperties(googletest) FetchContent_GetProperties(googletest)

View File

@ -30,6 +30,7 @@
@example ./examples/component/checkbox_in_frame.cpp @example ./examples/component/checkbox_in_frame.cpp
@example ./examples/component/menu2.cpp @example ./examples/component/menu2.cpp
@example ./examples/component/tab_horizontal.cpp @example ./examples/component/tab_horizontal.cpp
@example ./examples/component/slider.cpp
@example ./examples/component/input.cpp @example ./examples/component/input.cpp
@example ./examples/component/homescreen.cpp @example ./examples/component/homescreen.cpp
@example ./examples/component/radiobox.cpp @example ./examples/component/radiobox.cpp

View File

@ -13,9 +13,10 @@ example(input)
example(menu) example(menu)
example(menu2) example(menu2)
example(menu_style) example(menu_style)
example(modal_dialog)
example(radiobox) example(radiobox)
example(radiobox_in_frame) example(radiobox_in_frame)
example(slider)
example(tab_horizontal) example(tab_horizontal)
example(tab_vertical) example(tab_vertical)
example(toggle) example(toggle)
example(modal_dialog)

View File

@ -1,8 +1,14 @@
#include "ftxui/component/button.hpp" #include <functional> // for function
#include <memory> // for unique_ptr, make_u...
#include <string> // for wstring
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/component.hpp" #include "ftxui/component/button.hpp" // for Button
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;

View File

@ -1,8 +1,7 @@
#include "ftxui/component/checkbox.hpp" #include "ftxui/component/checkbox.hpp"
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/component.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/container.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp"
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,15 @@
#include "ftxui/component/checkbox.hpp" #include <memory> // for allocator_traits<>...
#include "ftxui/component/container.hpp" #include <string> // for operator+, wstring
#include "ftxui/component/input.hpp" #include <utility> // for move
#include "ftxui/component/menu.hpp" #include <vector> // for vector
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp" #include "ftxui/component/checkbox.hpp" // for CheckBox
#include "ftxui/screen/string.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui; using namespace ftxui;

View File

@ -1,11 +1,20 @@
#include "ftxui/component/button.hpp" #include <functional> // for function
#include "ftxui/component/checkbox.hpp" #include <memory> // for allocator, unique_ptr
#include "ftxui/component/container.hpp" #include <string> // for wstring
#include "ftxui/component/input.hpp" #include <vector> // for vector
#include "ftxui/component/menu.hpp"
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/button.hpp" // for Button
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/checkbox.hpp" // for CheckBox
#include "ftxui/component/toggle.hpp" #include "ftxui/component/component.hpp" // for Component, Compone...
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/input.hpp" // for Input
#include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/slider.hpp" // for Slider
#include "ftxui/component/toggle.hpp" // for Toggle
#include "ftxui/dom/elements.hpp" // for separator, operator|
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;
@ -20,6 +29,13 @@ class MyComponent : public Component {
Input input; Input input;
Button button; Button button;
int slider_value_1_ = 12;
int slider_value_2_ = 56;
int slider_value_3_ = 128;
ComponentPtr slider_1_ = Slider(L"R:", &slider_value_1_, 0, 256, 1);
ComponentPtr slider_2_ = Slider(L"G:", &slider_value_2_, 0, 256, 1);
ComponentPtr slider_3_ = Slider(L"B:", &slider_value_3_, 0, 256, 1);
public: public:
MyComponent() { MyComponent() {
Add(&container); Add(&container);
@ -54,17 +70,26 @@ class MyComponent : public Component {
input.placeholder = L"Input placeholder"; input.placeholder = L"Input placeholder";
container.Add(&input); container.Add(&input);
container.Add(slider_1_.get());
container.Add(slider_2_.get());
container.Add(slider_3_.get());
button.label = L"Quit"; button.label = L"Quit";
button.on_click = [&] { on_quit(); }; button.on_click = [&] { on_quit(); };
container.Add(&button); container.Add(&button);
} }
Element Render(std::wstring name, Component& component) { Element Render(std::wstring name, Element element) {
return hbox({ return hbox({
text(name) | size(WIDTH, EQUAL, 8), text(name) | size(WIDTH, EQUAL, 8),
separator(), separator(),
component.Render(), element | xflex,
}); }) |
xflex;
}
Element Render(std::wstring name, Component& component) {
return Render(name, component.Render());
} }
Element Render() override { Element Render() override {
@ -78,11 +103,18 @@ class MyComponent : public Component {
separator(), separator(),
Render(L"radiobox", radiobox), Render(L"radiobox", radiobox),
separator(), separator(),
Render(L"input", input) | size(WIDTH, LESS_THAN, 30), Render(L"input", input) | size(WIDTH, LESS_THAN, 50),
separator(),
Render(L"slider", //
vbox({
slider_1_->Render(),
slider_2_->Render(),
slider_3_->Render(),
})),
separator(), separator(),
Render(L"button", button), Render(L"button", button),
}) | }) |
border; xflex | size(WIDTH, GREATER_THAN, 40) | border;
} }
std::function<void()> on_quit = [] {}; std::function<void()> on_quit = [] {};

View File

@ -1,14 +1,23 @@
#include <cmath> #include <chrono> // for operator""s, chron...
#include <thread> #include <cmath> // for sin
#include <functional> // for ref, reference_wra...
#include <string> // for allocator, wstring
#include <thread> // for sleep_for, thread
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/checkbox.hpp" #include "ftxui/component/checkbox.hpp" // for CheckBox
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/input.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" #include "ftxui/component/event.hpp" // for Event, Event::Custom
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/input.hpp" // for Input
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/toggle.hpp" #include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/screen/string.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/toggle.hpp" // for Toggle
#include "ftxui/dom/elements.hpp" // for text, operator|
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/color.hpp" // for Color, Color::Blue...
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,6 @@
#include "ftxui/component/input.hpp" #include "ftxui/component/input.hpp"
#include "ftxui/component/container.hpp" // for Container
#include <iostream> #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/screen/string.hpp"
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,11 @@
#include "ftxui/component/menu.hpp" #include <functional> // for function
#include <iostream> // for basic_ostream::ope...
#include <string> // for wstring, allocator
#include <vector> // for vector
#include <chrono> #include "ftxui/component/menu.hpp" // for Menu
#include <iostream> #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include <thread> #include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/component/screen_interactive.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,11 +1,14 @@
#include <chrono> #include <functional> // for function
#include <iostream> #include <string> // for wstring, allocator
#include <thread> #include <vector> // for vector
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/menu.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/screen/string.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, separator, bold
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,15 @@
#include <iostream> #include <functional> // for function
#include <thread> #include <initializer_list> // for initializer_list
#include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/menu.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/screen/string.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for operator|, Element
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/color.hpp" // for Color, Color::Blue
using namespace ftxui; using namespace ftxui;
@ -13,7 +18,14 @@ class MyComponent : public Component {
MyComponent() { MyComponent() {
Add(&container); Add(&container);
for (Menu* menu : {&menu_1, &menu_2, &menu_3, &menu_4, &menu_5, &menu_6}) { for (Menu* menu : {
&menu_1,
&menu_2,
&menu_3,
&menu_4,
&menu_5,
&menu_6,
}) {
container.Add(menu); container.Add(menu);
menu->entries = { menu->entries = {
L"Monkey", L"Dog", L"Cat", L"Bird", L"Elephant", L"Monkey", L"Dog", L"Cat", L"Bird", L"Elephant",
@ -21,22 +33,27 @@ class MyComponent : public Component {
menu->on_enter = [this]() { on_enter(); }; menu->on_enter = [this]() { on_enter(); };
} }
menu_2.selected_style = color(Color::Blue);
menu_2.focused_style = bold | color(Color::Blue); menu_2.focused_style = bold | color(Color::Blue);
menu_2.selected_style = color(Color::Blue);
menu_2.selected_focused_style = bold | color(Color::Blue);
menu_3.selected_style = color(Color::Blue); menu_3.selected_style = color(Color::Blue);
menu_3.focused_style = bgcolor(Color::Blue); menu_3.focused_style = bgcolor(Color::Blue);
menu_3.selected_focused_style = bgcolor(Color::Blue);
menu_4.selected_style = bgcolor(Color::Blue); menu_4.selected_style = bgcolor(Color::Blue);
menu_4.focused_style = bgcolor(Color::BlueLight); menu_4.focused_style = bgcolor(Color::BlueLight);
menu_4.selected_focused_style = bgcolor(Color::BlueLight);
menu_5.normal_style = bgcolor(Color::Blue); menu_5.normal_style = bgcolor(Color::Blue);
menu_5.selected_style = bgcolor(Color::Yellow); menu_5.selected_style = bgcolor(Color::Yellow);
menu_5.focused_style = bgcolor(Color::Red); menu_5.focused_style = bgcolor(Color::Red);
menu_5.selected_focused_style = bgcolor(Color::Red);
menu_6.normal_style = dim | color(Color::Blue); menu_6.normal_style = dim | color(Color::Blue);
menu_6.selected_style = color(Color::Blue); menu_6.selected_style = color(Color::Blue);
menu_6.focused_style = bold | color(Color::Blue); menu_6.focused_style = bold | color(Color::Blue);
menu_6.selected_focused_style = bold | color(Color::Blue);
} }
std::function<void()> on_enter = []() {}; std::function<void()> on_enter = []() {};

View File

@ -1,7 +1,14 @@
#include "ftxui/component/button.hpp" #include <functional> // for function
#include "ftxui/component/component.hpp" #include <memory> // for allocator_traits<>...
#include "ftxui/component/container.hpp" #include <string> // for operator+, wstring
#include "ftxui/component/screen_interactive.hpp" #include <vector> // for vector
#include "ftxui/component/button.hpp" // for Button
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;

View File

@ -1,8 +1,5 @@
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/component.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
using namespace ftxui; using namespace ftxui;

View File

@ -1,11 +1,12 @@
#include "ftxui/component/checkbox.hpp" #include <string> // for wstring, operator+
#include "ftxui/component/container.hpp" #include <vector> // for vector
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/toggle.hpp" #include "ftxui/dom/elements.hpp" // for Element, operator|
#include "ftxui/screen/string.hpp" #include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui; using namespace ftxui;

View File

@ -0,0 +1,84 @@
#include <functional> // for function
#include <memory> // for allocator, unique_ptr
#include <string> // for operator+, to_wstring
#include "ftxui/component/component.hpp" // for Component, Compone...
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Escape
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/slider.hpp" // for Slider
#include "ftxui/dom/elements.hpp" // for separator, operator|
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/color.hpp" // for Color
using namespace ftxui;
Element ColorTile(int red, int green, int blue) {
return text(L"") | size(WIDTH, GREATER_THAN, 14) |
size(HEIGHT, GREATER_THAN, 7) | bgcolor(Color::RGB(red, green, blue));
}
Element ColorString(int red, int green, int blue) {
return text(L"RGB = (" + //
std::to_wstring(red) + L"," + //
std::to_wstring(green) + L"," + //
std::to_wstring(blue) + L")" //
);
}
class MyComponent : public Component {
public:
MyComponent(int* red, int* green, int* blue, std::function<void(void)> quit)
: red_(red), green_(green), blue_(blue), quit_(quit) {
Add(&container_);
container_.Add(slider_red_.get());
container_.Add(slider_green_.get());
container_.Add(slider_blue_.get());
}
Element Render() {
return hbox({
ColorTile(*red_, *green_, *blue_),
separator(),
vbox({
slider_red_->Render(),
separator(),
slider_green_->Render(),
separator(),
slider_blue_->Render(),
separator(),
ColorString(*red_, *green_, *blue_),
}) | xflex,
}) |
border | size(WIDTH, LESS_THAN, 80);
}
bool OnEvent(Event event) {
if (event == Event::Return || event == Event::Escape)
quit_();
return Component::OnEvent(event);
}
private:
int* red_;
int* green_;
int* blue_;
Container container_ = Container::Vertical();
ComponentPtr slider_red_ = Slider(L"Red :", red_, 0, 255, 1);
ComponentPtr slider_green_ = Slider(L"Green:", green_, 0, 255, 1);
ComponentPtr slider_blue_ = Slider(L"Blue :", blue_, 0, 255, 1);
std::function<void(void)> quit_;
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
int red = 128;
int green = 25;
int blue = 100;
auto component = MyComponent(&red, &green, &blue, screen.ExitLoopClosure());
screen.Loop(&component);
}
// 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.

View File

@ -1,11 +1,14 @@
#include <iostream> #include <functional> // for function
#include <thread> #include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/component/toggle.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/screen/string.hpp" #include "ftxui/component/toggle.hpp" // for Toggle
#include "ftxui/dom/elements.hpp" // for Element, operator|
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,12 @@
#include <iostream> #include <functional> // for function
#include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp" #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/menu.hpp" #include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/toggle.hpp" #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/screen/string.hpp" #include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;

View File

@ -1,12 +1,7 @@
#include "ftxui/component/toggle.hpp" #include "ftxui/component/toggle.hpp"
#include "ftxui/component/container.hpp" // for Container
#include <chrono> #include "ftxui/component/event.hpp" // for Event, Event::Return
#include <iostream> #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include <thread>
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/screen/string.hpp"
using namespace ftxui; using namespace ftxui;

View File

@ -1,8 +1,9 @@
#include <chrono>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include <thread>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,14 +1,16 @@
#include <cmath>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/color_info.hpp> #include <ftxui/screen/color_info.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp> #include <ftxui/screen/terminal.hpp>
#include <iostream> #include <memory>
#include <utility>
#include "ftxui/screen/string.hpp" #include <vector>
using namespace ftxui; using namespace ftxui;
#include "./color_info_sorted_2d.ipp" // ColorInfoSorted2D. #include "./color_info_sorted_2d.ipp" // ColorInfoSorted2D.
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
// clang-format off // clang-format off

View File

@ -1,10 +1,13 @@
#include <algorithm>
#include <cmath>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/color_info.hpp> #include <ftxui/screen/color_info.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp> #include <string>
#include <iostream> #include <utility>
#include <vector>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
#include "ftxui/screen/string.hpp" #include "ftxui/screen/string.hpp"
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,11 @@
#include <cmath>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp> #include <memory>
#include <iostream> #include <utility>
#include "ftxui/screen/string.hpp" #include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,11 @@
#include <cmath>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp> #include <memory>
#include <iostream> #include <utility>
#include "ftxui/screen/string.hpp" #include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -2,8 +2,12 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <thread> #include <thread>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;
using namespace std::chrono_literals; using namespace std::chrono_literals;

View File

@ -2,9 +2,15 @@
#include <cmath> #include <cmath>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp> #include <functional>
#include <iostream> #include <iostream>
#include <string>
#include <thread> #include <thread>
#include <vector>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
class Graph { class Graph {
public: public:

View File

@ -1,7 +1,11 @@
#include <stddef.h>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp> #include <ftxui/screen/string.hpp>
#include <iostream> #include <string>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,10 +1,14 @@
#include <chrono> #include <chrono>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <thread> #include <thread>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;
using namespace std::chrono_literals; using namespace std::chrono_literals;

View File

@ -4,9 +4,16 @@
#include <ftxui/screen/string.hpp> #include <ftxui/screen/string.hpp>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <memory>
#include <string>
#include <thread> #include <thread>
#include <utility>
#include <vector> #include <vector>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
/// @example examples/dom/package_manage.cpp /// @example examples/dom/package_manage.cpp
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {

View File

@ -1,7 +1,10 @@
#include <stdio.h>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp> #include <string>
#include <iostream>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,7 +1,12 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp> #include <ftxui/screen/string.hpp>
#include <iostream> #include <memory>
#include <string>
#include <utility>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -3,7 +3,13 @@
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <ftxui/screen/string.hpp> #include <ftxui/screen/string.hpp>
#include <iostream> #include <iostream>
#include <string>
#include <thread> #include <thread>
#include <utility>
#include <vector>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <stdio.h>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include <iostream> #include <vector>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
int main(void) { int main(void) {
using namespace ftxui; using namespace ftxui;

View File

@ -69,10 +69,8 @@
]; ];
const url_search_params = new URLSearchParams(window.location.search); const url_search_params = new URLSearchParams(window.location.search);
const example_index = url_search_params.get("id") || 16; const example = url_search_params.get("file") || "./dom/color_gallery.js"
const example = example_list[example_index]; const select = document.getElementById("selectExample");
var select = document.getElementById("selectExample");
for(var i = 0; i < example_list.length; i++) { for(var i = 0; i < example_list.length; i++) {
var opt = example_list[i]; var opt = example_list[i];
@ -81,9 +79,10 @@
el.value = opt; el.value = opt;
select.appendChild(el); select.appendChild(el);
} }
select.selectedIndex = example_index; select.selectedIndex = example_list.findIndex(path => path == example) || 0;
select.addEventListener("change", () => { select.addEventListener("change", () => {
location.href = (location.href).split('?')[0] + "?id=" + select.selectedIndex; location.href = (location.href).split('?')[0] + "?file=" +
example_list[select.selectedIndex];
}); });
let stdin_buffer = []; let stdin_buffer = [];

View File

@ -2,32 +2,83 @@
// Use of this source code is governed by the MIT license that can be found in // Use of this source code is governed by the MIT license that can be found in
// the LICENSE file. // the LICENSE file.
#include <chrono> #include <stddef.h> // for size_t
#include <ftxui/component/component.hpp> #include <algorithm> // for max
#include <ftxui/component/screen_interactive.hpp> #include <ftxui/component/component.hpp> // for Component
#include <ftxui/screen/string.hpp> #include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include <iostream> #include <string> // for allocator, operator+
#include <thread> #include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left
#include "ftxui/dom/elements.hpp" // for text, vbox, window
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui; using namespace ftxui;
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"(" + //
std::to_wstring(event.mouse().x) + L"," +
std::to_wstring(event.mouse().y) + L")";
} else {
out += L"(special)";
}
return out;
}
class DrawKey : public Component { class DrawKey : public Component {
public: public:
~DrawKey() override = default; ~DrawKey() override = default;
Element Render() override { Element Render() override {
Elements children; Elements children;
for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) { for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
std::wstring code; children.push_back(text(Stringify(keys[i])));
for (auto& it : keys[i].input())
code += L" " + std::to_wstring((unsigned int)it);
code = L"(" + code + L" ) -> ";
if (keys[i].is_character())
code += keys[i].character();
else
code += L"(special)";
children.push_back(text(code));
} }
return window(text(L"keys"), vbox(std::move(children))); return window(text(L"keys"), vbox(std::move(children)));
} }

View File

@ -2,10 +2,14 @@
#define FTXUI_COMPONENT_BUTTON_HPP #define FTXUI_COMPONENT_BUTTON_HPP
#include <functional> #include <functional>
#include <string>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief A button. An action is associated to the click event. /// @brief A button. An action is associated to the click event.
/// @ingroup dom /// @ingroup dom
@ -25,6 +29,9 @@ class Button : public Component {
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
private:
Box box_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -0,0 +1,18 @@
#ifndef FTXUI_CAPTURED_MOUSE_HPP
#define FTXUI_CAPTURED_MOUSE_HPP
#include <memory>
namespace ftxui {
class CapturedMouseInterface {
public:
virtual ~CapturedMouseInterface() {}
};
using CapturedMouse = std::unique_ptr<CapturedMouseInterface>;
} // namespace ftxui
#endif /* end of include guard: FTXUI_CAPTURED_MOUSE_HPP */
// 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.

View File

@ -1,11 +1,13 @@
#ifndef FTXUI_COMPONENT_CHECKBOX_HPP #ifndef FTXUI_COMPONENT_CHECKBOX_HPP
#define FTXUI_COMPONENT_CHECKBOX_HPP #define FTXUI_COMPONENT_CHECKBOX_HPP
#include <functional> #include <string>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief A Checkbox. It can be checked or unchecked.Display an element on a /// @brief A Checkbox. It can be checked or unchecked.Display an element on a
/// ftxui::Screen. /// ftxui::Screen.
@ -38,7 +40,10 @@ class CheckBox : public Component {
bool OnEvent(Event) override; bool OnEvent(Event) override;
private: private:
bool OnMouseEvent(Event event);
int cursor_position = 0; int cursor_position = 0;
Box box_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -1,13 +1,17 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HPP #ifndef FTXUI_COMPONENT_COMPONENT_HPP
#define FTXUI_COMPONENT_COMPONENT_HPP #define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/component/event.hpp" #include <memory> // for unique_ptr
#include "ftxui/dom/elements.hpp" #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for CaptureMouse
#include "ftxui/dom/elements.hpp" // for Element
namespace ftxui { namespace ftxui {
class Delegate; class Delegate;
class Focus; class Focus;
struct Event;
/// @brief It implement rendering itself as ftxui::Element. It implement /// @brief It implement rendering itself as ftxui::Element. It implement
/// keyboard navigation by responding to ftxui::Event. /// keyboard navigation by responding to ftxui::Event.
@ -51,15 +55,19 @@ class Component {
// Configure all the ancestors to give focus to this component. // Configure all the ancestors to give focus to this component.
void TakeFocus(); void TakeFocus();
protected:
CapturedMouse CaptureMouse(const Event& event);
std::vector<Component*> children_;
private: private:
Component* parent_ = nullptr; Component* parent_ = nullptr;
void Detach(); void Detach();
void Attach(Component* parent); void Attach(Component* parent);
protected:
std::vector<Component*> children_;
}; };
using ComponentPtr = std::unique_ptr<Component>;
} // namespace ftxui } // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */ #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */

View File

@ -2,6 +2,8 @@
#define FTXUI_COMPONENT_CONTAINER_HPP #define FTXUI_COMPONENT_CONTAINER_HPP
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/component/event.hpp"
#include "ftxui/dom/elements.hpp"
namespace ftxui { namespace ftxui {
@ -36,6 +38,9 @@ class Container : public Component {
int selected_ = 0; int selected_ = 0;
int* selector_ = nullptr; int* selector_ = nullptr;
private:
bool OnMouseEvent(Event event);
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -1,14 +1,15 @@
#ifndef FTXUI_COMPONENT_EVENT_HPP #ifndef FTXUI_COMPONENT_EVENT_HPP
#define FTXUI_COMPONENT_EVENT_HPP #define FTXUI_COMPONENT_EVENT_HPP
#include <array> #include <ftxui/component/mouse.hpp> // for Mouse
#include <ftxui/component/receiver.hpp> #include <string> // for string, operator==
#include <functional>
#include <string>
#include <vector> #include <vector>
namespace ftxui { namespace ftxui {
class ScreenInteractive;
class Component;
/// @brief Represent an event. It can be key press event, a terminal resize, or /// @brief Represent an event. It can be key press event, a terminal resize, or
/// more ... /// more ...
/// ///
@ -26,8 +27,10 @@ struct Event {
static Event Character(char); static Event Character(char);
static Event Character(wchar_t); static Event Character(wchar_t);
static Event Character(const std::string&); static Event Character(std::string);
static Event Special(const std::string&); static Event Special(std::string);
static Event Mouse(std::string, Mouse mouse);
static Event CursorReporting(std::string, int x, int y);
// --- Arrow --- // --- Arrow ---
static const Event ArrowLeft; static const Event ArrowLeft;
@ -54,8 +57,18 @@ struct Event {
static Event Custom; static Event Custom;
//--- Method section --------------------------------------------------------- //--- Method section ---------------------------------------------------------
bool is_character() const { return is_character_; } bool is_character() const { return type_ == Type::Character; }
wchar_t character() const { return character_; } wchar_t character() const { return character_; }
bool is_mouse() const { return type_ == Type::Mouse; }
struct Mouse& mouse() {
return mouse_;
}
bool is_cursor_reporting() const { return type_ == Type::CursorReporting; }
int cursor_x() const { return cursor_.x; }
int cursor_y() const { return cursor_.y; }
const std::string& input() const { return input_; } const std::string& input() const { return input_; }
bool operator==(const Event& other) const { return input_ == other.input_; } bool operator==(const Event& other) const { return input_ == other.input_; }
@ -63,11 +76,30 @@ struct Event {
//--- State section ---------------------------------------------------------- //--- State section ----------------------------------------------------------
private: private:
std::string input_; friend Component;
bool is_character_ = false; friend ScreenInteractive;
wchar_t character_ = U'?'; enum class Type {
Unknown,
Character,
Mouse,
CursorReporting,
};
Type type_ = Type::Unknown;
struct Cursor {
int x;
int y;
}; };
union {
wchar_t character_ = U'?';
struct Mouse mouse_;
struct Cursor cursor_;
};
std::string input_;
ScreenInteractive* screen_ = nullptr;
};
} // namespace ftxui } // namespace ftxui

View File

@ -2,10 +2,14 @@
#define FTXUI_COMPONENT_INPUT_H_ #define FTXUI_COMPONENT_INPUT_H_
#include <functional> #include <functional>
#include <string>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief An input box. The user can type text into it. /// @brief An input box. The user can type text into it.
/// @ingroup component. /// @ingroup component.
@ -27,6 +31,11 @@ class Input : public Component {
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event);
Box input_box_;
Box cursor_box_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -2,11 +2,15 @@
#define FTXUI_COMPONENT_MENU #define FTXUI_COMPONENT_MENU
#include <functional> #include <functional>
#include <string>
#include <vector>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief A list of items. The user can navigate through them. /// @brief A list of items. The user can navigate through them.
/// @ingroup component /// @ingroup component
@ -19,10 +23,12 @@ class Menu : public Component {
// State. // State.
std::vector<std::wstring> entries = {}; std::vector<std::wstring> entries = {};
int selected = 0; int selected = 0;
int focused = 0;
Decorator normal_style = nothing;
Decorator focused_style = inverted; Decorator focused_style = inverted;
Decorator selected_style = bold; Decorator selected_style = bold;
Decorator normal_style = nothing; Decorator selected_focused_style = focused_style | selected_style;
// State update callback. // State update callback.
std::function<void()> on_change = []() {}; std::function<void()> on_change = []() {};
@ -31,6 +37,11 @@ class Menu : public Component {
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event);
std::vector<Box> boxes_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -0,0 +1,44 @@
#ifndef FTXUI_COMPONENT_MOUSE_HPP
#define FTXUI_COMPONENT_MOUSE_HPP
namespace ftxui {
/// @brief A mouse event. It contains the coordinate of the mouse, the button
/// pressed and the modifier (shift, ctrl, meta).
/// @ingroup component
struct Mouse {
enum Button {
Left = 0,
Middle = 1,
Right = 2,
None = 3,
WheelUp = 4,
WheelDown = 5,
};
enum Motion {
Released = 0,
Pressed = 1,
};
// Button
Button button;
// Motion
Motion motion;
// Modifiers:
bool shift;
bool meta;
bool control;
// Coordinates:
int x;
int y;
};
} // 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.
#endif /* end of include guard: FTXUI_COMPONENT_MOUSE_HPP */

View File

@ -1,11 +1,15 @@
#ifndef FTXUI_COMPONENT_RADIOBOX_HPP #ifndef FTXUI_COMPONENT_RADIOBOX_HPP
#define FTXUI_COMPONENT_RADIOBOX_HPP #define FTXUI_COMPONENT_RADIOBOX_HPP
#include <functional> #include <string>
#include <vector>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief A list of selectable element. One and only one can be selected at /// @brief A list of selectable element. One and only one can be selected at
/// the same time. /// the same time.
@ -39,7 +43,9 @@ class RadioBox : public Component {
bool OnEvent(Event) override; bool OnEvent(Event) override;
private: private:
bool OnMouseEvent(Event event);
int cursor_position = 0; int cursor_position = 0;
std::vector<Box> boxes_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -1,13 +1,13 @@
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_ #ifndef FTXUI_COMPONENT_RECEIVER_HPP_
#define FTXUI_COMPONENT_RECEIVER_HPP_ #define FTXUI_COMPONENT_RECEIVER_HPP_
#include <atomic> #include <atomic> // for atomic
#include <condition_variable> #include <condition_variable> // for condition_variable
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <memory> #include <memory> // for unique_ptr, make_unique
#include <mutex> #include <mutex> // for mutex, unique_lock
#include <queue> #include <queue> // for queue
namespace ftxui { namespace ftxui {
@ -38,6 +38,7 @@ namespace ftxui {
// clang-format off // clang-format off
template<class T> class SenderImpl; template<class T> class SenderImpl;
template<class T> class ReceiverImpl; template<class T> class ReceiverImpl;
template<class T> using Sender = std::unique_ptr<SenderImpl<T>>; template<class T> using Sender = std::unique_ptr<SenderImpl<T>>;
template<class T> using Receiver = std::unique_ptr<ReceiverImpl<T>>; template<class T> using Receiver = std::unique_ptr<ReceiverImpl<T>>;
template<class T> Receiver<T> MakeReceiver(); template<class T> Receiver<T> MakeReceiver();

View File

@ -1,19 +1,18 @@
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP #ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP #define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#include <atomic> #include <atomic> // for atomic
#include <condition_variable>
#include <ftxui/component/receiver.hpp> #include <ftxui/component/receiver.hpp>
#include <functional> #include <memory> // for unique_ptr
#include <memory> #include <string> // for string
#include <mutex>
#include <queue>
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" #include "ftxui/component/event.hpp"
#include "ftxui/screen/screen.hpp" #include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui { namespace ftxui {
class Component; class Component;
struct Event;
class ScreenInteractive : public Screen { class ScreenInteractive : public Screen {
public: public:
@ -27,6 +26,7 @@ class ScreenInteractive : public Screen {
std::function<void()> ExitLoopClosure(); std::function<void()> ExitLoopClosure();
void PostEvent(Event event); void PostEvent(Event event);
CapturedMouse CaptureMouse();
private: private:
void Draw(Component* component); void Draw(Component* component);
@ -52,6 +52,11 @@ class ScreenInteractive : public Screen {
std::string reset_cursor_position; std::string reset_cursor_position;
std::atomic<bool> quit_ = false; std::atomic<bool> quit_ = false;
int cursor_x_ = 0;
int cursor_y_ = 0;
bool mouse_captured = false;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -0,0 +1,23 @@
#ifndef FTXUI_COMPONENT_SLIDER_HPP
#define FTXUI_COMPONENT_SLIDER_HPP
#include <string>
#include "ftxui/component/component.hpp"
namespace ftxui {
// ComponentPtr Slider(std::string label,
// float* value,
// float min = 0.f,
// float max = 100.f,
// float increment = (max - min) * 0.05f);
template <class T> // T = {int, float}
ComponentPtr Slider(std::wstring label, T* value, T min, T max, T increment);
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_SLIDER_HPP */
// 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.

View File

@ -1,12 +1,15 @@
#ifndef FTXUI_COMPONENT_TOGGLE_H_ #ifndef FTXUI_COMPONENT_TOGGLE_H_
#define FTXUI_COMPONENT_TOGGLE_H_ #define FTXUI_COMPONENT_TOGGLE_H_
#include <functional>
#include <string> #include <string>
#include <vector>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {
struct Event;
/// @brief An horizontal list of elements. The user can navigate through them. /// @brief An horizontal list of elements. The user can navigate through them.
/// @ingroup component /// @ingroup component
@ -16,12 +19,14 @@ class Toggle : public Component {
~Toggle() override = default; ~Toggle() override = default;
// State. // State.
int selected = 0;
std::vector<std::wstring> entries = {L"On", L"Off"}; std::vector<std::wstring> entries = {L"On", L"Off"};
int selected = 0;
int focused = 0;
Decorator normal_style = dim;
Decorator focused_style = inverted; Decorator focused_style = inverted;
Decorator selected_style = bold; Decorator selected_style = bold;
Decorator normal_style = dim; Decorator selected_focused_style = focused_style | selected_style;
// Callback. // Callback.
std::function<void()> on_change = []() {}; std::function<void()> on_change = []() {};
@ -30,6 +35,10 @@ class Toggle : public Component {
// Component implementation. // Component implementation.
Element Render() override; Element Render() override;
bool OnEvent(Event) override; bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event event);
std::vector<Box> boxes_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp" #include "ftxui/screen/color.hpp"
#include "ftxui/screen/screen.hpp" #include "ftxui/screen/screen.hpp"
@ -77,6 +78,9 @@ enum Direction { WIDTH, HEIGHT };
enum Constraint { LESS_THAN, EQUAL, GREATER_THAN }; enum Constraint { LESS_THAN, EQUAL, GREATER_THAN };
Decorator size(Direction, Constraint, int value); Decorator size(Direction, Constraint, int value);
// --
Decorator reflect(Box& box);
// --- Frame --- // --- Frame ---
// A frame is a scrollable area. The internal area is potentially larger than // A frame is a scrollable area. The internal area is potentially larger than
// the external one. The internal area is scrolled in order to make visible the // the external one. The internal area is scrolled in order to make visible the

View File

@ -1,16 +1,18 @@
#ifndef FTXUI_DOM_NODE_HPP #ifndef FTXUI_DOM_NODE_HPP
#define FTXUI_DOM_NODE_HPP #define FTXUI_DOM_NODE_HPP
#include <memory> #include <memory> // for shared_ptr
#include <vector> #include <vector> // for vector
#include "ftxui/dom/requirement.hpp" #include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" #include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" #include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {
class Node; class Node;
class Screen;
using Element = std::shared_ptr<Node>; using Element = std::shared_ptr<Node>;
using Elements = std::vector<std::shared_ptr<Node>>; using Elements = std::vector<std::shared_ptr<Node>>;

View File

@ -1,3 +1,4 @@
// IWYU pragma: private, include "ftxui/dom/elements.hpp"
#include <type_traits> #include <type_traits>
template <class T> template <class T>

View File

@ -10,6 +10,7 @@ struct Box {
int y_max; int y_max;
static Box Intersection(Box a, Box b); static Box Intersection(Box a, Box b);
bool Contain(int x, int y);
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -1,8 +1,8 @@
#ifndef FTXUI_SCREEN_COLOR #ifndef FTXUI_SCREEN_COLOR
#define FTXUI_SCREEN_COLOR #define FTXUI_SCREEN_COLOR
#include <cstdint> #include <stdint.h> // for uint8_t
#include <string> #include <string> // for wstring
#ifdef RGB #ifdef RGB
// Workaround for wingdi.h (via Windows.h) defining macros that break things. // Workaround for wingdi.h (via Windows.h) defining macros that break things.

View File

@ -1,6 +1,7 @@
#ifndef FTXUI_SCREEN_COLOR_INFO_HPP #ifndef FTXUI_SCREEN_COLOR_INFO_HPP
#define FTXUI_SCREEN_COLOR_INFO_HPP #define FTXUI_SCREEN_COLOR_INFO_HPP
#include <stdint.h>
#include <ftxui/screen/color.hpp> #include <ftxui/screen/color.hpp>
namespace ftxui { namespace ftxui {

8
iwyu.imp Normal file
View File

@ -0,0 +1,8 @@
[
{ symbol: [ "VMIN", private, "<termios.h>", public ] },
{ symbol: [ "VTIME", private, "<termios.h>", public ] },
{ symbol: [ "ECHO", private, "<termios.h>", public ] },
{ symbol: [ "ICANON", private, "<termios.h>", public ] },
{ symbol: [ "termios", private, "<termios.h>", public ] },
{ symbol: [ "TCSANOW", private, "<termios.h>", public ] },
]

View File

@ -1,17 +1,35 @@
#include "ftxui/component/button.hpp" #include <functional> // for function
#include <memory> // for shared_ptr
#include <functional> #include "ftxui/component/button.hpp"
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event, Event::Return
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
namespace ftxui { namespace ftxui {
Element Button::Render() { Element Button::Render() {
if (Focused()) auto style = Focused() ? inverted : nothing;
return text(label) | border | inverted; return text(label) | border | style | reflect(box_);
else
return text(label) | border;
} }
bool Button::OnEvent(Event event) { bool Button::OnEvent(Event event) {
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) { if (event == Event::Return) {
on_click(); on_click();
return true; return true;

View File

@ -1,6 +1,11 @@
#include "ftxui/component/checkbox.hpp" #include <functional> // for function
#include <memory> // for shared_ptr
#include <functional> #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/event.hpp" // for Event, Event::Return
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
namespace ftxui { namespace ftxui {
@ -9,10 +14,14 @@ Element CheckBox::Render() {
auto style = is_focused ? focused_style : unfocused_style; auto style = is_focused ? focused_style : unfocused_style;
auto focus_management = is_focused ? focus : state ? select : nothing; auto focus_management = is_focused ? focus : state ? select : nothing;
return hbox(text(state ? checked : unchecked), return hbox(text(state ? checked : unchecked),
text(label) | style | focus_management); text(label) | style | focus_management) |
reflect(box_);
} }
bool CheckBox::OnEvent(Event event) { bool CheckBox::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
if (event == Event::Character(' ') || event == Event::Return) { if (event == Event::Character(' ') || event == Event::Return) {
state = !state; state = !state;
on_change(); on_change();
@ -21,6 +30,24 @@ bool CheckBox::OnEvent(Event event) {
return false; return false;
} }
bool CheckBox::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
state = !state;
on_change();
return true;
}
return false;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,11 +1,20 @@
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include <assert.h>
#include <algorithm> #include <algorithm>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/event.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui { namespace ftxui {
namespace {
class CaptureMouseImpl : public CapturedMouseInterface {
public:
~CaptureMouseImpl() override {}
};
}
Component::~Component() { Component::~Component() {
Detach(); Detach();
} }
@ -97,6 +106,16 @@ void Component::TakeFocus() {
} }
} }
/// @brief Take the CapturedMouse if available. There is only one component of
/// them. It represents a component taking priority over others.
/// @argument event
/// @ingroup component
CapturedMouse Component::CaptureMouse(const Event& event) {
if (!event.screen_)
return std::make_unique<CaptureMouseImpl>();
return event.screen_->CaptureMouse();
}
/// @brief Detach this children from its parent. /// @brief Detach this children from its parent.
/// @see Attach /// @see Attach
/// @see Detach /// @see Detach

View File

@ -1,6 +1,8 @@
#include "ftxui/component/container.hpp" #include "ftxui/component/container.hpp"
#include <stddef.h>
#include <algorithm> #include <algorithm>
#include <vector>
namespace ftxui { namespace ftxui {
@ -30,6 +32,9 @@ Container Container::Tab(int* selector) {
} }
bool Container::OnEvent(Event event) { bool Container::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused()) if (!Focused())
return false; return false;
@ -115,6 +120,17 @@ Element Container::TabRender() {
return text(L"Empty container"); return text(L"Empty container");
} }
bool Container::OnMouseEvent(Event event) {
if (selector_)
return ActiveChild()->OnEvent(event);
for (Component* child : children_) {
if (child->OnEvent(event))
return true;
}
return false;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,6 +1,10 @@
#include "ftxui/component/container.hpp" #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver
#include <memory> // for allocator
#include "gtest/gtest.h" #include "ftxui/component/container.hpp"
#include "ftxui/screen/box.hpp" // for ftxui
#include "gtest/gtest_pred_impl.h" // for AssertionResult, EXPECT_EQ, EXPEC...
using namespace ftxui; using namespace ftxui;

View File

@ -1,16 +1,16 @@
#include "ftxui/component/event.hpp" #include "ftxui/component/event.hpp"
#include <iostream> #include "ftxui/component/mouse.hpp"
#include "ftxui/screen/string.hpp" #include "ftxui/screen/string.hpp"
namespace ftxui { namespace ftxui {
// static // static
Event Event::Character(const std::string& input) { Event Event::Character(std::string input) {
Event event; Event event;
event.input_ = input;
event.is_character_ = true;
event.character_ = to_wstring(input)[0]; event.character_ = to_wstring(input)[0];
event.input_ = std::move(input);
event.type_ = Type::Character;
return event; return event;
} }
@ -23,15 +23,34 @@ Event Event::Character(char c) {
Event Event::Character(wchar_t c) { Event Event::Character(wchar_t c) {
Event event; Event event;
event.input_ = {(char)c}; event.input_ = {(char)c};
event.is_character_ = true; event.type_ = Type::Character;
event.character_ = c; event.character_ = c;
return event; return event;
} }
// static // static
Event Event::Special(const std::string& input) { Event Event::Mouse(std::string input, struct Mouse mouse) {
Event event; Event event;
event.input_ = std::move(input); event.input_ = std::move(input);
event.type_ = Type::Mouse;
event.mouse_ = mouse;
return event;
}
// static
Event Event::Special(std::string input) {
Event event;
event.input_ = std::move(input);
return event;
}
// static
Event Event::CursorReporting(std::string input, int x, int y) {
Event event;
event.input_ = std::move(input);
event.type_ = Type::CursorReporting;
event.cursor_.x = x;
event.cursor_.y = y;
return event; return event;
} }

View File

@ -1,8 +1,11 @@
#include "ftxui/component/input.hpp" #include "ftxui/component/input.hpp"
#include <algorithm> #include <algorithm>
#include <memory>
#include "ftxui/screen/string.hpp" #include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/mouse.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui { namespace ftxui {
@ -15,14 +18,15 @@ Element Input::Render() {
// Placeholder. // Placeholder.
if (content.size() == 0) { if (content.size() == 0) {
if (is_focused) if (is_focused)
return text(placeholder) | focus | dim | inverted | main_decorator; return text(placeholder) | focus | dim | inverted | main_decorator |
reflect(input_box_);
else else
return text(placeholder) | dim | main_decorator; return text(placeholder) | dim | main_decorator | reflect(input_box_);
} }
// Not focused. // Not focused.
if (!is_focused) if (!is_focused)
return text(content) | main_decorator; return text(content) | main_decorator | reflect(input_box_);
std::wstring part_before_cursor = content.substr(0, cursor_position); std::wstring part_before_cursor = content.substr(0, cursor_position);
std::wstring part_at_cursor = cursor_position < (int)content.size() std::wstring part_at_cursor = cursor_position < (int)content.size()
@ -37,13 +41,18 @@ Element Input::Render() {
return return
hbox( hbox(
text(part_before_cursor), text(part_before_cursor),
text(part_at_cursor) | underlined | focused, text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
text(part_after_cursor) text(part_after_cursor)
) | flex | inverted | frame | main_decorator; ) | flex | inverted | frame | main_decorator | reflect(input_box_);
// clang-format off // clang-format on
} }
bool Input::OnEvent(Event event) { bool Input::OnEvent(Event event) {
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position)); cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
if (event.is_mouse())
return OnMouseEvent(event);
std::wstring c; std::wstring c;
// Backspace. // Backspace.
@ -105,6 +114,28 @@ bool Input::OnEvent(Event event) {
return false; return false;
} }
bool Input::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (!input_box_.Contain(event.mouse().x, event.mouse().y))
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
int new_cursor_position =
cursor_position + event.mouse().x - cursor_box_.x_min;
new_cursor_position =
std::max(0, std::min<int>(content.size(), new_cursor_position));
if (cursor_position != new_cursor_position) {
cursor_position = new_cursor_position;
on_change();
}
}
return true;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,4 +1,5 @@
#include "ftxui/component/input.hpp" #include "ftxui/component/input.hpp"
#include "ftxui/component/event.hpp"
#include "gtest/gtest.h" #include "gtest/gtest.h"

View File

@ -1,25 +1,43 @@
#include "ftxui/component/menu.hpp" #include "ftxui/component/menu.hpp"
#include <stddef.h>
#include <algorithm> #include <algorithm>
#include <iostream> #include <memory>
#include <utility>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/mouse.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui { namespace ftxui {
Element Menu::Render() { Element Menu::Render() {
std::vector<Element> elements; Elements elements;
bool is_focused = Focused(); bool is_menu_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
auto style = (selected != int(i)) bool is_focused = (focused == int(i)) && is_menu_focused;
? normal_style bool is_selected = (selected == int(i));
: is_focused ? focused_style : selected_style;
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select; auto style = is_selected
auto icon = (selected != int(i)) ? L" " : L"> "; ? (is_focused ? selected_focused_style : selected_style)
elements.push_back(text(icon + entries[i]) | style | focused); : (is_focused ? focused_style : normal_style);
auto focus_management = !is_selected ? nothing
: is_menu_focused ? focus
: select;
auto icon = is_selected ? L"> " : L" ";
elements.push_back(text(icon + entries[i]) | style | focus_management |
reflect(boxes_[i]));
} }
return vbox(std::move(elements)); return vbox(std::move(elements));
} }
bool Menu::OnEvent(Event event) { bool Menu::OnEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused()) if (!Focused())
return false; return false;
@ -36,6 +54,7 @@ bool Menu::OnEvent(Event event) {
selected = std::max(0, std::min(int(entries.size()) - 1, selected)); selected = std::max(0, std::min(int(entries.size()) - 1, selected));
if (selected != old_selected) { if (selected != old_selected) {
focused = selected;
on_change(); on_change();
return true; return true;
} }
@ -48,6 +67,27 @@ bool Menu::OnEvent(Event event) {
return false; return false;
} }
bool Menu::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
TakeFocus();
focused = i;
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Released) {
if (selected != i) {
selected = i;
on_change();
}
return true;
}
}
return false;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,27 +1,41 @@
#include "ftxui/component/radiobox.hpp" #include "ftxui/component/radiobox.hpp"
#include <stddef.h>
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <memory>
#include <utility>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/mouse.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui { namespace ftxui {
Element RadioBox::Render() { Element RadioBox::Render() {
std::vector<Element> elements; std::vector<Element> elements;
bool is_focused = Focused(); bool is_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
auto style = auto style =
(focused == int(i) && is_focused) ? focused_style : unfocused_style; (focused == int(i) && is_focused) ? focused_style : unfocused_style;
auto focus_management = auto focus_management = (focused != int(i)) ? nothing
(focused != int(i)) ? nothing : is_focused ? focus : select; : is_focused ? focus
: select;
const std::wstring& symbol = selected == int(i) ? checked : unchecked; const std::wstring& symbol = selected == int(i) ? checked : unchecked;
elements.push_back(hbox(text(symbol), text(entries[i]) | style) | elements.push_back(hbox(text(symbol), text(entries[i]) | style) |
focus_management); focus_management | reflect(boxes_[i]));
} }
return vbox(std::move(elements)); return vbox(std::move(elements));
} }
bool RadioBox::OnEvent(Event event) { bool RadioBox::OnEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused()) if (!Focused())
return false; return false;
@ -50,6 +64,30 @@ bool RadioBox::OnEvent(Event event) {
return false; return false;
} }
bool RadioBox::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
focused = i;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
cursor_position = i;
TakeFocus();
if (selected != i) {
selected = i;
on_change();
}
return true;
}
}
return false;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,6 +1,10 @@
#include "ftxui/component/radiobox.hpp" #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
#include "gtest/gtest.h" #include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Tab, Event::TabReverse
#include "ftxui/component/mouse.hpp" // for ftxui
#include "ftxui/component/radiobox.hpp"
#include "gtest/gtest_pred_impl.h" // for EXPECT_EQ, Test, TEST
using namespace ftxui; using namespace ftxui;

View File

@ -1,8 +1,10 @@
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult
#include <thread> // for thread
#include <utility> // for move
#include "ftxui/component/receiver.hpp" #include "ftxui/component/receiver.hpp"
#include "gtest/gtest_pred_impl.h" // for AssertionResult, Test, EXPECT_EQ
#include <thread>
#include "gtest/gtest.h"
using namespace ftxui; using namespace ftxui;

View File

@ -1,18 +1,24 @@
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/screen_interactive.hpp"
#include <stdio.h> #include <stdio.h> // for fileno, stdin
#include <algorithm> // for copy, max, min
#include <csignal> // for signal, SIGINT
#include <cstdlib> // for exit, NULL
#include <iostream> // for cout, ostream
#include <stack> // for stack
#include <thread> // for thread
#include <utility> // for move
#include <vector> // for vector
#include <algorithm> #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include <csignal> #include "ftxui/component/component.hpp" // for Component
#include <cstdlib> #include "ftxui/component/event.hpp" // for Event
#include <iostream> #include "ftxui/component/mouse.hpp" // for Mouse
#include <stack> #include "ftxui/component/receiver.hpp" // for ReceiverImpl
#include <thread> #include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputPa...
#include "ftxui/dom/node.hpp" // for Node, Render
#include "ftxui/component/component.hpp" #include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/component/terminal_input_parser.hpp" #include "ftxui/screen/terminal.hpp" // for Terminal::Dimen...
#include "ftxui/screen/string.hpp"
#include "ftxui/screen/terminal.hpp"
#if defined(_WIN32) #if defined(_WIN32)
#define DEFINE_CONSOLEV2_PROPERTIES #define DEFINE_CONSOLEV2_PROPERTIES
@ -25,8 +31,9 @@
#error Must be compiled in UNICODE mode #error Must be compiled in UNICODE mode
#endif #endif
#else #else
#include <termios.h> #include <sys/select.h> // for select, FD_ISSET
#include <unistd.h> #include <termios.h> // for tcsetattr, tcge...
#include <unistd.h> // for STDIN_FILENO, read
#endif #endif
// Quick exit is missing in standard CLang headers // Quick exit is missing in standard CLang headers
@ -40,15 +47,14 @@ namespace {
void Flush() { void Flush() {
// Emscripten doesn't implement flush. We interpret zero as flush. // Emscripten doesn't implement flush. We interpret zero as flush.
std::cout << std::flush << (char)0; std::cout << '\0' << std::flush;
} }
constexpr int timeout_milliseconds = 20; constexpr int timeout_milliseconds = 20;
constexpr int timeout_microseconds = timeout_milliseconds * 1000; constexpr int timeout_microseconds = timeout_milliseconds * 1000;
#if defined(_WIN32) #if defined(_WIN32)
void EventListener(std::atomic<bool>* quit, void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
Sender<Event> out) {
auto console = GetStdHandle(STD_INPUT_HANDLE); auto console = GetStdHandle(STD_INPUT_HANDLE);
auto parser = TerminalInputParser(out->Clone()); auto parser = TerminalInputParser(out->Clone());
while (!*quit) { while (!*quit) {
@ -68,8 +74,7 @@ void EventListener(std::atomic<bool>* quit,
std::vector<INPUT_RECORD> records{number_of_events}; std::vector<INPUT_RECORD> records{number_of_events};
DWORD number_of_events_read = 0; DWORD number_of_events_read = 0;
ReadConsoleInput(console, records.data(), ReadConsoleInput(console, records.data(), (DWORD)records.size(),
(DWORD)records.size(),
&number_of_events_read); &number_of_events_read);
records.resize(number_of_events_read); records.resize(number_of_events_read);
@ -114,7 +119,7 @@ void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
} }
#else #else
#include <sys/time.h> #include <sys/time.h> // for timeval
int CheckStdinReady(int usec_timeout) { int CheckStdinReady(int usec_timeout) {
timeval tv = {0, usec_timeout}; timeval tv = {0, usec_timeout};
@ -146,14 +151,53 @@ void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
#endif #endif
static const char* HIDE_CURSOR = "\x1B[?25l"; const std::string CSI = "\x1b[";
static const char* SHOW_CURSOR = "\x1B[?25h";
static const char* DISABLE_LINE_WRAP = "\x1B[7l"; // DEC: Digital Equipment Corporation
static const char* ENABLE_LINE_WRAP = "\x1B[7h"; enum class DECMode {
kLineWrap = 7,
kMouseX10 = 9,
kCursor = 25,
kMouseVt200 = 1000,
kMouseAnyEvent = 1003,
kMouseUtf8 = 1005,
kMouseSgrExtMode = 1006,
kMouseUrxvtMode = 1015,
kMouseSgrPixelsMode = 1016,
kAlternateScreen = 1049,
};
static const char* USE_ALTERNATIVE_SCREEN = "\x1B[?1049h"; // Device Status Report (DSR) {
static const char* USE_NORMAL_SCREEN = "\x1B[?1049l"; enum class DSRMode {
kCursor = 6,
};
const std::string Serialize(std::vector<DECMode> parameters) {
bool first = true;
std::string out;
for (DECMode parameter : parameters) {
if (!first)
out += ";";
out += std::to_string(int(parameter));
first = false;
}
return out;
}
// DEC Private Mode Set (DECSET)
const std::string Set(std::vector<DECMode> parameters) {
return CSI + "?" + Serialize(parameters) + "h";
}
// DEC Private Mode Reset (DECRST)
const std::string Reset(std::vector<DECMode> parameters) {
return CSI + "?" + Serialize(parameters) + "l";
}
// Device Status Report (DSR)
const std::string DeviceStatusReport(DSRMode ps) {
return CSI + std::to_string(int(ps)) + "n";
}
using SignalHandler = void(int); using SignalHandler = void(int);
std::stack<std::function<void()>> on_exit_functions; std::stack<std::function<void()>> on_exit_functions;
@ -177,6 +221,15 @@ void OnResize(int /* signal */) {
on_resize(); on_resize();
} }
class CapturedMouseImpl : public CapturedMouseInterface {
public:
CapturedMouseImpl(std::function<void(void)> callback) : callback_(callback) {}
~CapturedMouseImpl() override { callback_(); }
private:
std::function<void(void)> callback_;
};
} // namespace } // namespace
ScreenInteractive::ScreenInteractive(int dimx, ScreenInteractive::ScreenInteractive(int dimx,
@ -217,6 +270,14 @@ void ScreenInteractive::PostEvent(Event event) {
event_sender_->Send(event); event_sender_->Send(event);
} }
CapturedMouse ScreenInteractive::CaptureMouse() {
if (mouse_captured)
return nullptr;
mouse_captured = true;
return std::make_unique<CapturedMouseImpl>(
[this] { mouse_captured = false; });
}
void ScreenInteractive::Loop(Component* component) { void ScreenInteractive::Loop(Component* component) {
// Install a SIGINT handler and restore the old handler on exit. // Install a SIGINT handler and restore the old handler on exit.
auto old_sigint_handler = std::signal(SIGINT, OnExit); auto old_sigint_handler = std::signal(SIGINT, OnExit);
@ -274,23 +335,48 @@ void ScreenInteractive::Loop(Component* component) {
install_signal_handler(SIGWINCH, OnResize); install_signal_handler(SIGWINCH, OnResize);
#endif #endif
// Commit state:
auto flush = [&] {
Flush();
on_exit_functions.push([] { Flush(); });
};
auto enable = [&](std::vector<DECMode> parameters) {
std::cout << Set(parameters);
on_exit_functions.push([=] { std::cout << Reset(parameters); });
};
auto disable = [&](std::vector<DECMode> parameters) {
std::cout << Reset(parameters);
on_exit_functions.push([=] { std::cout << Set(parameters); });
};
flush();
if (use_alternative_screen_) { if (use_alternative_screen_) {
std::cout << USE_ALTERNATIVE_SCREEN; enable({
on_exit_functions.push([] { std::cout << USE_NORMAL_SCREEN; }); DECMode::kAlternateScreen,
});
} }
// Hide the cursor and show it at exit. // On exit, reset cursor one line after the current drawing.
std::cout << HIDE_CURSOR; on_exit_functions.push(
std::cout << DISABLE_LINE_WRAP; [=] { std::cout << reset_cursor_position << std::endl; });
Flush();
on_exit_functions.push([&] { disable({
std::cout << reset_cursor_position; DECMode::kCursor,
std::cout << SHOW_CURSOR; DECMode::kLineWrap,
std::cout << ENABLE_LINE_WRAP;
std::cout << std::endl;
Flush();
}); });
enable({
// DECMode::kMouseVt200,
DECMode::kMouseAnyEvent,
DECMode::kMouseUtf8,
DECMode::kMouseSgrExtMode,
});
flush();
auto event_listener = auto event_listener =
std::thread(&EventListener, &quit_, event_receiver_->MakeSender()); std::thread(&EventListener, &quit_, event_receiver_->MakeSender());
@ -298,13 +384,32 @@ void ScreenInteractive::Loop(Component* component) {
while (!quit_) { while (!quit_) {
if (!event_receiver_->HasPending()) { if (!event_receiver_->HasPending()) {
std::cout << reset_cursor_position << ResetPosition(); std::cout << reset_cursor_position << ResetPosition();
static int i = -2;
if (i % 10 == 0)
std::cout << DeviceStatusReport(DSRMode::kCursor);
++i;
Draw(component); Draw(component);
std::cout << ToString() << set_cursor_position; std::cout << ToString() << set_cursor_position;
Flush(); Flush();
Clear(); Clear();
} }
Event event; Event event;
if (event_receiver_->Receive(&event)) if (!event_receiver_->Receive(&event))
break;
if (event.is_cursor_reporting()) {
cursor_x_ = event.cursor_x();
cursor_y_ = event.cursor_y();
continue;
}
if (event.is_mouse()) {
event.mouse().x -= cursor_x_;
event.mouse().y -= cursor_y_;
}
event.screen_ = this;
component->OnEvent(event); component->OnEvent(event);
} }

View File

@ -0,0 +1,119 @@
#include "ftxui/component/slider.hpp"
#include <memory>
#include <utility>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/mouse.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
namespace ftxui {
template <class T>
class SliderImpl : public Component {
public:
SliderImpl(std::wstring label, T* value, T min, T max, T increment)
: label_(label),
value_(value),
min_(min),
max_(max),
increment_(increment) {}
Element Render() {
auto gauge_color =
Focused() ? color(Color::GrayLight) : color(Color::GrayDark);
float percent = float(*value_ - min_) / float(max_ - min_);
return hbox({
text(label_) | dim | vcenter,
hbox({
text(L"["),
gauge(percent) | underlined | xflex | reflect(gauge_box_),
text(L"]"),
}) | xflex,
}) |
gauge_color | xflex | reflect(box_);
}
bool OnEvent(Event event) final {
if (event.is_mouse())
return OnMouseEvent(event);
if (event == Event::ArrowLeft || event == Event::Character('h')) {
*value_ -= increment_;
*value_ = std::max(*value_, min_);
return true;
}
if (event == Event::ArrowRight || event == Event::Character('l')) {
*value_ += increment_;
*value_ = std::min(*value_, max_);
return true;
}
return Component::OnEvent(event);
}
bool OnMouseEvent(Event event) {
if (captured_mouse_ && event.mouse().motion == Mouse::Released) {
captured_mouse_ = nullptr;
return true;
}
if (box_.Contain(event.mouse().x, event.mouse().y) &&
CaptureMouse(event)) {
TakeFocus();
}
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed &&
gauge_box_.Contain(event.mouse().x, event.mouse().y) &&
!captured_mouse_) {
captured_mouse_ = CaptureMouse(event);
}
if (captured_mouse_) {
*value_ = min_ + (event.mouse().x - gauge_box_.x_min) * (max_ - min_) /
(gauge_box_.x_max - gauge_box_.x_min);
*value_ = std::max(min_, std::min(max_, *value_));
return true;
}
return false;
}
private:
std::wstring label_;
T* value_;
T min_;
T max_;
T increment_ = 1;
Box box_;
Box gauge_box_;
CapturedMouse captured_mouse_;
};
template <class T>
ComponentPtr Slider(std::wstring label, T* value, T min, T max, T increment) {
return std::make_unique<SliderImpl<T>>(std::move(label), value, min, max,
increment);
}
template ComponentPtr Slider(std::wstring label,
int* value,
int min,
int max,
int increment);
template ComponentPtr Slider(std::wstring label,
float* value,
float min,
float max,
float increment);
} // 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.

View File

@ -1,5 +1,8 @@
#include "ftxui/component/terminal_input_parser.hpp" #include "ftxui/component/terminal_input_parser.hpp"
#include <utility>
#include "ftxui/component/event.hpp"
namespace ftxui { namespace ftxui {
TerminalInputParser::TerminalInputParser(Sender<Event> out) TerminalInputParser::TerminalInputParser(Sender<Event> out)
@ -30,8 +33,8 @@ bool TerminalInputParser::Eat() {
return position_ < (int)pending_.size(); return position_ < (int)pending_.size();
} }
void TerminalInputParser::Send(TerminalInputParser::Type type) { void TerminalInputParser::Send(TerminalInputParser::Output output) {
switch (type) { switch (output.type) {
case UNCOMPLETED: case UNCOMPLETED:
return; return;
@ -48,10 +51,22 @@ void TerminalInputParser::Send(TerminalInputParser::Type type) {
out_->Send(Event::Special(std::move(pending_))); out_->Send(Event::Special(std::move(pending_)));
pending_.clear(); pending_.clear();
return; return;
case MOUSE:
out_->Send(Event::Mouse(std::move(pending_), output.mouse));
pending_.clear();
return;
case CURSOR_REPORTING:
out_->Send(Event::CursorReporting(std::move(pending_), output.cursor.x,
output.cursor.y));
pending_.clear();
return;
} }
// NOT_REACHED().
} }
TerminalInputParser::Type TerminalInputParser::Parse() { TerminalInputParser::Output TerminalInputParser::Parse() {
if (!Eat()) if (!Eat())
return UNCOMPLETED; return UNCOMPLETED;
@ -75,7 +90,7 @@ TerminalInputParser::Type TerminalInputParser::Parse() {
return ParseUTF8(); return ParseUTF8();
} }
TerminalInputParser::Type TerminalInputParser::ParseUTF8() { TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
unsigned char head = static_cast<unsigned char>(Current()); unsigned char head = static_cast<unsigned char>(Current());
for (int i = 0; i < 3; ++i, head <<= 1) { for (int i = 0; i < 3; ++i, head <<= 1) {
if ((head & 0b11000000) != 0b11000000) if ((head & 0b11000000) != 0b11000000)
@ -86,7 +101,7 @@ TerminalInputParser::Type TerminalInputParser::ParseUTF8() {
return CHARACTER; return CHARACTER;
} }
TerminalInputParser::Type TerminalInputParser::ParseESC() { TerminalInputParser::Output TerminalInputParser::ParseESC() {
if (!Eat()) if (!Eat())
return UNCOMPLETED; return UNCOMPLETED;
switch (Current()) { switch (Current()) {
@ -103,7 +118,7 @@ TerminalInputParser::Type TerminalInputParser::ParseESC() {
} }
} }
TerminalInputParser::Type TerminalInputParser::ParseDCS() { TerminalInputParser::Output TerminalInputParser::ParseDCS() {
// Parse until the string terminator ST. // Parse until the string terminator ST.
while (1) { while (1) {
if (!Eat()) if (!Eat())
@ -122,19 +137,45 @@ TerminalInputParser::Type TerminalInputParser::ParseDCS() {
} }
} }
TerminalInputParser::Type TerminalInputParser::ParseCSI() { TerminalInputParser::Output TerminalInputParser::ParseCSI() {
bool altered = false;
int argument = 0;
std::vector<int> arguments;
while (true) { while (true) {
if (!Eat()) if (!Eat())
return UNCOMPLETED; return UNCOMPLETED;
if (Current() >= '0' && Current() <= '9') if (Current() == '<') {
altered = true;
continue; continue;
}
if (Current() == ';') if (Current() >= '0' && Current() <= '9') {
argument *= 10;
argument += int(Current() - '0');
continue; continue;
}
if (Current() >= ' ' && Current() <= '~') if (Current() == ';') {
arguments.push_back(argument);
argument = 0;
continue;
}
if (Current() >= ' ' && Current() <= '~' && Current() != '<') {
arguments.push_back(argument);
argument = 0;
switch (Current()) {
case 'M':
return ParseMouse(altered, true, std::move(arguments));
case 'm':
return ParseMouse(altered, false, std::move(arguments));
case 'R':
return ParseCursorReporting(std::move(arguments));
default:
return SPECIAL; return SPECIAL;
}
}
// Invalid ESC in CSI. // Invalid ESC in CSI.
if (Current() == '\x1B') if (Current() == '\x1B')
@ -142,7 +183,7 @@ TerminalInputParser::Type TerminalInputParser::ParseCSI() {
} }
} }
TerminalInputParser::Type TerminalInputParser::ParseOSC() { TerminalInputParser::Output TerminalInputParser::ParseOSC() {
// Parse until the string terminator ST. // Parse until the string terminator ST.
while (true) { while (true) {
if (!Eat()) if (!Eat())
@ -156,4 +197,39 @@ TerminalInputParser::Type TerminalInputParser::ParseOSC() {
return SPECIAL; return SPECIAL;
} }
} }
TerminalInputParser::Output TerminalInputParser::ParseMouse(
bool altered,
bool pressed,
std::vector<int> arguments) {
if (arguments.size() != 3)
return SPECIAL;
(void)altered;
Output output(MOUSE);
output.mouse.button = Mouse::Button((arguments[0] & 3) + //
((arguments[0] & 64) >> 4));
output.mouse.motion = Mouse::Motion(pressed);
output.mouse.shift = bool(arguments[0] & 4);
output.mouse.meta = bool(arguments[0] & 8);
output.mouse.x = arguments[1];
output.mouse.y = arguments[2];
return output;
}
TerminalInputParser::Output TerminalInputParser::ParseCursorReporting(
std::vector<int> arguments) {
if (arguments.size() != 2)
return SPECIAL;
Output output(CURSOR_REPORTING);
output.cursor.y = arguments[0];
output.cursor.x = arguments[1];
return output;
}
} // namespace ftxui } // 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.

View File

@ -1,10 +1,13 @@
#ifndef FTXUI_COMPONENT_TERMINAL_INPUT_PARSER #ifndef FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
#define FTXUI_COMPONENT_TERMINAL_INPUT_PARSER #define FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
#include "ftxui/component/event.hpp" #include <memory> // for unique_ptr
#include "ftxui/component/receiver.hpp" #include <string> // for string
#include <vector> // for vector
#include <string> #include "ftxui/component/event.hpp" // IWYU pragma: keep
#include "ftxui/component/mouse.hpp" // for Mouse
#include "ftxui/component/receiver.hpp" // for SenderImpl
namespace ftxui { namespace ftxui {
@ -20,18 +23,38 @@ class TerminalInputParser {
bool Eat(); bool Eat();
enum Type { enum Type {
UNCOMPLETED = 0, UNCOMPLETED,
DROP = 1, DROP,
CHARACTER = 2, CHARACTER,
SPECIAL = 3, SPECIAL,
MOUSE,
CURSOR_REPORTING,
}; };
void Send(Type type);
Type Parse(); struct CursorReporting {
Type ParseUTF8(); int x;
Type ParseESC(); int y;
Type ParseDCS(); };
Type ParseCSI();
Type ParseOSC(); struct Output {
Type type;
union {
Mouse mouse;
CursorReporting cursor;
};
Output(Type type) : type(type) {}
};
void Send(Output type);
Output Parse();
Output ParseUTF8();
Output ParseESC();
Output ParseDCS();
Output ParseCSI();
Output ParseOSC();
Output ParseMouse(bool altered, bool pressed, std::vector<int> arguments);
Output ParseCursorReporting(std::vector<int> arguments);
Sender<Event> out_; Sender<Event> out_;
int position_ = -1; int position_ = -1;

View File

@ -1,7 +1,9 @@
#include "ftxui/component/terminal_input_parser.hpp" #include <gtest/gtest-message.h> // for Message
#include "ftxui/component/receiver.hpp" #include <gtest/gtest-test-part.h> // for TestPartResult
#include "gtest/gtest.h" #include "ftxui/component/receiver.hpp" // for MakeReceiver, ReceiverImpl
#include "ftxui/component/terminal_input_parser.hpp"
#include "gtest/gtest_pred_impl.h" // for AssertionResult, Test, Suite...
using namespace ftxui; using namespace ftxui;
@ -66,6 +68,84 @@ TEST(Event, EscapeKeyEnoughWait) {
EXPECT_FALSE(event_receiver->Receive(&received)); EXPECT_FALSE(event_receiver->Receive(&received));
} }
TEST(Event, MouseLeftClick) {
auto event_receiver = MakeReceiver<Event>();
{
auto parser = TerminalInputParser(event_receiver->MakeSender());
parser.Add('\x1B');
parser.Add('[');
parser.Add('3');
parser.Add('2');
parser.Add(';');
parser.Add('1');
parser.Add('2');
parser.Add(';');
parser.Add('4');
parser.Add('2');
parser.Add('M');
}
Event received;
EXPECT_TRUE(event_receiver->Receive(&received));
EXPECT_TRUE(received.is_mouse());
EXPECT_EQ(Mouse::Left, received.mouse().button);
EXPECT_EQ(12, received.mouse().x);
EXPECT_EQ(42, received.mouse().y);
EXPECT_FALSE(event_receiver->Receive(&received));
}
TEST(Event, MouseMiddleClick) {
auto event_receiver = MakeReceiver<Event>();
{
auto parser = TerminalInputParser(event_receiver->MakeSender());
parser.Add('\x1B');
parser.Add('[');
parser.Add('3');
parser.Add('3');
parser.Add(';');
parser.Add('1');
parser.Add('2');
parser.Add(';');
parser.Add('4');
parser.Add('2');
parser.Add('M');
}
Event received;
EXPECT_TRUE(event_receiver->Receive(&received));
EXPECT_TRUE(received.is_mouse());
EXPECT_EQ(Mouse::Middle, received.mouse().button);
EXPECT_EQ(12, received.mouse().x);
EXPECT_EQ(42, received.mouse().y);
EXPECT_FALSE(event_receiver->Receive(&received));
}
TEST(Event, MouseRightClick) {
auto event_receiver = MakeReceiver<Event>();
{
auto parser = TerminalInputParser(event_receiver->MakeSender());
parser.Add('\x1B');
parser.Add('[');
parser.Add('3');
parser.Add('4');
parser.Add(';');
parser.Add('1');
parser.Add('2');
parser.Add(';');
parser.Add('4');
parser.Add('2');
parser.Add('M');
}
Event received;
EXPECT_TRUE(event_receiver->Receive(&received));
EXPECT_TRUE(received.is_mouse());
EXPECT_EQ(Mouse::Right, received.mouse().button);
EXPECT_EQ(12, received.mouse().x);
EXPECT_EQ(42, received.mouse().y);
EXPECT_FALSE(event_receiver->Receive(&received));
}
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in // Use of this source code is governed by the MIT license that can be found in
// the LICENSE file. // the LICENSE file.

View File

@ -1,29 +1,43 @@
#include "ftxui/component/toggle.hpp" #include <stddef.h> // for size_t
#include <algorithm> // for max, min
#include <memory> // for shared_ptr, alloca...
#include <utility> // for move
#include <algorithm> #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/toggle.hpp"
namespace ftxui { namespace ftxui {
Element Toggle::Render() { Element Toggle::Render() {
bool is_focused = Focused();
Elements children; Elements children;
bool is_toggle_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
// Separator. // Separator.
if (i != 0) if (i != 0)
children.push_back(separator()); children.push_back(separator());
// Entry. bool is_focused = (focused == int(i)) && is_toggle_focused;
auto style = (selected != int(i)) bool is_selected = (selected == int(i));
? normal_style
: is_focused ? focused_style : selected_style; auto style = is_selected
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select; ? (is_focused ? selected_focused_style : selected_style)
children.push_back(text(entries[i]) | style | focused); : (is_focused ? focused_style : normal_style);
auto focus_management = !is_selected ? nothing
: is_toggle_focused ? focus
: select;
children.push_back(text(entries[i]) | style | focus_management |
reflect(boxes_[i]));
} }
return hbox(std::move(children)); return hbox(std::move(children));
} }
bool Toggle::OnEvent(Event event) { bool Toggle::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
int old_selected = selected; int old_selected = selected;
if (event == Event::ArrowLeft || event == Event::Character('h')) if (event == Event::ArrowLeft || event == Event::Character('h'))
selected--; selected--;
@ -37,6 +51,7 @@ bool Toggle::OnEvent(Event event) {
selected = std::max(0, std::min(int(entries.size()) - 1, selected)); selected = std::max(0, std::min(int(entries.size()) - 1, selected));
if (old_selected != selected) { if (old_selected != selected) {
focused = selected;
on_change(); on_change();
return true; return true;
} }
@ -49,6 +64,28 @@ bool Toggle::OnEvent(Event event) {
return false; return false;
} }
bool Toggle::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
TakeFocus();
focused = i;
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
TakeFocus();
if (selected != i) {
selected = i;
on_change();
}
return true;
}
}
return false;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,6 +1,10 @@
#include "ftxui/component/toggle.hpp" #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
#include "gtest/gtest.h" #include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Return, Event::Tab, Event::TabReverse
#include "ftxui/component/mouse.hpp" // for ftxui
#include "ftxui/component/toggle.hpp"
#include "gtest/gtest_pred_impl.h" // for AssertionResult, EXPECT_EQ, Test, EXPECT_TRUE, EXPECT_FALSE, TEST
using namespace ftxui; using namespace ftxui;

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,7 +1,14 @@
#include <algorithm> #include <algorithm> // for max
#include <iterator> // for begin, end
#include <memory> // for make_shared, __shared_ptr_access
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp" // for unpack, Element, Decorator, Elements, border, borderWith, window
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,5 @@
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,7 +1,12 @@
#include <algorithm> #include <algorithm> // for max
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp" // for Element, Elements, dbox
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,10 @@
#include <memory>
#include <vector>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp"
#include "ftxui/dom/requirement.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,8 +1,14 @@
#include <algorithm> #include <algorithm> // for max, min
#include <memory> // for make_shared, shared_ptr, __shared_ptr_access
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp" // for Element, unpack, focus, frame, select, xframe, yframe
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp" // for Node
#include "ftxui/util/autoreset.hpp" #include "ftxui/dom/requirement.hpp" // for Requirement, Requirement::FOCUSED, Requirement::SELECTED
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Screen, Screen::Cursor
#include "ftxui/util/autoreset.hpp" // for AutoReset
namespace ftxui { namespace ftxui {

View File

@ -1,5 +1,9 @@
#include <memory>
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp" #include "ftxui/dom/requirement.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui { namespace ftxui {

View File

@ -1,6 +1,12 @@
#include "ftxui/dom/elements.hpp" #include <gtest/gtest-message.h> // for Message
#include "ftxui/screen/screen.hpp" #include <gtest/gtest-test-part.h> // for TestPartResult
#include "gtest/gtest.h" #include <memory> // for allocator
#include "ftxui/dom/elements.hpp" // for gauge
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/screen.hpp" // for Screen
#include "gtest/gtest_pred_impl.h" // for Test, SuiteApiResolver, EXPECT_EQ
using namespace ftxui; using namespace ftxui;
using namespace ftxui; using namespace ftxui;

View File

@ -1,4 +1,12 @@
#include "ftxui/dom/elements.hpp" #include <functional> // for function
#include <memory> // for make_shared
#include <vector> // for vector
#include "ftxui/dom/elements.hpp" // for GraphFunction, Element, graph
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui { namespace ftxui {

View File

@ -1,7 +1,12 @@
#include <algorithm> #include <algorithm> // for max
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp" // for Element, Elements, hbox
#include "ftxui/dom/node.hpp" #include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui { namespace ftxui {

Some files were not shown because too many files have changed in this diff Show More