Simple FTXUI Template with event handler, logger and multiple-pages paradigm.
Find a file Use this template
2025-05-09 11:36:10 +02:00
img Added gif. 2025-05-09 11:36:10 +02:00
include Added many comments to main objects used. 2025-05-09 10:40:55 +02:00
src Redundant ui calls. 2025-05-09 11:31:45 +02:00
.gitattributes Added gif. 2025-05-09 11:36:10 +02:00
.gitignore Added page1 and page2 examples. 2025-05-08 17:57:41 +02:00
CMakeLists.txt Added many comments to main objects used. 2025-05-09 10:40:55 +02:00
dependencies.cmake Fixed compilation under linux. 2025-05-06 23:29:52 +02:00
README.md Added gif. 2025-05-09 11:36:10 +02:00

FTXUI Template

Static Badge Static Badge

index

Template example using the FTXUI library with a proper logger, thread-safe event handler and multiple pages paradigm. This template can be used in any project and is a good way to start a project using a simple and not-so-verbose approach.

Build the project

You can simply use CMake with the following commands:

mkdir build && cd build
cmake ..
cmake --build . --parallel

Using the Interface

Event

Main thread-safe event handler. You can set a custom function in it that will be called once the loop is starting. It is recommended to run the eventhandler in a separated thread.

Each event that is added or handled is thread safe.

main.cpp


static inline void loop_event(EventHandler &handler, const EventPayload &payload)
{
    // Called each time there is a new event in the event buffer...
}

int main(void)
{
    // Basic usage
    EventHandler handler;
    handler.set_handler(loop_event);
    Page1 page1(handler);
    // Starting the event loop in a separated thread.
    std::thread t_event(&EventHandler::loop, &handler);
    t_event.join();
    return 0;
}

page1.cpp

void Page1::change_screen()
{
    this->send_event(EventType::SWITCH_SCREEN, static_cast<size_t>(1));
}

Logger

'Logger' is a general Logger that can be instanciated anywhere as a singleton. It can have a maximum size for its buffer that can be changed on the go. Each LoggerType can be gathered from each data which can lead to multiple ways to print out a log.

Logger& logger = Logger::GetInstance();
logger.push_back("Hello, world!", LoggerType::PRINT);
// std::vector<std::string>& buffer = logger.get_buffer();

UserInterface

'UserInterface' is the main object that can be instanciated anywhere as a singleton you can add any pages to it and cycle through them using the select_screen function.

UserInterface &ui = UserInterface::GetInstance();

int main(void)
{
    // Event declaration
    EventHandler handler;

    // Page declaration
    PageX pagex(handler);
    // other pages...

    ui.add_screen(&pagex);
    // other pages...

    ui.start();
    return 0;
}

Creating a new page

To create a new page, just create a parent class from Page and create a ftxui::Component named _page in the constructor. You can follow the examples given in the pages folder.