@onekeyfe/react-native-native-overlay 6.8.0-2 → 6.8.0-20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayHost.kt +37 -5
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayPackage.kt +1 -1
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastCardView.kt +869 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastSpec.kt +327 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterView.kt +574 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterViewManager.kt +54 -0
- package/android/src/test/java/com/onekey/nativeoverlay/NativeOverlayAccessibilityIsolationTest.kt +172 -0
- package/docs/SPEC.md +172 -11
- package/ios/NativeOverlayContainerView.swift +20 -4
- package/ios/NativeOverlayToastCardView.swift +572 -0
- package/ios/NativeOverlayToastSpec.swift +356 -0
- package/ios/NativeOverlayToasterView.swift +519 -0
- package/ios/RCTNativeOverlayToasterComponentView.h +11 -0
- package/ios/RCTNativeOverlayToasterComponentView.mm +85 -0
- package/lib/module/NativeOverlayToasterNativeComponent.ts +24 -0
- package/lib/module/OverlayStore.js +20 -3
- package/lib/module/OverlayStore.js.map +1 -1
- package/lib/module/OverlayView.web.js +9 -18
- package/lib/module/OverlayView.web.js.map +1 -1
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/toast/OverlayToastHost.js +42 -0
- package/lib/module/toast/OverlayToastHost.js.map +1 -0
- package/lib/module/toast/OverlayToastHost.web.js +32 -0
- package/lib/module/toast/OverlayToastHost.web.js.map +1 -0
- package/lib/module/toast/OverlayToastStore.js +217 -0
- package/lib/module/toast/OverlayToastStore.js.map +1 -0
- package/lib/module/toast/toastBridge.js +65 -0
- package/lib/module/toast/toastBridge.js.map +1 -0
- package/lib/module/toast/toastDomContent.web.js +298 -0
- package/lib/module/toast/toastDomContent.web.js.map +1 -0
- package/lib/module/toast/toastDomRenderer.web.js +695 -0
- package/lib/module/toast/toastDomRenderer.web.js.map +1 -0
- package/lib/module/toast/toastStyles.web.js +97 -0
- package/lib/module/toast/toastStyles.web.js.map +1 -0
- package/lib/module/toast/types.js +2 -0
- package/lib/module/toast/types.js.map +1 -0
- package/lib/module/web/overlayLayers.js +82 -0
- package/lib/module/web/overlayLayers.js.map +1 -1
- package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts +19 -0
- package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/OverlayStore.d.ts +8 -1
- package/lib/typescript/src/OverlayStore.d.ts.map +1 -1
- package/lib/typescript/src/OverlayView.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/toast/OverlayToastHost.d.ts +6 -0
- package/lib/typescript/src/toast/OverlayToastHost.d.ts.map +1 -0
- package/lib/typescript/src/toast/OverlayToastHost.web.d.ts +6 -0
- package/lib/typescript/src/toast/OverlayToastHost.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/OverlayToastStore.d.ts +47 -0
- package/lib/typescript/src/toast/OverlayToastStore.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastBridge.d.ts +11 -0
- package/lib/typescript/src/toast/toastBridge.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastDomContent.web.d.ts +15 -0
- package/lib/typescript/src/toast/toastDomContent.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastDomRenderer.web.d.ts +8 -0
- package/lib/typescript/src/toast/toastDomRenderer.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastStyles.web.d.ts +2 -0
- package/lib/typescript/src/toast/toastStyles.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/types.d.ts +240 -0
- package/lib/typescript/src/toast/types.d.ts.map +1 -0
- package/lib/typescript/src/web/overlayLayers.d.ts +2 -0
- package/lib/typescript/src/web/overlayLayers.d.ts.map +1 -1
- package/package.json +3 -2
- package/react-native.config.js +1 -0
- package/src/NativeOverlayToasterNativeComponent.ts +24 -0
- package/src/OverlayStore.ts +35 -4
- package/src/OverlayView.web.tsx +10 -11
- package/src/index.ts +23 -0
- package/src/toast/OverlayToastHost.tsx +62 -0
- package/src/toast/OverlayToastHost.web.tsx +29 -0
- package/src/toast/OverlayToastStore.ts +254 -0
- package/src/toast/toastBridge.ts +62 -0
- package/src/toast/toastDomContent.web.ts +351 -0
- package/src/toast/toastDomRenderer.web.ts +856 -0
- package/src/toast/toastStyles.web.ts +97 -0
- package/src/toast/types.ts +251 -0
- package/src/web/overlayLayers.ts +114 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import React
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
// The motion and gesture constants of the toaster this replaces.
|
|
5
|
+
private enum ToasterMetrics {
|
|
6
|
+
/// The first toast sits this far under the top safe-area inset.
|
|
7
|
+
static let topOffset: CGFloat = 16
|
|
8
|
+
static let gutter: CGFloat = 8
|
|
9
|
+
static let animationDuration: TimeInterval = 0.3
|
|
10
|
+
static let obstructionDuration: TimeInterval = 0.22
|
|
11
|
+
/// A dismissed card stays in the list this long, so `close` is reported
|
|
12
|
+
/// when the previous renderer unmounted the card.
|
|
13
|
+
static let removeDelay: TimeInterval = 1
|
|
14
|
+
/// Extra travel above the top edge for the resting-out position.
|
|
15
|
+
static let enterOvershoot: CGFloat = 50
|
|
16
|
+
static let fallbackHeight: CGFloat = 50
|
|
17
|
+
static let swipeDistance: CGFloat = 32
|
|
18
|
+
static let swipeFlickDistance: CGFloat = 12
|
|
19
|
+
static let swipeFlickVelocity: CGFloat = 600
|
|
20
|
+
/// Dragging against the dismiss direction moves the card a quarter as far.
|
|
21
|
+
static let swipeResistance: CGFloat = 4
|
|
22
|
+
static let fallbackMargin: CGFloat = 64
|
|
23
|
+
static let fallbackMaxPageWidth: CGFloat = 640
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// Quadratic ease-in-out, the timing curve of the animations this replaces.
|
|
27
|
+
private func toastAnimator(duration: TimeInterval) -> UIViewPropertyAnimator {
|
|
28
|
+
UIViewPropertyAnimator(
|
|
29
|
+
duration: duration,
|
|
30
|
+
timingParameters: UICubicTimingParameters(
|
|
31
|
+
controlPoint1: CGPoint(x: 0.455, y: 0.03), controlPoint2: CGPoint(x: 0.515, y: 0.955)
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// Lets touches that miss every card fall through to the windows below.
|
|
37
|
+
private final class ToastPassthroughView: UIView {
|
|
38
|
+
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
|
39
|
+
let hit = super.hitTest(point, with: event)
|
|
40
|
+
return hit === self ? nil : hit
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private final class ToastEntry {
|
|
45
|
+
let id: String
|
|
46
|
+
var item: NativeOverlayToastItem
|
|
47
|
+
let card: NativeOverlayToastCardView
|
|
48
|
+
/// Parent of the card: carries the stack position, the card the drag.
|
|
49
|
+
let slot: UIView = ToastPassthroughView()
|
|
50
|
+
var leaving = false
|
|
51
|
+
var reason = "programmatic"
|
|
52
|
+
/// Seconds of lifetime left, counted while not paused.
|
|
53
|
+
var remaining: TimeInterval?
|
|
54
|
+
var startedAt: Date?
|
|
55
|
+
var timer: Timer?
|
|
56
|
+
var removal: Timer?
|
|
57
|
+
var dragStart: CGFloat = 0
|
|
58
|
+
var dragged = false
|
|
59
|
+
var entered = false
|
|
60
|
+
|
|
61
|
+
init(item: NativeOverlayToastItem, cardColor: UIColor) {
|
|
62
|
+
id = item.spec.id
|
|
63
|
+
self.item = item
|
|
64
|
+
card = NativeOverlayToastCardView(spec: item.spec, cardColor: cardColor)
|
|
65
|
+
slot.addSubview(card)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// Draws the message toasts in the `toast` overlay level: stacking, timers,
|
|
70
|
+
/// swipe and motion all live here, on the main thread.
|
|
71
|
+
final class NativeOverlayToastPresenter: NSObject, NativeOverlayWindowOwner, UIGestureRecognizerDelegate {
|
|
72
|
+
var onEvent: ((_ id: String, _ generation: Int, _ type: String, _ value: String) -> Void)?
|
|
73
|
+
|
|
74
|
+
private weak var hostView: UIView?
|
|
75
|
+
private var window: UIWindow?
|
|
76
|
+
/// Rides under whatever hangs from the top of the window.
|
|
77
|
+
private let stage: UIView = ToastPassthroughView()
|
|
78
|
+
private var entries: [ToastEntry] = [] // newest first
|
|
79
|
+
private var config = NativeOverlayToasterConfig()
|
|
80
|
+
private var pressedCount = 0
|
|
81
|
+
private var obstructionShift: CGFloat = 0
|
|
82
|
+
|
|
83
|
+
init(hostView: UIView) {
|
|
84
|
+
self.hostView = hostView
|
|
85
|
+
super.init()
|
|
86
|
+
NotificationCenter.default.addObserver(
|
|
87
|
+
self, selector: #selector(layoutChanged), name: NativeOverlayKeyboard.didChange, object: nil
|
|
88
|
+
)
|
|
89
|
+
NotificationCenter.default.addObserver(
|
|
90
|
+
self, selector: #selector(layoutChanged),
|
|
91
|
+
name: UIDevice.orientationDidChangeNotification, object: nil
|
|
92
|
+
)
|
|
93
|
+
_ = NativeOverlayKeyboard.shared
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
deinit {
|
|
97
|
+
NotificationCenter.default.removeObserver(self)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private var reduceMotion: Bool {
|
|
101
|
+
config.respectReducedMotion && UIAccessibility.isReduceMotionEnabled
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// MARK: Sync
|
|
105
|
+
|
|
106
|
+
func sync(items: [NativeOverlayToastItem], config next: NativeOverlayToasterConfig) {
|
|
107
|
+
let configChanged =
|
|
108
|
+
next.contentWidth != config.contentWidth || next.contentMaxHeight != config.contentMaxHeight
|
|
109
|
+
|| next.cardColor != config.cardColor
|
|
110
|
+
config = next
|
|
111
|
+
let present = Set(items.map(\.spec.id))
|
|
112
|
+
// Cards the list no longer knows leave without ceremony.
|
|
113
|
+
for entry in entries where !present.contains(entry.id) {
|
|
114
|
+
remove(entry, report: false)
|
|
115
|
+
}
|
|
116
|
+
// Oldest first, so each new card joins in front of the previous one.
|
|
117
|
+
for item in items.reversed() {
|
|
118
|
+
var entry = entries.first { $0.id == item.spec.id }
|
|
119
|
+
if let existing = entry, existing.leaving, item.visible {
|
|
120
|
+
// Shown again while the previous card was leaving.
|
|
121
|
+
remove(existing, report: false)
|
|
122
|
+
entry = nil
|
|
123
|
+
}
|
|
124
|
+
guard let entry else {
|
|
125
|
+
if item.visible {
|
|
126
|
+
add(item)
|
|
127
|
+
} else {
|
|
128
|
+
onEvent?(item.spec.id, item.generation, "close", "programmatic")
|
|
129
|
+
}
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
if entry.item.revision != item.revision || configChanged {
|
|
133
|
+
entry.card.apply(spec: item.spec, cardColor: config.cardColor)
|
|
134
|
+
}
|
|
135
|
+
if entry.item.generation != item.generation {
|
|
136
|
+
// Shown again: the toast starts its lifetime over.
|
|
137
|
+
entry.remaining = lifetime(of: item.spec)
|
|
138
|
+
entry.startedAt = nil
|
|
139
|
+
entry.timer?.invalidate()
|
|
140
|
+
entry.timer = nil
|
|
141
|
+
}
|
|
142
|
+
entry.item = item
|
|
143
|
+
if !item.visible {
|
|
144
|
+
dismiss(entry, reason: "programmatic", report: false)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
layout(animated: true)
|
|
148
|
+
syncTimers()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
func invalidate() {
|
|
152
|
+
entries.forEach { entry in
|
|
153
|
+
entry.timer?.invalidate()
|
|
154
|
+
entry.removal?.invalidate()
|
|
155
|
+
entry.slot.removeFromSuperview()
|
|
156
|
+
}
|
|
157
|
+
entries = []
|
|
158
|
+
releaseWindow()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private func lifetime(of spec: NativeOverlayToastSpec) -> TimeInterval? {
|
|
162
|
+
spec.duration >= 0 ? spec.duration / 1000 : nil
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// MARK: Window
|
|
166
|
+
|
|
167
|
+
func overlaySceneDidDisconnect(window: UIWindow) {
|
|
168
|
+
if self.window === window {
|
|
169
|
+
self.window = nil
|
|
170
|
+
stage.removeFromSuperview()
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private func ensureWindow() -> UIView? {
|
|
175
|
+
if let root = window?.rootViewController?.view {
|
|
176
|
+
return root
|
|
177
|
+
}
|
|
178
|
+
guard let appWindow = hostView?.window else { return nil }
|
|
179
|
+
guard let overlayWindow = NativeOverlayWindowManager.shared.window(
|
|
180
|
+
for: self, level: .toast, stackOrder: 0, blocking: false, appWindow: appWindow
|
|
181
|
+
), let root = overlayWindow.rootViewController?.view else { return nil }
|
|
182
|
+
window = overlayWindow
|
|
183
|
+
stage.frame = root.bounds
|
|
184
|
+
stage.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
185
|
+
root.addSubview(stage)
|
|
186
|
+
return root
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private func releaseWindow() {
|
|
190
|
+
guard let window else { return }
|
|
191
|
+
stage.removeFromSuperview()
|
|
192
|
+
NativeOverlayWindowManager.shared.entryRemoved(from: window)
|
|
193
|
+
self.window = nil
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// The host joined a window: draw the toasts that were waiting for one.
|
|
197
|
+
func hostDidMoveToWindow() {
|
|
198
|
+
guard hostView?.window != nil, !entries.isEmpty else { return }
|
|
199
|
+
entries.filter { $0.slot.superview == nil }.reversed().forEach(attach)
|
|
200
|
+
layout(animated: true)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// MARK: Cards
|
|
204
|
+
|
|
205
|
+
private func add(_ item: NativeOverlayToastItem) {
|
|
206
|
+
let entry = ToastEntry(item: item, cardColor: config.cardColor)
|
|
207
|
+
entry.remaining = lifetime(of: item.spec)
|
|
208
|
+
entry.card.onAction = { [weak self, weak entry] actionId in
|
|
209
|
+
guard let entry, !entry.leaving else { return }
|
|
210
|
+
self?.onEvent?(entry.id, entry.item.generation, "action", actionId)
|
|
211
|
+
}
|
|
212
|
+
entry.card.onLink = { [weak self, weak entry] link in
|
|
213
|
+
guard let entry, !entry.leaving else { return }
|
|
214
|
+
self?.onEvent?(entry.id, entry.item.generation, "link", link)
|
|
215
|
+
}
|
|
216
|
+
entry.card.onPress = { [weak self, weak entry] in
|
|
217
|
+
guard let entry, !entry.leaving, entry.item.spec.pressable else { return }
|
|
218
|
+
self?.onEvent?(entry.id, entry.item.generation, "press", "")
|
|
219
|
+
}
|
|
220
|
+
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
|
|
221
|
+
pan.maximumNumberOfTouches = 1
|
|
222
|
+
pan.delegate = self
|
|
223
|
+
entry.card.addGestureRecognizer(pan)
|
|
224
|
+
// Press and hold pauses every toast's lifetime.
|
|
225
|
+
let press = UILongPressGestureRecognizer(target: self, action: #selector(handlePress(_:)))
|
|
226
|
+
press.minimumPressDuration = 0
|
|
227
|
+
press.cancelsTouchesInView = false
|
|
228
|
+
press.delegate = self
|
|
229
|
+
entry.card.addGestureRecognizer(press)
|
|
230
|
+
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
|
|
231
|
+
tap.delegate = self
|
|
232
|
+
entry.card.addGestureRecognizer(tap)
|
|
233
|
+
entries.insert(entry, at: 0)
|
|
234
|
+
attach(entry)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private func attach(_ entry: ToastEntry) {
|
|
238
|
+
guard ensureWindow() != nil else { return }
|
|
239
|
+
stage.addSubview(entry.slot)
|
|
240
|
+
entry.card.alpha = 0
|
|
241
|
+
UIAccessibility.post(notification: .announcement, argument: entry.card.announcement)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private func entry(for gesture: UIGestureRecognizer) -> ToastEntry? {
|
|
245
|
+
entries.first { $0.card === gesture.view }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private func dismiss(_ entry: ToastEntry, reason: String, report: Bool) {
|
|
249
|
+
guard !entry.leaving else { return }
|
|
250
|
+
entry.leaving = true
|
|
251
|
+
entry.reason = reason
|
|
252
|
+
entry.timer?.invalidate()
|
|
253
|
+
entry.timer = nil
|
|
254
|
+
if report {
|
|
255
|
+
onEvent?(entry.id, entry.item.generation, "dismiss", reason)
|
|
256
|
+
}
|
|
257
|
+
entry.removal = Timer.scheduledTimer(
|
|
258
|
+
withTimeInterval: ToasterMetrics.removeDelay, repeats: false
|
|
259
|
+
) { [weak self, weak entry] _ in
|
|
260
|
+
guard let self, let entry else { return }
|
|
261
|
+
self.remove(entry, report: true)
|
|
262
|
+
self.layout(animated: true)
|
|
263
|
+
}
|
|
264
|
+
layout(animated: true)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private func remove(_ entry: ToastEntry, report: Bool) {
|
|
268
|
+
entry.timer?.invalidate()
|
|
269
|
+
entry.removal?.invalidate()
|
|
270
|
+
entry.slot.removeFromSuperview()
|
|
271
|
+
entries.removeAll { $0 === entry }
|
|
272
|
+
if report {
|
|
273
|
+
onEvent?(entry.id, entry.item.generation, "close", entry.reason)
|
|
274
|
+
}
|
|
275
|
+
if entries.isEmpty {
|
|
276
|
+
pressedCount = 0
|
|
277
|
+
releaseWindow()
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// MARK: Layout
|
|
282
|
+
|
|
283
|
+
@objc private func layoutChanged() {
|
|
284
|
+
layout(animated: true)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private func contentWidth(in root: UIView) -> CGFloat {
|
|
288
|
+
config.contentWidth
|
|
289
|
+
?? min(root.bounds.width, ToasterMetrics.fallbackMaxPageWidth) - ToasterMetrics.fallbackMargin
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/// How far the whole stack rides down to rest under the top obstruction.
|
|
293
|
+
private func computeObstructionShift(in root: UIView) -> CGFloat {
|
|
294
|
+
guard config.topObstruction > 0 else { return 0 }
|
|
295
|
+
let toastTop = root.safeAreaInsets.top + ToasterMetrics.topOffset
|
|
296
|
+
let keyboardTop = NativeOverlayKeyboard.shared.top(in: root)
|
|
297
|
+
let usableBottom = keyboardTop ?? (root.bounds.height - root.safeAreaInsets.bottom)
|
|
298
|
+
let top = min(
|
|
299
|
+
config.topObstruction + config.topObstructionGap,
|
|
300
|
+
usableBottom - config.topObstructionRoom
|
|
301
|
+
)
|
|
302
|
+
return max(0, top - toastTop)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private func layout(animated: Bool) {
|
|
306
|
+
guard let root = window?.rootViewController?.view, !entries.isEmpty else { return }
|
|
307
|
+
let width = contentWidth(in: root)
|
|
308
|
+
let safe = root.safeAreaInsets
|
|
309
|
+
let top = safe.top + ToasterMetrics.topOffset
|
|
310
|
+
let animate = animated && !reduceMotion
|
|
311
|
+
|
|
312
|
+
let shift = computeObstructionShift(in: root)
|
|
313
|
+
if shift != obstructionShift {
|
|
314
|
+
obstructionShift = shift
|
|
315
|
+
let apply = { self.stage.transform = CGAffineTransform(translationX: 0, y: shift) }
|
|
316
|
+
if animate {
|
|
317
|
+
let animator = toastAnimator(duration: ToasterMetrics.obstructionDuration)
|
|
318
|
+
animator.addAnimations(apply)
|
|
319
|
+
animator.startAnimation()
|
|
320
|
+
} else {
|
|
321
|
+
apply()
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Oldest at the top, newest under it: a toast keeps its place when a
|
|
326
|
+
// newer one arrives and moves up when an older one leaves.
|
|
327
|
+
var offset: CGFloat = 0
|
|
328
|
+
for entry in entries.reversed() {
|
|
329
|
+
let size = entry.card.layoutCard(
|
|
330
|
+
contentWidth: width, contentMaxHeight: config.contentMaxHeight
|
|
331
|
+
)
|
|
332
|
+
let x = safe.left + (root.bounds.width - safe.left - safe.right - size.width) / 2
|
|
333
|
+
// Where the card rests before it enters and after it leaves.
|
|
334
|
+
let hidden = -(size.height > 0 ? size.height : ToasterMetrics.fallbackHeight)
|
|
335
|
+
- safe.top - ToasterMetrics.enterOvershoot
|
|
336
|
+
let targetY = top + (entry.leaving ? hidden : offset)
|
|
337
|
+
let targetAlpha: CGFloat = entry.leaving ? 0 : 1
|
|
338
|
+
if !entry.leaving {
|
|
339
|
+
offset += size.height + ToasterMetrics.gutter
|
|
340
|
+
}
|
|
341
|
+
let frame = CGRect(x: x, y: targetY, width: size.width, height: size.height)
|
|
342
|
+
if !entry.entered {
|
|
343
|
+
entry.entered = true
|
|
344
|
+
entry.slot.frame = CGRect(x: x, y: top + hidden, width: size.width, height: size.height)
|
|
345
|
+
}
|
|
346
|
+
entry.slot.bounds.size = size
|
|
347
|
+
entry.card.center = CGPoint(x: size.width / 2, y: size.height / 2)
|
|
348
|
+
let apply = {
|
|
349
|
+
entry.slot.frame = frame
|
|
350
|
+
entry.card.alpha = targetAlpha
|
|
351
|
+
}
|
|
352
|
+
if animate {
|
|
353
|
+
let animator = toastAnimator(duration: ToasterMetrics.animationDuration)
|
|
354
|
+
animator.addAnimations(apply)
|
|
355
|
+
animator.startAnimation()
|
|
356
|
+
} else {
|
|
357
|
+
apply()
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// MARK: Timers
|
|
363
|
+
|
|
364
|
+
private func syncTimers() {
|
|
365
|
+
let paused = pressedCount > 0
|
|
366
|
+
for entry in entries where !entry.leaving {
|
|
367
|
+
guard let remaining = entry.remaining else { continue }
|
|
368
|
+
if paused {
|
|
369
|
+
if let startedAt = entry.startedAt {
|
|
370
|
+
entry.remaining = max(0, remaining - Date().timeIntervalSince(startedAt))
|
|
371
|
+
entry.startedAt = nil
|
|
372
|
+
}
|
|
373
|
+
entry.timer?.invalidate()
|
|
374
|
+
entry.timer = nil
|
|
375
|
+
continue
|
|
376
|
+
}
|
|
377
|
+
guard entry.timer == nil else { continue }
|
|
378
|
+
entry.startedAt = Date()
|
|
379
|
+
entry.timer = Timer.scheduledTimer(
|
|
380
|
+
withTimeInterval: max(remaining, 0), repeats: false
|
|
381
|
+
) { [weak self, weak entry] _ in
|
|
382
|
+
guard let self, let entry else { return }
|
|
383
|
+
entry.timer = nil
|
|
384
|
+
self.dismiss(entry, reason: "timeout", report: true)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// MARK: Gestures
|
|
390
|
+
|
|
391
|
+
func gestureRecognizer(
|
|
392
|
+
_ gestureRecognizer: UIGestureRecognizer,
|
|
393
|
+
shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer
|
|
394
|
+
) -> Bool {
|
|
395
|
+
gestureRecognizer is UILongPressGestureRecognizer || other is UILongPressGestureRecognizer
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
|
399
|
+
if gestureRecognizer is UITapGestureRecognizer {
|
|
400
|
+
guard let entry = entry(for: gestureRecognizer), entry.item.spec.pressable else { return false }
|
|
401
|
+
if touch.view is UIControl || touch.view is ToastTextLabel { return false }
|
|
402
|
+
}
|
|
403
|
+
return true
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
@objc private func handlePress(_ gesture: UILongPressGestureRecognizer) {
|
|
407
|
+
switch gesture.state {
|
|
408
|
+
case .began:
|
|
409
|
+
pressedCount += 1
|
|
410
|
+
case .ended, .cancelled, .failed:
|
|
411
|
+
pressedCount = max(0, pressedCount - 1)
|
|
412
|
+
default:
|
|
413
|
+
return
|
|
414
|
+
}
|
|
415
|
+
syncTimers()
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
@objc private func handleTap(_ gesture: UITapGestureRecognizer) {
|
|
419
|
+
guard let entry = entry(for: gesture), !entry.leaving, !entry.dragged,
|
|
420
|
+
entry.item.spec.pressable else { return }
|
|
421
|
+
onEvent?(entry.id, entry.item.generation, "press", "")
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
@objc private func handlePan(_ gesture: UIPanGestureRecognizer) {
|
|
425
|
+
guard let entry = entry(for: gesture), !entry.leaving else { return }
|
|
426
|
+
let card = entry.card
|
|
427
|
+
// Toasts land at the top, so the dismiss direction is up.
|
|
428
|
+
let translation = gesture.translation(in: stage)
|
|
429
|
+
switch gesture.state {
|
|
430
|
+
case .began:
|
|
431
|
+
card.layer.removeAllAnimations()
|
|
432
|
+
entry.dragStart = card.transform.ty
|
|
433
|
+
entry.dragged = true
|
|
434
|
+
case .changed:
|
|
435
|
+
let drag = translation.y <= 0
|
|
436
|
+
? entry.dragStart + translation.y
|
|
437
|
+
: entry.dragStart + translation.y / ToasterMetrics.swipeResistance
|
|
438
|
+
card.transform = CGAffineTransform(translationX: 0, y: drag)
|
|
439
|
+
case .ended:
|
|
440
|
+
let distance = -translation.y
|
|
441
|
+
let velocity = -gesture.velocity(in: stage).y
|
|
442
|
+
let dismissed =
|
|
443
|
+
translation.y < 0 && abs(distance) > abs(translation.x)
|
|
444
|
+
&& (distance >= ToasterMetrics.swipeDistance
|
|
445
|
+
|| (distance >= ToasterMetrics.swipeFlickDistance
|
|
446
|
+
&& velocity >= ToasterMetrics.swipeFlickVelocity))
|
|
447
|
+
if dismissed {
|
|
448
|
+
dismiss(entry, reason: "swipe", report: true)
|
|
449
|
+
} else {
|
|
450
|
+
settle(card)
|
|
451
|
+
}
|
|
452
|
+
DispatchQueue.main.async { entry.dragged = false }
|
|
453
|
+
case .cancelled, .failed:
|
|
454
|
+
settle(card)
|
|
455
|
+
entry.dragged = false
|
|
456
|
+
default:
|
|
457
|
+
break
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private func settle(_ card: UIView) {
|
|
462
|
+
guard !reduceMotion else {
|
|
463
|
+
card.transform = .identity
|
|
464
|
+
return
|
|
465
|
+
}
|
|
466
|
+
let animator = toastAnimator(duration: ToasterMetrics.animationDuration)
|
|
467
|
+
animator.addAnimations { card.transform = .identity }
|
|
468
|
+
animator.startAnimation()
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/// The zero-size host mounted by `OverlayToastHost`. It only carries the toast
|
|
473
|
+
/// list and settings across the bridge; the cards are drawn in the `toast`
|
|
474
|
+
/// overlay window.
|
|
475
|
+
@objc(NativeOverlayToasterView)
|
|
476
|
+
public final class NativeOverlayToasterView: UIView {
|
|
477
|
+
@objc public var onToastEvent: RCTDirectEventBlock?
|
|
478
|
+
|
|
479
|
+
private lazy var presenter: NativeOverlayToastPresenter = {
|
|
480
|
+
let presenter = NativeOverlayToastPresenter(hostView: self)
|
|
481
|
+
presenter.onEvent = { [weak self] id, generation, type, value in
|
|
482
|
+
self?.onToastEvent?([
|
|
483
|
+
"toastId": id, "generation": generation, "type": type, "value": value,
|
|
484
|
+
])
|
|
485
|
+
}
|
|
486
|
+
return presenter
|
|
487
|
+
}()
|
|
488
|
+
|
|
489
|
+
private var toasts = ""
|
|
490
|
+
private var config = ""
|
|
491
|
+
|
|
492
|
+
override public init(frame: CGRect) {
|
|
493
|
+
super.init(frame: frame)
|
|
494
|
+
isUserInteractionEnabled = false
|
|
495
|
+
isHidden = true
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
@available(*, unavailable)
|
|
499
|
+
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
500
|
+
|
|
501
|
+
@objc public func update(toasts nextToasts: String, config nextConfig: String) {
|
|
502
|
+
guard nextToasts != toasts || nextConfig != config else { return }
|
|
503
|
+
toasts = nextToasts
|
|
504
|
+
config = nextConfig
|
|
505
|
+
presenter.sync(
|
|
506
|
+
items: NativeOverlayToastItem.parse(nextToasts),
|
|
507
|
+
config: NativeOverlayToasterConfig(jsonString: nextConfig)
|
|
508
|
+
)
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
override public func didMoveToWindow() {
|
|
512
|
+
super.didMoveToWindow()
|
|
513
|
+
presenter.hostDidMoveToWindow()
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
@objc public func invalidate() {
|
|
517
|
+
presenter.invalidate()
|
|
518
|
+
}
|
|
519
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#import "RCTNativeOverlayToasterComponentView.h"
|
|
2
|
+
|
|
3
|
+
#import <react/renderer/components/RNCNativeOverlay/ComponentDescriptors.h>
|
|
4
|
+
#import <react/renderer/components/RNCNativeOverlay/EventEmitters.h>
|
|
5
|
+
#import <react/renderer/components/RNCNativeOverlay/Props.h>
|
|
6
|
+
#import <react/renderer/components/RNCNativeOverlay/RCTComponentViewHelpers.h>
|
|
7
|
+
|
|
8
|
+
#import <React/RCTComponent.h>
|
|
9
|
+
#import <React/RCTFabricComponentsPlugins.h>
|
|
10
|
+
|
|
11
|
+
#if __has_include(<NativeOverlay/NativeOverlay-Swift.h>)
|
|
12
|
+
#import <NativeOverlay/NativeOverlay-Swift.h>
|
|
13
|
+
#elif __has_include("NativeOverlay/NativeOverlay-Swift.h")
|
|
14
|
+
#import "NativeOverlay/NativeOverlay-Swift.h"
|
|
15
|
+
#else
|
|
16
|
+
#import "NativeOverlay-Swift.h"
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
using namespace facebook::react;
|
|
20
|
+
|
|
21
|
+
@implementation RCTNativeOverlayToasterComponentView {
|
|
22
|
+
NativeOverlayToasterView *_toasterView;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
26
|
+
{
|
|
27
|
+
return concreteComponentDescriptorProvider<RNCNativeOverlayToasterComponentDescriptor>();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
- (instancetype)initWithFrame:(CGRect)frame
|
|
31
|
+
{
|
|
32
|
+
if (self = [super initWithFrame:frame]) {
|
|
33
|
+
static const auto defaultProps = std::make_shared<const RNCNativeOverlayToasterProps>();
|
|
34
|
+
_props = defaultProps;
|
|
35
|
+
_toasterView = [NativeOverlayToasterView new];
|
|
36
|
+
__weak __typeof(self) weakSelf = self;
|
|
37
|
+
_toasterView.onToastEvent = ^(NSDictionary *body) {
|
|
38
|
+
__typeof(self) strongSelf = weakSelf;
|
|
39
|
+
if (!strongSelf || !strongSelf->_eventEmitter) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
auto emitter =
|
|
43
|
+
std::static_pointer_cast<const RNCNativeOverlayToasterEventEmitter>(strongSelf->_eventEmitter);
|
|
44
|
+
NSString *toastId = body[@"toastId"] ?: @"";
|
|
45
|
+
NSNumber *generation = body[@"generation"];
|
|
46
|
+
NSString *type = body[@"type"] ?: @"";
|
|
47
|
+
NSString *value = body[@"value"] ?: @"";
|
|
48
|
+
emitter->onToastEvent({
|
|
49
|
+
.toastId = std::string(toastId.UTF8String),
|
|
50
|
+
.generation = generation ? generation.intValue : 0,
|
|
51
|
+
.type = std::string(type.UTF8String),
|
|
52
|
+
.value = std::string(value.UTF8String),
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
self.contentView = _toasterView;
|
|
56
|
+
}
|
|
57
|
+
return self;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The host is created once and keeps the toast window alive across updates.
|
|
61
|
+
+ (BOOL)shouldBeRecycled
|
|
62
|
+
{
|
|
63
|
+
return NO;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
|
|
67
|
+
{
|
|
68
|
+
const auto &newProps = *std::static_pointer_cast<RNCNativeOverlayToasterProps const>(props);
|
|
69
|
+
[_toasterView updateWithToasts:[NSString stringWithUTF8String:newProps.toasts.c_str()]
|
|
70
|
+
config:[NSString stringWithUTF8String:newProps.config.c_str()]];
|
|
71
|
+
[super updateProps:props oldProps:oldProps];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
- (void)invalidate
|
|
75
|
+
{
|
|
76
|
+
[_toasterView invalidate];
|
|
77
|
+
[super invalidate];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@end
|
|
81
|
+
|
|
82
|
+
Class<RCTComponentViewProtocol> RNCNativeOverlayToasterCls(void)
|
|
83
|
+
{
|
|
84
|
+
return RCTNativeOverlayToasterComponentView.class;
|
|
85
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { codegenNativeComponent } from 'react-native';
|
|
2
|
+
|
|
3
|
+
import type { CodegenTypes, HostComponent, ViewProps } from 'react-native';
|
|
4
|
+
|
|
5
|
+
type INativeOverlayToastEvent = Readonly<{
|
|
6
|
+
toastId: string;
|
|
7
|
+
generation: CodegenTypes.Int32;
|
|
8
|
+
/** `dismiss` | `close` | `action` | `link` | `press`. */
|
|
9
|
+
type: string;
|
|
10
|
+
/** The dismiss reason, action id or link of the event; empty otherwise. */
|
|
11
|
+
value: string;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
export interface INativeOverlayToasterNativeProps extends ViewProps {
|
|
15
|
+
/** JSON of `IOverlayToastItem[]`, newest first. */
|
|
16
|
+
toasts?: CodegenTypes.WithDefault<string, ''>;
|
|
17
|
+
/** JSON of `IOverlayToasterConfig`. */
|
|
18
|
+
config?: CodegenTypes.WithDefault<string, ''>;
|
|
19
|
+
onToastEvent?: CodegenTypes.DirectEventHandler<INativeOverlayToastEvent>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default codegenNativeComponent<INativeOverlayToasterNativeProps>(
|
|
23
|
+
'RNCNativeOverlayToaster'
|
|
24
|
+
) as HostComponent<INativeOverlayToasterNativeProps>;
|
|
@@ -135,6 +135,19 @@ export class OverlayStore {
|
|
|
135
135
|
}
|
|
136
136
|
dropped.forEach(e => this.notifyRemoved(e.id, reason));
|
|
137
137
|
}
|
|
138
|
+
setFocusedPageHost(hostKey) {
|
|
139
|
+
if (this.focusedPageHost === hostKey) return;
|
|
140
|
+
this.focusedPageHost = hostKey;
|
|
141
|
+
this.emit();
|
|
142
|
+
}
|
|
143
|
+
isPageHostFocused(hostKey) {
|
|
144
|
+
return this.focusedPageHost === undefined || this.focusedPageHost === hostKey;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** @internal Web supplies its DOM host check for blocking and Escape. */
|
|
148
|
+
setPageHostAvailabilityCheck(isAvailable) {
|
|
149
|
+
this.isPageHostAvailable = isAvailable;
|
|
150
|
+
}
|
|
138
151
|
|
|
139
152
|
/** Native / navigation signal: the owning page was covered, detached or restored. */
|
|
140
153
|
setPageVisible(ownerKey, visible) {
|
|
@@ -180,7 +193,7 @@ export class OverlayStore {
|
|
|
180
193
|
resolveBack(focusedHostKey) {
|
|
181
194
|
// Non-blocking entries take back / Escape only when they ask to be
|
|
182
195
|
// dismissible (an anchored popover); toasts do not.
|
|
183
|
-
const candidates = this.snapshot.entries.filter(e => e.phase === 'active' && !e.suspended && (e.blocking || e.dismissible) && (e.scope === 'global' || focusedHostKey === undefined || e.hostKey === focusedHostKey));
|
|
196
|
+
const candidates = this.snapshot.entries.filter(e => e.phase === 'active' && !e.suspended && (e.blocking || e.dismissible) && (e.scope === 'global' || this.isPageHostInteractive(e.hostKey) && (focusedHostKey === undefined || e.hostKey === focusedHostKey)));
|
|
184
197
|
const top = candidates[candidates.length - 1];
|
|
185
198
|
if (!top) {
|
|
186
199
|
return {
|
|
@@ -195,23 +208,27 @@ export class OverlayStore {
|
|
|
195
208
|
};
|
|
196
209
|
}
|
|
197
210
|
|
|
198
|
-
/** Topmost visible blocking entry; everything rendered below it is inert. */
|
|
199
211
|
/** The entry whose `statusBarStyle` applies (see `findStatusBarOwner`). */
|
|
200
212
|
getStatusBarOwner() {
|
|
201
213
|
return findStatusBarOwner(this.snapshot);
|
|
202
214
|
}
|
|
215
|
+
|
|
216
|
+
/** Topmost visible blocking entry; everything rendered below it is inert. */
|
|
203
217
|
getBlockingTop() {
|
|
204
218
|
const {
|
|
205
219
|
entries
|
|
206
220
|
} = this.snapshot;
|
|
207
221
|
for (let i = entries.length - 1; i >= 0; i -= 1) {
|
|
208
222
|
const entry = entries[i];
|
|
209
|
-
if (entry && entry.phase !== 'closing' && !entry.suspended && entry.blocking) {
|
|
223
|
+
if (entry && entry.phase !== 'closing' && !entry.suspended && entry.blocking && (entry.scope === 'global' || this.isPageHostInteractive(entry.hostKey))) {
|
|
210
224
|
return entry;
|
|
211
225
|
}
|
|
212
226
|
}
|
|
213
227
|
return undefined;
|
|
214
228
|
}
|
|
229
|
+
isPageHostInteractive(hostKey) {
|
|
230
|
+
return this.isPageHostFocused(hostKey) && (this.isPageHostAvailable?.(hostKey) ?? true);
|
|
231
|
+
}
|
|
215
232
|
dismissWithoutEmit(id, reason) {
|
|
216
233
|
const queuedIndex = this.queued.findIndex(e => e.id === id);
|
|
217
234
|
if (queuedIndex !== -1) {
|