-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (140 loc) · 5.74 KB
/
Copy pathmain.cpp
File metadata and controls
171 lines (140 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <string>
#include <memory>
#include <thread>
#include <atomic>
#include <chrono>
#include <iostream>
#include <functional>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#endif
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
struct ThreadArgs {
std::atomic<int> counter{0};
std::atomic<int> timer_value{0}; // Timer value that increments every 5 seconds
std::atomic<bool> is_running{true};
std::atomic<bool> ui_ready{false};
std::atomic<bool> confirm_exit{false}; // Exit confirmation modal state
ThreadArgs() = default;
ThreadArgs(int c, bool r, bool u) : counter(c), is_running(r), ui_ready(u) {}
};
// ============================================================================
// FTXUI thread function — takes ThreadArgs by reference so main and UI share same instance
// ============================================================================
void RunUIThread(ThreadArgs& args) {
using namespace ftxui;
auto screen = ScreenInteractive::TerminalOutput();
// Timer thread: increments timer_value by 1 every 5 seconds, separate from the UI
std::thread timer_thread([&args]() {
while (args.is_running.load()) {
std::this_thread::sleep_for(std::chrono::seconds(5));
if (!args.is_running.load())
break;
args.timer_value++;
}
});
auto button = Button("Click me!", [&args]() {
args.counter++; // atomic safe increment
});
auto renderer = Renderer(button, [&args, &button]() {
// Basic layout
auto title = text("--- FTXUI Function Separation Example ---") | bold | color(Color::Blue);
// bold: display text in bold. color(Color::Blue): set text color to blue.
std::string counter_str = "Button click count: " + std::to_string(args.counter.load());
auto counter_view = text(counter_str) | color(Color::Green);
std::string timer_str = "Timer value (5s interval): " + std::to_string(args.timer_value.load());
auto timer_view = text(timer_str) | color(Color::Yellow);
auto button_view = align_right(button->Render());
auto footer = text("Press 'Ctrl + Q' to exit.") | dim;
// dim: make the text less prominent compared to surrounding UI.
auto content = vbox({
title,
separator(),
counter_view,
timer_view,
button_view,
separator(),
footer
}) | border;
// If the exit confirmation flag is set, add a simple confirmation box
if (args.confirm_exit.load()) {
auto ask_quit_text = text("Do you want to exit?") | bold;
auto select_text = text("Choice: 'y' or 'n'") | dim;
auto modal = vbox({
ask_quit_text,
select_text
}) | border | color(Color::Yellow2);
// Use explicit spacer
auto ret = vbox({
content, // existing screen
text("") | size(HEIGHT, EQUAL, 1), // small spacer
hcenter(modal) // place modal centered on the screen
});
return ret;
}
return content;
});
auto exit_screen = screen.ExitLoopClosure();
auto component = CatchEvent(renderer, [&](Event event) {
// 1) If confirmation modal is already shown: 'y' to exit, 'n' or ESC to cancel
if (args.confirm_exit.load()) {
if (event == Event::Character('y') || event == Event::Character('Y')) {
args.is_running = false;
exit_screen();
return true;
}
if (event == Event::Character('n') || event == Event::Character('N') || event == Event::Escape) {
args.confirm_exit = false; // cancel
return true;
}
// Consume all other input while modal is shown so it isn't passed to child widgets
return true;
}
// 2) Normal operation: Show confirmation modal when Ctrl+Q is pressed
if (event == Event::Special("\x11")) { // Ctrl + Q
args.confirm_exit = true;
return true;
}
return false;
});
args.ui_ready = true;
screen.Loop(component); // Start UI loop
// After UI loop ends, clean up timer thread
args.is_running = false;
if (timer_thread.joinable()) {
timer_thread.join();
}
}
// ============================================================================
// main
// ============================================================================
int main() {
#ifdef _WIN32
// for non-English characters to display correctly in Windows console
::SetConsoleOutputCP(CP_UTF8);
::SetConsoleCP(CP_UTF8);
#endif
// Atomics are now owned by ThreadArgs
ThreadArgs args; // counter:0, timer_value:0, is_running:true, ui_ready:false
// Pass ThreadArgs by reference into the thread
std::thread ui_thread(RunUIThread, std::ref(args));
while (!args.ui_ready.load()) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
}
while (args.is_running.load()) {
// Main thread loop (You can add any other logic here if needed)
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
}
if (ui_thread.joinable()) {
ui_thread.join(); // Wait for the UI thread to finish before exiting main
}
std::cout << std::endl << "[Info] Program exited successfully." << std::endl;
std::cout << "Final button click count: " << args.counter.load() << " times" << std::endl;
std::cout << "Final timer value: " << args.timer_value.load() << std::endl;
return 0;
}