@@ -251,14 +251,14 @@ auto FApplication::sendEvent (FObject* receiver, FEvent* event ) -> bool
251251}
252252
253253// ----------------------------------------------------------------------
254- void FApplication::queueEvent (FObject* receiver, FEvent* event)
254+ void FApplication::queueEvent (FObject* receiver, std::unique_ptr< FEvent>&& event)
255255{
256- if ( ! (bool (receiver) && bool (event)) )
256+ if ( ! (bool (receiver) && bool (event. get () )) )
257257 return ;
258258
259259 // queue this event
260260 setQueued (*event);
261- event_queue.emplace_back (receiver, event);
261+ event_queue.emplace_back (receiver, std::move ( event) );
262262}
263263
264264// ----------------------------------------------------------------------
@@ -268,7 +268,7 @@ void FApplication::sendQueuedEvents()
268268 {
269269 const auto & event_pair = event_queue.front ();
270270 setQueued (*event_pair.second , false );
271- sendEvent (event_pair.first , event_pair.second );
271+ sendEvent (event_pair.first , event_pair.second . get () );
272272 event_queue.pop_front ();
273273 }
274274}
@@ -285,10 +285,7 @@ auto FApplication::eventInQueue() const -> bool
285285// ----------------------------------------------------------------------
286286auto FApplication::removeQueuedEvent (const FObject* receiver) -> bool
287287{
288- if ( ! eventInQueue () )
289- return false ;
290-
291- if ( ! receiver )
288+ if ( ! receiver || ! eventInQueue () )
292289 return false ;
293290
294291 bool retval{false };
@@ -313,6 +310,31 @@ void FApplication::queueDraw (FWidget* widget)
313310{
314311 // Queue the widget for redrawing
315312 draw_queue.push_back (widget);
313+
314+ // We only need one draw event to redraw all the widgets in the queue
315+ if ( need_widget_redraw )
316+ return ;
317+
318+ need_widget_redraw = true ;
319+
320+ auto ev = std::make_unique<FEvent>(Event::Draw);
321+ FApplication::queueEvent (this , std::move (ev));
322+ }
323+
324+ // ----------------------------------------------------------------------
325+ auto FApplication::removeQueuedDraw (const FWidget* widget) -> bool
326+ {
327+ if ( ! widget || draw_queue.empty () )
328+ return false ;
329+
330+ const auto queue_end = draw_queue.end ();
331+ const auto last = std::remove (draw_queue.begin (), queue_end, widget);
332+ draw_queue.erase (last, queue_end);
333+
334+ if ( draw_queue.empty () )
335+ need_widget_redraw = false ;
336+
337+ return true ;
316338}
317339
318340// ----------------------------------------------------------------------
@@ -425,6 +447,30 @@ void FApplication::processExternalUserEvent()
425447 // This method can be overloaded and replaced by own code
426448}
427449
450+ // ----------------------------------------------------------------------
451+ auto FApplication::event (FEvent* ev) -> bool
452+ {
453+ const auto event_type = ev->getType ();
454+
455+ if ( event_type == Event::Draw )
456+ {
457+ onDraw (ev);
458+ return true ;
459+ }
460+
461+ return FWidget::event (ev);
462+ }
463+
464+ // ----------------------------------------------------------------------
465+ void FApplication::onDraw (FEvent*)
466+ {
467+ // This event handler can be reimplemented in a subclass
468+ // to receive draw events
469+
470+ processRedraw ();
471+ need_widget_redraw = false ;
472+ }
473+
428474
429475// private methods of FApplication
430476// ----------------------------------------------------------------------
@@ -1378,7 +1424,7 @@ void FApplication::processRedraw()
13781424 // Sort the queue by hierarchy depth
13791425 std::sort ( processing_queue.begin ()
13801426 , processing_queue.end ()
1381- , [] (FWidget* lhs, FWidget* rhs)
1427+ , [] (const FWidget* lhs, const FWidget* rhs)
13821428 {
13831429 return lhs->getDepth () < rhs->getDepth ();
13841430 } );
@@ -1445,7 +1491,6 @@ auto FApplication::processNextEvent() -> bool
14451491 processResizeEvent (); // when the terminal size has changed
14461492 processDialogResizeMove ();
14471493 sendQueuedEvents ();
1448- processRedraw ();
14491494 processTerminalUpdate (); // for changed regions on the terminal
14501495 flush (); // Flush output buffer (via an instance of FOutput)
14511496 processLogger ();
0 commit comments