mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-17 00:18:11 +08:00
Refactor component containers.
This commit is contained in:
@@ -1,31 +1,29 @@
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
#include "ftxui/component/container.hpp"
|
||||
#include "ftxui/component/input.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentVertical {
|
||||
class MyComponent : public Component {
|
||||
public:
|
||||
MyComponent(Delegate* delegate)
|
||||
: ComponentVertical(delegate),
|
||||
input_1(delegate->NewChild()),
|
||||
input_2(delegate->NewChild()),
|
||||
input_3(delegate->NewChild()) {
|
||||
MyComponent() {
|
||||
Add(&container);
|
||||
container.Add(&input_1);
|
||||
container.Add(&input_2);
|
||||
container.Add(&input_3);
|
||||
|
||||
input_1.placeholder = L"input1";
|
||||
input_2.placeholder = L"input2";
|
||||
input_3.placeholder = L"input3";
|
||||
Focus(&input_1);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = []() {};
|
||||
|
||||
private:
|
||||
Container container = Container::Vertical();
|
||||
Input input_1;
|
||||
Input input_2;
|
||||
Input input_3;
|
||||
@@ -44,7 +42,7 @@ class MyComponent : ComponentVertical {
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent component(screen.delegate());
|
||||
MyComponent component;
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
screen.Loop(&component);
|
||||
}
|
||||
|
@@ -8,10 +8,13 @@
|
||||
int main(int argc, const char* argv[]) {
|
||||
using namespace ftxui;
|
||||
auto screen = ScreenInteractive::FixedSize(30, 3);
|
||||
Menu menu(screen.delegate());
|
||||
|
||||
Menu menu;
|
||||
menu.entries = {L"entry 1", L"entry 2", L"entry 3"};
|
||||
menu.selected = 0;
|
||||
menu.on_enter = screen.ExitLoopClosure();
|
||||
|
||||
screen.Loop();
|
||||
screen.Loop(&menu);
|
||||
|
||||
std::cout << "Selected element = " << menu.selected << std::endl;
|
||||
}
|
||||
|
@@ -2,20 +2,20 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/component/component_horizontal.hpp"
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
#include "ftxui/component/container.hpp"
|
||||
#include "ftxui/component/menu.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentHorizontal {
|
||||
class MyComponent : public Component {
|
||||
public:
|
||||
MyComponent(Delegate* delegate)
|
||||
: ComponentHorizontal(delegate),
|
||||
left_menu(delegate->NewChild()),
|
||||
right_menu(delegate->NewChild()) {
|
||||
MyComponent() {
|
||||
Add(&container);
|
||||
container.Add(&left_menu);
|
||||
container.Add(&right_menu);
|
||||
|
||||
left_menu.entries = {L"0%", L"10%", L"20%", L"30%", L"40%", L"50%",
|
||||
L"60%", L"70%", L"80%", L"90%"};
|
||||
right_menu.entries = {L"0%", L"1%", L"2%", L"3%", L"4%", L"5%",
|
||||
@@ -23,11 +23,11 @@ class MyComponent : ComponentHorizontal {
|
||||
|
||||
left_menu.on_enter = [this]() { on_enter(); };
|
||||
right_menu.on_enter = [this]() { on_enter(); };
|
||||
Focus(&left_menu);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = [](){};
|
||||
private:
|
||||
Container container = Container::Horizontal();
|
||||
Menu left_menu;
|
||||
Menu right_menu;
|
||||
|
||||
@@ -66,7 +66,7 @@ class MyComponent : ComponentHorizontal {
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent component(screen.delegate());
|
||||
MyComponent component;
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
screen.Loop(&component);
|
||||
}
|
||||
|
@@ -1,25 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/component/component_horizontal.hpp"
|
||||
#include "ftxui/component/container.hpp"
|
||||
#include "ftxui/component/menu.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentHorizontal {
|
||||
class MyComponent : public Component {
|
||||
public:
|
||||
MyComponent(Delegate* delegate)
|
||||
: ComponentHorizontal(delegate),
|
||||
menu_1(delegate->NewChild()),
|
||||
menu_2(delegate->NewChild()),
|
||||
menu_3(delegate->NewChild()),
|
||||
menu_4(delegate->NewChild()),
|
||||
menu_5(delegate->NewChild()),
|
||||
menu_6(delegate->NewChild()) {
|
||||
MyComponent() {
|
||||
Add(&container);
|
||||
|
||||
for(Menu* menu : {&menu_1, &menu_2, &menu_3, &menu_4, &menu_5, &menu_6}) {
|
||||
container.Add(menu);
|
||||
menu->entries = {
|
||||
L"Monkey",
|
||||
L"Dog",
|
||||
@@ -46,12 +41,11 @@ class MyComponent : ComponentHorizontal {
|
||||
menu_6.normal_style = dim | color(Color::Blue);
|
||||
menu_6.selected_style = color(Color::Blue);
|
||||
menu_6.active_style = bold | color(Color::Blue);
|
||||
|
||||
Focus(&menu_1);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = [](){};
|
||||
private:
|
||||
Container container = Container::Horizontal();
|
||||
Menu menu_1;
|
||||
Menu menu_2;
|
||||
Menu menu_3;
|
||||
@@ -75,7 +69,7 @@ class MyComponent : ComponentHorizontal {
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent component(screen.delegate());
|
||||
MyComponent component;
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
screen.Loop(&component);
|
||||
}
|
||||
|
@@ -1,28 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
#include "ftxui/component/container.hpp"
|
||||
#include "ftxui/component/menu.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/component/toggle.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentVertical {
|
||||
class MyComponent : public Component {
|
||||
public:
|
||||
MyComponent(Delegate* delegate)
|
||||
: ComponentVertical(delegate),
|
||||
toggle(delegate->NewChild()),
|
||||
menu(delegate->NewChild()) {
|
||||
MyComponent() {
|
||||
Add(&container);
|
||||
container.Add(&toggle);
|
||||
container.Add(&menu);
|
||||
|
||||
toggle.options = {L" left ", L" middle ", L" end "};
|
||||
menu.entries = {L" top ", L" middle ", L" bottom "};
|
||||
Focus(&toggle);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = [](){};
|
||||
private:
|
||||
Container container = Container::Vertical();
|
||||
Toggle toggle;
|
||||
Menu menu;
|
||||
|
||||
@@ -38,7 +38,7 @@ class MyComponent : ComponentVertical {
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent component(screen.delegate());
|
||||
MyComponent component;
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
screen.Loop(&component);
|
||||
}
|
||||
|
@@ -2,33 +2,32 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "ftxui/component/component_horizontal.hpp"
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
#include "ftxui/component/container.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/component/toggle.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
class MyComponent : ComponentVertical {
|
||||
class MyComponent : public Component {
|
||||
public:
|
||||
MyComponent(Delegate* delegate)
|
||||
: ComponentVertical(delegate),
|
||||
toggle_1(delegate->NewChild()),
|
||||
toggle_2(delegate->NewChild()),
|
||||
toggle_3(delegate->NewChild()),
|
||||
toggle_4(delegate->NewChild()) {
|
||||
MyComponent() {
|
||||
Add(&container);
|
||||
container.Add(&toggle_1);
|
||||
container.Add(&toggle_2);
|
||||
container.Add(&toggle_3);
|
||||
container.Add(&toggle_4);
|
||||
|
||||
toggle_1.options = {L"On", L"Off"};
|
||||
toggle_2.options = {L"Enabled", L"Disabled"};
|
||||
toggle_3.options = {L"10€", L"0€"};
|
||||
toggle_4.options = {L"Nothing", L"One element", L"Several elements"};
|
||||
|
||||
Focus(&toggle_1);
|
||||
}
|
||||
|
||||
std::function<void()> on_enter = []() {};
|
||||
|
||||
private:
|
||||
Container container = Container::Vertical();
|
||||
Toggle toggle_1;
|
||||
Toggle toggle_2;
|
||||
Toggle toggle_3;
|
||||
@@ -45,23 +44,11 @@ class MyComponent : ComponentVertical {
|
||||
hbox(text(L" * Number of elements : "), toggle_4.Render())
|
||||
);
|
||||
}
|
||||
|
||||
bool OnEvent(Event event) override {
|
||||
if (ComponentVertical::OnEvent(event))
|
||||
return true;
|
||||
|
||||
if (event == Event::Return) {
|
||||
on_enter();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
MyComponent component(screen.delegate());
|
||||
MyComponent component;
|
||||
component.on_enter = screen.ExitLoopClosure();
|
||||
screen.Loop();
|
||||
screen.Loop(&component);
|
||||
}
|
||||
|
Reference in New Issue
Block a user