| 1 | import SwiftUI |
| 2 | |
| 3 | enum KeyboardOverlap { |
| 4 | /// How far the keyboard covers a region whose bottom edge is at `bottom` |
| 5 | /// (screen coordinates): how much to lift that region's bottom so it sits |
| 6 | /// directly above the keyboard. A hidden keyboard (no frame, or one below |
| 7 | /// the region) covers nothing. |
| 8 | static func lift(keyboard: CGRect, bottom: CGFloat) -> CGFloat { |
| 9 | guard !keyboard.isNull, !keyboard.isEmpty else { return 0 } |
| 10 | return max(0, bottom - keyboard.minY) |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | #if os(iOS) |
| 15 | import UIKit |
| 16 | |
| 17 | /// The software keyboard as the system reports it, for the whole app. |
| 18 | /// |
| 19 | /// SwiftUI's own keyboard avoidance can miss a keyboard that is already up, |
| 20 | /// so the conversation once laid its composer out behind the keyboard, with |
| 21 | /// the feed running under it. Screens with a bottom composer read the |
| 22 | /// keyboard's frame from here instead (`KeyboardPinned`). |
| 23 | @MainActor @Observable final class KeyboardFrame { |
| 24 | static let shared = KeyboardFrame() |
| 25 | /// The keyboard's frame in screen coordinates; empty while it is hidden. |
| 26 | private(set) var frame: CGRect = .null |
| 27 | @ObservationIgnored private var observers: [NSObjectProtocol] = [] |
| 28 | |
| 29 | private init() { |
| 30 | let center = NotificationCenter.default |
| 31 | observers.append(center.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: .main) { note in |
| 32 | let end = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? .null |
| 33 | MainActor.assumeIsolated { KeyboardFrame.shared.frame = end } |
| 34 | }) |
| 35 | observers.append(center.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in |
| 36 | MainActor.assumeIsolated { KeyboardFrame.shared.frame = .null } |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | /// Starts listening; call once at launch so a keyboard already up is known. |
| 41 | static func start() { _ = shared } |
| 42 | |
| 43 | /// Puts the keyboard away, whichever field has it. |
| 44 | static func dismiss() { |
| 45 | UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Keeps a screen's bottom (its composer) directly above the keyboard, from |
| 50 | /// the keyboard's reported frame rather than SwiftUI's keyboard safe area. |
| 51 | struct KeyboardPinned: ViewModifier { |
| 52 | @State private var keyboard = KeyboardFrame.shared |
| 53 | func body(content: Content) -> some View { |
| 54 | GeometryReader { proxy in |
| 55 | let bottom = proxy.frame(in: .global).maxY |
| 56 | let lift = KeyboardOverlap.lift(keyboard: keyboard.frame, bottom: bottom) |
| 57 | content |
| 58 | .frame(width: proxy.size.width, height: max(0, proxy.size.height - lift), alignment: .top) |
| 59 | .frame(width: proxy.size.width, height: proxy.size.height, alignment: .top) |
| 60 | .animation(.easeOut(duration: 0.25), value: lift) |
| 61 | } |
| 62 | .ignoresSafeArea(.keyboard, edges: .bottom) |
| 63 | } |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | extension View { |
| 68 | /// iPhone: the composer stays directly above the keyboard (KeyboardPinned). |
| 69 | func keyboardPinned() -> some View { |
| 70 | #if os(iOS) |
| 71 | modifier(KeyboardPinned()) |
| 72 | #else |
| 73 | self |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | /// iPhone: reading puts the keyboard away: any scroll dismisses it at once. |
| 78 | func dismissesKeyboardWhileReading() -> some View { |
| 79 | #if os(iOS) |
| 80 | self.scrollDismissesKeyboard(.immediately) |
| 81 | #else |
| 82 | self.scrollDismissesKeyboard(.interactively) |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | /// iPhone: a tap on the background behind this content dismisses the |
| 87 | /// keyboard. Cards, links and fields sit above it and keep their taps. |
| 88 | func tapDismissesKeyboard() -> some View { |
| 89 | #if os(iOS) |
| 90 | self.background { Color.clear.contentShape(Rectangle()).onTapGesture { KeyboardFrame.dismiss() } } |
| 91 | #else |
| 92 | self |
| 93 | #endif |
| 94 | } |
| 95 | } |
| 96 | |