Repository Public
1// SP Notes iOS2// File: Features/Editor/EditorKeyboardAvoider.swift3// Description: Keeps the caret above the keyboard without moving the layout: the editor's text view keeps its full frame and only4// its bottom content inset follows the keyboard (formatting bar included), animated with it.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import UIKit910@MainActor11final class EditorKeyboardAvoider {12 private weak var textView: UITextView?13 private var observers: [NSObjectProtocol] = []1415 func install(on textView: UITextView) {16 guard self.textView !== textView else { return }17 uninstall()18 self.textView = textView19 let center = NotificationCenter.default20 for name in [UIResponder.keyboardWillChangeFrameNotification, UIResponder.keyboardWillHideNotification] {21 observers.append(center.addObserver(forName: name, object: nil, queue: .main) { [weak self] notification in22 let frame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue23 let duration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0.2524 let hiding = notification.name == UIResponder.keyboardWillHideNotification25 MainActor.assumeIsolated { self?.apply(keyboardFrame: hiding ? nil : frame, duration: duration) }26 })27 }28 }2930 func uninstall() {31 observers.forEach(NotificationCenter.default.removeObserver)32 observers = []33 if let textView { Self.setBottomInset(0, on: textView) }34 textView = nil35 }3637 /// The part of the text view covered by the keyboard, beyond what its safe area already accounts for.38 static func bottomInset(viewFrameInWindow: CGRect, keyboardFrameInWindow: CGRect?, safeAreaBottom: CGFloat) -> CGFloat {39 guard let keyboard = keyboardFrameInWindow, keyboard.height > 0 else { return 0 }40 let overlap = viewFrameInWindow.maxY - max(keyboard.minY, viewFrameInWindow.minY)41 return max(0, overlap - safeAreaBottom)42 }4344 private static func setBottomInset(_ inset: CGFloat, on textView: UITextView) {45 guard abs(textView.contentInset.bottom - inset) > 0.5 else { return }46 textView.contentInset.bottom = inset47 textView.verticalScrollIndicatorInsets.bottom = inset48 }4950 private func apply(keyboardFrame: CGRect?, duration: TimeInterval) {51 guard let textView, let window = textView.window else { return }52 // The keyboard frame is in screen coordinates; the window may not fill the screen (Split View, Stage Manager).53 let keyboard = keyboardFrame.map { window.convert($0, from: window.screen.coordinateSpace) }54 let frame = textView.convert(textView.bounds, to: window)55 let inset = Self.bottomInset(viewFrameInWindow: frame, keyboardFrameInWindow: keyboard, safeAreaBottom: textView.safeAreaInsets.bottom)56 UIView.animate(withDuration: max(0.01, duration), delay: 0, options: [.beginFromCurrentState, .curveEaseInOut]) {57 Self.setBottomInset(inset, on: textView)58 } completion: { _ in59 // Only the body's caret is followed; the title sits at the top of the page.60 guard inset > 0, textView.isFirstResponder, let range = textView.selectedTextRange else { return }61 let caret = textView.caretRect(for: range.end)62 if !caret.isNull, !caret.isInfinite { textView.scrollRectToVisible(caret.insetBy(dx: 0, dy: -12), animated: true) }63 }64 }65}66