File: input_style.cpp

package info (click to toggle)
ftxui 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,084 kB
  • sloc: cpp: 23,669; xml: 211; sh: 25; javascript: 20; python: 16; makefile: 15
file content (97 lines) | stat: -rw-r--r-- 3,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 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.
#include <ftxui/dom/linear_gradient.hpp>  // for LinearGradient
#include <ftxui/screen/color.hpp>  // for Color, Color::White, Color::Red, Color::Blue, Color::Black, Color::GrayDark, ftxui
#include <functional>              // for function
#include <string>                  // for allocator, string
#include <utility>                 // for move

#include "ftxui/component/component.hpp"  // for Input, Horizontal, Vertical, operator|
#include "ftxui/component/component_base.hpp"     // for Component
#include "ftxui/component/component_options.hpp"  // for InputState, InputOption
#include "ftxui/component/screen_interactive.hpp"  // for ScreenInteractive
#include "ftxui/dom/elements.hpp"  // for operator|=, Element, bgcolor, operator|, separatorEmpty, color, borderEmpty, separator, text, center, dim, hbox, vbox, border, borderDouble, borderRounded

int main() {
  using namespace ftxui;

  InputOption style_1 = InputOption::Default();

  InputOption style_2 = InputOption::Spacious();

  InputOption style_3 = InputOption::Spacious();
  style_3.transform = [](InputState state) {
    state.element |= borderEmpty;

    if (state.is_placeholder) {
      state.element |= dim;
    }

    if (state.focused) {
      state.element |= borderDouble;
      state.element |= bgcolor(Color::White);
      state.element |= color(Color::Black);
    } else if (state.hovered) {
      state.element |= borderRounded;
      state.element |= bgcolor(LinearGradient(90, Color::Blue, Color::Red));
      state.element |= color(Color::White);
    } else {
      state.element |= border;
      state.element |= bgcolor(LinearGradient(0, Color::Blue, Color::Red));
      state.element |= color(Color::White);
    }

    return state.element;
  };

  InputOption style_4 = InputOption::Spacious();
  style_4.transform = [](InputState state) {
    state.element = hbox({
        text("Theorem") | center | borderEmpty | bgcolor(Color::Red),
        separatorEmpty(),
        separator() | color(Color::White),
        separatorEmpty(),
        std::move(state.element),
    });

    state.element |= borderEmpty;
    if (state.is_placeholder) {
      state.element |= dim;
    }

    if (state.focused) {
      state.element |= bgcolor(Color::Black);
    } else {
      state.element |= bgcolor(Color::Blue);
    }

    if (state.hovered) {
      state.element |= bgcolor(Color::GrayDark);
    }

    return vbox({state.element, separatorEmpty()});
  };

  auto generateUiFromStyle = [&](InputOption style) {
    auto first_name = new std::string();   // Leaked
    auto middle_name = new std::string();  // Leaked
    auto last_name = new std::string();    // Leaked
    return Container::Vertical({
               Input(first_name, "first name", style),
               Input(middle_name, "middle name", style),
               Input(last_name, "last name", style),
           }) |
           borderEmpty;
  };

  auto ui = Container::Horizontal({
      generateUiFromStyle(style_1),
      generateUiFromStyle(style_2),
      generateUiFromStyle(style_3),
      generateUiFromStyle(style_4),
  });

  auto screen = ScreenInteractive::TerminalOutput();
  screen.Loop(ui);
}