Skip to content

Commit f353474

Browse files
Add scrollbar example.
1 parent fbd56cd commit f353474

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

examples/component/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ example(radiobox)
3838
example(radiobox_in_frame)
3939
example(renderer)
4040
example(resizable_split)
41+
example(scrollbar)
4142
example(slider)
4243
example(slider_direction)
4344
example(slider_rgb)

examples/component/scrollbar.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
7+
using namespace ftxui;
8+
9+
Component DummyWindowContent() {
10+
class Impl : public ComponentBase {
11+
private:
12+
float scroll_x = 0.9;
13+
float scroll_y = 0.1;
14+
15+
public:
16+
Impl() {
17+
auto content = Renderer([=] {
18+
const std::string lorem =
19+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
20+
"do eiusmod tempor incididunt ut labore et dolore magna "
21+
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
22+
"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis "
23+
"aute irure dolor in reprehenderit in voluptate velit esse "
24+
"cillum dolore eu fugiat nulla pariatur. Excepteur sint "
25+
"occaecat cupidatat non proident, sunt in culpa qui officia "
26+
"deserunt mollit anim id est laborum.";
27+
return vbox({
28+
text(lorem.substr(0, -1)), text(lorem.substr(5, -1)), text(""),
29+
text(lorem.substr(10, -1)), text(lorem.substr(15, -1)), text(""),
30+
text(lorem.substr(20, -1)), text(lorem.substr(25, -1)), text(""),
31+
text(lorem.substr(30, -1)), text(lorem.substr(35, -1)), text(""),
32+
text(lorem.substr(40, -1)), text(lorem.substr(45, -1)), text(""),
33+
text(lorem.substr(50, -1)), text(lorem.substr(55, -1)), text(""),
34+
text(lorem.substr(60, -1)), text(lorem.substr(65, -1)), text(""),
35+
text(lorem.substr(70, -1)), text(lorem.substr(75, -1)), text(""),
36+
text(lorem.substr(80, -1)), text(lorem.substr(85, -1)), text(""),
37+
text(lorem.substr(90, -1)), text(lorem.substr(95, -1)), text(""),
38+
text(lorem.substr(100, -1)), text(lorem.substr(105, -1)), text(""),
39+
text(lorem.substr(110, -1)), text(lorem.substr(115, -1)), text(""),
40+
text(lorem.substr(120, -1)), text(lorem.substr(125, -1)), text(""),
41+
text(lorem.substr(130, -1)), text(lorem.substr(135, -1)), text(""),
42+
text(lorem.substr(140, -1)),
43+
});
44+
});
45+
46+
auto scrollable_content = Renderer(content, [&, content] {
47+
return content->Render() |
48+
focusPositionRelative(1.f - scroll_x, scroll_y) | frame | flex;
49+
});
50+
51+
SliderOption<float> option_x;
52+
option_x.value = &scroll_x;
53+
option_x.min = 0.f;
54+
option_x.max = 1.f;
55+
option_x.increment = 0.1f;
56+
option_x.direction = Direction::Left;
57+
option_x.color_active = Color::Blue;
58+
option_x.color_inactive = Color::BlueLight;
59+
auto scrollbar_x = Slider(option_x);
60+
61+
SliderOption<float> option_y;
62+
option_y.value = &scroll_y;
63+
option_y.min = 0.f;
64+
option_y.max = 1.f;
65+
option_y.increment = 0.1f;
66+
option_y.direction = Direction::Down;
67+
option_y.color_active = Color::Yellow;
68+
option_y.color_inactive = Color::YellowLight;
69+
auto scrollbar_y = Slider(option_y);
70+
71+
Add(Container::Vertical({
72+
Container::Horizontal({
73+
scrollable_content,
74+
scrollbar_y,
75+
}) | flex,
76+
Container::Horizontal({
77+
scrollbar_x,
78+
Renderer([] { return text(L"x"); }),
79+
}),
80+
}));
81+
}
82+
};
83+
return Make<Impl>();
84+
}
85+
86+
int main() {
87+
auto window_1 = Window({
88+
.inner = DummyWindowContent(),
89+
.title = "First window",
90+
.width = 80,
91+
.height = 30,
92+
});
93+
94+
auto window_2 = Window({
95+
.inner = DummyWindowContent(),
96+
.title = "My window",
97+
.left = 40,
98+
.top = 20,
99+
.width = 80,
100+
.height = 30,
101+
});
102+
103+
auto window_container = Container::Stacked({
104+
window_1,
105+
window_2,
106+
});
107+
108+
auto screen = ScreenInteractive::Fullscreen();
109+
screen.Loop(window_container);
110+
111+
return EXIT_SUCCESS;
112+
}

src/ftxui/component/window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ResizeDecorator : public NodeDecorator {
9393

9494
Element DefaultRenderState(const WindowRenderState& state) {
9595
Element element = state.inner;
96-
if (state.active) {
96+
if (!state.active) {
9797
element |= dim;
9898
}
9999

0 commit comments

Comments
 (0)