From 368b8139c732baae9e587175ca118f50d5ccd0dc Mon Sep 17 00:00:00 2001 From: ilya-fedin Date: Fri, 26 Jun 2020 18:22:41 +0400 Subject: [PATCH] Use public API to convert QPixmap to NSImage (#75) --- Telegram/SourceFiles/platform/mac/mac_touchbar.mm | 10 ++++------ .../platform/mac/notifications_manager_mac.mm | 4 +--- Telegram/SourceFiles/platform/mac/specific_mac.h | 4 ++++ Telegram/SourceFiles/platform/mac/specific_mac.mm | 12 ++++++++++++ Telegram/SourceFiles/platform/mac/specific_mac_p.mm | 4 +--- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm index 47da8a592..fc0e85b48 100644 --- a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm @@ -42,8 +42,6 @@ #include "facades.h" #include "app.h" -NSImage *qt_mac_create_nsimage(const QPixmap &pm); - namespace { //https://developer.apple.com/design/human-interface-guidelines/macos/touch-bar/touch-bar-icons-and-images/ constexpr auto kIdealIconSize = 36; @@ -143,7 +141,7 @@ NSImage *CreateNSImageFromStyleIcon(const style::icon &icon, int size = kIdealIc const auto instance = icon.instance(QColor(255, 255, 255, 255), 100); auto pixmap = QPixmap::fromImage(instance); pixmap.setDevicePixelRatio(cRetinaFactor()); - NSImage *image = [qt_mac_create_nsimage(pixmap) autorelease]; + NSImage *image = [Platform::ToNSImage(pixmap) autorelease]; [image setSize:NSMakeSize(size, size)]; return image; } @@ -163,7 +161,7 @@ NSImage *CreateNSImageFromEmoji(EmojiPtr emoji) { 0, 0); #endif // OS_MAC_OLD - return [qt_mac_create_nsimage(pixmap) autorelease]; + return [Platform::ToNSImage(pixmap) autorelease]; } int WidthFromString(NSString *s) { @@ -649,7 +647,7 @@ void AppendEmojiPacks(std::vector &to) { - (void) updateImage:(QPixmap)pixmap { NSButton *button = self.view; - NSImage *image = [qt_mac_create_nsimage(pixmap) autorelease]; + NSImage *image = [Platform::ToNSImage(pixmap) autorelease]; [image setSize:NSMakeSize(kCircleDiameter, kCircleDiameter)]; [button.cell setImage:image]; } @@ -709,7 +707,7 @@ void AppendEmojiPacks(std::vector &to) { - (void)updateImage { const auto size = _dimensions .scaled(kCircleDiameter, kCircleDiameter, Qt::KeepAspectRatio); - _imageView.image = [qt_mac_create_nsimage( + _imageView.image = [Platform::ToNSImage( _image->pixSingle( size.width(), size.height(), diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index 4bdcc0108..c9637e14e 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -45,8 +45,6 @@ using Manager = Platform::Notifications::Manager; } // namespace -NSImage *qt_mac_create_nsimage(const QPixmap &pm); - @interface NotificationDelegate : NSObject { } @@ -244,7 +242,7 @@ void Manager::Private::showNotification( auto userpic = peer->isSelf() ? Ui::EmptyUserpic::GenerateSavedMessages(st::notifyMacPhotoSize) : peer->genUserpic(userpicView, st::notifyMacPhotoSize); - NSImage *img = [qt_mac_create_nsimage(userpic) autorelease]; + NSImage *img = [ToNSImage(userpic) autorelease]; [notification setContentImage:img]; } diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.h b/Telegram/SourceFiles/platform/mac/specific_mac.h index fc591e968..6721430b6 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac.h +++ b/Telegram/SourceFiles/platform/mac/specific_mac.h @@ -10,6 +10,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/platform_specific.h" #include "platform/mac/specific_mac_p.h" +Q_FORWARD_DECLARE_OBJC_CLASS(NSImage); + namespace Data { class LocationPoint; } // namespace Data @@ -28,6 +30,8 @@ inline QImage GetImageFromClipboard() { return {}; } +NSImage *ToNSImage(const QPixmap &pixmap); + namespace ThirdParty { inline void start() { diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.mm b/Telegram/SourceFiles/platform/mac/specific_mac.mm index 00ab6fec0..4f5a59e06 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac.mm @@ -266,6 +266,18 @@ bool AutostartSupported() { return false; } +NSImage *ToNSImage(const QPixmap &pixmap) { + if (pixmap.isNull()) + return 0; + CGImageRef cgimage = pixmap.toImage().toCGImage(); + NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage]; + NSImage *image = [[NSImage alloc] init]; + [image addRepresentation:bitmapRep]; + [bitmapRep release]; + CFRelease(cgimage); + return image; +} + } // namespace Platform void psNewVersion() { diff --git a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm index 3864fcd9a..a5acc6d6a 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm @@ -40,8 +40,6 @@ constexpr auto kIgnoreActivationTimeoutMs = 500; } // namespace -NSImage *qt_mac_create_nsimage(const QPixmap &pm); - using Platform::Q2NSString; using Platform::NS2QString; @@ -211,7 +209,7 @@ void SetApplicationIcon(const QIcon &icon) { if (!icon.isNull()) { auto pixmap = icon.pixmap(1024, 1024); pixmap.setDevicePixelRatio(cRetinaFactor()); - image = static_cast(qt_mac_create_nsimage(pixmap)); + image = static_cast(ToNSImage(pixmap)); } [[NSApplication sharedApplication] setApplicationIconImage:image]; [image release];