Skip to content

Commit ba3def0

Browse files
committed
MacOS: Remove runtime version check and use deployment target version instead
1 parent 800060d commit ba3def0

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ if(NOT DEFINED CMAKE_CXX_STANDARD)
2929
set (CMAKE_CXX_STANDARD 23)
3030
endif()
3131

32+
message("Target: ${CMAKE_OSX_DEPLOYMENT_TARGET}")
33+
3234
add_subdirectory(src)
3335

3436
option(NFD_BUILD_TESTS "Build tests for nfd" OFF)

src/nfd_cocoa.m

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@
99
#include <Availability.h>
1010
#include "nfd.h"
1111

12-
// At least one of NFD_NEEDS_ALLOWEDCONTENTTYPES and NFD_NEEDS_ALLOWEDFILETYPES will be defined
13-
#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && defined(__MAC_12_0) && \
14-
__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_12_0
15-
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
16-
#define NFD_NEEDS_ALLOWEDCONTENTTYPES
17-
#if !defined(__MAC_OS_X_VERSION_MIN_ALLOWED) || !defined(__MAC_12_0) || \
18-
__MAC_OS_X_VERSION_MIN_ALLOWED < __MAC_12_0
19-
#define NFD_NEEDS_ALLOWEDFILETYPES
20-
#endif
12+
// MacOS is deprecating the allowedFileTypes property in favour of allowedContentTypes, so we have
13+
// to introduce this breaking change. Define NFD_MACOS_ALLOWEDCONTENTTYPES to 1 to have it set the
14+
// allowedContentTypes property of the SavePanel or OpenPanel. Define
15+
// NFD_MACOS_ALLOWEDCONTENTTYPES to 0 to have it set the allowedFileTypes property of the SavePanel
16+
// or OpenPanel. If NFD_MACOS_ALLOWEDCONTENTTYPES is undefined, then it will set it to 1 if
17+
// __MAC_OS_X_VERSION_MIN_REQUIRED >= 11.0, and 0 otherwise.
18+
#if !defined(NFD_MACOS_ALLOWEDCONTENTTYPES)
19+
#if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || !defined(__MAC_11_0) || \
20+
__MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_11_0
21+
#define NFD_MACOS_ALLOWEDCONTENTTYPES 0
2122
#else
22-
#define NFD_NEEDS_ALLOWEDFILETYPES
23+
#define NFD_MACOS_ALLOWEDCONTENTTYPES 1
24+
#endif
25+
#endif
26+
27+
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
28+
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
2329
#endif
2430

2531
static const char* g_errorstr = NULL;
@@ -40,7 +46,7 @@ static void NFDi_Free(void* ptr) {
4046
free(ptr);
4147
}
4248

43-
#if defined(NFD_NEEDS_ALLOWEDCONTENTTYPES)
49+
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
4450
// Returns an NSArray of UTType representing the content types.
4551
static NSArray* BuildAllowedContentTypes(const nfdnfilteritem_t* filterList,
4652
nfdfiltersize_t filterCount) {
@@ -81,9 +87,7 @@ static void NFDi_Free(void* ptr) {
8187

8288
return returnArray;
8389
}
84-
#endif
85-
86-
#if defined(NFD_NEEDS_ALLOWEDFILETYPES)
90+
#else
8791
// Returns an NSArray of NSString representing the file types.
8892
static NSArray* BuildAllowedFileTypes(const nfdnfilteritem_t* filterList,
8993
nfdfiltersize_t filterCount) {
@@ -130,21 +134,8 @@ static void AddFilterListToDialog(NSSavePanel* dialog,
130134
assert(filterList);
131135

132136
// Make NSArray of file types and set it on the dialog
133-
// We use setAllowedFileTypes or setAllowedContentTypes depending on the OS version
134-
#if defined(NFD_NEEDS_ALLOWEDCONTENTTYPES) && defined(NFD_NEEDS_ALLOWEDFILETYPES)
135-
// If both are needed, it means we have to do a runtime check
136-
if (@available(macOS 12.0, *)) {
137-
NSArray* allowedContentTypes = BuildAllowedContentTypes(filterList, filterCount);
138-
[dialog setAllowedContentTypes:allowedContentTypes];
139-
} else {
140-
NSArray* allowedFileTypes = BuildAllowedFileTypes(filterList, filterCount);
141-
// Unfortunately @available doesn't silence deprecation warnings so we need these pragmas
142-
#pragma clang diagnostic push
143-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
144-
[dialog setAllowedFileTypes:allowedFileTypes];
145-
#pragma clang diagnostic pop
146-
}
147-
#elif defined(NFD_NEEDS_ALLOWEDCONTENTTYPES)
137+
// We use setAllowedFileTypes or setAllowedContentTypes depending on the deployment target
138+
#if NFD_MACOS_ALLOWEDCONTENTTYPES == 1
148139
NSArray* allowedContentTypes = BuildAllowedContentTypes(filterList, filterCount);
149140
[dialog setAllowedContentTypes:allowedContentTypes];
150141
#else

0 commit comments

Comments
 (0)