From a4c6e7c93e2ffee12f8a23145bff3f7fc88da9ee Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 30 Jul 2023 10:59:04 +0800 Subject: [PATCH] PrefUI // Use NSPathControl instead. --- .../PrefUI/VwrPrefPaneCassette.swift | 28 ++++++++++++--- .../PrefUI/VwrPrefPaneDictionary.swift | 34 ++++++++++++++++--- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Source/Modules/UIModules/PrefUI/VwrPrefPaneCassette.swift b/Source/Modules/UIModules/PrefUI/VwrPrefPaneCassette.swift index cefd7ed8..c2173d97 100644 --- a/Source/Modules/UIModules/PrefUI/VwrPrefPaneCassette.swift +++ b/Source/Modules/UIModules/PrefUI/VwrPrefPaneCassette.swift @@ -34,8 +34,6 @@ struct VwrPrefPaneCassette: View { // MARK: - Main View - private var fdrCassetteDataDefault: String { "" } - private static let dlgOpenFile = NSOpenPanel() var body: some View { @@ -46,8 +44,30 @@ struct VwrPrefPaneCassette: View { SSPreferences.Settings.Section(bottomDivider: true) { Text(LocalizedStringKey("Choose your desired cassette file path. Will be omitted if invalid.")) HStack { - TextField(fdrCassetteDataDefault, text: $cassettePath).disabled(true) - .help(cassettePath) + PathControl(pathDroppable: $cassettePath) { pathControl in + pathControl.allowedTypes = ["cin2", "cin", "vcin"] + pathControl.heightAnchor.constraint(equalToConstant: 20).isActive = true + pathControl.widthAnchor.constraint(equalToConstant: CtlPrefUIShared.maxDescriptionWidth).isActive = true + pathControl.placeholderString = "Please drag the desired target from Finder to this place.".localized + } acceptDrop: { pathControl, info in + let urls = info.draggingPasteboard.readObjects(forClasses: [NSURL.self]) + guard let url = urls?.first as? URL else { return false } + let bolPreviousPathValidity = LMMgr.checkCassettePathValidity( + PrefMgr.shared.cassettePath.expandingTildeInPath) + if LMMgr.checkCassettePathValidity(url.path) { + cassettePath = url.path + pathControl.url = url + LMMgr.loadCassetteData() + BookmarkManager.shared.saveBookmark(for: url) + return true + } + // On Error: + IMEApp.buzz() + if !bolPreviousPathValidity { + cassettePath = "" + } + return false + } Button { if NSEvent.modifierFlags == .option, !cassettePath.isEmpty { NSWorkspace.shared.activateFileViewerSelecting( diff --git a/Source/Modules/UIModules/PrefUI/VwrPrefPaneDictionary.swift b/Source/Modules/UIModules/PrefUI/VwrPrefPaneDictionary.swift index a2d974ea..784db6cb 100644 --- a/Source/Modules/UIModules/PrefUI/VwrPrefPaneDictionary.swift +++ b/Source/Modules/UIModules/PrefUI/VwrPrefPaneDictionary.swift @@ -60,8 +60,32 @@ struct VwrPrefPaneDictionary: View { Group { Text(LocalizedStringKey("Choose your desired user data folder path. Will be omitted if invalid.")) HStack { - TextField(fdrUserDataDefault, text: $userDataFolderSpecified).disabled(true) - .help(userDataFolderSpecified) + PathControl(pathDroppable: $userDataFolderSpecified) { pathControl in + pathControl.allowedTypes = ["public.folder", "public.directory"] + pathControl.heightAnchor.constraint(equalToConstant: 20).isActive = true + pathControl.widthAnchor.constraint(equalToConstant: CtlPrefUIShared.maxDescriptionWidth).isActive = true + } acceptDrop: { pathControl, info in + let urls = info.draggingPasteboard.readObjects(forClasses: [NSURL.self]) + guard let url = urls?.first as? URL else { return false } + let bolPreviousFolderValidity = LMMgr.checkIfSpecifiedUserDataFolderValid( + PrefMgr.shared.userDataFolderSpecified.expandingTildeInPath) + var newPath = url.path + newPath.ensureTrailingSlash() + if LMMgr.checkIfSpecifiedUserDataFolderValid(newPath) { + userDataFolderSpecified = newPath + pathControl.url = url + BookmarkManager.shared.saveBookmark(for: url) + AppDelegate.shared.updateDirectoryMonitorPath() + return true + } + // On Error: + IMEApp.buzz() + if !bolPreviousFolderValidity { + userDataFolderSpecified = fdrUserDataDefault + pathControl.url = URL(fileURLWithPath: fdrUserDataDefault) + } + return false + } Button { if NSEvent.modifierFlags == .option, !userDataFolderSpecified.isEmpty { NSWorkspace.shared.activateFileViewerSelecting( @@ -96,13 +120,13 @@ struct VwrPrefPaneDictionary: View { } else { IMEApp.buzz() if !bolPreviousFolderValidity { - userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) + userDataFolderSpecified = fdrUserDataDefault } return } } else { if !bolPreviousFolderValidity { - userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) + userDataFolderSpecified = fdrUserDataDefault } return } @@ -112,7 +136,7 @@ struct VwrPrefPaneDictionary: View { Text("...") } Button { - userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) + userDataFolderSpecified = fdrUserDataDefault } label: { Text("↻") }