Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Created April 15, 2021 11:19
Show Gist options
  • Save OskarGroth/4866905c17998e02715d42468f5a018a to your computer and use it in GitHub Desktop.
Save OskarGroth/4866905c17998e02715d42468f5a018a to your computer and use it in GitHub Desktop.
import Cocoa
import SwiftUI
@main
struct StatusBugApp: App {
@StateObject var coordinator = StatusCoordinator()
var body: some Scene {
WindowGroup {
EmptyView()
}
}
}
// Make sure you have two desktops (screen or spaces) with different tinting status bars
class StatusItem<Content: View>: NSHostingView<Content> {
override func layout() {
super.layout()
print("Layout view")
}
override func layoutSubtreeIfNeeded() {
super.layoutSubtreeIfNeeded()
print("Layout subtree")
}
override func viewWillDraw() {
super.viewWillDraw()
print("View will draw")
}
}
class StatusCoordinator: ObservableObject {
let statusItem: NSStatusItem
init() {
let statusView = StatusItem(rootView: Text("Hello, world!").fixedSize())
statusView.translatesAutoresizingMaskIntoConstraints = false
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.addSubview(statusView)
NSLayoutConstraint.activate([
statusView.leadingAnchor.constraint(equalTo: statusItem.button!.leadingAnchor),
statusView.trailingAnchor.constraint(equalTo: statusItem.button!.trailingAnchor),
statusView.heightAnchor.constraint(equalToConstant: 20),
statusView.centerYAnchor.constraint(equalTo: statusItem.button!.centerYAnchor)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment