@@ -32,6 +32,7 @@ using namespace ftxui;
3232struct GlobalState {
3333 std::mutex mutex;
3434 std::map<std::string, ShapeMsg> shapes;
35+ ScreenInteractive* screen = nullptr ;
3536} g_state;
3637
3738int main () {
@@ -49,13 +50,22 @@ int main() {
4950 return -1 ;
5051 }
5152
53+ auto screen = ScreenInteractive::Fullscreen ();
54+ g_state.screen = &screen;
55+
5256 // 2. Setup Participant and Handlers
5357 auto group = std::make_shared<dmq::Participant>(transport);
5458 static Serializer<void (ShapeMsg)> serializer;
5559
5660 auto shapeHandler = [](const std::string& topic, ShapeMsg msg) {
57- std::lock_guard<std::mutex> lock (g_state.mutex );
58- g_state.shapes [topic] = msg;
61+ {
62+ std::lock_guard<std::mutex> lock (g_state.mutex );
63+ g_state.shapes [topic] = msg;
64+ }
65+ // Data-Driven Refresh: Trigger UI redraw immediately on data arrival
66+ if (g_state.screen ) {
67+ g_state.screen ->PostEvent (Event::Custom);
68+ }
5969 };
6070
6171 group->RegisterHandler <ShapeMsg>(SystemTopic::SquareId, serializer, [&](ShapeMsg m) { shapeHandler (SystemTopic::Square, m); });
@@ -67,26 +77,21 @@ int main() {
6777 std::thread netThread ([&]() {
6878 while (running) {
6979 group->ProcessIncoming ();
70- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
80+ std::this_thread::sleep_for (std::chrono::milliseconds (5 )); // High-freq check
7181 }
7282 });
7383
7484 // 4. FTXUI Render Loop
75- auto screen = ScreenInteractive::Fullscreen ();
76-
7785 auto renderer = Renderer ([&] {
78- // Create a canvas that fills the available space
7986 auto c = Canvas (200 , 100 );
8087
8188 {
8289 std::lock_guard<std::mutex> lock (g_state.mutex );
8390 for (auto const & [topic, shape] : g_state.shapes ) {
8491 if (topic == SystemTopic::Square) {
85- // Draw Square (using block coordinates)
8692 for (int x=0 ; x<10 ; ++x)
8793 for (int y=0 ; y<10 ; ++y) c.DrawBlock (shape.x * 2 + x, shape.y * 2 + y, true , Color::Blue);
8894 } else if (topic == SystemTopic::Circle) {
89- // Draw Circle
9095 c.DrawBlockCircle (shape.x * 2 , shape.y * 2 , 10 , Color::Red);
9196 }
9297 }
@@ -99,34 +104,24 @@ int main() {
99104 canvas (std::move (c)) | flex | border,
100105 hbox ({
101106 filler (),
102- text (" Newest data received via DataBus Multicast | 'q' to quit" ) | dim,
107+ text (" Data-Driven Rendering (Sync with DataBus) | 'q' to quit" ) | dim,
103108 filler ()
104109 })
105110 });
106111 });
107112
108- // Handle 'q' to quit and sink all other events to prevent character artifacts
109113 auto component = CatchEvent (renderer, [&](Event event) {
110114 if (event == Event::Character (' q' )) {
111115 screen.Exit ();
112116 return true ;
113117 }
114- return true ; // Sink all events
115- });
116-
117- // Refresh UI at 20fps to reduce flickering
118- std::thread refresher ([&] {
119- while (running) {
120- std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
121- screen.PostEvent (Event::Custom);
122- }
118+ return false ;
123119 });
124120
125121 screen.Loop (component);
126122
127123 running = false ;
128124 netThread.join ();
129- refresher.join ();
130125 transport.Close ();
131126
132127 std::cout << " \r " << std::flush;
0 commit comments