2023-04-16 17:23:13 +00:00
|
|
|
//
|
|
|
|
// KeyboardReader.swift
|
|
|
|
// ComposeUI
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 3/7/23.
|
|
|
|
//
|
|
|
|
|
2023-10-20 02:50:20 +00:00
|
|
|
#if !os(visionOS)
|
|
|
|
|
2023-04-16 17:23:13 +00:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class KeyboardReader: ObservableObject {
|
2023-04-29 22:49:02 +00:00
|
|
|
// @Published var isVisible = false
|
|
|
|
@Published var keyboardHeight: CGFloat = 0
|
|
|
|
|
|
|
|
var isVisible: Bool {
|
|
|
|
// when a hardware keyboard is connected, the height is very short, so we don't consider that being "visible"
|
|
|
|
keyboardHeight > 72
|
|
|
|
}
|
2023-04-16 17:23:13 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willShow), name: UIResponder.keyboardWillShowNotification, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willHide), name: UIResponder.keyboardWillHideNotification, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func willShow(_ notification: Foundation.Notification) {
|
|
|
|
let endFrame = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
|
2023-04-29 22:49:02 +00:00
|
|
|
// isVisible = endFrame.height > 72
|
|
|
|
keyboardHeight = endFrame.height
|
2023-04-16 17:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func willHide() {
|
2023-04-29 22:49:02 +00:00
|
|
|
// sometimes willHide is called during a SwiftUI view update
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
// self.isVisible = false
|
|
|
|
self.keyboardHeight = 0
|
|
|
|
}
|
2023-04-16 17:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 02:50:20 +00:00
|
|
|
|
|
|
|
#endif
|