From 74d706887ad7da5a9c5d99fea86ac45d7ba14943 Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Mon, 16 Dec 2024 23:45:20 +0100 Subject: [PATCH] DispatchQueue improvements --- Submodules/UIKit/include/DispatchQueue.h | 9 ++++++++- Submodules/UIKit/include/UIKit.h | 1 + Submodules/UIKit/lib/DispatchQueue.cpp | 20 +++++++++++++++---- .../TestViewController/TestViewController.cpp | 14 +++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Submodules/UIKit/include/DispatchQueue.h b/Submodules/UIKit/include/DispatchQueue.h index fb23ea4..a2f1746 100644 --- a/Submodules/UIKit/include/DispatchQueue.h +++ b/Submodules/UIKit/include/DispatchQueue.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,6 +13,11 @@ namespace NXKit { +struct DispatchQueueTask { + std::function func; + Timer delay; +}; + class DispatchQueue { public: DispatchQueue(const std::string& tag); @@ -22,12 +28,13 @@ class DispatchQueue { std::string tag() const { return _tag; } void async(const std::function& task); + void asyncAfter(double seconds, const std::function& task); bool isActive() { return _task_loop_active; } private: static std::shared_ptr _main; static std::shared_ptr _global; - std::queue> _queue; + std::queue _queue; std::string _tag; pthread_t _task_loop_thread = nullptr; diff --git a/Submodules/UIKit/include/UIKit.h b/Submodules/UIKit/include/UIKit.h index 4686739..6e00ce1 100644 --- a/Submodules/UIKit/include/UIKit.h +++ b/Submodules/UIKit/include/UIKit.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include diff --git a/Submodules/UIKit/lib/DispatchQueue.cpp b/Submodules/UIKit/lib/DispatchQueue.cpp index 3d23bbc..6c64eb5 100644 --- a/Submodules/UIKit/lib/DispatchQueue.cpp +++ b/Submodules/UIKit/lib/DispatchQueue.cpp @@ -41,7 +41,12 @@ DispatchQueue::~DispatchQueue() { void DispatchQueue::async(const std::function& task) { std::lock_guard guard(_m_async_mutex); - _queue.push(task); + _queue.push({ task }); +} + +void DispatchQueue::asyncAfter(double seconds, const std::function& task) { + std::lock_guard guard(_m_async_mutex); + _queue.push({ task, Timer(seconds * 1000) }); } void DispatchQueue::performAll() { @@ -51,16 +56,23 @@ void DispatchQueue::performAll() { link->func(); } - std::queue> copy; + std::queue copy; { std::lock_guard guard(_m_async_mutex); std::swap( _queue, copy ); } - + + Timer now; while (!copy.empty()) { auto task = copy.front(); copy.pop(); - task(); + + // If task is delayed, put it back into queue + if (now - task.delay < 0) { + _queue.push(task); + } else { + task.func(); + } } } diff --git a/app/Screens/TestViewController/TestViewController.cpp b/app/Screens/TestViewController/TestViewController.cpp index b8dd7b7..b58cfb8 100644 --- a/app/Screens/TestViewController/TestViewController.cpp +++ b/app/Screens/TestViewController/TestViewController.cpp @@ -59,6 +59,18 @@ void animateLabel(std::shared_ptr label) { // }); } +void switchLabelText(std::shared_ptr label) { + label->setText("Text number 1"); + label->sizeToFit(); + DispatchQueue::main()->asyncAfter(2, [label]() { + label->setText("Text number 2\nHere is more text to test sizeToFit"); + label->sizeToFit(); + DispatchQueue::main()->asyncAfter(2, [label]() { + switchLabelText(label); + }); + }); +} + TestViewController::TestViewController() { } @@ -110,6 +122,8 @@ void TestViewController::loadView() { label2->setFrame({ 100, 200, 0, 0 }); rootView->addSubview(label2); + switchLabelText(label2); + setView(rootView); }