iOS/Sources/App/WebView/ScaleFactorMutator.swift
mat1th 97834bfd5e
Update swift lint and format + appy fixes (#2585)
## Summary
Swift lint and swiftformat are outdated. This PR does update those +
applies the new formatting form swiftformat.
There is 1 swift file with a manual change:
`Sources/Vehicle/Templates/Areas/CarPlayAreasViewModel.swift`. This is
done because `swiftlint` did create the following swiftlint error:
`error: Cyclomatic Complexity Violation: Function should have complexity
10 or less; currently complexity is 11 (cyclomatic_complexity)`.

Because it does change a lot of files the question is if we want to
finetune the `swiftformat` rules.

## Screenshots
No user facing changes.

## Link to pull request in Documentation repository
NA

## Any other notes
NA
2024-02-22 13:06:39 +01:00

99 lines
3.1 KiB
Swift

import Foundation
import Shared
enum ScaleFactorMutator {
fileprivate static var sceneIdentifiers = Set<String>()
public static func record(sceneIdentifier: String) {
swizzleIfNeeded()
sceneIdentifiers.insert(sceneIdentifier)
}
private static var hasSwizzled = false
fileprivate static var didSwizzleScaleFactor = false
fileprivate static var didSwizzleSceneIdentifier = false
private static func swizzleIfNeeded() {
guard !hasSwizzled else { return }
defer { hasSwizzled = true }
if UIDevice.current.userInterfaceIdiom == .mac {
// we do not need to swizzle when using the mac idiom on macOS 11
return
}
#if targetEnvironment(macCatalyst)
func exchange(for klassString: String, original: Selector, with replacement: Selector) -> Bool {
guard
let klass = objc_getClass(klassString) as? AnyClass,
let originalMethod = class_getInstanceMethod(klass, original),
let replacementMethod = class_getInstanceMethod(klass, replacement) else {
Current.Log.error("couldn't get \(klassString) method \(original) and \(replacement)")
return false
}
method_exchangeImplementations(originalMethod, replacementMethod)
return true
}
// macOS 10.15 through 11.2
didSwizzleScaleFactor = exchange(
for: "UINSSceneView",
original: Selector(("setScaleFactor:")),
with: #selector(NSObject.setHa_scaleFactor(_:))
)
if !didSwizzleScaleFactor {
// macOS 11.3+
didSwizzleScaleFactor = exchange(
for: "UINSSceneView",
original: Selector(("setFixedSceneToSceneViewScaleFactor:")),
with: #selector(NSObject.setHa_scaleFactor(_:))
)
}
didSwizzleSceneIdentifier = exchange(
for: "UINSSceneView",
original: Selector(("setSceneIdentifier:")),
with: #selector(NSObject.setHa_sceneIdentifier(_:))
)
#endif
}
}
#if targetEnvironment(macCatalyst)
fileprivate extension NSObject {
var sceneIdentifier: String? {
if responds(to: Selector(("sceneIdentifier"))) {
return value(forKey: "sceneIdentifier") as? String
} else {
return nil
}
}
@objc func setHa_scaleFactor(_ scaleFactor: CGFloat) {
guard ScaleFactorMutator.didSwizzleScaleFactor else {
return
}
if let identifier = sceneIdentifier, ScaleFactorMutator.sceneIdentifiers.contains(identifier) {
setHa_scaleFactor(1.0)
} else {
setHa_scaleFactor(scaleFactor)
}
}
@objc func setHa_sceneIdentifier(_ identifier: String) {
guard ScaleFactorMutator.didSwizzleSceneIdentifier else {
return
}
setHa_sceneIdentifier(identifier)
if ScaleFactorMutator.sceneIdentifiers.contains(identifier) {
// this calls the UIKit/AppKit version
setHa_scaleFactor(1.0)
}
}
}
#endif