Просмотр исходного кода

feat: implement command-N as New Project

Sam Jaffe 2 недель назад
Родитель
Сommit
7dcdf193a9
2 измененных файлов с 38 добавлено и 0 удалено
  1. 4 0
      Todos/TodosApp.swift
  2. 34 0
      Todos/View/Menu/NewProjectMenu.swift

+ 4 - 0
Todos/TodosApp.swift

@@ -37,6 +37,10 @@ struct TodosApp: App {
     .modelContainer(sharedModelContainer)
     .commands {
       CommandGroup(replacing: .newItem) {
+        NewProjectMenu()
+          .modelContainer(sharedModelContainer)
+      }
+      CommandGroup(replacing: .saveItem) {
         SaveSnapshotMenu()
           .modelContainer(sharedModelContainer)
         SaveAsMenu()

+ 34 - 0
Todos/View/Menu/NewProjectMenu.swift

@@ -0,0 +1,34 @@
+//
+//  NewProjectMenu.swift
+//  Todos
+//
+//  Created by Sam Jaffe on 3/2/26.
+//
+
+import SwiftUI
+import SwiftData
+
+struct NewProjectMenu: View {
+  @Environment(\.modelContext) private var modelContext
+  @Query private var items: [Project]
+
+  var body: some View {
+    Button("New Project", action: addItem)
+      .keyboardShortcut("N", modifiers: [.command])
+  }
+  
+  private func addItem() {
+    withAnimation {
+      let newItem = Project(timestamp: Date())
+      let count = items.count(where: { $0.name.starts(with: "New Project") })
+      if (count > 0) {
+        newItem.name += " (\(count))"
+      }
+      modelContext.insert(newItem)
+    }
+  }
+}
+
+#Preview {
+  NewProjectMenu()
+}