import SwiftUI enum KeyboardOverlap { /// How far the keyboard covers a region whose bottom edge is at `bottom` /// (screen coordinates): how much to lift that region's bottom so it sits /// directly above the keyboard. A hidden keyboard (no frame, or one below /// the region) covers nothing. static func lift(keyboard: CGRect, bottom: CGFloat) -> CGFloat { guard !keyboard.isNull, !keyboard.isEmpty else { return 0 } return max(0, bottom - keyboard.minY) } } #if os(iOS) import UIKit /// The software keyboard as the system reports it, for the whole app. /// /// SwiftUI's own keyboard avoidance can miss a keyboard that is already up, /// so the conversation once laid its composer out behind the keyboard, with /// the feed running under it. Screens with a bottom composer read the /// keyboard's frame from here instead (`KeyboardPinned`). @MainActor @Observable final class KeyboardFrame { static let shared = KeyboardFrame() /// The keyboard's frame in screen coordinates; empty while it is hidden. private(set) var frame: CGRect = .null @ObservationIgnored private var observers: [NSObjectProtocol] = [] private init() { let center = NotificationCenter.default observers.append(center.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: .main) { note in let end = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? .null MainActor.assumeIsolated { KeyboardFrame.shared.frame = end } }) observers.append(center.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in MainActor.assumeIsolated { KeyboardFrame.shared.frame = .null } }) } /// Starts listening; call once at launch so a keyboard already up is known. static func start() { _ = shared } /// Puts the keyboard away, whichever field has it. static func dismiss() { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } /// Keeps a screen's bottom (its composer) directly above the keyboard, from /// the keyboard's reported frame rather than SwiftUI's keyboard safe area. struct KeyboardPinned: ViewModifier { @State private var keyboard = KeyboardFrame.shared func body(content: Content) -> some View { GeometryReader { proxy in let bottom = proxy.frame(in: .global).maxY let lift = KeyboardOverlap.lift(keyboard: keyboard.frame, bottom: bottom) content .frame(width: proxy.size.width, height: max(0, proxy.size.height - lift), alignment: .top) .frame(width: proxy.size.width, height: proxy.size.height, alignment: .top) .animation(.easeOut(duration: 0.25), value: lift) } .ignoresSafeArea(.keyboard, edges: .bottom) } } #endif extension View { /// iPhone: the composer stays directly above the keyboard (KeyboardPinned). func keyboardPinned() -> some View { #if os(iOS) modifier(KeyboardPinned()) #else self #endif } /// iPhone: reading puts the keyboard away: any scroll dismisses it at once. func dismissesKeyboardWhileReading() -> some View { #if os(iOS) self.scrollDismissesKeyboard(.immediately) #else self.scrollDismissesKeyboard(.interactively) #endif } /// iPhone: a tap on the background behind this content dismisses the /// keyboard. Cards, links and fields sit above it and keep their taps. func tapDismissesKeyboard() -> some View { #if os(iOS) self.background { Color.clear.contentShape(Rectangle()).onTapGesture { KeyboardFrame.dismiss() } } #else self #endif } }