Repository: libsdl-org/SDL
File: src/video/cocoa/SDL_cocoaclipboard.m (Objective-C)

Code before:
        NSInteger count;

        pasteboard = [NSPasteboard generalPasteboard];
        count = [pasteboard changeCount];
        if (count != data.clipboard_count) {
            if (count) {
                int nformats = 0;
                char **new_mime_types = GetMimeTypes(&nformats);
                if (new_mime_types) {
                    SDL_SendClipboardUpdate(false, new_mime_types, nformats);
                }
            }
            data.clipboard_count = count;
        }
    }
}

bool Cocoa_SetClipboardData(SDL_VideoDevice *_this)
{
    @autoreleasepool {
        SDL_CocoaVideoData *data = (__bridge SDL_CocoaVideoData *)_this->internal;
        NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];

        // SetClipboardText specialisation so that copied content works outside SDL
        if (_this->clipboard_callback && _this->num_clipboard_mime_types == 1) {
            if (strncmp(_this->clipboard_mime_types[0], "text/plain;charset=utf-8", 24) == 0) {
                [pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:nil];
                [pasteboard setString:@((char *)_this->clipboard_userdata) forType:NSPasteboardTypeString];
                data.clipboard_count = [pasteboard changeCount];
                return true;
            }
        }

        NSPasteboardItem *newItem = [NSPasteboardItem new];
        NSMutableArray *utiTypes = [NSMutableArray new];
        Cocoa_PasteboardDataProvider *provider = [[Cocoa_PasteboardDataProvider alloc] initWith: _this->clipboard_callback userData: _this->clipboard_userdata];
        BOOL itemResult = FALSE;
        BOOL writeResult = FALSE;

        if (_this->clipboard_callback) {
            for (int i = 0; i < _this->num_clipboard_mime_types; i++) {
                CFStringRef mimeType = CFStringCreateWithCString(NULL, _this->clipboard_mime_types[i], kCFStringEncodingUTF8);
                CFStringRef utiType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
                CFRelease(mimeType);

                [utiTypes addObject: (__bridge NSString *)utiType];
                CFRelease(utiType);
            }
            itemResult = [newItem setDataProvider: provider forTypes: utiTypes];
            if (itemResult == FALSE) {
                return SDL_SetError("Unable to set clipboard item data");

Review comment (suggestion):
```suggestion
        // SetClipboardText specialization so text is available after the app quits
        if (_this->clipboard_callback && _this->num_clipboard_mime_types == 1) {
            if (SDL_strncmp(_this->clipboard_mime_types[0], "text/plain;charset=utf-8", 24) == 0) {
```

Code after:
        NSInteger count;

        pasteboard = [NSPasteboard generalPasteboard];
        count = [pasteboard changeCount];
        if (count != data.clipboard_count) {
            if (count) {
                int nformats = 0;
                char **new_mime_types = GetMimeTypes(&nformats);
                if (new_mime_types) {
                    SDL_SendClipboardUpdate(false, new_mime_types, nformats);
                }
            }
            data.clipboard_count = count;
        }
    }
}

bool Cocoa_SetClipboardData(SDL_VideoDevice *_this)
{
    @autoreleasepool {
        SDL_CocoaVideoData *data = (__bridge SDL_CocoaVideoData *)_this->internal;
        NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];

        // SetClipboardText specialization so text is available after the app quits
        if (_this->clipboard_callback && _this->num_clipboard_mime_types == 1) {
            if (SDL_strncmp(_this->clipboard_mime_types[0], "text/plain;charset=utf-8", 24) == 0) {
                [pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:nil];
                [pasteboard setString:@((char *)_this->clipboard_userdata) forType:NSPasteboardTypeString];
                data.clipboard_count = [pasteboard changeCount];
                return true;
            }
        }

        NSPasteboardItem *newItem = [NSPasteboardItem new];
        NSMutableArray *utiTypes = [NSMutableArray new];
        Cocoa_PasteboardDataProvider *provider = [[Cocoa_PasteboardDataProvider alloc] initWith: _this->clipboard_callback userData: _this->clipboard_userdata];
        BOOL itemResult = FALSE;
        BOOL writeResult = FALSE;

        if (_this->clipboard_callback) {
            for (int i = 0; i < _this->num_clipboard_mime_types; i++) {
                CFStringRef mimeType = CFStringCreateWithCString(NULL, _this->clipboard_mime_types[i], kCFStringEncodingUTF8);
                CFStringRef utiType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
                CFRelease(mimeType);

                [utiTypes addObject: (__bridge NSString *)utiType];
                CFRelease(utiType);
            }
            itemResult = [newItem setDataProvider: provider forTypes: utiTypes];
            if (itemResult == FALSE) {
                return SDL_SetError("Unable to set clipboard item data");