FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
scrollbar.cpp
Go to the documentation of this file.
1// Copyright 2023 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 <ftxui/component/component.hpp>
5#include <ftxui/component/screen_interactive.hpp>
6#include <string>
7
8 using namespace ftxui;
9
10 Component DummyWindowContent() {
11 class Impl : public ComponentBase {
12 private:
13 float scroll_x = 0.1;
14 float scroll_y = 0.1;
15
16 public:
17 Impl() {
18 auto content = Renderer([=] {
19 const std::string lorem =
20 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
21 "do eiusmod tempor incididunt ut labore et dolore magna "
22 "aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
23 "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis "
24 "aute irure dolor in reprehenderit in voluptate velit esse "
25 "cillum dolore eu fugiat nulla pariatur. Excepteur sint "
26 "occaecat cupidatat non proident, sunt in culpa qui officia "
27 "deserunt mollit anim id est laborum.";
28 return vbox({
29 text(lorem.substr(0, -1)), text(lorem.substr(5, -1)), text(""),
30 text(lorem.substr(10, -1)), text(lorem.substr(15, -1)), text(""),
31 text(lorem.substr(20, -1)), text(lorem.substr(25, -1)), text(""),
32 text(lorem.substr(30, -1)), text(lorem.substr(35, -1)), text(""),
33 text(lorem.substr(40, -1)), text(lorem.substr(45, -1)), text(""),
34 text(lorem.substr(50, -1)), text(lorem.substr(55, -1)), text(""),
35 text(lorem.substr(60, -1)), text(lorem.substr(65, -1)), text(""),
36 text(lorem.substr(70, -1)), text(lorem.substr(75, -1)), text(""),
37 text(lorem.substr(80, -1)), text(lorem.substr(85, -1)), text(""),
38 text(lorem.substr(90, -1)), text(lorem.substr(95, -1)), text(""),
39 text(lorem.substr(100, -1)), text(lorem.substr(105, -1)), text(""),
40 text(lorem.substr(110, -1)), text(lorem.substr(115, -1)), text(""),
41 text(lorem.substr(120, -1)), text(lorem.substr(125, -1)), text(""),
42 text(lorem.substr(130, -1)), text(lorem.substr(135, -1)), text(""),
43 text(lorem.substr(140, -1)),
44 });
45 });
46
47 auto scrollable_content = Renderer(content, [&, content] {
48 return content->Render() | focusPositionRelative(scroll_x, scroll_y) |
49 frame | flex;
50 });
51
52 SliderOption<float> option_x;
53 option_x.value = &scroll_x;
54 option_x.min = 0.f;
55 option_x.max = 1.f;
56 option_x.increment = 0.1f;
57 option_x.direction = Direction::Right;
58 option_x.color_active = Color::Blue;
59 option_x.color_inactive = Color::BlueLight;
60 auto scrollbar_x = Slider(option_x);
61
62 SliderOption<float> option_y;
63 option_y.value = &scroll_y;
64 option_y.min = 0.f;
65 option_y.max = 1.f;
66 option_y.increment = 0.1f;
67 option_y.direction = Direction::Down;
68 option_y.color_active = Color::Yellow;
69 option_y.color_inactive = Color::YellowLight;
70 auto scrollbar_y = Slider(option_y);
71
72 Add(Container::Vertical({
73 Container::Horizontal({
74 scrollable_content,
75 scrollbar_y,
76 }) | flex,
77 Container::Horizontal({
78 scrollbar_x,
79 Renderer([] { return text(L"x"); }),
80 }),
81 }));
82 }
83 };
84 return Make<Impl>();
85}
86
87 int main() {
88 auto window_1 = Window({
89 .inner = DummyWindowContent(),
90 .title = "First window",
91 .width = 80,
92 .height = 30,
93 });
94
95 auto window_2 = Window({
96 .inner = DummyWindowContent(),
97 .title = "My window",
98 .left = 40,
99 .top = 20,
100 .width = 80,
101 .height = 30,
102 });
103
104 auto window_container = Container::Stacked({
105 window_1,
106 window_2,
107 });
108
109 auto screen = ScreenInteractive::Fullscreen();
110 screen.Loop(window_container);
111
112 return EXIT_SUCCESS;
113}
component.hpp
ftxui::ComponentBase::Add
void Add(Component children)
Add a child. @param child The child to be attached.
Definition component.cpp:70
ftxui::ScreenInteractive::Fullscreen
static ScreenInteractive Fullscreen()
Definition screen_interactive.cpp:376
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::Container::Horizontal
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition container.cpp:360
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition src/ftxui/component/renderer.cpp:61
ftxui::Window
Component Window(WindowOptions option)
A draggeable / resizeable window. To use multiple of them, they must be stacked using Container::Stac...
Definition src/ftxui/component/window.cpp:312
ftxui::Container::Stacked
Component Stacked(Components children)
A list of components to be stacked on top of each other. Events are propagated to the first component...
Definition container.cpp:432
ftxui::focusPositionRelative
Decorator focusPositionRelative(float x, float y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definition src/ftxui/dom/focus.cpp:31
ftxui::flex
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::Direction::Down
@ Down
ftxui::Direction::Right
@ Right
ftxui::Color::YellowLight
@ YellowLight
Definition color.hpp:65
ftxui::Color::Yellow
@ Yellow
Definition color.hpp:57
ftxui::Color::Blue
@ Blue
Definition color.hpp:58
ftxui::Color::BlueLight
@ BlueLight
Definition color.hpp:66
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition src/ftxui/component/slider.cpp:346
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
screen_interactive.hpp
DummyWindowContent
Component DummyWindowContent()
Definition scrollbar.cpp:10
main
int main()
Definition scrollbar.cpp:87
ftxui::SliderOption
Definition component_options.hpp:224
ftxui::SliderOption::min
ConstRef< T > min
Definition component_options.hpp:226
ftxui::SliderOption::direction
Direction direction
Definition component_options.hpp:229
ftxui::SliderOption::color_active
Color color_active
Definition component_options.hpp:230
ftxui::SliderOption::increment
ConstRef< T > increment
Definition component_options.hpp:228
ftxui::SliderOption::value
Ref< T > value
Definition component_options.hpp:225
ftxui::SliderOption::max
ConstRef< T > max
Definition component_options.hpp:227
ftxui::SliderOption::color_inactive
Color color_inactive
Definition component_options.hpp:231