summaryrefslogtreecommitdiffstats
path: root/src/gui/platform/darwin/qappleiconengine.mm
blob: 154d3b79bb4fbe453c22dc65c70ef2021177107d (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qappleiconengine_p.h"

#if defined(Q_OS_MACOS)
# include <AppKit/AppKit.h>
#elif defined (Q_OS_IOS)
# include <UIKit/UIKit.h>
#endif

#include <QtGui/qguiapplication.h>
#include <QtGui/qpainter.h>
#include <QtGui/qpalette.h>
#include <QtGui/qstylehints.h>

#include <QtGui/private/qcoregraphics_p.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

namespace {
auto *loadImage(const QString &iconName)
{
    static constexpr std::pair<QLatin1StringView, NSString *> iconMap[] = {
        {"address-book-new"_L1, @"folder.circle"},
        {"application-exit"_L1, @"xmark.circle"},
        {"appointment-new"_L1, @"hourglass.badge.plus"},
        {"call-start"_L1, @"phone.arrow.up.right"},
        {"call-stop"_L1, @"phone.arrow.down.left"},
        {"edit-clear"_L1, @"clear"},
        {"edit-copy"_L1, @"doc.on.doc"},
        {"edit-cut"_L1, @"scissors"},
        {"edit-delete"_L1, @"delete.left"},
        {"edit-find"_L1, @"magnifyingglass"},
        {"edit-find-replace"_L1, @"arrow.up.left.and.down.right.magnifyingglass"},
        {"edit-paste"_L1, @"clipboard"},
        {"edit-redo"_L1, @"arrowshape.turn.up.right"},
        {"edit-undo"_L1, @"arrowshape.turn.up.left"},
    };
    const auto it = std::find_if(std::begin(iconMap), std::end(iconMap), [iconName](const auto &c){
        return c.first == iconName;
    });
    NSString *systemIconName = it != std::end(iconMap) ? it->second : iconName.toNSString();
#if defined(Q_OS_MACOS)
    return [NSImage imageWithSystemSymbolName:systemIconName accessibilityDescription:nil];
#elif defined(Q_OS_IOS)
    return [UIImage systemImageNamed:systemIconName];
#endif
}
}

QAppleIconEngine::QAppleIconEngine(const QString &iconName)
    : m_iconName(iconName), m_image(loadImage(iconName))
{
    if (m_image)
        [m_image retain];
}

QAppleIconEngine::~QAppleIconEngine()
{
    if (m_image)
        [m_image release];
}

QIconEngine *QAppleIconEngine::clone() const
{
    return new QAppleIconEngine(m_iconName);
}

QString QAppleIconEngine::key() const
{
    return u"QAppleIconEngine"_s;
}

QString QAppleIconEngine::iconName()
{
    return m_iconName;
}

bool QAppleIconEngine::isNull()
{
    return m_image == nullptr;
}

QList<QSize> QAppleIconEngine::availableIconSizes()
{
    const qreal devicePixelRatio = qGuiApp->devicePixelRatio();
    const QList<QSize> sizes = {
        {qRound(16 * devicePixelRatio), qRound(16 * devicePixelRatio)},
        {qRound(32 * devicePixelRatio), qRound(32 * devicePixelRatio)},
        {qRound(64 * devicePixelRatio), qRound(64 * devicePixelRatio)},
        {qRound(128 * devicePixelRatio), qRound(128 * devicePixelRatio)},
        {qRound(256 * devicePixelRatio), qRound(256 * devicePixelRatio)},
    };
    return sizes;
}

QList<QSize> QAppleIconEngine::availableSizes(QIcon::Mode, QIcon::State)
{
    return availableIconSizes();
}

QSize QAppleIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
    return QIconEngine::actualSize(size, mode, state);
}

QPixmap QAppleIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
    return scaledPixmap(size, mode, state, 1.0);
}

