Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Window.h"
- // STD headers
- #include <cmath>
- #include <csignal>
- using namespace std::literals::chrono_literals;
- // external libs
- #include "intdiv.h"
- // project headers
- Window::Window(const std::string& t) : title(t), contents(emptyElement())
- {
- std::lock_guard<std::mutex> guard(inst_mutex);
- for (size_t i = 0; i < instances.size(); ++i)
- if (instances[i] == nullptr)
- {
- instanceNum = i;
- instances[i] = this;
- return;
- }
- instanceNum = instances.size();
- instances.emplace_back(this);
- }
- Window::~Window()
- {
- instances[instanceNum] = nullptr;
- std::lock_guard<std::mutex> guard(inst_mutex);
- while (instances.size() > 0 && instances.back() == nullptr)
- instances.pop_back();
- }
- void Window::updateContents() {}
- // STATIC SHIT
- void Window::init()
- {
- cout << "Spawning Window manager..." << endl;
- writer = std::thread(Window::writing_thread_callback);
- cout << "Done." << endl;
- }
- void Window::deinit()
- {
- //screen.Post(screen.ExitLoopClosure());
- screen.Exit();
- cout << "\tDespawning Window manager..." << endl;
- writer_exit = true;
- writer.join();
- cout << "\tDone." << endl;
- }
- void Window::writing_thread_callback()
- {
- // TABS
- auto spinner_tab_renderer = Renderer([&]()
- {
- Elements entries;
- for (int i = 0; i < 22; ++i)
- entries.push_back(spinner(i, 3 / 2) | bold | size(WIDTH, GREATER_THAN, 2) | border);
- return hflow(std::move(entries));
- }
- );
- auto spinner_tab_renderer2 = Renderer([&]()
- {
- std::vector<Elements> grid;
- std::lock_guard<std::mutex> lock(inst_mutex);
- size_t len = instances.size();
- size_t wdh = std::round(std::sqrt(len));
- size_t i = 0;
- while (i < len)
- {
- Elements row;
- for (size_t x = 0; (x < wdh) && (i < len); ++x, ++i)
- {
- Window& ins = *instances[i];
- ins.updateContents();
- Element win = window(text(ins.title), ins.contents) | flex;
- row.push_back(win);
- }
- grid.push_back(std::move(row));
- }
- return gridbox(std::move(grid));
- }
- );
- int tab_index = 0;
- std::vector<std::string> tab_entries = {
- "Main", "Downloaders", "Logs"
- };
- auto tab_selection = Menu(&tab_entries, &tab_index, MenuOption::Horizontal());
- auto tab_content = Container::Tab(
- {
- spinner_tab_renderer,
- spinner_tab_renderer2,
- },
- &tab_index);
- auto main_container = Container::Vertical({
- tab_selection,
- tab_content,
- });
- auto main_renderer = Renderer(main_container, [&] {
- return vbox({
- text("PlayerManager") | bold | hcenter,
- tab_selection->Render(),
- separator(),
- tab_content->Render() | flex,
- }) | borderHeavy | color(Color::White);
- });
- std::atomic<bool> refresh_ui_continue = true;
- std::thread refresh_ui([&]()
- {
- while (refresh_ui_continue)
- {
- std::this_thread::sleep_for(100ms);
- screen.Post(Event::Custom);
- }
- }
- );
- screen.Loop(main_renderer);
- refresh_ui_continue = false;
- refresh_ui.join();
- std::raise(SIGQUIT);
- }
- // TERMINAL
- void WindowTerminal::updateContents()
- {
- Elements innards;
- for (const auto& line : lines)
- innards.push_back(line);
- if (innards.size())
- innards.back() |= focus;
- contents = vbox(innards) | vscroll_indicator | yframe | yflex;
- }
- void WindowTerminal::set_max_lines(size_t m)
- {
- max_lines = m;
- while (lines.size() > max_lines)
- lines.pop_front();
- }
- void WindowTerminal::add_line(const std::string& l)
- {
- lines.push_back(text(l));
- if (lines.size() > max_lines)
- lines.pop_front();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement