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: >
mkdir build;
cd build;
emcmake cmake ..
-DFTXUI_BUILD_TESTS=ON;
emcmake cmake ..;
cmake --build . --config Release;

View File

@ -15,7 +15,7 @@ endif()
project(ftxui
LANGUAGES CXX
VERSION 0.3.${git_version}
VERSION 0.4.${git_version}
)
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_decorator.cpp
src/ftxui/dom/paragraph.cpp
src/ftxui/dom/reflect.cpp
src/ftxui/dom/separator.cpp
src/ftxui/dom/size.cpp
src/ftxui/dom/spinner.cpp
@ -76,15 +77,18 @@ add_library(dom
add_library(component
include/ftxui/component/button.hpp
include/ftxui/component/captured_mouse.hpp
include/ftxui/component/checkbox.hpp
include/ftxui/component/component.hpp
include/ftxui/component/container.hpp
include/ftxui/component/event.hpp
include/ftxui/component/input.hpp
include/ftxui/component/menu.hpp
include/ftxui/component/mouse.hpp
include/ftxui/component/radiobox.hpp
include/ftxui/component/receiver.hpp
include/ftxui/component/screen_interactive.hpp
include/ftxui/component/slider.hpp
include/ftxui/component/toggle.hpp
src/ftxui/component/button.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/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.hpp
src/ftxui/component/toggle.cpp
)
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(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)
target_include_directories(${lib}
@ -199,7 +212,7 @@ if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4")
FetchContent_Declare( googletest
GIT_REPOSITORY "https://github.com/google/googletest"
GIT_TAG release-1.10.0
GIT_TAG 23ef29555ef4789f555f1ba8c51b4c52975f0907
)
FetchContent_GetProperties(googletest)

View File

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

View File

@ -13,9 +13,10 @@ example(input)
example(menu)
example(menu2)
example(menu_style)
example(modal_dialog)
example(radiobox)
example(radiobox_in_frame)
example(slider)
example(tab_horizontal)
example(tab_vertical)
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/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
#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/screen/box.hpp" // for ftxui
using namespace ftxui;

View File

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

View File

@ -1,10 +1,15 @@
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
#include <memory> // for allocator_traits<>...
#include <string> // for operator+, wstring
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/checkbox.hpp" // for CheckBox
#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;

View File

@ -1,11 +1,20 @@
#include "ftxui/component/button.hpp"
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include <functional> // for function
#include <memory> // for allocator, unique_ptr
#include <string> // for wstring
#include <vector> // for vector
#include "ftxui/component/button.hpp" // for Button
#include "ftxui/component/checkbox.hpp" // for CheckBox
#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;
@ -20,6 +29,13 @@ class MyComponent : public Component {
Input input;
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:
MyComponent() {
Add(&container);
@ -54,17 +70,26 @@ class MyComponent : public Component {
input.placeholder = L"Input placeholder";
container.Add(&input);
container.Add(slider_1_.get());
container.Add(slider_2_.get());
container.Add(slider_3_.get());
button.label = L"Quit";
button.on_click = [&] { on_quit(); };
container.Add(&button);
}
Element Render(std::wstring name, Component& component) {
Element Render(std::wstring name, Element element) {
return hbox({
text(name) | size(WIDTH, EQUAL, 8),
separator(),
component.Render(),
});
element | xflex,
}) |
xflex;
}
Element Render(std::wstring name, Component& component) {
return Render(name, component.Render());
}
Element Render() override {
@ -78,11 +103,18 @@ class MyComponent : public Component {
separator(),
Render(L"radiobox", radiobox),
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(),
Render(L"button", button),
}) |
border;
xflex | size(WIDTH, GREATER_THAN, 40) | border;
}
std::function<void()> on_quit = [] {};

View File

@ -1,14 +1,23 @@
#include <cmath>
#include <thread>
#include <chrono> // for operator""s, chron...
#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/container.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/checkbox.hpp" // for CheckBox
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Custom
#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/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;

View File

@ -1,10 +1,6 @@
#include "ftxui/component/input.hpp"
#include <iostream>
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
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 <iostream>
#include <thread>
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/screen/box.hpp" // for ftxui
int main(int argc, const char* argv[]) {
using namespace ftxui;

View File

@ -1,11 +1,14 @@
#include <chrono>
#include <iostream>
#include <thread>
#include <functional> // for function
#include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for Menu
#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;

View File

@ -1,10 +1,15 @@
#include <iostream>
#include <thread>
#include <functional> // for function
#include <initializer_list> // for initializer_list
#include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for Menu
#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;
@ -13,7 +18,14 @@ class MyComponent : public Component {
MyComponent() {
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);
menu->entries = {
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_2.selected_style = 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.focused_style = bgcolor(Color::Blue);
menu_3.selected_focused_style = bgcolor(Color::Blue);
menu_4.selected_style = bgcolor(Color::Blue);
menu_4.focused_style = bgcolor(Color::BlueLight);
menu_4.selected_focused_style = bgcolor(Color::BlueLight);
menu_5.normal_style = bgcolor(Color::Blue);
menu_5.selected_style = bgcolor(Color::Yellow);
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.selected_style = 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 = []() {};

View File

@ -1,7 +1,14 @@
#include "ftxui/component/button.hpp"
#include "ftxui/component/component.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <functional> // for function
#include <memory> // for allocator_traits<>...
#include <string> // for operator+, wstring
#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;

View File

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

View File

@ -1,11 +1,12 @@
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
#include <string> // for wstring, operator+
#include <vector> // for vector
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/radiobox.hpp" // for RadioBox
#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;

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 <thread>
#include <functional> // for function
#include <string> // for wstring, allocator
#include <vector> // for vector
#include "ftxui/component/container.hpp"
#include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/radiobox.hpp" // for RadioBox
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#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;

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/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for Menu
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/screen/box.hpp" // for ftxui
using namespace ftxui;

View File

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

View File

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

View File

@ -1,14 +1,16 @@
#include <cmath>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/color_info.hpp>
#include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp>
#include <iostream>
#include "ftxui/screen/string.hpp"
#include <memory>
#include <utility>
#include <vector>
using namespace ftxui;
#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[]) {
// clang-format off

View File

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

View File

@ -1,10 +1,11 @@
#include <cmath>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp>
#include <iostream>
#include <memory>
#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[]) {
using namespace ftxui;

View File

@ -1,10 +1,11 @@
#include <cmath>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
#include <ftxui/screen/terminal.hpp>
#include <iostream>
#include <memory>
#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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

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

View File

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

View File

@ -1,7 +1,11 @@
#include <stddef.h>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.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[]) {
using namespace ftxui;

View File

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

View File

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

View File

@ -1,7 +1,10 @@
#include <stdio.h>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,7 +1,12 @@
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.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[]) {
using namespace ftxui;

View File

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

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,9 @@
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <stdio.h>
#include <ftxui/dom/elements.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[]) {
using namespace ftxui;

View File

@ -1,6 +1,10 @@
#include <ftxui/dom/elements.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) {
using namespace ftxui;

View File

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

View File

@ -2,32 +2,83 @@
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <chrono>
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/screen/string.hpp>
#include <iostream>
#include <thread>
#include <stddef.h> // for size_t
#include <algorithm> // for max
#include <ftxui/component/component.hpp> // for Component
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include <string> // for allocator, operator+
#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;
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 {
public:
~DrawKey() override = default;
Element Render() override {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) {
std::wstring code;
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));
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(Stringify(keys[i])));
}
return window(text(L"keys"), vbox(std::move(children)));
}

View File

@ -2,10 +2,14 @@
#define FTXUI_COMPONENT_BUTTON_HPP
#include <functional>
#include <string>
#include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui {
struct Event;
/// @brief A button. An action is associated to the click event.
/// @ingroup dom
@ -25,6 +29,9 @@ class Button : public Component {
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
Box box_;
};
} // 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
#define FTXUI_COMPONENT_CHECKBOX_HPP
#include <functional>
#include <string>
#include "ftxui/component/component.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui {
struct Event;
/// @brief A Checkbox. It can be checked or unchecked.Display an element on a
/// ftxui::Screen.
@ -38,7 +40,10 @@ class CheckBox : public Component {
bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event event);
int cursor_position = 0;
Box box_;
};
} // namespace ftxui

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,19 +1,18 @@
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
#include <atomic>
#include <condition_variable>
#include <atomic> // for atomic
#include <ftxui/component/receiver.hpp>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <memory> // for unique_ptr
#include <string> // for string
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp"
#include "ftxui/screen/screen.hpp"
#include "ftxui/screen/screen.hpp" // for Screen
namespace ftxui {
class Component;
struct Event;
class ScreenInteractive : public Screen {
public:
@ -27,6 +26,7 @@ class ScreenInteractive : public Screen {
std::function<void()> ExitLoopClosure();
void PostEvent(Event event);
CapturedMouse CaptureMouse();
private:
void Draw(Component* component);
@ -52,6 +52,11 @@ class ScreenInteractive : public Screen {
std::string reset_cursor_position;
std::atomic<bool> quit_ = false;
int cursor_x_ = 0;
int cursor_y_ = 0;
bool mouse_captured = false;
};
} // 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_
#define FTXUI_COMPONENT_TOGGLE_H_
#include <functional>
#include <string>
#include <vector>
#include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/box.hpp"
namespace ftxui {
struct Event;
/// @brief An horizontal list of elements. The user can navigate through them.
/// @ingroup component
@ -16,12 +19,14 @@ class Toggle : public Component {
~Toggle() override = default;
// State.
int selected = 0;
std::vector<std::wstring> entries = {L"On", L"Off"};
int selected = 0;
int focused = 0;
Decorator normal_style = dim;
Decorator focused_style = inverted;
Decorator selected_style = bold;
Decorator normal_style = dim;
Decorator selected_focused_style = focused_style | selected_style;
// Callback.
std::function<void()> on_change = []() {};
@ -30,6 +35,10 @@ class Toggle : public Component {
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
bool OnMouseEvent(Event event);
std::vector<Box> boxes_;
};
} // namespace ftxui

View File

@ -5,6 +5,7 @@
#include <memory>
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp"
#include "ftxui/screen/screen.hpp"
@ -77,6 +78,9 @@ enum Direction { WIDTH, HEIGHT };
enum Constraint { LESS_THAN, EQUAL, GREATER_THAN };
Decorator size(Direction, Constraint, int value);
// --
Decorator reflect(Box& box);
// --- Frame ---
// 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

View File

@ -1,16 +1,18 @@
#ifndef FTXUI_DOM_NODE_HPP
#define FTXUI_DOM_NODE_HPP
#include <memory>
#include <vector>
#include <memory> // for shared_ptr
#include <vector> // for vector
#include "ftxui/dom/requirement.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp"
namespace ftxui {
class Node;
class Screen;
using Element = 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>
template <class T>

View File

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

View File

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

View File

@ -1,6 +1,7 @@
#ifndef FTXUI_SCREEN_COLOR_INFO_HPP
#define FTXUI_SCREEN_COLOR_INFO_HPP
#include <stdint.h>
#include <ftxui/screen/color.hpp>
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 {
Element Button::Render() {
if (Focused())
return text(label) | border | inverted;
else
return text(label) | border;
auto style = Focused() ? inverted : nothing;
return text(label) | border | style | reflect(box_);
}
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) {
on_click();
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 {
@ -9,10 +14,14 @@ Element CheckBox::Render() {
auto style = is_focused ? focused_style : unfocused_style;
auto focus_management = is_focused ? focus : state ? select : nothing;
return hbox(text(state ? checked : unchecked),
text(label) | style | focus_management);
text(label) | style | focus_management) |
reflect(box_);
}
bool CheckBox::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
if (event == Event::Character(' ') || event == Event::Return) {
state = !state;
on_change();
@ -21,6 +30,24 @@ bool CheckBox::OnEvent(Event event) {
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
// Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,11 +1,20 @@
#include "ftxui/component/component.hpp"
#include <assert.h>
#include <algorithm>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/event.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui {
namespace {
class CaptureMouseImpl : public CapturedMouseInterface {
public:
~CaptureMouseImpl() override {}
};
}
Component::~Component() {
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.
/// @see Attach
/// @see Detach

View File

@ -1,6 +1,8 @@
#include "ftxui/component/container.hpp"
#include <stddef.h>
#include <algorithm>
#include <vector>
namespace ftxui {
@ -30,6 +32,9 @@ Container Container::Tab(int* selector) {
}
bool Container::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused())
return false;
@ -115,6 +120,17 @@ Element Container::TabRender() {
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
// 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;

View File

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

View File

@ -1,8 +1,11 @@
#include "ftxui/component/input.hpp"
#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 {
@ -15,14 +18,15 @@ Element Input::Render() {
// Placeholder.
if (content.size() == 0) {
if (is_focused)
return text(placeholder) | focus | dim | inverted | main_decorator;
return text(placeholder) | focus | dim | inverted | main_decorator |
reflect(input_box_);
else
return text(placeholder) | dim | main_decorator;
return text(placeholder) | dim | main_decorator | reflect(input_box_);
}
// Not 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_at_cursor = cursor_position < (int)content.size()
@ -37,13 +41,18 @@ Element Input::Render() {
return
hbox(
text(part_before_cursor),
text(part_at_cursor) | underlined | focused,
text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
text(part_after_cursor)
) | flex | inverted | frame | main_decorator;
// clang-format off
) | flex | inverted | frame | main_decorator | reflect(input_box_);
// clang-format on
}
bool Input::OnEvent(Event event) {
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
if (event.is_mouse())
return OnMouseEvent(event);
std::wstring c;
// Backspace.
@ -105,6 +114,28 @@ bool Input::OnEvent(Event event) {
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
// Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

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

View File

@ -1,25 +1,43 @@
#include "ftxui/component/menu.hpp"
#include <stddef.h>
#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 {
Element Menu::Render() {
std::vector<Element> elements;
bool is_focused = Focused();
Elements elements;
bool is_menu_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto style = (selected != int(i))
? normal_style
: is_focused ? focused_style : selected_style;
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select;
auto icon = (selected != int(i)) ? L" " : L"> ";
elements.push_back(text(icon + entries[i]) | style | focused);
bool is_focused = (focused == int(i)) && is_menu_focused;
bool is_selected = (selected == int(i));
auto style = is_selected
? (is_focused ? selected_focused_style : selected_style)
: (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));
}
bool Menu::OnEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused())
return false;
@ -36,6 +54,7 @@ bool Menu::OnEvent(Event event) {
selected = std::max(0, std::min(int(entries.size()) - 1, selected));
if (selected != old_selected) {
focused = selected;
on_change();
return true;
}
@ -48,6 +67,27 @@ bool Menu::OnEvent(Event event) {
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
// Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -1,27 +1,41 @@
#include "ftxui/component/radiobox.hpp"
#include <stddef.h>
#include <algorithm>
#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 {
Element RadioBox::Render() {
std::vector<Element> elements;
bool is_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto style =
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
auto focus_management =
(focused != int(i)) ? nothing : is_focused ? focus : select;
auto focus_management = (focused != int(i)) ? nothing
: is_focused ? focus
: select;
const std::wstring& symbol = selected == int(i) ? checked : unchecked;
elements.push_back(hbox(text(symbol), text(entries[i]) | style) |
focus_management);
focus_management | reflect(boxes_[i]));
}
return vbox(std::move(elements));
}
bool RadioBox::OnEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (event.is_mouse())
return OnMouseEvent(event);
if (!Focused())
return false;
@ -50,6 +64,30 @@ bool RadioBox::OnEvent(Event event) {
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
// 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;

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 <thread>
#include "gtest/gtest.h"
#include "gtest/gtest_pred_impl.h" // for AssertionResult, Test, EXPECT_EQ
using namespace ftxui;

View File

@ -1,18 +1,24 @@
#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 <csignal>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <thread>
#include "ftxui/component/component.hpp"
#include "ftxui/component/terminal_input_parser.hpp"
#include "ftxui/screen/string.hpp"
#include "ftxui/screen/terminal.hpp"
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse
#include "ftxui/component/receiver.hpp" // for ReceiverImpl
#include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputPa...
#include "ftxui/dom/node.hpp" // for Node, Render
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/terminal.hpp" // for Terminal::Dimen...
#if defined(_WIN32)
#define DEFINE_CONSOLEV2_PROPERTIES
@ -25,8 +31,9 @@
#error Must be compiled in UNICODE mode
#endif
#else
#include <termios.h>
#include <unistd.h>
#include <sys/select.h> // for select, FD_ISSET
#include <termios.h> // for tcsetattr, tcge...
#include <unistd.h> // for STDIN_FILENO, read
#endif
// Quick exit is missing in standard CLang headers
@ -40,15 +47,14 @@ namespace {
void 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_microseconds = timeout_milliseconds * 1000;
#if defined(_WIN32)
void EventListener(std::atomic<bool>* quit,
Sender<Event> out) {
void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
auto console = GetStdHandle(STD_INPUT_HANDLE);
auto parser = TerminalInputParser(out->Clone());
while (!*quit) {
@ -68,8 +74,7 @@ void EventListener(std::atomic<bool>* quit,
std::vector<INPUT_RECORD> records{number_of_events};
DWORD number_of_events_read = 0;
ReadConsoleInput(console, records.data(),
(DWORD)records.size(),
ReadConsoleInput(console, records.data(), (DWORD)records.size(),
&number_of_events_read);
records.resize(number_of_events_read);
@ -114,7 +119,7 @@ void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
}
#else
#include <sys/time.h>
#include <sys/time.h> // for timeval
int CheckStdinReady(int usec_timeout) {
timeval tv = {0, usec_timeout};
@ -146,14 +151,53 @@ void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
#endif
static const char* HIDE_CURSOR = "\x1B[?25l";
static const char* SHOW_CURSOR = "\x1B[?25h";
const std::string CSI = "\x1b[";
static const char* DISABLE_LINE_WRAP = "\x1B[7l";
static const char* ENABLE_LINE_WRAP = "\x1B[7h";
// DEC: Digital Equipment Corporation
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";
static const char* USE_NORMAL_SCREEN = "\x1B[?1049l";
// Device Status Report (DSR) {
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);
std::stack<std::function<void()>> on_exit_functions;
@ -177,6 +221,15 @@ void OnResize(int /* signal */) {
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
ScreenInteractive::ScreenInteractive(int dimx,
@ -217,6 +270,14 @@ void ScreenInteractive::PostEvent(Event 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) {
// Install a SIGINT handler and restore the old handler on exit.
auto old_sigint_handler = std::signal(SIGINT, OnExit);
@ -274,23 +335,48 @@ void ScreenInteractive::Loop(Component* component) {
install_signal_handler(SIGWINCH, OnResize);
#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_) {
std::cout << USE_ALTERNATIVE_SCREEN;
on_exit_functions.push([] { std::cout << USE_NORMAL_SCREEN; });
enable({
DECMode::kAlternateScreen,
});
}
// Hide the cursor and show it at exit.
std::cout << HIDE_CURSOR;
std::cout << DISABLE_LINE_WRAP;
Flush();
on_exit_functions.push([&] {
std::cout << reset_cursor_position;
std::cout << SHOW_CURSOR;
std::cout << ENABLE_LINE_WRAP;
std::cout << std::endl;
Flush();
// On exit, reset cursor one line after the current drawing.
on_exit_functions.push(
[=] { std::cout << reset_cursor_position << std::endl; });
disable({
DECMode::kCursor,
DECMode::kLineWrap,
});
enable({
// DECMode::kMouseVt200,
DECMode::kMouseAnyEvent,
DECMode::kMouseUtf8,
DECMode::kMouseSgrExtMode,
});
flush();
auto event_listener =
std::thread(&EventListener, &quit_, event_receiver_->MakeSender());
@ -298,13 +384,32 @@ void ScreenInteractive::Loop(Component* component) {
while (!quit_) {
if (!event_receiver_->HasPending()) {
std::cout << reset_cursor_position << ResetPosition();
static int i = -2;
if (i % 10 == 0)
std::cout << DeviceStatusReport(DSRMode::kCursor);
++i;
Draw(component);
std::cout << ToString() << set_cursor_position;
Flush();
Clear();
}
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);
}

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 <utility>
#include "ftxui/component/event.hpp"
namespace ftxui {
TerminalInputParser::TerminalInputParser(Sender<Event> out)
@ -30,8 +33,8 @@ bool TerminalInputParser::Eat() {
return position_ < (int)pending_.size();
}
void TerminalInputParser::Send(TerminalInputParser::Type type) {
switch (type) {
void TerminalInputParser::Send(TerminalInputParser::Output output) {
switch (output.type) {
case UNCOMPLETED:
return;
@ -48,10 +51,22 @@ void TerminalInputParser::Send(TerminalInputParser::Type type) {
out_->Send(Event::Special(std::move(pending_)));
pending_.clear();
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())
return UNCOMPLETED;
@ -75,7 +90,7 @@ TerminalInputParser::Type TerminalInputParser::Parse() {
return ParseUTF8();
}
TerminalInputParser::Type TerminalInputParser::ParseUTF8() {
TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
unsigned char head = static_cast<unsigned char>(Current());
for (int i = 0; i < 3; ++i, head <<= 1) {
if ((head & 0b11000000) != 0b11000000)
@ -86,7 +101,7 @@ TerminalInputParser::Type TerminalInputParser::ParseUTF8() {
return CHARACTER;
}
TerminalInputParser::Type TerminalInputParser::ParseESC() {
TerminalInputParser::Output TerminalInputParser::ParseESC() {
if (!Eat())
return UNCOMPLETED;
switch (Current()) {
@ -103,7 +118,7 @@ TerminalInputParser::Type TerminalInputParser::ParseESC() {
}
}
TerminalInputParser::Type TerminalInputParser::ParseDCS() {
TerminalInputParser::Output TerminalInputParser::ParseDCS() {
// Parse until the string terminator ST.
while (1) {
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) {
if (!Eat())
return UNCOMPLETED;
if (Current() >= '0' && Current() <= '9')
if (Current() == '<') {
altered = true;
continue;
}
if (Current() == ';')
if (Current() >= '0' && Current() <= '9') {
argument *= 10;
argument += int(Current() - '0');
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;
}
}
// Invalid ESC in CSI.
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.
while (true) {
if (!Eat())
@ -156,4 +197,39 @@ TerminalInputParser::Type TerminalInputParser::ParseOSC() {
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
// 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
#define FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
#include "ftxui/component/event.hpp"
#include "ftxui/component/receiver.hpp"
#include <memory> // for unique_ptr
#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 {
@ -20,18 +23,38 @@ class TerminalInputParser {
bool Eat();
enum Type {
UNCOMPLETED = 0,
DROP = 1,
CHARACTER = 2,
SPECIAL = 3,
UNCOMPLETED,
DROP,
CHARACTER,
SPECIAL,
MOUSE,
CURSOR_REPORTING,
};
void Send(Type type);
Type Parse();
Type ParseUTF8();
Type ParseESC();
Type ParseDCS();
Type ParseCSI();
Type ParseOSC();
struct CursorReporting {
int x;
int y;
};
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_;
int position_ = -1;

View File

@ -1,7 +1,9 @@
#include "ftxui/component/terminal_input_parser.hpp"
#include "ftxui/component/receiver.hpp"
#include <gtest/gtest-message.h> // for Message
#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;
@ -66,6 +68,84 @@ TEST(Event, EscapeKeyEnoughWait) {
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.
// Use of this source code is governed by the MIT license that can be found in
// 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 {
Element Toggle::Render() {
bool is_focused = Focused();
Elements children;
bool is_toggle_focused = Focused();
boxes_.resize(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
// Separator.
if (i != 0)
children.push_back(separator());
// Entry.
auto style = (selected != int(i))
? normal_style
: is_focused ? focused_style : selected_style;
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select;
children.push_back(text(entries[i]) | style | focused);
bool is_focused = (focused == int(i)) && is_toggle_focused;
bool is_selected = (selected == int(i));
auto style = is_selected
? (is_focused ? selected_focused_style : selected_style)
: (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));
}
bool Toggle::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
int old_selected = selected;
if (event == Event::ArrowLeft || event == Event::Character('h'))
selected--;
@ -37,6 +51,7 @@ bool Toggle::OnEvent(Event event) {
selected = std::max(0, std::min(int(entries.size()) - 1, selected));
if (old_selected != selected) {
focused = selected;
on_change();
return true;
}
@ -49,6 +64,28 @@ bool Toggle::OnEvent(Event event) {
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
// 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;

View File

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

View File

@ -1,5 +1,10 @@
#include <memory>
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp"
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
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/node.hpp"
#include "ftxui/dom/elements.hpp" // for unpack, Element, Decorator, Elements, border, borderWith, window
#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 {

View File

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

View File

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

View File

@ -1,5 +1,5 @@
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
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/node.hpp"
#include "ftxui/dom/elements.hpp" // for Element, Elements, dbox
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui {

View File

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

View File

@ -1,5 +1,10 @@
#include <memory>
#include <vector>
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/requirement.hpp"
#include "ftxui/screen/box.hpp"
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/node.hpp"
#include "ftxui/util/autoreset.hpp"
#include "ftxui/dom/elements.hpp" // for Element, unpack, focus, frame, select, xframe, yframe
#include "ftxui/dom/node.hpp" // for Node
#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 {

View File

@ -1,5 +1,9 @@
#include <memory>
#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 {

View File

@ -1,6 +1,12 @@
#include "ftxui/dom/elements.hpp"
#include "ftxui/screen/screen.hpp"
#include "gtest/gtest.h"
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult
#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;

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 {

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/node.hpp"
#include "ftxui/dom/elements.hpp" // for Element, Elements, hbox
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui {

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