mac/dialog: fix build for macOS older than 11
UniformTypeIdentifiers is only available since macOS 11.
added in 6b0e4f7 and #15859.
guard the allowedContentTypes UTType property of the NSOpenPanel and
add a dummy struct UTType and dummy allowedContentTypes property on
macOS versions that don't have those available.
This commit is contained in:
1 parent
a4d818b4f9
commit
065af28bc2
2 files changed
+25
-5
No files matched your search
+10
-5
@@ -16,7 +16,9 @@
|
||||
*/
|
||||
|
||||
import Cocoa
|
||||
#if HAVE_MACOS_11_FEATURES
|
||||
import UniformTypeIdentifiers
|
||||
#endif
|
||||
|
||||
class Dialog: NSObject, NSWindowDelegate, NSOpenSavePanelDelegate {
|
||||
var option: OptionHelper?
|
||||
@@ -26,30 +28,33 @@ class Dialog: NSObject, NSWindowDelegate, NSOpenSavePanelDelegate {
|
||||
}
|
||||
|
||||
func openFiles(title: String? = nil, path: URL? = nil) -> [String]? {
|
||||
let types: [UTType] = (TypeHelper.toStringArray(option?.root.video_exts) +
|
||||
let types: [String] = TypeHelper.toStringArray(option?.root.video_exts) +
|
||||
TypeHelper.toStringArray(option?.root.audio_exts) +
|
||||
TypeHelper.toStringArray(option?.root.image_exts) +
|
||||
TypeHelper.toStringArray(option?.root.archive_exts) +
|
||||
TypeHelper.toStringArray(option?.root.playlist_exts)).compactMap { UTType(filenameExtension: $0) }
|
||||
TypeHelper.toStringArray(option?.root.playlist_exts)
|
||||
return open(title: title, path: path, types: types)
|
||||
}
|
||||
|
||||
func openPlaylist(title: String? = nil, path: URL? = nil) -> String? {
|
||||
let types: [UTType] = TypeHelper.toStringArray(option?.root.playlist_exts).compactMap { UTType(filenameExtension: $0) }
|
||||
let types: [String] = TypeHelper.toStringArray(option?.root.playlist_exts)
|
||||
return open(title: title, path: path, directories: false, multiple: false, types: types)?.first
|
||||
}
|
||||
|
||||
func open(title: String? = nil, path: URL? = nil, files: Bool = true,
|
||||
directories: Bool = true, multiple: Bool = true, types: [UTType] = []) -> [String]? {
|
||||
directories: Bool = true, multiple: Bool = true, types: [String] = []) -> [String]? {
|
||||
let panel = NSOpenPanel()
|
||||
panel.title = title ?? panel.title
|
||||
panel.directoryURL = path
|
||||
panel.canChooseFiles = files
|
||||
panel.canChooseDirectories = directories
|
||||
panel.allowsMultipleSelection = multiple
|
||||
panel.allowedContentTypes = types
|
||||
panel.delegate = self
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
panel.allowedContentTypes = types.compactMap { UTType(filenameExtension: $0) }
|
||||
}
|
||||
|
||||
if panel.runModal() == .OK {
|
||||
return panel.urls.map { $0.path }
|
||||
}
|
||||
|
||||
@@ -51,3 +51,18 @@ extension CGColorSpace {
|
||||
static let itur_2100_PQ: CFString = kCGColorSpaceITUR_2100_PQ
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !HAVE_MACOS_11_FEATURES
|
||||
@available(macOS 11.0, *)
|
||||
public struct UTType: Sendable {
|
||||
public init?(filenameExtension: String) {}
|
||||
}
|
||||
|
||||
extension NSSavePanel {
|
||||
@available(macOS 11.0, *)
|
||||
public var allowedContentTypes: [UTType] {
|
||||
get { return [] }
|
||||
set { _ = newValue }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user