diff --git a/osdep/mac/dialog.swift b/osdep/mac/dialog.swift index 2223ba6a8b..718258d90b 100644 --- a/osdep/mac/dialog.swift +++ b/osdep/mac/dialog.swift @@ -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 } } diff --git a/osdep/mac/swift_compat.swift b/osdep/mac/swift_compat.swift index bac1abe082..08ad3f4ea9 100644 --- a/osdep/mac/swift_compat.swift +++ b/osdep/mac/swift_compat.swift @@ -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