NewProjectMenu.swift 686 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // NewProjectMenu.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 3/2/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct NewProjectMenu: View {
  10. @Environment(\.modelContext) private var modelContext
  11. @Query private var items: [Project]
  12. var body: some View {
  13. Button("New Project", action: addItem)
  14. .keyboardShortcut("N", modifiers: [.command])
  15. }
  16. private func addItem() {
  17. withAnimation {
  18. let newItem = Project(timestamp: Date())
  19. let count = items.count(where: { $0.name.starts(with: "New Project") })
  20. if (count > 0) {
  21. newItem.name += " (\(count))"
  22. }
  23. modelContext.insert(newItem)
  24. }
  25. }
  26. }
  27. #Preview {
  28. NewProjectMenu()
  29. }