FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
examples/component/button.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <memory> // for shared_ptr, __shared_ptr_access
5#include <string> // for operator+, to_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
12
13using namespace ftxui;
14
15// これは、カスタムスタイルでボタンを作成するためのヘルパー関数です。
16// スタイルは、EntryState を受け取り、
17// Element を返すラムダ関数によって定義されます。
18// ボタン内のテキストを中央に配置するために `center` を使用し、次に `border` を使用して
19// ボタンの周りに境界線を追加し、最後に `flex` を使用してボタンが
20// 利用可能なスペースを埋めるようにします。
22 auto option = ButtonOption::Animated();
23 option.transform = [](const EntryState& s) {
24 auto element = text(s.label);
25 if (s.focused) {
26 element |= bold;
27 }
28 return element | center | borderEmpty | flex;
29 };
30 return option;
31}
32
33int main() {
34 int value = 50;
35
36 // clang-format off
37 auto btn_dec_01 = Button("-1", [&] { value -= 1; }, Style());
38 auto btn_inc_01 = Button("+1", [&] { value += 1; }, Style());
39 auto btn_dec_10 = Button("-10", [&] { value -= 10; }, Style());
40 auto btn_inc_10 = Button("+10", [&] { value += 10; }, Style());
41 // clang-format on
42
43// コンポーネントのツリー。これは、キーボードを使用してナビゲートする方法を定義します。
44// 選択された `row` は、グリッドレイアウトを取得するために共有されます。
45
46 // 画面上のレンダリング方法を変更します。
47 auto component = Renderer(buttons, [&] {
48 return vbox({
49 text("value = " + std::to_string(value)),
50 separator(),
51 buttons->Render() | flex,
52 }) |
53 flex | border;
54 });
55
56 auto screen = ScreenInteractive::FitComponent();
57 screen.Loop(component);
58 return 0;
59}
ButtonOption Style()
static ButtonOption Animated()
アニメーションカラーを使用するButtonOptionを作成します。
static ScreenInteractive FitComponent()
描画されるコンポーネントの幅と高さに一致するScreenInteractiveを作成します。
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked. (ja: ボタンを描画します。クリックされたときに機能を実行します。)
Component Renderer(Component child, std::function< Element()>)
|child|に似ていますが、|render|をComponentRender()イベントとして使用する新しいコンポーネントを返します。
AnimatedButtonコンポーネントのオプション。
virtual void Render(Screen &screen)
要素をftxui::Screenに表示します。
Definition node.cpp:59
Element flex(Element)
子要素をコンテナに残されたスペースに比例して拡大させます。
Definition flex.cpp:120
Element bold(Element)
より強調したい要素に、太字フォントを使用します。
Definition bold.cpp:33
Element text(std::wstring text)
ユニコードテキストを表示します。
Definition text.cpp:160
Element vbox(Elements)
要素を縦に一つずつ表示するコンテナ。
Definition vbox.cpp:94
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
Element separator()
|ButtonOption|、|CheckboxOption|、|RadioboxOption|、|MenuEntryOption|、|MenuOption|からの変換の引数。