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

refactor: switch to using focused() as the listener control for show/hide of notes/tags/etc.

Sam Jaffe 3 недель назад
Родитель
Сommit
01f50383b2
3 измененных файлов с 21 добавлено и 19 удалено
  1. 7 5
      Todos/View/SubTaskView.swift
  2. 12 10
      Todos/View/TaskView.swift
  3. 2 4
      Todos/View/VisibilityTapper.swift

+ 7 - 5
Todos/View/SubTaskView.swift

@@ -10,9 +10,10 @@ import SwiftUI
 struct SubTaskView: View {
   @Binding var task: SubTask
   
-  @State private var showNotes: Bool = false
   @State private var hideNotes: Bool = false
 
+  @FocusState private var isFocused: Bool
+
   var body: some View {
     VStack {
       HStack {
@@ -27,19 +28,20 @@ struct SubTaskView: View {
         .fixedSize(horizontal: true, vertical: false)
 
         TextField("Task Name", text: $task.name)
+          .focused($isFocused)
       }
             
-      if showNotes {
+      if isFocused || !(hideNotes || task.notes.isEmpty){
         HStack {
           TextField("Notes", text: $task.notes)
             .font(.footnote)
+            .focused($isFocused)
             .padding(.leading, 30)
-          ShowHideTapper(shown: $showNotes, hideToggle: $hideNotes)
+          VisibilityTapper(hideToggle: $hideNotes)
+            .focused($isFocused)
         }
       }
       
-    }.onHover { yes in
-      showNotes = yes || !(hideNotes || task.notes.isEmpty)
     }
   }
 }

+ 12 - 10
Todos/View/TaskView.swift

@@ -9,11 +9,11 @@ import SwiftUI
 
 struct TaskView: View {
   @Binding var task: Task
-  @State private var showTags: Bool = false
+  
   @State private var hideTags: Bool = false
-
-  @State private var showNotes: Bool = false
   @State private var hideNotes: Bool = false
+  
+  @FocusState private var isFocused: Bool
 
   var body: some View {
     VStack {
@@ -36,6 +36,7 @@ struct TaskView: View {
         }
         
         TextField("Task Name", text: $task.name)
+          .focused($isFocused)
         Button() {
           task.subtasks.append(SubTask(name: "Subtask"))
         } label: {
@@ -44,21 +45,25 @@ struct TaskView: View {
         }
       }
       
-      if showTags || !(hideTags || task.tags.isEmpty) {
+      if isFocused || !(hideTags || task.tags.isEmpty) {
         HStack {
           TagBarView(tags: $task.tags)
             .font(.footnote)
+            .focused($isFocused)
             .padding(.leading, 30)
-          ShowHideTapper(shown: $showTags, hideToggle: $hideTags)
+          VisibilityTapper(hideToggle: $hideTags)
+            .focused($isFocused)
         }
       }
       
-      if showNotes || !(hideNotes || task.notes.isEmpty) {
+      if isFocused || !(hideNotes || task.notes.isEmpty) {
         HStack {
           TextField("Notes", text: $task.notes)
             .font(.footnote)
+            .focused($isFocused)
             .padding(.leading, 30)
-          ShowHideTapper(shown: $showNotes, hideToggle: $hideNotes)
+          VisibilityTapper(hideToggle: $hideNotes)
+            .focused($isFocused)
         }
       }
       
@@ -70,9 +75,6 @@ struct TaskView: View {
           }
         }
       }
-    }.onHover { yes in
-      showTags = yes
-      showNotes = yes
     }
   }
 }

+ 2 - 4
Todos/View/VisibilityTapper.swift

@@ -7,8 +7,7 @@
 
 import SwiftUI
 
-struct ShowHideTapper: View {
-  @Binding var shown: Bool
+struct VisibilityTapper: View {
   @Binding var hideToggle: Bool
   
   var body: some View {
@@ -23,7 +22,6 @@ struct ShowHideTapper: View {
 }
 
 #Preview {
-  @Previewable @State var shown = false
   @Previewable @State var hidden = false
-  ShowHideTapper(shown: $shown, hideToggle: $hidden)
+  VisibilityTapper(hideToggle: $hidden)
 }