// What the phone's screens ask for by UIKit's name, answered by AppKit.//// Since H2 `CottonUI` is ONE module for TWO destinations, and `swift build` on// a Mac compiles every screen for macOS whether or not the Mac app shows it// yet. The screens were written for the phone and say `UIImage`, `UIPasteboard`,// `UIScreen.main.scale`, `.keyboardType(.numberPad)` — names UIKit owns and// AppKit does not have. There are two ways to make that compile: a `#if os(iOS)`// at every one of ~80 sites, or ONE file that gives each name its AppKit// meaning. This is the one file, and the rule for what belongs in it://// A name goes here only when AppKit has the SAME THING under another name —// an image, a pasteboard, a screen's scale, a font's line height, a URL the// system opens. A phone-only BEHAVIOUR (haptics, the camera picker, the// edge-swipe pop gesture, the share sheet) is NOT// shimmed: it sits under `#if os(iOS)` at its own site, with the Mac's answer// — usually "nothing" — written there, where a reader can see it.//// Every definition is `#if os(macOS)`: on the phone the real UIKit type is in// scope and this file compiles to nothing. Nothing here is imported by name;// the phone's code does not know it exists, which is the point — the screens// stay verbatim. When a Mac screen wants an AppKit-native answer (H3+), it// writes AppKit directly rather than reaching for one of these.#if os(macOS)importAppKitimportSwiftUI// MARK: - Imagespublictypealias UIImage = NSImagepublictypealias UIColor = NSColorpublictypealias UIFont = NSFontextension NSImage {/// `UIImage(cgImage:)` — one pixel per point, as UIKit's does.public convenience init(cgImage: CGImage) {self.init(cgImage: cgImage, size: NSSize(width: cgImage.width, height: cgImage.height)) }/// `UIImage(cgImage:scale:orientation:)`. AppKit images carry no/// orientation — the raster is already the right way up — so the argument/// is accepted and ignored, and the scale divides the pixel size to points.public convenience init(cgImage: CGImage, scale: CGFloat, orientation: Orientation) {let size = NSSize(width: CGFloat(cgImage.width) / scale, height: CGFloat(cgImage.height) / scale)self.init(cgImage: cgImage, size: size) }/// `UIImage.Orientation`, with the one case a Mac image can have.publicenumOrientation: Sendable { case up }publicvar imageOrientation: Orientation { .up }/// `UIImage.scale`: points to pixels. An `NSImage` has no single scale — a/// representation does — so this is the identity, and callers that want/// pixels multiply by the screen's factor as they do on the phone.publicvar scale: CGFloat { 1 }/// `UIImage.cgImage`.publicvar cgImage: CGImage? { cgImage(forProposedRect: nil, context: nil, hints: nil) }/// `UIImage.jpegData(compressionQuality:)`.publicfunc jpegData(compressionQuality: CGFloat) -> Data? {guardlet cg = cgImage else { returnnil }return NSBitmapImageRep(cgImage: cg) .representation(using: .jpeg, properties: [.compressionFactor: compressionQuality]) }/// `UIImage(systemName:)` — an SF Symbol by name, `nil` when the system/// has no such symbol (the same answer UIKit gives).public convenience init?(systemName: String) {self.init(systemSymbolName: systemName, accessibilityDescription: nil) }/// `UIImage.pngData()`.publicfunc pngData() -> Data? {guardlet cg = cgImage else { returnnil }return NSBitmapImageRep(cgImage: cg).representation(using: .png, properties: [:]) }}extensionImage {/// `Image(uiImage:)`.publicinit(uiImage: NSImage) { self.init(nsImage: uiImage) }}extension NSFont {/// `UIFont.lineHeight`: the face's natural line box.publicvar lineHeight: CGFloat { ascender - descender + leading }}// MARK: - The pasteboard, the screen, the device, the system/// `UIPasteboard.general.string`.publicstruct UIPasteboard: Sendable {publicstaticvar general: UIPasteboard { UIPasteboard() }publicvar string: String? { get { NSPasteboard.general.string(forType: .string) } nonmutating set {let board = NSPasteboard.general board.clearContents()iflet newValue { board.setString(newValue, forType: .string) } } }}/// `UIScreen.main` — the scale and bounds the phone's screens size pixels by.publicstruct UIScreen: Sendable {publicstaticvar main: UIScreen { UIScreen() }publicvar scale: CGFloat { NSScreen.main?.backingScaleFactor ?? 2 }publicvar bounds: CGRect { NSScreen.main?.frame ?? CGRect(x: 0, y: 0, width: 1280, height: 800) }}/// `UIDevice.current.name`.publicstruct UIDevice: Sendable {publicstaticvar current: UIDevice { UIDevice() }publicvar name: String { Host.current().localizedName ?? "Mac" }}/// `UIApplication.shared.open(_:)` and the Settings URL.publicenum UIApplication {/// The phone's is `app-settings:`; the Mac's is System Settings. Neither/// opens the app's OWN pane on a Mac, and the two callers that use it are/// permission refusals, which macOS keys to the code signature anyway.publicstaticlet openSettingsURLString = "x-apple.systempreferences:"publicstructShared: Sendable {publicfuncopen(_ url: URL) { NSWorkspace.shared.open(url) } }publicstaticvar shared: Shared { Shared() }}// MARK: - The modifiers a Mac text field has no use for/// `TextInputAutocapitalization` — a software keyboard's setting. A Mac has/// a hardware one, so the modifier is accepted and does nothing.publicenumTextInputAutocapitalization: Sendable { case never, words, sentences, characters }/// `UIKeyboardType` — which software keyboard to raise. Same answer.publicenum UIKeyboardType: Sendable {case `default`, asciiCapable, numbersAndPunctuation, URL, numberPad, phonePadcase namePhonePad, emailAddress, decimalPad, twitter, webSearch, asciiCapableNumberPad}/// `PageTabViewStyle.IndexDisplayMode`.publicenumPageIndexDisplayMode: Sendable { case automatic, always, never }extensionView {publicfunc textInputAutocapitalization(_ autocapitalization: TextInputAutocapitalization?) -> someView {self }publicfunc keyboardType(_ type: UIKeyboardType) -> someView { self }/// `fullScreenCover` — a phone covers the screen; a Mac window sheets.publicfunc fullScreenCover<Content: View>( isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content ) -> someView { sheet(isPresented: isPresented, onDismiss: onDismiss, content: content) }publicfunc fullScreenCover<Item: Identifiable, Content: View>( item: Binding<Item?>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping (Item) -> Content ) -> someView { sheet(item: item, onDismiss: onDismiss, content: content) }}extensionPickerStylewhereSelf == MenuPickerStyle {/// `.wheel` — the Mac's nearest is a menu.publicstaticvar wheel: MenuPickerStyle { .menu }}extensionDatePickerStylewhereSelf == GraphicalDatePickerStyle {/// `.wheel` — the Mac's nearest is the graphical calendar.publicstaticvar wheel: GraphicalDatePickerStyle { .graphical }}extensionTabViewStylewhereSelf == DefaultTabViewStyle {/// `.page(indexDisplayMode:)` — no paging tabs on a Mac; the default.publicstaticfunc page(indexDisplayMode: PageIndexDisplayMode) -> DefaultTabViewStyle { .automatic }}#endif