Skip to content
Arthur Sonzogni edited this page Apr 16, 2023 · 2 revisions

Screen

This object will help you build a single frame. This contains a buffer that represent every pixel on the screen.

#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
// ...
ftxui::Dimention width;
auto screen = ftxui::Scene::Create(
    width,
    height
);
// ...

Now the screen is ready to get elements rendered.

#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
// ...
ftxui::Dimention width;
auto screen = ftxui::Scene::Create(
    width,
    height
);

auto element = ftxui::hbox({
    ftxui::text("Left") | ftxui::border,
    ftxui::text("Middle") | ftxui::border | ftxui::flex,
    ftxui::text("Right) | ftxui::border
});
// ...

Output:

┌────┐┌────────────────────────────────────┐┌─────┐
│left││middle                              ││right│
└────┘└────────────────────────────────────┘└─────┘

Interactive Screen

Getting a screen started is a first step, but there no interactivity with elements. This is where Interactive Screens comes

Creating an interactive screen

#include <ftxui/component/screen_interactive.hpp>
// ...
auto screen = ftxui::ScreenInteractive::Fullscreen();
// ...

Instead of taking in Elements, Screen Interactive take in components. These components can be utilized to navigate the program by using the arrow keys and interacting with widgets such as the checkbox. You can also make you own components. The use can navigates using the arrow keys and interact with widgets like checkbox/inputbox/... You can make you own components. Later on in this wiki there will be examples given for each components and how to interact with them.

auto screen = ftxui::ScreenInteractive::Fullscreen();

auto finalRenderer = ftxui::Renderer([&](){
  // ...
});

screen.Loop(finalComponent);

ftxui::Screen::Loop() will block the current thread. It is recommended to have this be on the main thread or on a separate thread.

Force Redraw

The screen works on an internal event system. We can take advantage of the event system to force the screen to redraw. Because ftxui::Screen::Loop() is a blocking function we must use threading.

// ...
auto screen = ftxui::ScreenInteractive::Fullscreen();

auto screenRedraw = std::thread([&](){
    while(running){
        screen.PostEvent(ftxui::Event::Custom);
	std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Prevent High CPU Usage.
    }
});

//...

screen.Loop(finalRenderer);
running = false;
screenRedraw.join();
//...

Closing the screen programmatically

When inside: ftxui::ScreenInteractive::Loop(), the loop can be ended by calling: ftxui::ScreenInteractive::Exit()

Clone this wiki locally