File: LazyIconResolver.cpp

package info (click to toggle)
plasma-discover 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,288 kB
  • sloc: cpp: 30,576; xml: 2,710; python: 311; sh: 5; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
// SPDX-FileCopyrightText: 2024 Harald Sitter <sitter@kde.org>

#include "LazyIconResolver.h"

#include <QCoreApplication>
#include <QEvent>

namespace
{
constexpr auto LoopEvent = QEvent::User;
} // namespace

LazyIconResolver *LazyIconResolver::instance()
{
    static LazyIconResolver resolver;
    return &resolver;
}

void LazyIconResolver::queue(AbstractResource *resource)
{
    if (m_resources.isEmpty()) {
        QCoreApplication::postEvent(this, new QEvent(LoopEvent), Qt::LowEventPriority);
    }
    m_resources.append(QPointer{resource});
}

void LazyIconResolver::resolve()
{
    if (m_resources.empty()) {
        return;
    }

    // We take the last so the most recent request gets resolved first.
    auto resource = m_resources.takeLast();
    if (!resource) {
        return;
    }

    if (resource->hasResolvedIcon()) {
        return;
    }

    resource->resolveIcon();
}

void LazyIconResolver::customEvent(QEvent *event)
{
    if (event->type() == LoopEvent) {
        resolve();
        // Schedule a new loop run unconditionally of whether we resolved anything. So long as the queue is not empty.
        if (!m_resources.isEmpty()) {
            QCoreApplication::postEvent(this, new QEvent(LoopEvent), Qt::LowEventPriority);
        }
    }
    QObject::customEvent(event);
}