Hi, I made a "Finder view" with SwiftUI, how is it possible to call viewModel.selectFileWithAnimation(file) when .contextMenu show?
Thank you!
@StateObject private var viewModel = FilesViewModel() // Use the view model to manage data and logic
var body: some View {
VStack {
// Horizontal scrolling of files and folders
ScrollView(.horizontal, showsIndicators: false) {
if viewModel.sortedAndFilteredFolderContents.isEmpty {
// Show "No Results" if no files or folders found
Text("No Results")
.font(.headline)
.foregroundColor(.almostWhite)
.padding()
} else {
HStack(spacing: 20) {
ForEach(viewModel.sortedAndFilteredFolderContents, id: \.self) { file in
VStack {
ZStack {
// Selection effect without matchedGeometryEffect
if viewModel.selectedFile == file {
RoundedRectangle(cornerRadius: 10)
.fill(Color.blue.opacity(0.2))
.transition(.opacity)
.frame(width: 80, height: 100)
.animation(.easeInOut(duration: 0.3), value: viewModel.selectedFile)
}
VStack {
Image(nsImage: viewModel.getFileIcon(for: file))
.resizable()
.frame(width: 64, height: 64)
.onTapGesture(count: 2) {
// Select and open file/folder on double-tap
viewModel.selectFileWithAnimation(file)
viewModel.openFileOrFolder(file)
}
.onTapGesture {
viewModel.selectFileWithAnimation(file)
}
.onLongPressGesture(minimumDuration: 0.5) {
// Rename file with long press
viewModel.rename(file)
}
.contextMenu {
// Define your context menu buttons here
Button(action: {
viewModel.showInFinder(file)
}) {
Text("Show in Finder")
Image(systemName: "folder")
}
Button(action: {
viewModel.moveToTrash(file)
}) {
Text("Move to Trash")
Image(systemName: "trash")
}
Button(action: {
viewModel.rename(file)
}) {
Text("Rename")
Image(systemName: "pencil")
}
Button(action: {
viewModel.makeAlias(for: file)
}) {
Text("Make Alias")
Image(systemName: "link")
}
Button(action: {
viewModel.compress(file)
}) {
Text("Compress \(file.lastPathComponent)")
Image(systemName: "archivebox")
}
Button(action: {
viewModel.copyFile(file)
}) {
Text("Copy")
Image(systemName: "doc.on.doc")
}
Button(action: {
viewModel.shareFile(file, in: dynaClip.view)
}) {
Text("Share")
Image(systemName: "square.and.arrow.up")
}
}
Text(file.lastPathComponent)
.font(.caption)
.lineLimit(1)
.truncationMode(.tail)
.frame(width: 80)
}
}
}
}
}
.padding(.top, 20)
}
}
@aviorrok Currently, there’s no way to get a callback for contextMenu appearing.
You can use Feedback Assistant to file an enhancement request for such an API. Please include an explanation of what you're trying to accomplish if you open the enhancement request.