-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: use swizzle instead of SceneDelegate and improvements for macOS
- Loading branch information
1 parent
ceeb910
commit 29a5fd7
Showing
59 changed files
with
1,437 additions
and
744 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// PreferenceHostingView.swift | ||
// Mochi | ||
// | ||
// Created by ErrorErrorError on 11/28/23. | ||
// | ||
// Source: https://gist.github.com/Amzd/01e1f69ecbc4c82c8586dcd292b1d30d | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
#if canImport(UIKit) | ||
@MainActor | ||
struct PreferenceHostingView<Content: View>: UIViewControllerRepresentable { | ||
init(content: @escaping () -> Content) { | ||
_ = UIViewController.swizzle() | ||
self.content = content | ||
} | ||
|
||
let content: () -> Content | ||
|
||
func makeUIViewController(context: Context) -> PreferenceHostingController<Content> { | ||
PreferenceHostingController(rootView: content) | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {} | ||
} | ||
|
||
extension UIViewController { | ||
static func swizzle() { | ||
Swizzle(UIViewController.self) { | ||
#selector(getter: childForHomeIndicatorAutoHidden) => #selector(__swizzledChildForHomeIndicatorAutoHidden) | ||
} | ||
} | ||
|
||
@objc func __swizzledChildForHomeIndicatorAutoHidden() -> UIViewController? { | ||
if self is OpaquePreferenceHostingController { | ||
return nil | ||
} else { | ||
return search() | ||
} | ||
} | ||
|
||
private func search() -> OpaquePreferenceHostingController? { | ||
if let result = children.compactMap({ $0 as? OpaquePreferenceHostingController }).first { | ||
return result | ||
} | ||
|
||
for child in children { | ||
if let result = child.search() { | ||
return result | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
#endif | ||
|
||
// Move to utils? | ||
struct Swizzle { | ||
@discardableResult | ||
init( | ||
_ type: AnyClass, | ||
@SwizzleSelectorsBuilder builder: () -> [SwizzleReplacer] | ||
) { | ||
builder().forEach { $0(type) } | ||
} | ||
} | ||
|
||
struct SwizzleReplacer { | ||
let original: Selector | ||
let swizzled: Selector | ||
|
||
func callAsFunction(_ type: AnyClass) { | ||
guard let originalMethod = class_getInstanceMethod(type, original), | ||
let swizzledMethod = class_getInstanceMethod(type, swizzled) else { | ||
return | ||
} | ||
|
||
method_exchangeImplementations(originalMethod, swizzledMethod) | ||
} | ||
} | ||
|
||
@resultBuilder | ||
enum SwizzleSelectorsBuilder { | ||
typealias Component = SwizzleReplacer | ||
|
||
static func buildBlock(_ components: Component...) -> [Component] { | ||
components | ||
} | ||
} | ||
|
||
infix operator => | ||
|
||
extension Selector { | ||
static func => (original: Selector, swizzled: Selector) -> SwizzleReplacer { | ||
.init(original: original, swizzled: swizzled) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.