PrefUI // Use NSPathControl instead.

This commit is contained in:
ShikiSuen 2023-07-30 10:59:04 +08:00
parent 45232ca499
commit a4c6e7c93e
2 changed files with 53 additions and 9 deletions

View File

@ -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(

View File

@ -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("")
}