Buttons.swift (15195B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 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().talerColor) 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 // .tint(WalletColors().talerColor) 152 } else { 153 Image(systemName: CHECKMARK) // 154 } 155 } 156 .tint(WalletColors().talerColor) 157 .talerFont(.title) 158 .accessibilityLabel(accessibilityLabelStr) 159 } 160 } 161 162 struct PlusButton : View { 163 let accessibilityLabelStr: String 164 let action: () -> Void 165 166 var body: some View { 167 Button(action: action) { 168 Image(systemName: PLUS) 169 } 170 .tint(WalletColors().talerColor) 171 .talerFont(.title) 172 .accessibilityLabel(accessibilityLabelStr) 173 } 174 } 175 176 @available(iOS 26.0, *) 177 struct SettingsButton26 : View { 178 let sortPriority: Double 179 180 var body: some View { 181 Button { 182 // will trigger NavigationLink 183 NotificationCenter.default.post(name: .SettingsAction, object: nil) 184 } label: { 185 Label(TalerTab.settings.title, systemImage: TalerTab.settings.sysImg) 186 .labelStyle(.iconOnly) 187 } 188 // .padding() 189 .buttonStyle(.glass) 190 .accessibilitySortPriority(sortPriority) 191 } 192 } 193 194 struct SettingsButton : View { 195 let accessibilityLabelStr: String 196 let action: () -> Void 197 198 var body: some View { 199 let button = Button(action: action) { 200 Image(systemName: SETTINGS) 201 } 202 .tint(WalletColors().talerColor) 203 .talerFont(.title) 204 .accessibilityLabel(accessibilityLabelStr) 205 // if #available(iOS 26.0, *) { 206 // return button.buttonStyle(.glass) 207 // } 208 return button 209 } 210 } 211 struct PasteButton : View { 212 let accessibilityLabelStr: String 213 let action: () -> Void 214 215 var body: some View { 216 let button = Button(action: action) { 217 Image(systemName: PASTE) 218 } 219 .tint(WalletColors().talerColor) 220 .talerFont(.title) 221 .accessibilityLabel(accessibilityLabelStr) 222 // if #available(iOS 26.0, *) { 223 // return button.buttonStyle(.glass) 224 // } 225 return button 226 } 227 } 228 229 struct ZoomInButton : View { 230 let accessibilityLabelStr: String 231 let action: () -> Void 232 233 var body: some View { 234 Button(action: action) { 235 Image(ICONNAME_ZOOM_IN, SYSTEM_ZOOM_IN) 236 } 237 .tint(WalletColors().talerColor) 238 .talerFont(.title) 239 .accessibilityLabel(accessibilityLabelStr) 240 } 241 } 242 243 struct ZoomOutButton : View { 244 let accessibilityLabelStr: String 245 let action: () -> Void 246 247 var body: some View { 248 Button(action: action) { 249 Image(ICONNAME_ZOOM_OUT, SYSTEM_ZOOM_OUT) 250 } 251 .tint(WalletColors().talerColor) 252 .talerFont(.title) 253 .accessibilityLabel(accessibilityLabelStr) 254 } 255 } 256 257 struct BackButton : View { 258 let action: () -> Void 259 260 var body: some View { 261 Button(action: action) { 262 let name = ICONNAME_INCOMING + ICONNAME_FILL 263 let sysName = SYSTEM_INCOMING4 + ICONNAME_FILL 264 Image(name, sysName, fallback: FALLBACK_INCOMING) 265 } 266 .tint(WalletColors().talerColor) 267 .talerFont(.largeTitle) 268 .accessibilityLabel(Text("Back", comment: "a11y")) 269 } 270 } 271 272 struct ForwardButton : View { 273 let enabled: Bool 274 let action: () -> Void 275 276 var body: some View { 277 let myAction = { 278 if enabled { 279 action() 280 } 281 } 282 Button(action: myAction) { 283 let imageName = enabled ? ICONNAME_OUTGOING + ICONNAME_FILL 284 : ICONNAME_OUTGOING 285 let sysName = enabled ? SYSTEM_OUTGOING4 + ICONNAME_FILL 286 : SYSTEM_OUTGOING4 287 Image(imageName, sysName, fallback: FALLBACK_OUTGOING) 288 } 289 .tint(WalletColors().talerColor) 290 .talerFont(.largeTitle) 291 .accessibilityLabel(Text("Continue", comment: "a11y")) 292 } 293 } 294 295 struct ArrowUpButton : View { 296 let action: () -> Void 297 298 var body: some View { 299 Button(action: action) { 300 Image(systemName: ARROW_TOP) // 301 } 302 .tint(WalletColors().talerColor) 303 .talerFont(.title2) 304 .accessibilityLabel(Text("Scroll up", comment: "a11y")) 305 } 306 } 307 308 struct ArrowDownButton : View { 309 let action: () -> Void 310 311 var body: some View { 312 Button(action: action) { 313 Image(systemName: ARROW_BOT) // 314 } 315 .tint(WalletColors().talerColor) 316 .talerFont(.title2) 317 .accessibilityLabel(Text("Scroll down", comment: "a11y")) 318 } 319 } 320 321 struct ReloadButton : View { 322 let disabled: Bool 323 let action: () -> Void 324 325 var body: some View { 326 Button(action: action) { 327 Image(systemName: RELOAD) // 328 } 329 .tint(WalletColors().talerColor) 330 .talerFont(.title) 331 .accessibilityLabel(Text("Reload", comment: "a11y")) 332 .disabled(disabled) 333 } 334 } 335 336 enum TalerButtonStyleType { 337 case plain 338 case bordered 339 case prominent 340 } 341 struct TalerButtonStyle: ButtonStyle { 342 var type: TalerButtonStyleType = .plain 343 var dimmed: Bool = false 344 var narrow: Bool = false 345 var one: Bool = false 346 var disabled: Bool = false 347 var aligned: TextAlignment = .center 348 var badge: String = EMPTYSTRING 349 350 @Environment(\.colorScheme) private var colorScheme 351 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 352 353 public func makeBody(configuration: ButtonStyleConfiguration) -> some View { 354 // configuration.role = type == .prominent ? .primary : .normal Only on macOS 355 MyBigButton(foreColor: foreColor(type: type, pressed: configuration.isPressed, 356 scheme: colorScheme, contrast: colorSchemeContrast, disabled: disabled), 357 backColor: backColor(type: type, pressed: configuration.isPressed, disabled: disabled), 358 dimmed: dimmed, 359 configuration: configuration, 360 disabled: disabled, 361 narrow: narrow, 362 one: one, 363 aligned: aligned, 364 badge: badge, 365 scheme: colorScheme, 366 contrast: colorSchemeContrast) 367 } 368 369 func foreColor(type: TalerButtonStyleType, 370 pressed: Bool, 371 scheme: ColorScheme, 372 contrast: ColorSchemeContrast, 373 disabled: Bool) -> Color { 374 if type == .plain { 375 return WalletColors().fieldForeground // primary text color 376 } 377 return WalletColors().buttonForeColor(pressed: pressed, 378 disabled: disabled, 379 scheme: scheme, 380 contrast: contrast, 381 prominent: type == .prominent) 382 } 383 func backColor(type: TalerButtonStyleType, pressed: Bool, disabled: Bool) -> Color { 384 if type == .plain && !pressed { 385 return Color.clear 386 } 387 return WalletColors().buttonBackColor(pressed: pressed, 388 disabled: disabled, 389 prominent: type == .prominent) 390 } 391 392 struct BackgroundView: View { 393 let color: Color 394 let dimmed: Bool 395 var body: some View { 396 RoundedRectangle( 397 cornerRadius: 15, 398 style: .continuous 399 ) 400 .fill(color) 401 .opacity(dimmed ? 0.6 : 1.0) 402 } 403 } 404 405 struct MyBigButton: View { 406 // var type: TalerButtonStyleType 407 let foreColor: Color 408 let backColor: Color 409 let dimmed: Bool 410 let configuration: ButtonStyle.Configuration 411 let disabled: Bool 412 let narrow: Bool 413 let one: Bool 414 let aligned: TextAlignment 415 var badge: String 416 var scheme: ColorScheme 417 var contrast: ColorSchemeContrast 418 419 var body: some View { 420 let aligned2: Alignment = (aligned == .center) ? Alignment.center 421 : (aligned == .leading) ? Alignment.leading 422 : Alignment.trailing 423 let hasBadge = !badge.isEmpty 424 let buttonLabel = configuration.label 425 .multilineTextAlignment(aligned) 426 .lineLimit(one ? 1 : 3) 427 .talerFont(.title3) // narrow ? .title3 : .title2 428 .frame(maxWidth: narrow ? nil : .infinity, alignment: aligned2) 429 .padding(.vertical, 10) 430 .padding(.horizontal, hasBadge ? 0 : 6) 431 .foregroundColor(foreColor) 432 .background(BackgroundView(color: backColor, dimmed: dimmed)) 433 .contentShape(Rectangle()) // make sure the button can be pressed even if backgroundColor == clear 434 .scaleEffect(configuration.isPressed ? 0.95 : 1) 435 .animation(.spring(response: 0.1), value: configuration.isPressed) 436 .disabled(disabled) 437 if hasBadge { 438 let badgeColor: Color = (badge == CONFIRM_BANK) ? WalletColors().confirm 439 : WalletColors().attention 440 let badgeV = Image(systemName: badge) 441 .talerFont(.caption) 442 HStack(alignment: .top, spacing: 0) { 443 badgeV.foregroundColor(.clear) 444 buttonLabel 445 badgeV.foregroundColor(badgeColor) 446 } 447 } else { 448 buttonLabel 449 } 450 } 451 } 452 } 453 // MARK: - 454 #if DEBUG 455 fileprivate struct ContentView_Previews: PreviewProvider { 456 static var previews: some View { 457 let testButtonTitle = String("Placeholder") 458 Button(testButtonTitle) {} 459 .buttonStyle(TalerButtonStyle(type: .bordered, aligned: .trailing)) 460 } 461 } 462 #endif