1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2025-02-03 18:57:46 +01:00
This commit is contained in:
Chase Carroll 2022-12-05 20:22:50 -05:00
parent 4689c8e78f
commit 5648b13517

View File

@ -9,12 +9,10 @@
import UIKit
/// View that represents a single touch from the user.
fileprivate final class TouchView: UIView {
fileprivate lazy var blurView: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterialLight)
return UIVisualEffectView(effect: blurEffect)
}()
private let blurView: UIVisualEffectView
override var frame: CGRect {
didSet {
@ -23,6 +21,9 @@ fileprivate final class TouchView: UIView {
}
override init(frame: CGRect) {
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterialLight)
blurView = UIVisualEffectView(effect: blurEffect)
super.init(frame: frame)
backgroundColor = .clear
@ -47,6 +48,7 @@ fileprivate final class TouchView: UIView {
}
/// `UIWindow` subclass that renders visual representations of the user's touches.
public final class TouchesVisibleWindow: UIWindow {
public var touchesVisible = false {
@ -57,9 +59,9 @@ public final class TouchesVisibleWindow: UIWindow {
}
}
fileprivate var touchViews: [UITouch : TouchView] = [:]
private var touchViews: [UITouch : TouchView] = [:]
fileprivate func newTouchView() -> TouchView {
private func newTouchView() -> TouchView {
let touchSize = 44.0
return TouchView(frame: CGRect(
origin: .zero,
@ -70,7 +72,7 @@ public final class TouchesVisibleWindow: UIWindow {
))
}
fileprivate func cleanupTouch(_ touch: UITouch) {
private func cleanupTouch(_ touch: UITouch) {
guard let touchView = touchViews[touch] else {
return
}
@ -79,7 +81,7 @@ public final class TouchesVisibleWindow: UIWindow {
touchViews.removeValue(forKey: touch)
}
fileprivate func cleanUpAllTouches() {
private func cleanUpAllTouches() {
for (_, touchView) in touchViews {
touchView.removeFromSuperview()
}
@ -88,42 +90,39 @@ public final class TouchesVisibleWindow: UIWindow {
}
public override func sendEvent(_ event: UIEvent) {
if !touchesVisible {
super.sendEvent(event)
return
}
let touches = event.allTouches
guard
let touches = touches,
touches.count > 0
else {
cleanUpAllTouches()
super.sendEvent(event)
return
}
for touch in touches {
let touchLocation = touch.location(in: self)
switch touch.phase {
case .began:
let touchView = newTouchView()
touchView.center = touchLocation
addSubview(touchView)
touchViews[touch] = touchView
case .moved:
guard let touchView = touchViews[touch] else {
return
if touchesVisible {
let touches = event.allTouches
guard
let touches = touches,
touches.count > 0
else {
cleanUpAllTouches()
super.sendEvent(event)
return
}
for touch in touches {
let touchLocation = touch.location(in: self)
switch touch.phase {
case .began:
let touchView = newTouchView()
touchView.center = touchLocation
addSubview(touchView)
touchViews[touch] = touchView
case .moved:
guard let touchView = touchViews[touch] else {
return
}
touchView.center = touchLocation
case .ended, .cancelled:
cleanupTouch(touch)
default:
break
}
touchView.center = touchLocation
case .ended, .cancelled:
cleanupTouch(touch)
default:
break
}
}