はじめに
このガイドでは、プロジェクトに FTXUI を追加し、静的なレイアウトを描画して、小さなインタラクティブアプリケーションを構築するまでの手順を解説します。
1. プロジェクトに FTXUI を追加する
cmake
include(FetchContent)
FetchContent_Declare(ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/FTXUI
GIT_TAG v7.0.3
)
FetchContent_MakeAvailable(ftxui)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE ftxui::ftxui)cpp
// Download ftxui_all.hpp from the GitHub release artifacts:
#define FTXUI_IMPLEMENTATION
#include "ftxui_all.hpp"python
bazel_dep(name = "ftxui", version = "7.0.3")ini
[wrap-git]
url = https://github.com/ArthurSonzogni/FTXUI.git
revision = v7.0.3json
{
"dependencies": ["ftxui"]
}ini
[requires]
ftxui/7.0.3
[generators]
CMakeDeps
CMakeToolchainより詳しい手順については、インストールガイドを参照してください。
2. 静的なレイアウト (DOM)
hbox、vbox、およびパイプで連結できるデコレータ(| border、| bold、| flex など)を組み合わせて、宣言的にドキュメントを構築します。
cpp
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
int main() {
using namespace ftxui;
Element title = text("FTXUI Dashboard") | bold | center;
Element left_panel = text("Left Panel") | border;
Element right_panel = vbox({
text("Main Content Area") | flex,
separator(),
text("Status: Ready") | dim,
})
| border
| flex;
Element document = vbox({
title,
separator(),
hbox({left_panel, right_panel}) | flex,
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document);
screen.Print();
}3. インタラクティブ機能の追加 (Component)
通常の C++ の状態変数を Menu、Input、Button などのコンポーネントでラップし、App のイベントループ上で実行します。
cpp
#include <ftxui/component/app.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/dom/elements.hpp>
int main() {
using namespace ftxui;
std::vector<std::string> entries = {"Dashboard", "Settings", "Logs"};
int selected = 0;
auto menu = Menu(&entries, &selected) | border;
auto app = App::TerminalOutput();
app.Loop(menu);
}4. レイアウト、ショートカット、モーダルの組み合わせ
イベントとフォーカスを制御する Container、独自のレイアウトを組む Renderer(container, ...)、キーボードショートカットを処理する CatchEvent を組み合わせます。
cpp
#include <ftxui/component/app.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/dom/elements.hpp>
int main() {
using namespace ftxui;
auto app = App::Fullscreen();
std::string name;
bool show_about = false;
auto input = Input(&name, "Enter your name...");
auto about_btn = Button("About", [&] { show_about = true; });
auto quit_btn = Button("Quit", app.ExitLoopClosure());
auto buttons = Container::Horizontal({about_btn, quit_btn});
auto controls = Container::Vertical({input, buttons});
auto main_ui = Renderer(controls, [&] {
auto greeting =
text("Hello " + (name.empty() ? "FTXUI" : name) + "!") | bold | hcenter;
auto input_row = hbox({text(" Name: "), input->Render()});
auto button_row = hbox({about_btn->Render(), quit_btn->Render()}) | hcenter;
return vbox({
greeting,
separator(),
input_row,
separator(),
button_row,
})
| border
| center;
});
auto modal = Button("Close (Esc)", [&] { show_about = false; })
| border
| clear_under;
auto root = main_ui
| Modal(modal, &show_about)
| CatchEvent([&](Event e) {
if (show_about && e == Event::Escape) {
show_about = false;
return true;
}
return false;
});
app.Loop(root);
}次のステップ
- メンタルモデル: Element、Component、イベントルーティングの仕組み。
- クックブック: バックグラウンドスレッド、スクロール、動的リスト、モーダルなどの実践パターン。
- Elements および Components の API リファレンス。
- ftxui-starter テンプレートリポジトリ、または
/llms.txt//llms-full.txt。