From 065af28bc2901a25058e041de0be8ea1d5cf05c0 Mon Sep 17 00:00:00 2001 From: der richter Date: Sun, 7 Sep 2025 17:18:30 +0200 Subject: [PATCH] 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. --- osdep/mac/dialog.swift | 15 ++++++++++----- osdep/mac/swift_compat.swift | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) 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