From 17c7d67b964bc2cb31905b397854514066d7fcd4 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 22 Sep 2026 16:10:37 +0200 Subject: [PATCH] Use some modern APIs and fix warnings on the latest macOS SDK --- Base.lproj/Queue.xib | 46 +++++---- Base.lproj/QueueOptions.xib | 95 ++++++++++--------- Classes/ArtworkSelectorViewItem.swift | 16 +++- Classes/ArtworkSelectorViewItem.xib | 9 +- Classes/DocumentWindowController.swift | 9 +- Classes/PresetManager.swift | 16 ++-- .../PropertyView/MovieViewController.swift | 23 ++--- Classes/Queue.swift | 9 +- Classes/QueueController.swift | 62 +++++++----- Classes/SaveOptions.swift | 11 ++- Classes/TokenFieldCell.swift | 2 +- Classes/TracksViewController.swift | 44 ++++----- PasteboardItem.swift | 64 +++++++++++++ Subler.xcodeproj/project.pbxproj | 4 + 14 files changed, 254 insertions(+), 156 deletions(-) create mode 100644 PasteboardItem.swift diff --git a/Base.lproj/Queue.xib b/Base.lproj/Queue.xib index b4f695a..e973315 100755 --- a/Base.lproj/Queue.xib +++ b/Base.lproj/Queue.xib @@ -1,8 +1,8 @@ - + - + @@ -20,26 +20,26 @@ - + - + - + - + - + @@ -52,26 +52,23 @@ - + - - - + + - + - - + + + + + + + + + @@ -100,7 +106,7 @@ - + @@ -117,7 +123,7 @@ - + @@ -138,7 +144,7 @@ - + diff --git a/Base.lproj/QueueOptions.xib b/Base.lproj/QueueOptions.xib index 53878e7..2be513f 100644 --- a/Base.lproj/QueueOptions.xib +++ b/Base.lproj/QueueOptions.xib @@ -1,8 +1,8 @@ - + - + @@ -15,12 +15,12 @@ - - + + - + @@ -122,7 +122,7 @@ - + @@ -130,7 +130,7 @@ - + @@ -138,7 +138,7 @@ - + @@ -149,7 +149,7 @@ - + @@ -168,7 +168,7 @@ - + @@ -176,7 +176,7 @@ - + @@ -208,7 +208,7 @@ - + @@ -235,7 +235,7 @@ - + @@ -262,7 +262,7 @@ - + @@ -276,7 +276,7 @@ - + @@ -290,7 +290,7 @@ - + @@ -318,7 +318,7 @@ - + @@ -329,7 +329,7 @@ - + @@ -358,7 +358,7 @@ - + @@ -403,7 +403,7 @@ - + @@ -431,7 +431,7 @@ - + @@ -442,7 +442,7 @@ - + @@ -470,7 +470,7 @@ - + @@ -587,7 +587,7 @@ - + @@ -612,7 +612,7 @@ - + @@ -641,7 +641,7 @@ - + @@ -694,7 +694,7 @@ - + @@ -705,7 +705,7 @@ - + @@ -733,7 +733,7 @@ - + @@ -763,7 +763,7 @@ - + @@ -843,6 +843,7 @@ + diff --git a/Classes/ArtworkSelectorViewItem.swift b/Classes/ArtworkSelectorViewItem.swift index f9a8d74..73b3054 100644 --- a/Classes/ArtworkSelectorViewItem.swift +++ b/Classes/ArtworkSelectorViewItem.swift @@ -10,7 +10,7 @@ import AVFoundation final class ArtworkSelectorViewItemLabel : NSTextField { - @IBInspectable var highlightColor: NSColor = .alternateSelectedControlColor + @IBInspectable var highlightColor: NSColor = .selectedContentBackgroundColor @IBInspectable var highlightTextColor: NSColor = .alternateSelectedControlTextColor @IBInspectable var cornerRadius: CGFloat = 3 @@ -162,7 +162,7 @@ final class ArtworkSelectorViewItemView: NSView { backgroundLayer.anchorPoint = CGPoint.zero backgroundLayer.position = CGPoint(x: 0, y: paddingBottom) - backgroundLayer.backgroundColor = NSColor.controlHighlightColor.cgColor + backgroundLayer.backgroundColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor backgroundLayer.cornerRadius = 8 backgroundLayer.isHidden = true backgroundLayer.isOpaque = true @@ -171,7 +171,7 @@ final class ArtworkSelectorViewItemView: NSView { emptyLayer.position = CGPoint(x: padding, y: padding + paddingBottom) emptyLayer.lineWidth = 3.0 emptyLayer.lineDashPattern = [12,5] - emptyLayer.strokeColor = NSColor.secondarySelectedControlColor.cgColor + emptyLayer.strokeColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor emptyLayer.fillColor = NSColor.windowBackgroundColor.cgColor emptyLayer.isOpaque = true @@ -197,7 +197,14 @@ final class ArtworkSelectorViewItemView: NSView { } private func updateBackgroundColor() { - if #available(OSX 10.14, *) { + if #available(macOS 11, *) { + effectiveAppearance.performAsCurrentDrawingAppearance { + imageLayer.shadowColor = NSColor.labelColor.cgColor + backgroundLayer.backgroundColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor + emptyLayer.strokeColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor + emptyLayer.fillColor = NSColor.windowBackgroundColor.cgColor + } + } else { let saved = NSAppearance.current NSAppearance.current = effectiveAppearance @@ -337,6 +344,7 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem { // MARK: Actions + // FIXME override func mouseUp(with event: NSEvent) { if event.clickCount > 1, let action = doubleAction { target?.performSelector(onMainThread: action, with: nil, waitUntilDone: true) diff --git a/Classes/ArtworkSelectorViewItem.xib b/Classes/ArtworkSelectorViewItem.xib index 43b5561..4ea10f3 100644 --- a/Classes/ArtworkSelectorViewItem.xib +++ b/Classes/ArtworkSelectorViewItem.xib @@ -1,8 +1,8 @@ - + - + @@ -19,7 +19,7 @@ - + @@ -27,7 +27,7 @@ - + @@ -40,7 +40,6 @@ - diff --git a/Classes/DocumentWindowController.swift b/Classes/DocumentWindowController.swift index 312cb0c..c23a74c 100644 --- a/Classes/DocumentWindowController.swift +++ b/Classes/DocumentWindowController.swift @@ -7,6 +7,7 @@ import Cocoa import MP42Foundation +import UniformTypeIdentifiers final class DocumentWindowController: NSWindowController, TracksViewControllerDelegate, MetadataSearchViewControllerDelegate, FileImportControllerDelegate, ProgressViewControllerDelegate, NSDraggingDestination, NSUserInterfaceValidations { @@ -464,7 +465,13 @@ final class DocumentWindowController: NSWindowController, TracksViewControllerDe panel.allowsMultipleSelection = false panel.canChooseFiles = true panel.canChooseDirectories = false - panel.allowedFileTypes = ["mp4", "m4v", "m4a", "xml", "nfo"] + if #available(macOS 12, *) { + panel.allowedContentTypes = [.mpeg4Movie, .mpeg4Audio, + .appleProtectedMPEG4Video, + .xml, UTType(filenameExtension: "nfo")!] + } else { + panel.allowedFileTypes = ["mp4", "m4v", "m4a", "xml", "nfo"] + } panel.beginSheetModal(for: windowForSheet) { (response) in if response == NSApplication.ModalResponse.OK, let url = panel.url { diff --git a/Classes/PresetManager.swift b/Classes/PresetManager.swift index 7ef0743..b6e96b1 100644 --- a/Classes/PresetManager.swift +++ b/Classes/PresetManager.swift @@ -117,12 +117,12 @@ final class PresetManager { let resourceValues = try fileURL.resourceValues(forKeys: [.isDirectoryKey]) if resourceValues.isDirectory == false, let version = version(of: fileURL) { let data = try Data(contentsOf: fileURL) - let unarchiver = NSKeyedUnarchiver(forReadingWith: data) - unarchiver.requiresSecureCoding = true - defer { unarchiver.finishDecoding() } + if let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: data) { + defer { unarchiver.finishDecoding() } - if version == 2, let preset = try? unarchiver.decodeTopLevelObject(of: [MetadataPreset.self], forKey: NSKeyedArchiveRootObjectKey) as? MetadataPreset { - return preset + if version == 2, let preset = try? unarchiver.decodeTopLevelObject(of: [MetadataPreset.self], forKey: NSKeyedArchiveRootObjectKey) as? MetadataPreset { + return preset + } } } throw LoadError.unsupportedFile @@ -148,13 +148,11 @@ final class PresetManager { } private func save(preset: Preset) throws { - let data = NSMutableData() - let archiver = NSKeyedArchiver(forWritingWith: data) - archiver.requiresSecureCoding = true + let archiver = NSKeyedArchiver(requiringSecureCoding: true) archiver.encode(preset, forKey: NSKeyedArchiveRootObjectKey) archiver.finishEncoding() - try data.write(to: preset.fileURL, options: [.atomic]) + try archiver.encodedData.write(to: preset.fileURL, options: [.atomic]) preset.changed = false } diff --git a/Classes/PropertyView/MovieViewController.swift b/Classes/PropertyView/MovieViewController.swift index c8d2db8..b7360c8 100644 --- a/Classes/PropertyView/MovieViewController.swift +++ b/Classes/PropertyView/MovieViewController.swift @@ -932,7 +932,11 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie panel.allowsMultipleSelection = true panel.canChooseFiles = true panel.canChooseDirectories = false - panel.allowedFileTypes = ["public.image"] + if #available(macOS 11, *) { + panel.allowedContentTypes = [.image] + } else { + panel.allowedFileTypes = ["public.image"] + } guard let window = view.window else { return } @@ -994,14 +998,11 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, writePromiseTo url: URL, completionHandler: @escaping (Error?) -> Void) { if let userInfo = filePromiseProvider.userInfo as? [String: AnyObject] { do { - if let indexPathData = userInfo[FilePromiseProvider.UserInfoKeys.indexPathKey] as? Data { - if let indexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? IndexPath { - let item = artworks[indexPath.last!] - if let image = item.imageValue { - try image.data?.write(to: url) - completionHandler(nil) - } - } + if let indexPathData = userInfo[FilePromiseProvider.UserInfoKeys.indexPathKey] as? Data, + let indexPath = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSIndexPath.classForCoder()], from: indexPathData) as? IndexPath, + let image = artworks[indexPath.last!].imageValue { + try image.data?.write(to: url) + completionHandler(nil) } } catch { fatalError("failed to unarchive indexPath from promise provider.") @@ -1114,7 +1115,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie if let pasteboardItem = draggingItem.item as? NSPasteboardItem { do { if let indexPathData = pasteboardItem.data(forType: .artworkDragType), - let itemIndexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? IndexPath { + let itemIndexPath = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSIndexPath.classForCoder()], from:indexPathData) as? IndexPath { indexes.insert(itemIndexPath) } } catch { @@ -1246,7 +1247,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie for pasteboardItem in items { do { if let indexPathData = pasteboardItem.data(forType: .artworkDragType), - let itemIndexPath = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(indexPathData) as? IndexPath { + let itemIndexPath = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSIndexPath.classForCoder()], from:indexPathData) as? IndexPath { indexes.insert(itemIndexPath) } } catch { diff --git a/Classes/Queue.swift b/Classes/Queue.swift index f084df5..f6f10a7 100644 --- a/Classes/Queue.swift +++ b/Classes/Queue.swift @@ -46,8 +46,7 @@ final class Queue { do { let data = try Data(contentsOf: url) - let unarchiver = NSKeyedUnarchiver(forReadingWith: data) - unarchiver.requiresSecureCoding = true + let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data) if let decodedItems = try unarchiver.decodeTopLevelObject(of: [NSArray.classForCoder(), NSMutableArray.classForCoder(), QueueItem.classForCoder()], forKey: NSKeyedArchiveRootObjectKey) as? [QueueItem] { items = decodedItems } else { @@ -63,13 +62,11 @@ final class Queue { func saveToDisk() throws { arrayQueue.sync { - let data = NSMutableData() - let archiver = NSKeyedArchiver(forWritingWith: data) - archiver.requiresSecureCoding = true + let archiver = NSKeyedArchiver(requiringSecureCoding: true) archiver.encode(items, forKey: NSKeyedArchiveRootObjectKey) archiver.finishEncoding() - data.write(to: url, atomically: true) + try? archiver.encodedData.write(to: url, options: [.atomic]) } } diff --git a/Classes/QueueController.swift b/Classes/QueueController.swift index a7fe81f..c90c619 100644 --- a/Classes/QueueController.swift +++ b/Classes/QueueController.swift @@ -19,10 +19,15 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel private var windowController: OptionsViewController? private let toolbarDelegate = QueueToolbarDelegate() - private let tablePasteboardType = NSPasteboard.PasteboardType("SublerQueueTableViewDataType") private lazy var docImg: NSImage = { // Load a generic movie icon to display in the table view - let img = NSWorkspace.shared.icon(forFileType: "mov") + let img = { + if #available(macOS 12, *) { + return NSWorkspace.shared.icon(for: .quickTimeMovie) + } else { + return NSWorkspace.shared.icon(forFileType: "mov") + } + }() img.size = NSSize(width: 16, height: 16) return img }() @@ -74,7 +79,7 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel } self.window?.toolbar = toolbar - table.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL, tablePasteboardType]) + table.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL, .tableViewIndex]) progressBar.isHidden = true let main = OperationQueue.main @@ -771,41 +776,48 @@ final class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDel //MARK: Drag & Drop - func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool { - let data = try? NSKeyedArchiver.archivedData(withRootObject: rowIndexes, requiringSecureCoding: true) - pboard.declareTypes([tablePasteboardType], owner: self) - pboard.setData(data, forType: tablePasteboardType) - return true + func tableView(_ tableView: NSTableView, + pasteboardWriterForRow row: Int) -> (any NSPasteboardWriting)? { + let item = PasteboardItem(index: row, type: .tableViewIndex) + return item } - func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { - if info.draggingSource == nil { - tableView.setDropRow(row, dropOperation: .above) + func tableView(_ tableView: NSTableView, + validateDrop info: NSDraggingInfo, + proposedRow row: Int, + proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { + guard dropOperation == .above else { return [] } + + if let source = info.draggingSource as? NSTableView, + tableView == source + { + return .move + } else if info.draggingSource == nil { return .copy - } else if let source = info.draggingSource as? NSTableView, tableView == source && dropOperation == .above { - return .every } else { return [] } } - func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool { - let pboard = info.draggingPasteboard - - if let source = info.draggingSource as? NSTableView, source == tableView, let rowData = pboard.data(forType: tablePasteboardType), let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: rowData) as? IndexSet { + func tableView(_ tableView: NSTableView, + acceptDrop info: NSDraggingInfo, + row: Int, + dropOperation: NSTableView.DropOperation) -> Bool { + guard let pasteboardItems = info.draggingPasteboard.pasteboardItems + else { return false } + if let source = info.draggingSource as? NSTableView, + source == tableView + { + let rowIndexes = IndexSet(pasteboardItems.compactMap { $0.integer(forType: .tableViewIndex) }) let items = queue.items(at: rowIndexes) move(items: items, at: row) return true - - } else { - - if pboard.types?.contains(NSPasteboard.PasteboardType.fileURL) ?? false { - if let items = pboard.readObjects(forClasses: [NSURL.classForCoder()], options: [:]) as? [URL] { - insert(contentOf: items, at: row) - } - return true + } else if info.draggingPasteboard.types?.contains(NSPasteboard.PasteboardType.fileURL) ?? false { + if let items = info.draggingPasteboard.readObjects(forClasses: [NSURL.classForCoder()], options: [:]) as? [URL] { + insert(contentOf: items, at: row) } + return true } return false diff --git a/Classes/SaveOptions.swift b/Classes/SaveOptions.swift index bd6a074..b586a88 100644 --- a/Classes/SaveOptions.swift +++ b/Classes/SaveOptions.swift @@ -43,10 +43,15 @@ final class SaveOptions: NSViewController { let types = doc.writableTypes(for: .saveAsOperation) fileFormat.removeAllItems() - + for type in types { - let name = UTTypeCopyDescription(type as CFString)?.takeRetainedValue() as String? ?? type - fileFormat.addItem(withTitle: name) + if #available(macOS 11, *) { + let name = UTType(type)?.localizedDescription ?? type + fileFormat.addItem(withTitle: name) + } else { + let name = UTTypeCopyDescription(type as CFString)?.takeRetainedValue() as String? ?? type + fileFormat.addItem(withTitle: name) + } } } diff --git a/Classes/TokenFieldCell.swift b/Classes/TokenFieldCell.swift index 6377594..e7f6c43 100644 --- a/Classes/TokenFieldCell.swift +++ b/Classes/TokenFieldCell.swift @@ -18,7 +18,7 @@ final class TokenFieldCell : NSTokenFieldCell { set (newBackgroundStyle) { super.backgroundStyle = newBackgroundStyle if newBackgroundStyle == NSView.BackgroundStyle.emphasized { - textColor = NSColor.controlHighlightColor + textColor = NSColor.highlightColor } else { textColor = NSColor.controlTextColor } diff --git a/Classes/TracksViewController.swift b/Classes/TracksViewController.swift index c8dbfc8..97a710d 100644 --- a/Classes/TracksViewController.swift +++ b/Classes/TracksViewController.swift @@ -30,7 +30,6 @@ final class TracksViewController: NSViewController, NSTableViewDataSource, NSTab } @IBOutlet var tracksTable: ExpandedTableView! - private let pasteboardType = NSPasteboard.PasteboardType(rawValue: "SublerTableViewDataType") private static let languagesMenu: NSMenu = { let menu = NSMenu() @@ -50,7 +49,7 @@ final class TracksViewController: NSViewController, NSTableViewDataSource, NSTab override func viewDidLoad() { super.viewDidLoad() - tracksTable.registerForDraggedTypes([pasteboardType]) + tracksTable.registerForDraggedTypes([.tableViewIndex]) tracksTable.doubleAction = #selector(doubleClickAction) tracksTable.scrollRowToVisible(0) } @@ -245,32 +244,26 @@ final class TracksViewController: NSViewController, NSTableViewDataSource, NSTab // MARK: Drag & drop func tableView(_ tableView: NSTableView, - writeRowsWith rowIndexes: IndexSet, - to pboard: NSPasteboard) -> Bool { - guard let firstRow = rowIndexes.first, - let track = track(at: firstRow), track.isMuxed == false - else { return false } - - let data = try? NSKeyedArchiver.archivedData(withRootObject: rowIndexes, requiringSecureCoding: true) - pboard.declareTypes([pasteboardType], owner: self) - pboard.setData(data, forType: pasteboardType) - return true + pasteboardWriterForRow row: Int) -> (any NSPasteboardWriting)? { + return PasteboardItem(index: row, type: .tableViewIndex) } func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, - proposedDropOperation: NSTableView.DropOperation) -> NSDragOperation { - let count = mp4.tracks.count + 1 + proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { + guard dropOperation == .above else { return [] } - if proposedDropOperation == .above { - if row < count && row != 0, let track = track(at: row), track.isMuxed == false { - return .every - } - else if row == count { - return .every - } + guard let source = info.draggingSource as? NSTableView, + tableView == source else { return [] } + + let count = mp4.tracks.count + 1 + if row < count && row != 0, let track = track(at: row), track.isMuxed == false { + return .move + } else if row == count { + return .move } + return [] } @@ -278,12 +271,15 @@ final class TracksViewController: NSViewController, NSTableViewDataSource, NSTab acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool { - let data: Data = info.draggingPasteboard.data(forType: pasteboardType)! - if let rowIndexes: IndexSet = NSKeyedUnarchiver.unarchiveObject(with: data) as? IndexSet, rowIndexes.isEmpty == false { + guard let pasteboardItems = info.draggingPasteboard.pasteboardItems + else { return false } + if let source = info.draggingSource as? NSTableView, + source == tableView + { + let rowIndexes = IndexSet(pasteboardItems.compactMap { $0.integer(forType: .tableViewIndex) }) let tracks = mp4.tracks.enumerated().filter { rowIndexes.contains($0.offset + 1)} .map { $0.element } mp4.moveTracks(tracks, to: UInt(row - 1)) - tableView.reloadData() return true } diff --git a/PasteboardItem.swift b/PasteboardItem.swift new file mode 100644 index 0000000..dccb08e --- /dev/null +++ b/PasteboardItem.swift @@ -0,0 +1,64 @@ +// +// PasteboardItem.swift +// Subler +// +// Created by Damiano Galassi on 22/09/2026. +// + +import Cocoa + +extension NSPasteboard.PasteboardType { + static let tableViewIndex = NSPasteboard.PasteboardType("org.subler.tableViewIndex") +} + +class PasteboardItem: NSObject, NSPasteboardWriting { + let index: Int + let type: NSPasteboard.PasteboardType + + init(index: Int, type: NSPasteboard.PasteboardType) { + self.index = index + self.type = type + } + + func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] { + return [type] + } + + func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? { + switch type { + case self.type: + return index + default: + return nil + } + } +} + +extension NSPasteboardItem { + func integer(forType type: NSPasteboard.PasteboardType) -> Int? { + guard let data = data(forType: type) else { return nil } + let plist = try? PropertyListSerialization.propertyList( + from: data, + options: .mutableContainers, + format: nil) + return plist as? Int + } +} + +extension Array { + mutating func move(from start: Index, to end: Index) { + guard (0..