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 // MARK: - Main View
private var fdrCassetteDataDefault: String { "" }
private static let dlgOpenFile = NSOpenPanel() private static let dlgOpenFile = NSOpenPanel()
var body: some View { var body: some View {
@ -46,8 +44,30 @@ struct VwrPrefPaneCassette: View {
SSPreferences.Settings.Section(bottomDivider: true) { SSPreferences.Settings.Section(bottomDivider: true) {
Text(LocalizedStringKey("Choose your desired cassette file path. Will be omitted if invalid.")) Text(LocalizedStringKey("Choose your desired cassette file path. Will be omitted if invalid."))
HStack { HStack {
TextField(fdrCassetteDataDefault, text: $cassettePath).disabled(true) PathControl(pathDroppable: $cassettePath) { pathControl in
.help(cassettePath) 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 { Button {
if NSEvent.modifierFlags == .option, !cassettePath.isEmpty { if NSEvent.modifierFlags == .option, !cassettePath.isEmpty {
NSWorkspace.shared.activateFileViewerSelecting( NSWorkspace.shared.activateFileViewerSelecting(

View File

@ -60,8 +60,32 @@ struct VwrPrefPaneDictionary: View {
Group { Group {
Text(LocalizedStringKey("Choose your desired user data folder path. Will be omitted if invalid.")) Text(LocalizedStringKey("Choose your desired user data folder path. Will be omitted if invalid."))
HStack { HStack {
TextField(fdrUserDataDefault, text: $userDataFolderSpecified).disabled(true) PathControl(pathDroppable: $userDataFolderSpecified) { pathControl in
.help(userDataFolderSpecified) 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 { Button {
if NSEvent.modifierFlags == .option, !userDataFolderSpecified.isEmpty { if NSEvent.modifierFlags == .option, !userDataFolderSpecified.isEmpty {
NSWorkspace.shared.activateFileViewerSelecting( NSWorkspace.shared.activateFileViewerSelecting(
@ -96,13 +120,13 @@ struct VwrPrefPaneDictionary: View {
} else { } else {
IMEApp.buzz() IMEApp.buzz()
if !bolPreviousFolderValidity { if !bolPreviousFolderValidity {
userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) userDataFolderSpecified = fdrUserDataDefault
} }
return return
} }
} else { } else {
if !bolPreviousFolderValidity { if !bolPreviousFolderValidity {
userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) userDataFolderSpecified = fdrUserDataDefault
} }
return return
} }
@ -112,7 +136,7 @@ struct VwrPrefPaneDictionary: View {
Text("...") Text("...")
} }
Button { Button {
userDataFolderSpecified = LMMgr.dataFolderPath(isDefaultFolder: true) userDataFolderSpecified = fdrUserDataDefault
} label: { } label: {
Text("") Text("")
} }