From 4365bfa98da127e5161f8af21c810d7c48d9a7b5 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 9 Aug 2024 12:31:38 -0400 Subject: [PATCH] A thread version of a UI that updates the the background. --- ftx_thread_test.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++ meson.build | 3 ++ 2 files changed, 92 insertions(+) create mode 100644 ftx_thread_test.cpp diff --git a/ftx_thread_test.cpp b/ftx_thread_test.cpp new file mode 100644 index 0000000..0d60306 --- /dev/null +++ b/ftx_thread_test.cpp @@ -0,0 +1,89 @@ + +// 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 EXIT_SUCCESS +#include // for milliseconds +#include // for sleep_for, thread +#include +#include // for Event +#include // for ftxui +#include // for text, separator, Element, operator|, vbox, border +#include // for allocator, shared_ptr +#include // for operator+, to_string +#include // for sleep_for + +#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|= +#include "ftxui/component/loop.hpp" // for Loop +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive + +using namespace ftxui; + +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() { + auto screen = ScreenInteractive::Fullscreen(); + + // Create a component counting the number of frames drawn and event handled. + int custom_loop_count = 0; + int frame_count = 0; + int event_count = 0; + int hp = 100; + int row = 0; + auto hit_button = Button("Hit", [&] { hp -= 1; }, Style()); + auto hard_button = Button("Hard", [&] { hp -= 10; }, Style()); + auto heal_button = Button("Heal", [&] { hp += 10; }, Style()); + + auto buttons = Container::Horizontal({ + hit_button, hard_button, heal_button}, &row); + + auto component = Renderer(buttons, [&] { + frame_count++; + + return vbox({ + paragraph("I'm baby mustache hammock squid, stumptown echo park lumbersexual PBR&B glossier iceland pabst irony mlkshk skateboard migas kombucha. Lyft meggings organic tacos. IPhone microdosing bodega boys, fit locavore jawn cloud bread neutral milk hotel trust fund live-edge selfies portland lyft vice. Pug swag af slow-carb."), + separator(), + paragraph(fmt::format("HP {} frames {} events {} custom {}", + hp, frame_count, event_count, custom_loop_count)), + separator(), + hbox({ + text("HP"), + gauge(hp / 100.0f), + }), + separator(), + buttons->Render(), + }) | + border; + }); + + component |= CatchEvent([&](Event) -> bool { + event_count++; + return false; + }); + + Loop loop(&screen, component); + + std::atomic refresh_ui_continue = true; + std::thread refresh_ui([&] { + while(refresh_ui_continue) { + screen.Post(Event::Custom); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + }); + + screen.Loop(component); + refresh_ui_continue = false; + refresh_ui.join(); + + return EXIT_SUCCESS; +} diff --git a/meson.build b/meson.build index 24375f0..52cfb16 100644 --- a/meson.build +++ b/meson.build @@ -35,3 +35,6 @@ executable('regtest', 'regtest.cpp', executable('ftxtest', 'ftxtest.cpp', dependencies: dependencies) + +executable('ftx_thread_test', 'ftx_thread_test.cpp', + dependencies: dependencies)