Files
test2/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
Ankit Meel f04a7a07e3 macOS: Add open files to system recent files
Completes the TODO in GHOST_SystemPathsCocoa::addToSystemRecentFiles
Also renames the filename parameter to the more appropriate filepath.

The recently opened/saved file will now also show up in:
- Blender Dock icon > Right click.
- Three finger swipe down in Open Blender i.e., App Expose

Based on a earlier contribution by @jenkm.

Pull Request: https://projects.blender.org/blender/blender/pulls/107174
2023-04-20 23:53:08 +05:30

123 lines
3.6 KiB
Plaintext

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2010 Blender Foundation */
#import <AppKit/NSDocumentController.h>
#import <Foundation/Foundation.h>
#include "GHOST_Debug.hh"
#include "GHOST_SystemPathsCocoa.hh"
#pragma mark initialization/finalization
GHOST_SystemPathsCocoa::GHOST_SystemPathsCocoa() {}
GHOST_SystemPathsCocoa::~GHOST_SystemPathsCocoa() {}
#pragma mark Base directories retrieval
static const char *GetApplicationSupportDir(const char *versionstr,
const NSSearchPathDomainMask mask,
char *tempPath,
const std::size_t len_tempPath)
{
@autoreleasepool {
const NSArray *const paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, mask, YES);
if ([paths count] == 0) {
return NULL;
}
const NSString *const basePath = [paths objectAtIndex:0];
snprintf(tempPath,
len_tempPath,
"%s/Blender/%s",
[basePath cStringUsingEncoding:NSASCIIStringEncoding],
versionstr);
}
return tempPath;
}
const char *GHOST_SystemPathsCocoa::getSystemDir(int, const char *versionstr) const
{
static char tempPath[512] = "";
return GetApplicationSupportDir(versionstr, NSLocalDomainMask, tempPath, sizeof(tempPath));
}
const char *GHOST_SystemPathsCocoa::getUserDir(int, const char *versionstr) const
{
static char tempPath[512] = "";
return GetApplicationSupportDir(versionstr, NSUserDomainMask, tempPath, sizeof(tempPath));
}
const char *GHOST_SystemPathsCocoa::getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
{
static char tempPath[512] = "";
@autoreleasepool {
NSSearchPathDirectory ns_directory;
switch (type) {
case GHOST_kUserSpecialDirDesktop:
ns_directory = NSDesktopDirectory;
break;
case GHOST_kUserSpecialDirDocuments:
ns_directory = NSDocumentDirectory;
break;
case GHOST_kUserSpecialDirDownloads:
ns_directory = NSDownloadsDirectory;
break;
case GHOST_kUserSpecialDirMusic:
ns_directory = NSMusicDirectory;
break;
case GHOST_kUserSpecialDirPictures:
ns_directory = NSPicturesDirectory;
break;
case GHOST_kUserSpecialDirVideos:
ns_directory = NSMoviesDirectory;
break;
case GHOST_kUserSpecialDirCaches:
ns_directory = NSCachesDirectory;
break;
default:
GHOST_ASSERT(
false,
"GHOST_SystemPathsCocoa::getUserSpecialDir(): Invalid enum value for type parameter");
return NULL;
}
const NSArray *const paths = NSSearchPathForDirectoriesInDomains(
ns_directory, NSUserDomainMask, YES);
if ([paths count] == 0) {
return NULL;
}
const NSString *const basePath = [paths objectAtIndex:0];
strncpy(tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding], sizeof(tempPath));
}
return tempPath;
}
const char *GHOST_SystemPathsCocoa::getBinaryDir() const
{
static char tempPath[512] = "";
@autoreleasepool {
const NSString *const basePath = [[NSBundle mainBundle] bundlePath];
if (basePath == nil) {
return NULL;
}
strcpy(tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
}
return tempPath;
}
void GHOST_SystemPathsCocoa::addToSystemRecentFiles(const char *filepath) const
{
@autoreleasepool {
NSURL *const file_url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:filepath]];
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:file_url];
}
}