mana-swift-ui/Sources/ManaAuthUI/Components/ManaFields.swift
Till JS c863080f55 ManaAuthUI: Login-AutoFill via .username; ThemeGallery an non-optionales signatureAppName
- ManaFields: neuer manaUsernameField() (.username-ContentType) — iOS-AutoFill
  erkennt das Login-Konto-Feld als Credential und überschreibt es auch nach
  ersten Tippzeichen (.emailAddress füllte oft nur leere Felder). Sign-up/Reset
  bleiben bei manaEmailField().
- ManaLoginView: Email-Feld nutzt manaUsernameField().
- ManaThemeGallery: signatureAppName ist non-optional → if-let entfernt, Hinweis
  nennt die Signatur-App immer; Demo-unlocked-Set auf reale Theme-Cases
  (paper/lume/twilight) gesetzt.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 17:14:21 +02:00

226 lines
7.1 KiB
Swift

import ManaThemeUI
import ManaTokens
import SwiftUI
#if canImport(AppKit)
import AppKit
#endif
/// Brand-gepflegtes Eingabefeld für Email/Name/Token. SecureField-
/// Variante darunter (`ManaSecureField`).
///
/// Settings für Email-Input (Keyboard-Type, no autocap/autocorrect)
/// können via `.manaEmailField()`-Modifier convenience gesetzt werden.
public struct ManaTextField: View {
@Environment(\.manaBrand) private var brand
private let placeholder: String
@Binding private var text: String
public init(_ placeholder: String, text: Binding<String>) {
self.placeholder = placeholder
_text = text
}
public var body: some View {
TextField(placeholder, text: $text)
.padding(.vertical, 12)
.padding(.horizontal, 16)
.background(brand.surface, in: RoundedRectangle.mana(Radius.md))
.overlay(
RoundedRectangle.mana(Radius.md)
.strokeBorder(brand.border, lineWidth: 0.5)
)
.foregroundStyle(brand.foreground)
}
}
/// Passwort-Feld. Gleiche Optik wie ``ManaTextField``, aber maskiert.
///
/// Rechts im Feld sitzt ein Augen-Button, der das Passwort sichtbar
/// macht (`SecureField` ↔ `TextField`). Default verborgen. Der Button
/// erscheint erst, wenn etwas im Feld steht. Auf macOS warnt ein
/// Feststelltasten-Symbol bei aktivem Caps-Lock — wie in Apples
/// eigenen Passwortfeldern.
public struct ManaSecureField: View {
@Environment(\.manaBrand) private var brand
private let placeholder: String
@Binding private var text: String
private let textContentType: TextContentType
@State private var isRevealed = false
@FocusState private var isFocused: Bool
#if os(macOS)
@State private var capsLockOn = false
@State private var flagsMonitor: Any?
#endif
/// - Parameter textContentType: `.password` für Login-Eingabe,
/// `.newPassword` für Sign-Up oder Reset (löst iOS-Passwort-
/// Vorschlag-Sheet aus).
public init(
_ placeholder: String,
text: Binding<String>,
textContentType: TextContentType = .password
) {
self.placeholder = placeholder
_text = text
self.textContentType = textContentType
}
public var body: some View {
HStack(spacing: 8) {
field
#if os(iOS)
.textContentType(uiTextContentType)
#elseif os(macOS)
.textContentType(nsTextContentType)
#endif
.focused($isFocused)
#if os(macOS)
capsLockIndicator
#endif
if !text.isEmpty {
revealButton
}
}
.padding(.vertical, 12)
.padding(.horizontal, 16)
.background(brand.surface, in: RoundedRectangle.mana(Radius.md))
.overlay(
RoundedRectangle.mana(Radius.md)
.strokeBorder(brand.border, lineWidth: 0.5)
)
.foregroundStyle(brand.foreground)
// Leeres Feld immer maskiert lassen — kein „verwaister"
// Klartext-Zustand ohne sichtbaren Verbergen-Button.
.onChange(of: text) { _, newValue in
if newValue.isEmpty { isRevealed = false }
}
#if os(macOS)
.onAppear { startCapsLockMonitor() }
.onDisappear { stopCapsLockMonitor() }
#endif
}
@ViewBuilder
private var field: some View {
if isRevealed {
TextField(placeholder, text: $text)
.autocorrectionDisabled()
#if os(iOS)
.textInputAutocapitalization(.never)
#endif
} else {
SecureField(placeholder, text: $text)
}
}
private var revealButton: some View {
Button {
isRevealed.toggle()
// Fokus über den Feld-Tausch hinweg halten.
isFocused = true
} label: {
Image(systemName: isRevealed ? "eye.slash" : "eye")
.foregroundStyle(brand.mutedForeground)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
#if os(macOS)
.focusable(false)
#endif
.accessibilityLabel(isRevealed ? "Passwort verbergen" : "Passwort anzeigen")
}
#if os(macOS)
@ViewBuilder
private var capsLockIndicator: some View {
if capsLockOn {
Image(systemName: "capslock.fill")
.foregroundStyle(brand.error)
.accessibilityLabel("Feststelltaste aktiv")
.transition(.opacity)
}
}
private func startCapsLockMonitor() {
capsLockOn = NSEvent.modifierFlags.contains(.capsLock)
guard flagsMonitor == nil else { return }
flagsMonitor = NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { event in
capsLockOn = event.modifierFlags.contains(.capsLock)
return event
}
}
private func stopCapsLockMonitor() {
if let monitor = flagsMonitor {
NSEvent.removeMonitor(monitor)
flagsMonitor = nil
}
}
#endif
public enum TextContentType: Sendable {
case password
case newPassword
}
#if os(iOS)
private var uiTextContentType: UITextContentType {
switch textContentType {
case .password: .password
case .newPassword: .newPassword
}
}
#elseif os(macOS)
private var nsTextContentType: NSTextContentType {
switch textContentType {
case .password: .password
case .newPassword: .newPassword
}
}
#endif
}
public extension View {
/// Convenience-Modifier für Email-Felder: kein Autocaps, kein
/// Autocorrect, Email-Keyboard auf iOS, Email-textContentType.
func manaEmailField() -> some View {
modifier(EmailFieldModifier())
}
/// Wie ``manaEmailField()``, aber mit `.username`-ContentType statt
/// `.emailAddress`. Für das Account-Feld im **Login**: iOS-AutoFill
/// erkennt es dann als Credential-Account und überschreibt das Feld
/// auch dann, wenn der User schon ein paar Zeichen getippt hat
/// (`.emailAddress` füllte oft nur leere Felder). Sign-Up/Reset bleiben
/// bewusst bei ``manaEmailField()`` (reine Email-Eingabe).
func manaUsernameField() -> some View {
modifier(UsernameFieldModifier())
}
}
private struct EmailFieldModifier: ViewModifier {
func body(content: Content) -> some View {
content
.textContentType(.emailAddress)
.autocorrectionDisabled()
#if os(iOS)
.keyboardType(.emailAddress)
.textInputAutocapitalization(.never)
#endif
}
}
private struct UsernameFieldModifier: ViewModifier {
func body(content: Content) -> some View {
content
.textContentType(.username)
.autocorrectionDisabled()
#if os(iOS)
.keyboardType(.emailAddress)
.textInputAutocapitalization(.never)
#endif
}
}