Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / VisualizerFavoriteGesture.swift

Raw 1.7 KB on 0e4fe45e403a1b6b5a57aa68addd681995d37722
1import SwiftUI
2
3/// Resolve a double tap before a presentation's single tap can hide its controls.
4/// The same gesture also recognizes a mouse double click on macOS.
5struct VisualizerFavoriteGesture: ViewModifier {
6 let favorite: () -> Bool
7 var singleTap: (() -> Void)? = nil
8 @State private var confirmation = 0
9 @State private var showsConfirmation = false
10
11 func body(content: Content) -> some View {
12 content.contentShape(Rectangle())
13 .gesture(TapGesture(count: 2).exclusively(before: TapGesture(count: 1)).onEnded { value in
14 switch value {
15 case .first: saveFavorite()
16 case .second: singleTap?()
17 }
18 })
19 .overlay {
20 if showsConfirmation {
21 Label("Visualizer favorited", systemImage: "heart.fill")
22 .font(.headline).padding(16)
23 .foregroundStyle(.white)
24 .background(.ultraThinMaterial, in: Capsule())
25 .allowsHitTesting(false)
26 .accessibilityIdentifier("visualizerFavoriteConfirmation")
27 }
28 }
29 .accessibilityAction(named: Text("Favorite visualizer"), saveFavorite)
30 .task(id: confirmation) {
31 guard confirmation > 0 else { return }
32 do { try await Task.sleep(for: .seconds(2)) } catch { return }
33 showsConfirmation = false
34 }
35 }
36
37 private func saveFavorite() {
38 guard favorite() else { return }
39 confirmation += 1
40 showsConfirmation = true
41 AccessibilityNotification.Announcement("Visualizer favorited").post()
42 }
43}