FTXUI 7.0.3
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/app.hpp" // for App
8#include "ftxui/component/captured_mouse.hpp" // for ftxui
9#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
10#include "ftxui/component/component_base.hpp" // for ComponentBase
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// 利用可能なスペースを埋めるようにします。
21ButtonOption Style() {
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 int row = 0;
46 auto buttons = Container::Vertical({
47 Container::Horizontal({btn_dec_01, btn_inc_01}, &row) | flex,
48 Container::Horizontal({btn_dec_10, btn_inc_10}, &row) | flex,
49 });
50
51 // スクリーン上でのレンダリング方法を変更する:
52 auto component = Renderer(buttons, [&] {
53 return vbox({
54 text("value = " + std::to_string(value)),
55 separator(),
56 buttons->Render() | flex,
57 }) |
58 flex | border;
59 });
60
61 auto screen = App::FitComponent();
62 screen.Loop(component);
63 return 0;
64}
ButtonOption Style()
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
クリックすると関数を実行するボタンを描画します。
Component Renderer(Component child, std::function< Element()>)
|child| に似た、|render| をComponent::Render()イベントとして 使用する新しいComponentを返します。
Element text(std::string_view text)
UTF8エンコードされたユニコードテキストを表示します。
Element flex(Element child)
子要素をコンテナに残されたスペースに比例して拡大させます。
Definition flex.cpp:93
Element bold(Element child)
より強調したい要素に、太字フォントを使用します。
Definition bold.cpp:33
Element vbox(Elements children)
要素を縦に一つずつ表示するコンテナ。
Definition vbox.cpp:95
FTXUI ftxui::名前空間
Definition animation.hpp:11
const Element & element
Definition node.hpp:94
int value
Definition elements.hpp:192