Skip to content

Instantly share code, notes, and snippets.

@devxoul
Created May 8, 2020 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/5925b2a5f88bab272e1087102b869a5d to your computer and use it in GitHub Desktop.
Save devxoul/5925b2a5f88bab272e1087102b869a5d to your computer and use it in GitHub Desktop.
NSStatusBarStub Draft
final class NSStatusBarStub: NSStatusBar {
override func statusItem(withLength length: CGFloat) -> NSStatusItem {
let alloc = NSStatusItem.perform(NSSelectorFromString("alloc"))!.takeRetainedValue() as! NSStatusItem
let selector = NSSelectorFromString("_initInStatusBar:withLength:withPriority:hidden:")
let imp = alloc.method(for: selector)
typealias ObjcInitBlock = @convention(c) (NSStatusItem, Selector, NSStatusBar, CGFloat, Int, Bool) -> NSStatusItem
let initializer = unsafeBitCast(imp, to: ObjcInitBlock.self)
let statusItem = initializer(alloc, selector, self, length, 1, true) // it won't work the last parameter("hidden") is set to `false`
return statusItem
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment