From f609c128461e0a51762152f6883e316dafb26c79 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sat, 30 Mar 2024 11:01:28 +0100 Subject: [PATCH 1/2] Revert change to button example. (#835) It was introduced mistakenly by: https://github.com/ArthurSonzogni/FTXUI/commit/f495ce029cb3a9b4c8309e4456e494f8b6b09967 --- examples/component/button.cpp | 138 ++++++++++++++-------------------- 1 file changed, 55 insertions(+), 83 deletions(-) diff --git a/examples/component/button.cpp b/examples/component/button.cpp index 86ff8f9d..b336a6e0 100644 --- a/examples/component/button.cpp +++ b/examples/component/button.cpp @@ -1,92 +1,64 @@ -#include -#include -#include -#include +// 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 // for shared_ptr, __shared_ptr_access +#include // for operator+, to_string -#include -#include -#include +#include "ftxui/component/captured_mouse.hpp" // for ftxui +#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer +#include "ftxui/component/component_base.hpp" // for ComponentBase +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive +#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border using namespace ftxui; -using namespace std; -enum class ConnectionMethod { ByName, ByAddress }; - -void promptForConnectionMethod(); - -void promptForServerName(); -void promptForServerAddress(); -void promptForServerManager(); +// This is a helper function to create a button with a custom style. +// The style is defined by a lambda function that takes an EntryState and +// returns an Element. +// We are using `center` to center the text inside the button, then `border` to +// add a border around the button, and finally `flex` to make the button fill +// the available space. +ButtonOption Style() { + auto option = ButtonOption::Animated(); + option.transform = [](const EntryState& s) { + auto element = text(s.label); + if (s.focused) { + element |= bold; + } + return element | center | borderEmpty | flex; + }; + return option; +} int main() { - promptForConnectionMethod(); + int value = 50; + // clang-format off + auto btn_dec_01 = Button("-1", [&] { value += 1; }, Style()); + auto btn_inc_01 = Button("+1", [&] { value -= 1; }, Style()); + auto btn_dec_10 = Button("-10", [&] { value -= 10; }, Style()); + auto btn_inc_10 = Button("+10", [&] { value += 10; }, Style()); + // clang-format on + + // The tree of components. This defines how to navigate using the keyboard. + // The selected `row` is shared to get a grid layout. + int row = 0; + auto buttons = Container::Vertical({ + Container::Horizontal({btn_dec_01, btn_inc_01}, &row) | flex, + Container::Horizontal({btn_dec_10, btn_inc_10}, &row) | flex, + }); + + // Modify the way to render them on screen: + auto component = Renderer(buttons, [&] { + return vbox({ + text("value = " + std::to_string(value)), + separator(), + buttons->Render() | flex, + }) | + flex | border; + }); + + auto screen = ScreenInteractive::FitComponent(); + screen.Loop(component); return 0; } - -void promptForConnectionMethod() { - auto screen = ScreenInteractive::TerminalOutput(); - std::vector connect_choice{ - "Connect by name...", - "Connect by address and port...", - "Exit", - }; - int selected = 0; - MenuOption option; - option.on_enter = [&] { - if (selected == 0) { - promptForServerManager(); - } else if (selected == 1) { - promptForServerAddress(); - } else if (selected == 2) { - screen.Exit(); - } - }; - - auto connect_menu = Menu(&connect_choice, &selected, option); - - auto renderer = Renderer(connect_menu, [&] { - return vbox({ - text("Welcome, my client!") | color(Color::Red3Bis) | bold | - center, - text(""), - text("Selected = " + std::to_string(selected)) | - color(Color::LightGreenBis) | bold | center, - - text(""), - text("Welcome to my first working multiplayer game, Medium " - "Boxes.") | - color(Color::LightSkyBlue1), - text(""), - text("Now, choose how you'd prefer to connect to a server!") | - color(Color::LightCyan3) | center, - text(""), - connect_menu->Render() | border, - }) | - center; - }); - - screen.Loop(renderer); -} - -void promptForServerName() {} - -void promptForServerAddress() { - auto screen = ScreenInteractive::TerminalOutput(); -} - -void promptForServerManager() { - auto screen = ScreenInteractive::TerminalOutput(); - auto renderer = Renderer([&] { - return vbox({ - text("Now, please enter the server manager's address and " - "port, so that you'll be able to see all the available " - "public servers!") | - color(Color::LightGreenBis), - gauge(0), - }) | - center; - }); - - screen.Loop(renderer); -} From 2216f3a5daaee3d41eec0684a63ae599c3fd991a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20Gustav=20Larsen?= <164860481+jglanycon@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:32:19 +0200 Subject: [PATCH 2/2] Problem with setting the cursor position on the right screen edge when drawing. (#831) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When moving the cursor back to its original location, a problem arises when cursor placed in the right edge column, where an off by one error occur. This pull request will resolve this problem. Co-authored-by: Jørn Gustav Larsen Co-authored-by: Jørn Gustav Larsen Co-authored-by: ArthurSonzogni --- CHANGELOG.md | 1 + src/ftxui/component/screen_interactive.cpp | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a976af7..4963ac57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ current (development) - Bugfix: `Input` `onchange` was not called on backspace or delete key. Fixed by @chrysante in chrysante in PR #776. - Bugfix: Propertly restore cursor shape on exit. See #792. +- Bugfix: Fix cursor position in when in the last column. See #831. ### Dom - Feature: Add `hscroll_indicator`. It display an horizontal indicator diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index c446f2ee..dc729acb 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -844,10 +844,18 @@ void ScreenInteractive::Draw(Component component) { const int dx = dimx_ - 1 - cursor_.x + int(dimx_ != terminal.dimx); const int dy = dimy_ - 1 - cursor_.y; - set_cursor_position = "\x1B[" + std::to_string(dy) + "A" + // - "\x1B[" + std::to_string(dx) + "D"; - reset_cursor_position = "\x1B[" + std::to_string(dy) + "B" + // - "\x1B[" + std::to_string(dx) + "C"; + set_cursor_position.clear(); + reset_cursor_position.clear(); + + if (dy != 0) { + set_cursor_position += "\x1B[" + std::to_string(dy) + "A"; + reset_cursor_position += "\x1B[" + std::to_string(dy) + "B"; + } + + if (dx != 0) { + set_cursor_position += "\x1B[" + std::to_string(dx) + "D"; + reset_cursor_position += "\x1B[" + std::to_string(dx) + "C"; + } if (cursor_.shape == Cursor::Hidden) { set_cursor_position += "\033[?25l";