Skip to content

Commit

Permalink
Add hover modifier to see the full directory path and click to copy it
Browse files Browse the repository at this point in the history
  • Loading branch information
yllfejziu committed Nov 19, 2024
1 parent 7409b23 commit 006f03e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ struct ProjectFormView: View {
@State private var outputPathValid = false
@State private var stringsDirectoryValid = false
@State private var outputSwiftCodeFileDirectoryValid = false

@State private var copied = false

var onSave: ((Project) -> Void)? = nil
var onDelete: ((Project) -> Void)? = nil
var onLocalize: ((Project) -> Void)? = nil
Expand Down Expand Up @@ -52,6 +53,19 @@ struct ProjectFormView: View {
deleteButton(for: viewModel.project).padding()
}
.padding()
.overlay {
if copied {
Text("Copied to clipboard!")
.padding(8)
.background(
Color.black
.opacity(0.4)
)
.clipShape(RoundedRectangle(cornerRadius: 6))
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.padding(.top, 6)
}
}
}
}

Expand Down Expand Up @@ -104,7 +118,8 @@ private extension ProjectFormView {
bookmarkDataKey: viewModel.project.bookmarkDataForDirectoryPath,
directoryPath: $viewModel.project.directoryPath,
isValid: $outputPathValid,
onDirectorySelected: updateDirectoryPaths
onDirectorySelected: updateDirectoryPaths,
onDirectoryCopied: showCopiedMessage
)
}) {
Text(.init(Lingua.ProjectForm.outputDirectoryHelp))
Expand Down Expand Up @@ -135,7 +150,8 @@ private extension ProjectFormView {
title: Lingua.ProjectForm.stringsDirectory,
bookmarkDataKey: viewModel.project.bookmarkDataForStringsDirectory,
directoryPath: $viewModel.project.swiftCode.stringsDirectory,
isValid: $stringsDirectoryValid
isValid: $stringsDirectoryValid,
onDirectoryCopied: showCopiedMessage
)
}) {
Text(.init(Lingua.ProjectForm.lprojDirectoryHelp))
Expand All @@ -147,7 +163,8 @@ private extension ProjectFormView {
title: Lingua.ProjectForm.linguaSwiftOutputDirectory,
bookmarkDataKey: viewModel.project.bookmarkDataForOutputSwiftCodeFileDirectory ,
directoryPath: $viewModel.project.swiftCode.outputSwiftCodeFileDirectory,
isValid: $outputSwiftCodeFileDirectoryValid
isValid: $outputSwiftCodeFileDirectoryValid,
onDirectoryCopied: showCopiedMessage
)
}) {
Text(.init(Lingua.ProjectForm.linguaSwiftOutputDirectoryHelp))
Expand Down Expand Up @@ -242,4 +259,15 @@ private extension ProjectFormView {
try? firstLprojFullURL.saveBookmarkData(forKey: viewModel.project.bookmarkDataForStringsDirectory)
}
}

func showCopiedMessage() {
withAnimation {
copied = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
withAnimation {
self.copied = false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,46 @@ struct DirectoryInputField: View {

@Binding var directoryPath: String
@Binding var isValid: Bool

@State private var isHovered = false

var onDirectorySelected: ((String) -> Void)? = nil

var onDirectoryCopied: (() -> Void)? = nil

var body: some View {
HStack {
ValidatingTextField(title: title, validation: RequiredRule(), isDisabled: true, text: $directoryPath, isValid: $isValid)
.overlay {
// Transparent overlay to capture tap gesture
Color.clear
.contentShape(Rectangle())
.onTapGesture {
copyPathToClipboard()
onDirectoryCopied?()
}
}
.onHover { hovering in
isHovered = hovering
}
Button(Lingua.ProjectForm.inputDirectoryButton) {
chooseDirectory()
}
}
.padding(.vertical, 5)
.background(
GeometryReader { geometry in
if isHovered {
Text(directoryPath)
.font(.caption)
.padding(8)
.background(Color.black.opacity(0.8))
.foregroundColor(.white)
.cornerRadius(8)
.frame(width: geometry.size.width, alignment: .center)
.offset(y: -geometry.size.height)
.transition(.opacity)
}
}
)
}
}

Expand Down Expand Up @@ -53,4 +82,9 @@ private extension DirectoryInputField {
directoryPath = ""
}
}

private func copyPathToClipboard() {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(directoryPath, forType: .string)
}
}

0 comments on commit 006f03e

Please sign in to comment.