TipNSPopover
A subclass of NSPopover that displays a popover tip in AppKit applications.
Declaration
@MainActor @objc @preconcurrency final class TipNSPopoverOverview
Use this to create a tip you want to display and lay out as a NSPopover.
Adding and removing TipNSView from your app is done by listening to a tip’s shouldDisplayUpdates or statusUpdates.
import Cocoa
import TipKit
struct CatTracksFeatureTip: Tip {
var title: Text {
Text("Sample tip title")
}
var message: Text? {
Text("Sample tip message")
}
var image: Image? {
Image(systemName: "globe")
}
}
class CatTracksViewController: NSViewController {
@IBOutlet weak var catTracksFeatureButton: NSButton!
private var catTracksFeatureTip = CatTracksFeatureTip()
private var tipObservationTask: Task<Void, Never>?
private var tipPopover: TipNSPopover?
override func viewDidAppear() {
super.viewDidAppear()
tipObservationTask = tipObservationTask ?? Task { @MainActor in
for await shouldDisplay in catTracksFeatureTip.shouldDisplayUpdates {
if shouldDisplay {
tipPopover = TipNSPopover(catTracksFeatureTip)
tipPopover?.show(relativeTo: catTracksFeatureButton.bounds, of: catTracksFeatureButton, preferredEdge: .minY)
}
else {
tipPopover?.close()
tipPopover = nil
}
}
}
}
override func viewDidDisappear() {
super.viewDidDisappear()
tipObservationTask?.cancel()
tipObservationTask = nil
}
}