From ce011a4d3721f810e0cf42e17f1623d884e7bd8d Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 23 Sep 2026 21:29:18 +0200 Subject: [PATCH] Cleanup the Save As destination feature a bit --- Base.lproj/GeneralPrefsViewController.xib | 88 +++++++++++------------ Classes/GeneralPrefsViewController.swift | 3 +- Classes/MP42FileAdditions.swift | 2 +- Classes/Prefs.swift | 59 ++++++++------- Classes/SaveOptions.swift | 6 +- 5 files changed, 85 insertions(+), 73 deletions(-) diff --git a/Base.lproj/GeneralPrefsViewController.xib b/Base.lproj/GeneralPrefsViewController.xib index 13e196e..517fa4a 100755 --- a/Base.lproj/GeneralPrefsViewController.xib +++ b/Base.lproj/GeneralPrefsViewController.xib @@ -1,8 +1,8 @@ - + - + @@ -26,22 +26,22 @@ - + - + - - + + - - + + @@ -49,7 +49,7 @@ - - + + @@ -81,7 +81,7 @@ - + - - + + @@ -111,7 +111,7 @@ - - + + @@ -129,7 +129,7 @@ - - + + - - + + - - + + @@ -163,7 +163,7 @@ - + @@ -179,7 +179,7 @@ - + @@ -205,33 +205,33 @@ - - + + - - + + - - - + + + - - + + @@ -280,12 +280,6 @@ - - - - - - @@ -320,6 +314,12 @@ + + + + + + diff --git a/Classes/GeneralPrefsViewController.swift b/Classes/GeneralPrefsViewController.swift index 219fb6a..569387c 100644 --- a/Classes/GeneralPrefsViewController.swift +++ b/Classes/GeneralPrefsViewController.swift @@ -55,7 +55,8 @@ class GeneralPrefsViewController: NSViewController { } if let url = Prefs.saveAsCustomLocation { - menu.insertItem(menuItem(url: url), at: 2) + menu.insertItem(NSMenuItem.separator(), at: 2) + menu.insertItem(menuItem(url: url), at: 3) } selectCurrentSaveLocation() diff --git a/Classes/MP42FileAdditions.swift b/Classes/MP42FileAdditions.swift index 5e63a45..57ad3ef 100644 --- a/Classes/MP42FileAdditions.swift +++ b/Classes/MP42FileAdditions.swift @@ -91,7 +91,7 @@ extension MP42File { } } - func preferredFileDirectory() -> URL? { + func firstSourceDirectoryURL() -> URL? { return tracks.compactMap { $0.url }.first?.deletingLastPathComponent() } diff --git a/Classes/Prefs.swift b/Classes/Prefs.swift index a98d7ba..28a852b 100644 --- a/Classes/Prefs.swift +++ b/Classes/Prefs.swift @@ -26,11 +26,6 @@ struct Stored : Registable { let key: String let defaultValue: T - init(key: String, defaultValue: T) { - self.key = key - self.defaultValue = defaultValue - } - var wrappedValue: T { get { ud.object(forKey: key) as? T ?? defaultValue } set { ud.set(newValue, forKey: key) } @@ -41,16 +36,41 @@ struct Stored : Registable { } } +@propertyWrapper +struct StoredEnum : Registable { + let key: String + let defaultValue: T + + var wrappedValue: T { + get { T(rawValue: (ud.object(forKey: key) as? T.RawValue) ?? defaultValue.rawValue) ?? defaultValue } + set { ud.set(newValue.rawValue, forKey: key) } + } + + func register(in dictionary: inout [String :Any]) { + dictionary[key] = defaultValue.rawValue + } +} + +@propertyWrapper +struct StoredURL : Registable { + let key: String + let defaultValue: URL? + + var wrappedValue: URL? { + get { ud.url(forKey: key) ?? defaultValue } + set { ud.set(newValue, forKey: key) } + } + + func register(in dictionary: inout [String :Any]) { + dictionary[key] = defaultValue + } +} + @propertyWrapper struct StoredCodable : Registable { let key: String let defaultValue: T - init(key: String, defaultValue: T) { - self.key = key - self.defaultValue = defaultValue - } - var wrappedValue: T { get { guard let data = ud.data(forKey: key) else { return defaultValue } @@ -88,7 +108,7 @@ enum Prefs { _audioBitrate, _audioDRC, _audioConvertAC3, _audioKeepAC3, _audioConvertDts, _audioDtsOptions, _subtitleConvertBitmap, _ratingsCountry, _chaptersPreviewPosition, _chaptersPreviewTrack, _mp464bitOffset, _mp464bitTimes, _mp4SaveAsOptimize, _forceHvc1, - _logFormat, _saveAsLocationValue]) + _logFormat, _saveAsLocation]) } @Stored(key: "NSApplicationCrashOnException", defaultValue: true) @@ -172,21 +192,12 @@ enum Prefs { @Stored(key: "SBLogFormat", defaultValue: 0) static var logFormat: Int // 0 = Time Only, 1 = Date and Time - @Stored(key: "SBSaveAsLocationMode", defaultValue: 0) - private static var saveAsLocationValue: Int - - static var saveAsLocation: SaveAsLocation { - get { SaveAsLocation(rawValue: saveAsLocationValue) ?? .lastUsed } - set { saveAsLocationValue = newValue.rawValue } - } - - private static let saveAsCustomLocationKey = "SBSaveAsCustomLocation" + @StoredEnum(key: "SBSaveAsLocationMode", defaultValue: .lastUsed) + static var saveAsLocation: SaveAsLocation /// The folder used when saveAsLocation is set to custom. - static var saveAsCustomLocation: URL? { - get { ud.url(forKey: saveAsCustomLocationKey) } - set { ud.set(newValue, forKey: saveAsCustomLocationKey) } - } + @StoredURL(key: "SBSaveAsCustomLocation", defaultValue: nil) + static var saveAsCustomLocation: URL? } enum MetadataPrefs { diff --git a/Classes/SaveOptions.swift b/Classes/SaveOptions.swift index f846482..5e54154 100644 --- a/Classes/SaveOptions.swift +++ b/Classes/SaveOptions.swift @@ -62,7 +62,7 @@ final class SaveOptions: NSViewController { case .lastUsed: return nil case .sameAsFile: - return doc.fileURL?.deletingLastPathComponent() ?? doc.mp4.preferredFileDirectory() + return doc.fileURL?.deletingLastPathComponent() ?? doc.mp4.firstSourceDirectoryURL() case .custom: return Prefs.saveAsCustomLocation } @@ -83,8 +83,8 @@ final class SaveOptions: NSViewController { savePanel?.nameFieldStringValue = filename } - if let directory = preferredDirectory(for: doc) { - savePanel?.directoryURL = directory + if let url = preferredDirectory(for: doc) { + savePanel?.directoryURL = url } setFileType(filenameExtension: fileType)