gtsam_points
Loading...
Searching...
No Matches
progress_modal.hpp
Go to the documentation of this file.
1#ifndef GUIK_PROGRESS_MODAL_HPP
2#define GUIK_PROGRESS_MODAL_HPP
3
4#include <any>
5#include <mutex>
6#include <atomic>
7#include <thread>
8#include <string>
9#include <optional>
10#include <iostream>
11#include <functional>
12
13#include <imgui.h>
14
16
17namespace guik {
18
20public:
21 ProgressModal(const std::string& modal_name) : modal_name(modal_name), running(false), max(0), current(0) {}
22 virtual ~ProgressModal() override {
23 if(thread.joinable()) {
24 thread.join();
25 }
26 }
27
28 virtual void set_title(const std::string& title) override {
29 std::lock_guard<std::mutex> lock(mutex);
30 this->title = title;
31 }
32
33 virtual void set_text(const std::string& text) override {
34 std::lock_guard<std::mutex> lock(mutex);
35 this->text = text;
36 }
37
38 virtual void set_maximum(int max) override {
39 this->max = max;
40 this->current = 0;
41 }
42 virtual void set_current(int current) override {
43 this->current = current;
44 }
45 virtual void increment() override {
46 current++;
47 }
48
49 template<typename T>
50 void open(const std::string& task_name, const std::function<T(ProgressInterface& progress)>& task) {
51 this->task_name = task_name;
52 this->title.clear();
53 this->text.clear();
54
55 ImGui::OpenPopup(modal_name.c_str());
56 result_ = nullptr;
57 running = true;
58 current = 0;
59
60 thread = std::thread([this, task]() {
61 result_ = task(*this);
62 running = false;
63 });
64 }
65
66 template<typename T>
67 std::optional<T> run(const std::string& task_name) {
68 if(task_name != this->task_name) {
69 return std::nullopt;
70 }
71
72 bool terminated = false;
73 if(ImGui::BeginPopupModal(modal_name.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar)) {
74 {
75 std::lock_guard<std::mutex> lock(mutex);
76 if(!title.empty()) {
77 ImGui::Text("%s", title.c_str());
78 }
79
80 ImGui::Text("%c %s", "|/-\\"[(int)(ImGui::GetTime() / 0.05f) & 3], text.c_str());
81 }
82
83 float fraction = current / static_cast<float>(max);
84 ImGui::ProgressBar(fraction, ImVec2(128, 16));
85
86 if(!running) {
87 thread.join();
88 ImGui::CloseCurrentPopup();
89 terminated = true;
90 }
91 ImGui::EndPopup();
92 }
93
94 if(!result_.has_value() || !terminated) {
95 return std::nullopt;
96 }
97
98 T ret = std::any_cast<T>(result_);
99 result_ = nullptr;
100 return ret;
101 }
102
103 bool is_running() const {
104 return running;
105 }
106
107private:
108 std::mutex mutex;
109 std::string modal_name;
110 std::string title;
111 std::string text;
112
113 std::atomic_bool running;
114 std::atomic_int max;
115 std::atomic_int current;
116
117 std::string task_name;
118
119 std::thread thread;
120 std::any result_;
121};
122
123} // namespace guik
124
125#endif
Definition progress_modal.hpp:19
virtual ~ProgressModal() override
Definition progress_modal.hpp:22
bool is_running() const
Definition progress_modal.hpp:103
void open(const std::string &task_name, const std::function< T(ProgressInterface &progress)> &task)
Definition progress_modal.hpp:50
virtual void increment() override
Definition progress_modal.hpp:45
virtual void set_maximum(int max) override
Definition progress_modal.hpp:38
virtual void set_current(int current) override
Definition progress_modal.hpp:42
std::optional< T > run(const std::string &task_name)
Definition progress_modal.hpp:67
virtual void set_title(const std::string &title) override
Definition progress_modal.hpp:28
ProgressModal(const std::string &modal_name)
Definition progress_modal.hpp:21
virtual void set_text(const std::string &text) override
Definition progress_modal.hpp:33
Definition drawable_container.hpp:9
Definition progress_interface.hpp:8