From 84287eb217bdc43a8dcc91871dd958af46764815 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 26 Sep 2021 17:30:41 +0200 Subject: [PATCH] Use vscroll_indicator in examples. --- examples/component/checkbox_in_frame.cpp | 3 ++- examples/component/homescreen.cpp | 34 +++++++++++++----------- examples/component/menu_in_frame.cpp | 3 ++- examples/component/radiobox_in_frame.cpp | 3 ++- src/ftxui/dom/scroll_indicator.cpp | 19 +++++++------ 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/examples/component/checkbox_in_frame.cpp b/examples/component/checkbox_in_frame.cpp index 6a40dbf..512e0dd 100644 --- a/examples/component/checkbox_in_frame.cpp +++ b/examples/component/checkbox_in_frame.cpp @@ -24,7 +24,8 @@ int main(int argc, const char* argv[]) { } auto renderer = Renderer(container, [&] { - return container->Render() | frame | size(HEIGHT, LESS_THAN, 10) | border; + return container->Render() | vscroll_indicator | frame | + size(HEIGHT, LESS_THAN, 10) | border; }); auto screen = ScreenInteractive::FitComponent(); diff --git a/examples/component/homescreen.cpp b/examples/component/homescreen.cpp index 0ae19fa..87b6634 100644 --- a/examples/component/homescreen.cpp +++ b/examples/component/homescreen.cpp @@ -214,23 +214,25 @@ int main(int argc, const char* argv[]) { }; auto compiler_renderer = Renderer(compiler_component, [&] { - auto compiler_win = window(text("Compiler"), compiler->Render() | frame); - auto flags_win = window(text("Flags"), flags->Render() | frame); + auto compiler_win = window(text("Compiler"), + compiler->Render() | vscroll_indicator | frame); + auto flags_win = + window(text("Flags"), flags->Render() | vscroll_indicator | frame); auto executable_win = window(text("Executable:"), executable_->Render()); auto input_win = - window(text("Input"), - hbox({ - vbox({ - hbox({ - text("Add: "), - input_add->Render(), - }) | size(WIDTH, EQUAL, 20) | - size(HEIGHT, EQUAL, 1), - filler(), - }), - separator(), - input->Render() | frame | size(HEIGHT, EQUAL, 3) | flex, - })); + window(text("Input"), hbox({ + vbox({ + hbox({ + text("Add: "), + input_add->Render(), + }) | size(WIDTH, EQUAL, 20) | + size(HEIGHT, EQUAL, 1), + filler(), + }), + separator(), + input->Render() | vscroll_indicator | frame | + size(HEIGHT, EQUAL, 3) | flex, + })); return vbox({ hbox({ compiler_win, @@ -240,7 +242,7 @@ int main(int argc, const char* argv[]) { input_win | size(WIDTH, EQUAL, 60), }), filler(), - }) | size(HEIGHT, LESS_THAN, 6), + }) | size(HEIGHT, LESS_THAN, 8), hflow(render_command()) | flex_grow, }) | flex_grow | border; diff --git a/examples/component/menu_in_frame.cpp b/examples/component/menu_in_frame.cpp index 1cfda32..b8bec21 100644 --- a/examples/component/menu_in_frame.cpp +++ b/examples/component/menu_in_frame.cpp @@ -18,7 +18,8 @@ int main(int argc, const char* argv[]) { entries.push_back("Entry " + std::to_string(i)); auto radiobox = Menu(&entries, &selected); auto renderer = Renderer(radiobox, [&] { - return radiobox->Render() | frame | size(HEIGHT, LESS_THAN, 10) | border; + return radiobox->Render() | vscroll_indicator | frame | + size(HEIGHT, LESS_THAN, 10) | border; }); auto screen = ScreenInteractive::FitComponent(); diff --git a/examples/component/radiobox_in_frame.cpp b/examples/component/radiobox_in_frame.cpp index 90cdc12..2ba6434 100644 --- a/examples/component/radiobox_in_frame.cpp +++ b/examples/component/radiobox_in_frame.cpp @@ -18,7 +18,8 @@ int main(int argc, const char* argv[]) { entries.push_back("RadioBox " + std::to_string(i)); auto radiobox = Radiobox(&entries, &selected); auto renderer = Renderer(radiobox, [&] { - return radiobox->Render() | frame | size(HEIGHT, LESS_THAN, 10) | border; + return radiobox->Render() | vscroll_indicator | frame | + size(HEIGHT, LESS_THAN, 10) | border; }); auto screen = ScreenInteractive::FitComponent(); diff --git a/src/ftxui/dom/scroll_indicator.cpp b/src/ftxui/dom/scroll_indicator.cpp index b63a69d..b003cbd 100644 --- a/src/ftxui/dom/scroll_indicator.cpp +++ b/src/ftxui/dom/scroll_indicator.cpp @@ -31,17 +31,20 @@ Element vscroll_indicator(Element child) { const Box& stencil = screen.stencil; - float size_inner = box_.y_max - box_.y_min; - float size_outter = stencil.y_max - stencil.y_min; - float start_y = stencil.y_min + - (stencil.y_min - box_.y_min) * size_outter / size_inner; - float end_y = stencil.y_min + - (stencil.y_max - box_.y_min) * size_outter / size_inner; + int size_inner = box_.y_max - box_.y_min; + int size_outter = stencil.y_max - stencil.y_min; + if (size_outter >= size_inner) + return; + + int start_y = 2 * stencil.y_min + 2 * float(stencil.y_min - box_.y_min) * + (size_outter - 1) / size_inner; + int size = 2 * float(size_outter) * (size_outter - 1) / size_inner + 2; + size = std::max(size, 1); const int x = stencil.x_max; for (int y = stencil.y_min; y <= stencil.y_max; ++y) { - bool up = (2 * y + -1 >= 2 * start_y) && (2 * y + -1 <= 2 * end_y); - bool down = (2 * y + 0 >= 2 * start_y) && (2 * y + 0 <= 2 * end_y); + bool up = (2 * y + -1 >= start_y) && (2 * y - 1 <= start_y + size); + bool down = (2 * y - 0 >= start_y) && (2 * y - 0 <= start_y + size); const char* c = up ? (down ? "┃" : "╹") : (down ? "╻" : " "); screen.PixelAt(x, y).character = c;