Buttons.swift (15181B)
1 /* 2 * This file is part of GNU Taler, ©2022-26 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import SwiftUI 9 import Foundation 10 import AVFoundation 11 12 extension ShapeStyle where Self == Color { 13 static var random: Color { 14 Color( 15 red: .random(in: 0...1), 16 green: .random(in: 0...1), 17 blue: .random(in: 0...1) 18 ) 19 } 20 } 21 22 struct HamburgerButton : View { 23 let action: () -> Void 24 25 var body: some View { 26 Button(action: action) { 27 Image(systemName: HAMBURGER) 28 // Image(systemName: "sidebar.squares.leading") // 29 } 30 .talerFont(.title) 31 .accessibilityLabel(Text("Main Menu", comment: "a11y")) 32 } 33 } 34 35 struct LinkButton: View { 36 let destination: URL 37 let hintTitle: String 38 let buttonTitle: String 39 let a11yHint: String 40 let badge: String 41 42 @AppStorage("minimalistic") var minimalistic: Bool = false 43 44 var body: some View { 45 VStack(alignment: .leading) { 46 if !minimalistic { // show hint that the user should authorize on bank website 47 Text(hintTitle) 48 .fixedSize(horizontal: false, vertical: true) // wrap in scrollview 49 .multilineTextAlignment(.leading) // otherwise 50 .listRowSeparator(.hidden) 51 } 52 Link(destination: destination) { 53 HStack(spacing: 8.0) { 54 Image(systemName: LINK) 55 Text(buttonTitle) 56 } 57 } 58 .buttonStyle(TalerButtonStyle(type: .prominent, badge: badge)) 59 .accessibilityHint(a11yHint) 60 } 61 } 62 } 63 64 struct QRButton : View { 65 let hideTitle: Bool 66 let action: () -> Void 67 68 @AppStorage("minimalistic") var minimalistic: Bool = false 69 @State private var showCameraAlert: Bool = false 70 71 private var openSettingsButton: some View { 72 Button("Open Settings") { 73 showCameraAlert = false 74 UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) 75 } 76 } 77 let closingAnnouncement = String(localized: "Closing Camera", comment: "a11y") 78 79 var defaultPriorityAnnouncement = String(localized: "Opening Camera", comment: "a11y") 80 81 var highPriorityAnnouncement: AttributedString { 82 var highPriorityString = AttributedString(localized: "Camera Active", comment: "a11y") 83 if #available(iOS 17.0, *) { 84 highPriorityString.accessibilitySpeechAnnouncementPriority = .high 85 } 86 return highPriorityString 87 } 88 @MainActor 89 private func checkCameraAvailable() -> Void { 90 // Open Camera when QR-Button was tapped 91 announce(defaultPriorityAnnouncement) 92 93 AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) -> Void in 94 if granted { 95 action() 96 if #available(iOS 17.0, *) { 97 AccessibilityNotification.Announcement(highPriorityAnnouncement).post() 98 } else { 99 let cameraActive = String(localized: "Camera Active", comment: "a11y") 100 announce(cameraActive) 101 } 102 } else { 103 showCameraAlert = true 104 } 105 }) 106 } 107 108 var body: some View { 109 let dismissAlertButton = Button("Cancel", role: .cancel) { 110 announce(closingAnnouncement) 111 showCameraAlert = false 112 } 113 let scanText = String(localized: "Scan QR code", comment: "Button title, a11y") 114 let qrImage = Image(systemName: QRBUTTON) 115 let qrText = Text(qrImage) 116 Button(action: checkCameraAvailable) { 117 if hideTitle { 118 qrImage 119 .resizable() 120 .scaledToFit() 121 .foregroundStyle(WalletColors().primaryAccent) 122 } else if minimalistic { 123 let width = UIScreen.screenWidth / 7 124 qrText.talerFont(.largeTitle) 125 .padding(.horizontal, width) 126 } else { 127 HStack(spacing: 16) { 128 qrText.talerFont(.title) 129 Text(scanText) 130 }.padding(.horizontal) 131 } 132 } 133 .accessibilityLabel(scanText) 134 .alert("Scanning QR-codes requires access to the camera", 135 isPresented: $showCameraAlert, 136 actions: { openSettingsButton 137 dismissAlertButton }, 138 message: { Text("Please allow camera access in Settings.") }) // Scanning QR-codes 139 } 140 } 141 142 struct DoneButton : View { 143 let titleStr: String? 144 let accessibilityLabelStr: String 145 let action: () -> Void 146 147 var body: some View { 148 Button(action: action) { 149 if let titleStr { 150 Text(titleStr) 151 } else { 152 Image(systemName: CHECKMARK) // 153 } 154 } 155 .tint(WalletColors().primaryAccent) 156 .talerFont(.title) 157 .accessibilityLabel(accessibilityLabelStr) 158 } 159 } 160 161 struct PlusButton : View { 162 let accessibilityLabelStr: String 163 let action: () -> Void 164 165 var body: some View { 166 Button(action: action) { 167 Image(systemName: PLUS) 168 } 169 .tint(WalletColors().primaryAccent) 170 .talerFont(.title) 171 .accessibilityLabel(accessibilityLabelStr) 172 } 173 } 174 175 @available(iOS 26.0, *) 176 struct SettingsButton26 : View { 177 let sortPriority: Double 178 179 var body: some View { 180 Button { 181 // will trigger NavigationLink 182 NotificationCenter.default.post(name: .SettingsAction, object: nil) 183 } label: { 184 Label(TalerTab.settings.title, systemImage: TalerTab.settings.sysImg) 185 .labelStyle(.iconOnly) 186 } 187 // .padding() 188 .buttonStyle(.glass) 189 .accessibilitySortPriority(sortPriority) 190 } 191 } 192 193 struct SettingsButton : View { 194 let accessibilityLabelStr: String 195 let action: () -> Void 196 197 var body: some View { 198 let button = Button(action: action) { 199 Image(systemName: SETTINGS) 200 } 201 .tint(WalletColors().primaryAccent) 202 .talerFont(.title) 203 .accessibilityLabel(accessibilityLabelStr) 204 // if #available(iOS 26.0, *) { 205 // return button.buttonStyle(.glass) 206 // } 207 return button 208 } 209 } 210 struct TalerPasteButton : View { 211 let accessibilityLabelStr: String 212 let action: () -> Void 213 214 var body: some View { 215 let button = Button(action: action) { 216 Image(systemName: PASTE) 217 } 218 .tint(WalletColors().primaryAccent) 219 .talerFont(.title) 220 .accessibilityLabel(accessibilityLabelStr) 221 // if #available(iOS 26.0, *) { 222 // return button.buttonStyle(.glass) 223 // } 224 return button 225 } 226 } 227 228 struct ZoomInButton : View { 229 let accessibilityLabelStr: String 230 let action: () -> Void 231 232 var body: some View { 233 Button(action: action) { 234 Image(ICONNAME_ZOOM_IN, SYSTEM_ZOOM_IN) 235 } 236 .tint(WalletColors().primaryAccent) 237 .talerFont(.title) 238 .accessibilityLabel(accessibilityLabelStr) 239 } 240 } 241 242 struct ZoomOutButton : View { 243 let accessibilityLabelStr: String 244 let action: () -> Void 245 246 var body: some View { 247 Button(action: action) { 248 Image(ICONNAME_ZOOM_OUT, SYSTEM_ZOOM_OUT) 249 } 250 .tint(WalletColors().primaryAccent) 251 .talerFont(.title) 252 .accessibilityLabel(accessibilityLabelStr) 253 } 254 } 255 256 struct BackButton : View { 257 let action: () -> Void 258 259 var body: some View { 260 Button(action: action) { 261 let name = ICONNAME_INCOMING + ICONNAME_FILL 262 let sysName = SYSTEM_INCOMING4 + ICONNAME_FILL 263 Image(name, sysName, fallback: FALLBACK_INCOMING) 264 } 265 .tint(WalletColors().primaryAccent) 266 .talerFont(.largeTitle) 267 .accessibilityLabel(Text("Back", comment: "a11y")) 268 } 269 } 270 271 struct ForwardButton : View { 272 let enabled: Bool 273 let action: () -> Void 274 275 var body: some View { 276 let myAction = { 277 if enabled { 278 action() 279 } 280 } 281 Button(action: myAction) { 282 let imageName = enabled ? ICONNAME_OUTGOING + ICONNAME_FILL 283 : ICONNAME_OUTGOING 284 let sysName = enabled ? SYSTEM_OUTGOING4 + ICONNAME_FILL 285 : SYSTEM_OUTGOING4 286 Image(imageName, sysName, fallback: FALLBACK_OUTGOING) 287 } 288 .tint(WalletColors().primaryAccent) 289 .talerFont(.largeTitle) 290 .accessibilityLabel(Text("Continue", comment: "a11y")) 291 } 292 } 293 294 struct ArrowUpButton : View { 295 let action: () -> Void 296 297 var body: some View { 298 Button(action: action) { 299 Image(systemName: ARROW_TOP) // 300 } 301 .tint(WalletColors().primaryAccent) 302 .talerFont(.title2) 303 .accessibilityLabel(Text("Scroll up", comment: "a11y")) 304 } 305 } 306 307 struct ArrowDownButton : View { 308 let action: () -> Void 309 310 var body: some View { 311 Button(action: action) { 312 Image(systemName: ARROW_BOT) // 313 } 314 .tint(WalletColors().primaryAccent) 315 .talerFont(.title2) 316 .accessibilityLabel(Text("Scroll down", comment: "a11y")) 317 } 318 } 319 320 struct ReloadButton : View { 321 let disabled: Bool 322 let action: () -> Void 323 324 var body: some View { 325 Button(action: action) { 326 Image(systemName: RELOAD) // 327 } 328 .tint(WalletColors().primaryAccent) 329 .talerFont(.title) 330 .accessibilityLabel(Text("Reload", comment: "a11y")) 331 .disabled(disabled) 332 } 333 } 334 335 enum TalerButtonStyleType { 336 case plain 337 case bordered 338 case prominent 339 } 340 struct TalerButtonStyle: ButtonStyle { 341 var type: TalerButtonStyleType = .plain 342 var dimmed: Bool = false 343 var narrow: Bool = false 344 var one: Bool = false 345 var disabled: Bool = false 346 var aligned: TextAlignment = .center 347 var badge: String = EMPTYSTRING 348 349 @Environment(\.colorScheme) private var colorScheme 350 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 351 352 public func makeBody(configuration: ButtonStyleConfiguration) -> some View { 353 // configuration.role = type == .prominent ? .primary : .normal Only on macOS 354 MyBigButton(foreColor: foreColor(type: type, pressed: configuration.isPressed, 355 scheme: colorScheme, contrast: colorSchemeContrast, disabled: disabled), 356 backColor: backColor(type: type, pressed: configuration.isPressed, disabled: disabled), 357 dimmed: dimmed, 358 configuration: configuration, 359 disabled: disabled, 360 narrow: narrow, 361 one: one, 362 aligned: aligned, 363 badge: badge, 364 scheme: colorScheme, 365 contrast: colorSchemeContrast) 366 } 367 368 func foreColor(type: TalerButtonStyleType, 369 pressed: Bool, 370 scheme: ColorScheme, 371 contrast: ColorSchemeContrast, 372 disabled: Bool) -> Color { 373 if type == .plain { 374 return WalletColors().fieldForeground // primary text color 375 } 376 return WalletColors().buttonForeColor(pressed: pressed, 377 disabled: disabled, 378 scheme: scheme, 379 contrast: contrast, 380 prominent: type == .prominent) 381 } 382 func backColor(type: TalerButtonStyleType, pressed: Bool, disabled: Bool) -> Color { 383 if type == .plain && !pressed { 384 return Color.clear 385 } 386 return WalletColors().buttonBackColor(pressed: pressed, 387 disabled: disabled, 388 prominent: type == .prominent) 389 } 390 391 struct BackgroundView: View { 392 let color: Color 393 let dimmed: Bool 394 var body: some View { 395 RoundedRectangle( 396 cornerRadius: 15, 397 style: .continuous 398 ) 399 .fill(color) 400 .opacity(dimmed ? 0.6 : 1.0) 401 } 402 } 403 404 struct MyBigButton: View { 405 // var type: TalerButtonStyleType 406 let foreColor: Color 407 let backColor: Color 408 let dimmed: Bool 409 let configuration: ButtonStyle.Configuration 410 let disabled: Bool 411 let narrow: Bool 412 let one: Bool 413 let aligned: TextAlignment 414 var badge: String 415 var scheme: ColorScheme 416 var contrast: ColorSchemeContrast 417 418 var body: some View { 419 let aligned2: Alignment = (aligned == .center) ? Alignment.center 420 : (aligned == .leading) ? Alignment.leading 421 : Alignment.trailing 422 let hasBadge = !badge.isEmpty 423 let buttonLabel = configuration.label 424 .multilineTextAlignment(aligned) 425 .lineLimit(one ? 1 : 3) 426 .talerFont(.title3) // narrow ? .title3 : .title2 427 .frame(maxWidth: narrow ? nil : .infinity, alignment: aligned2) 428 .padding(.vertical, 10) 429 .padding(.horizontal, hasBadge ? 0 : 6) 430 .foregroundColor(foreColor) 431 .background(BackgroundView(color: backColor, dimmed: dimmed)) 432 .contentShape(Rectangle()) // make sure the button can be pressed even if backgroundColor == clear 433 .scaleEffect(configuration.isPressed ? 0.95 : 1) 434 .animation(.spring(response: 0.1), value: configuration.isPressed) 435 .disabled(disabled) 436 if hasBadge { 437 let badgeColor: Color = (badge == CONFIRM_BANK) ? WalletColors().confirm 438 : WalletColors().attention 439 let badgeV = Image(systemName: badge) 440 .talerFont(.caption) 441 HStack(alignment: .top, spacing: 0) { 442 badgeV.foregroundColor(.clear) 443 buttonLabel 444 badgeV.foregroundColor(badgeColor) 445 } 446 } else { 447 buttonLabel 448 } 449 } 450 } 451 } 452 // MARK: - 453 #if DEBUG 454 fileprivate struct ContentView_Previews: PreviewProvider { 455 static var previews: some View { 456 let testButtonTitle = String("Placeholder") 457 Button(testButtonTitle) {} 458 .buttonStyle(TalerButtonStyle(type: .bordered, aligned: .trailing)) 459 } 460 } 461 #endif