Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from zenangst/feature/view-injection
Adds view injection via swizzling
- Loading branch information
Showing
6 changed files
with
78 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
#if os(macOS) | ||
import Cocoa | ||
public typealias ViewController = NSViewController | ||
public typealias View = NSView | ||
#else | ||
import UIKit | ||
public typealias ViewController = UIViewController | ||
public typealias View = UIView | ||
#endif |
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,38 @@ | ||
#if os(macOS) | ||
import Cocoa | ||
public typealias Rect = NSRect | ||
#else | ||
import UIKit | ||
public typealias Rect = CGRect | ||
#endif | ||
|
||
extension View { | ||
// Swizzle initializers for views if application is compiled in debug mode. | ||
static func _swizzleViews() { | ||
#if DEBUG | ||
DispatchQueue.once(token: "com.zenangst.Vaccine.swizzleViews") { | ||
let originalSelector = #selector(View.init(frame:)) | ||
let swizzledSelector = #selector(View.init(vaccineFrame:)) | ||
Swizzling.swizzle(View.self, | ||
originalSelector: originalSelector, | ||
swizzledSelector: swizzledSelector) | ||
} | ||
#endif | ||
|
||
} | ||
|
||
@objc public convenience init(vaccineFrame frame: Rect) { | ||
self.init(vaccineFrame: frame) | ||
let selector = #selector(View.vaccine_view_injected(_:)) | ||
if responds(to: selector) { | ||
addInjection(with: selector, invoke: nil) | ||
} | ||
} | ||
|
||
@objc func vaccine_view_injected(_ notification: Notification) { | ||
let selector = Selector("loadView") | ||
if responds(to: selector), Injection.objectWasInjected(self, in: notification) { | ||
perform(selector) | ||
} | ||
} | ||
} |
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