namespace {
#if defined(Q_OS_MACOS)
auto *configuredImage(const NSImage *image, const QColor &color)
{
    auto *config = [NSImageSymbolConfiguration configurationWithPointSize:48
                                               weight:NSFontWeightRegular
                                               scale:NSImageSymbolScaleLarge];
    if (@available(macOS 12, *)) {
        auto *primaryColor = [NSColor colorWithSRGBRed:color.redF()
                                                 green:color.greenF()
                                                  blue:color.blueF()
                                                 alpha:color.alphaF()];

        auto *colorConfig = [NSImageSymbolConfiguration configurationWithHierarchicalColor:primaryColor];
        config = [config configurationByApplyingConfiguration:colorConfig];
    }

    return [image imageWithSymbolConfiguration:config];
}
#elif defined(Q_OS_IOS)
auto *configuredImage(const UIImage *image, const QColor &color)
{
    auto *config = [UIImageSymbolConfiguration configurationWithPointSize:48
                                               weight:UIImageSymbolWeightRegular
                                               scale:UIImageSymbolScaleLarge];

    if (@available(iOS 15, *)) {
        auto *primaryColor = [UIColor colorWithRed:color.redF()
                                             green:color.greenF()
                                              blue:color.blueF()
                                             alpha:color.alphaF()];

        auto *colorConfig = [UIImageSymbolConfiguration configurationWithHierarchicalColor:primaryColor];
        config = [config configurationByApplyingConfiguration:colorConfig];
    }
    return [image imageByApplyingSymbolConfiguration:config];
}
#endif
}

namespace {
template <typename Image>
QPixmap imageToPixmap(const Image *image, QSizeF renderSize)
{
    if constexpr (std::is_same_v<Image, NSImage>)
        return qt_mac_toQPixmap(image, renderSize.toSize());
    else
        return QPixmap::fromImage(qt_mac_toQImage(image, renderSize.toSize()));
}
}

QPixmap QAppleIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
{
    const quint64 cacheKey = calculateCacheKey(mode, state);
    if (cacheKey != m_cacheKey || m_pixmap.size() != size || m_pixmap.devicePixelRatio() != scale) {
        QColor color;
        const QPalette palette;
        switch (mode) {
        case QIcon::Normal:
            color = palette.color(QPalette::Inactive, QPalette::Text);
            break;
        case QIcon::Disabled:
            color = palette.color(QPalette::Disabled, QPalette::Text);
            break;
        case QIcon::Active:
            color = palette.color(QPalette::Active, QPalette::Text);
            break;
        case QIcon::Selected:
            color = palette.color(QPalette::Active, QPalette::HighlightedText);
            break;
        }
        const auto *image = configuredImage(m_image, color);

        // the size we want is typically square, but the icon might not be. So
        // ask for a pixmap with the same aspect ratio as the icon, and then
        // center that within a pixmap of the requested size.
        QSizeF renderSize = size * scale;
        const bool aspectRatioAdjusted = image.size.width != image.size.height;
        if (aspectRatioAdjusted) {
            double aspectRatio = image.size.width / image.size.height;
            // don't grow
            if (aspectRatio < 1)
                renderSize.rwidth() = renderSize.height() * aspectRatio;
            else
                renderSize.rheight() = renderSize.width() / aspectRatio;
        }

        QPixmap iconPixmap = imageToPixmap(image, renderSize);
        iconPixmap.setDevicePixelRatio(scale);

        if (aspectRatioAdjusted) {
            m_pixmap = QPixmap(size * scale);
            m_pixmap.fill(Qt::transparent);
            m_pixmap.setDevicePixelRatio(scale);

            QPainter painter(&m_pixmap);
            const QSize offset = ((m_pixmap.deviceIndependentSize()
                                - iconPixmap.deviceIndependentSize()) / 2).toSize();
            painter.drawPixmap(offset.width(), offset.height(), iconPixmap);
        } else {
            m_pixmap = iconPixmap;
        }
        m_cacheKey = cacheKey;
    }
    return m_pixmap;
}

void QAppleIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
{
    const qreal scale = painter->device()->devicePixelRatio();
    // TODO: render the image directly if we don't have the pixmap yet and paint on an image
    painter->drawPixmap(rect, scaledPixmap(rect.size(), mode, state, scale));
}

QT_END_NAMESPACE