// 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) import AppKit import SwiftUI // MARK: - Images public typealias UIImage = NSImage public typealias UIColor = NSColor public typealias UIFont = NSFont extension 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. public enum Orientation: Sendable { case up } public var 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. public var scale: CGFloat { 1 } /// `UIImage.cgImage`. public var cgImage: CGImage? { cgImage(forProposedRect: nil, context: nil, hints: nil) } /// `UIImage.jpegData(compressionQuality:)`. public func jpegData(compressionQuality: CGFloat) -> Data? { guard let cg = cgImage else { return nil } 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()`. public func pngData() -> Data? { guard let cg = cgImage else { return nil } return NSBitmapImageRep(cgImage: cg).representation(using: .png, properties: [:]) } } extension Image { /// `Image(uiImage:)`. public init(uiImage: NSImage) { self.init(nsImage: uiImage) } } extension NSFont { /// `UIFont.lineHeight`: the face's natural line box. public var lineHeight: CGFloat { ascender - descender + leading } } // MARK: - The pasteboard, the screen, the device, the system /// `UIPasteboard.general.string`. public struct UIPasteboard: Sendable { public static var general: UIPasteboard { UIPasteboard() } public var string: String? { get { NSPasteboard.general.string(forType: .string) } nonmutating set { let board = NSPasteboard.general board.clearContents() if let newValue { board.setString(newValue, forType: .string) } } } } /// `UIScreen.main` — the scale and bounds the phone's screens size pixels by. public struct UIScreen: Sendable { public static var main: UIScreen { UIScreen() } public var scale: CGFloat { NSScreen.main?.backingScaleFactor ?? 2 } public var bounds: CGRect { NSScreen.main?.frame ?? CGRect(x: 0, y: 0, width: 1280, height: 800) } } /// `UIDevice.current.name`. public struct UIDevice: Sendable { public static var current: UIDevice { UIDevice() } public var name: String { Host.current().localizedName ?? "Mac" } } /// `UIApplication.shared.open(_:)` and the Settings URL. public enum 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. public static let openSettingsURLString = "x-apple.systempreferences:" public struct Shared: Sendable { public func open(_ url: URL) { NSWorkspace.shared.open(url) } } public static var 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. public enum TextInputAutocapitalization: Sendable { case never, words, sentences, characters } /// `UIKeyboardType` — which software keyboard to raise. Same answer. public enum UIKeyboardType: Sendable { case `default`, asciiCapable, numbersAndPunctuation, URL, numberPad, phonePad case namePhonePad, emailAddress, decimalPad, twitter, webSearch, asciiCapableNumberPad } /// `PageTabViewStyle.IndexDisplayMode`. public enum PageIndexDisplayMode: Sendable { case automatic, always, never } extension View { public func textInputAutocapitalization(_ autocapitalization: TextInputAutocapitalization?) -> some View { self } public func keyboardType(_ type: UIKeyboardType) -> some View { self } /// `fullScreenCover` — a phone covers the screen; a Mac window sheets. public func fullScreenCover( isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content ) -> some View { sheet(isPresented: isPresented, onDismiss: onDismiss, content: content) } public func fullScreenCover( item: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping (Item) -> Content ) -> some View { sheet(item: item, onDismiss: onDismiss, content: content) } } extension PickerStyle where Self == MenuPickerStyle { /// `.wheel` — the Mac's nearest is a menu. public static var wheel: MenuPickerStyle { .menu } } extension DatePickerStyle where Self == GraphicalDatePickerStyle { /// `.wheel` — the Mac's nearest is the graphical calendar. public static var wheel: GraphicalDatePickerStyle { .graphical } } extension TabViewStyle where Self == DefaultTabViewStyle { /// `.page(indexDisplayMode:)` — no paging tabs on a Mac; the default. public static func page(indexDisplayMode: PageIndexDisplayMode) -> DefaultTabViewStyle { .automatic } } #endif