Open History to see the last commit
1import ExpoModulesCore
2import UIKit
3
4/// Reports the body geometry of a native navigation column.
5final class T3LayoutMetricsView: ExpoView {
6 let onMetricsChange = EventDispatcher()
7 private var lastMetrics: NSDictionary?
8
9 required init(appContext: AppContext? = nil) {
10 super.init(appContext: appContext)
11 isUserInteractionEnabled = false
12 registerForTraitChanges([UITraitHorizontalSizeClass.self, UITraitVerticalSizeClass.self]) { (view: T3LayoutMetricsView, _: UITraitCollection) in
13 view.publishMetrics()
14 }
15 }
16
17 override func didMoveToWindow() {
18 super.didMoveToWindow()
19 lastMetrics = nil
20 publishMetrics()
21 }
22
23 override func layoutSubviews() {
24 super.layoutSubviews()
25 publishMetrics()
26 }
27
28 override func safeAreaInsetsDidChange() {
29 super.safeAreaInsetsDidChange()
30 publishMetrics()
31 }
32
33 private func publishMetrics() {
34 guard window != nil, bounds.width > 0, bounds.height > 0 else { return }
35 let metrics: [String: Any] = [
36 "width": bounds.width, "height": bounds.height,
37 "horizontalSizeClass": traitCollection.horizontalSizeClass == .regular ? "regular" : "compact",
38 "safeArea": [
39 "top": safeAreaInsets.top, "bottom": safeAreaInsets.bottom,
40 "left": safeAreaInsets.left, "right": safeAreaInsets.right,
41 ],
42 ]
43 let next = metrics as NSDictionary
44 guard lastMetrics?.isEqual(next) != true else { return }
45 lastMetrics = next
46 onMetricsChange(metrics)
47 }
48}