Skip to content

mattmassicotte/nsui

Build Status Platforms Discord

NSUI

AppKit and UIKit without conditional compilation

There are small differences between otherwise-source-compatible classes in AppKit and UIKit and it drives everyone bananas. I was inspired by Nick Lockwood to make this into a package.

When in doubt, UIKit wins. This keeps things familiar to the most people.

Integration

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/mattmassicotte/nsui")
]

Usage

// not only does this import the NSUI package, but it also will correctly make either AppKit or UIKit accessible depending on the build target platform.
import NSUI

// Make use of the cross-platform wrappers
let textView = NSUITextView()

There are cases where it is necessary to provide cross-platform wrappers around functions, accessors, or initializers. These account for type differences, including cases where value differ only in optionality.

// SwiftUI
Color(nsuiColor: NSUIColor)
Image(nsuiImage: NSUIImage)
NSUIControlActiveState

// AppKit/UIKit
NSUIFont.init(nsuiDescriptor:, size:)
NSUIFontDescriptor.nsuiWithSymbolicTraits(_:)
NSUITextView.nsuiLayoutManager
NSUITextView.nsuiSelectedRange

NSUI also includes both NSUIViewRepresentable and NSUIViewControllerRepresentable to wrap your custom view subclasses.

Types

All Types and their UIKit & AppKit correspondance are defined in the Aliases.swift file. That file only defines the NSUI type corresponding to the proper platform.

Here's the current list of types supported in NSUI:

NSUIActivityIndicatorView
NSUIApplication
NSUIApplicationDelegate
NSUIApplicationDelegateAdaptor
NSUIBezierPath

// Collection Views
NSUICollectionDataSource
NSUICollectionView
NSUICollectionViewItem
NSUICollectionViewDelegate
NSUICollectionViewLayout
NSUICollectionViewFlowLayout
NSUICollectionViewLayoutAttributes
NSUICollectionViewDelegateFlowLayout

NSUIButton
NSUIColor
NSUIEdgeInsets
NSUIFont
NSUIFontDescriptor
NSUIHostingController
NSUIImage
NSUILongPressGestureRecognizer
NSUINib
NSUIPasteboard
NSUIResponder
NSUIStackView
NSUIStoryboard
NSUITapGestureRecognizer
NSUITextField
NSUITextFieldDelegate
NSUITextView
NSUIViewController
NSUIWorkspace

If you are looking for event key mappings, particularly UIKeyboardHIDUsage and UIKeyModifierFlags, check out KeyCodes.

Conventions

NSUI is not a multi-platform framework to replace both UIKit and AppKit. As stated above, NSUI takes the stance that the API from UIKit is the first-class citizen. However, in some cases, both AppKit and UIKit are very close to each other.

Let's take, for example, the NSProgressIndicator and UIActivityIndicator classes. To start the spinning indicator view, you need to issue startAnimating() for UIKit and startAnimation(sender:) for AppKit.

In order to bridge that slight difference, a bridging function is created in the Views.swift file which looks like this:

extension NSUIActivityIndicator {
    public func startAnimating() {
        self.startAnimation(nil)
    }
    
    public func stopAnimating() {
        self.stopAnimation(nil)
    }
}

This extension is wrapped within a: #if canImport(AppKit) and provides the UIKit functional equivalent to the AppKit class.

Enhancing NSUI

If you need to enhance NSUI, follow the following guidelines:

  1. Check if the type you need is defined in the Aliases.swift file.
  2. If your type needs to bridging functions, define them in a dedicated file.
  3. Prioritize UIKit implementations, unless type-mismatches prevent it.

Alternatives

Contributing and Collaboration

I would love to hear from you! Issues or pull requests work great. A Discord server is also available for live help, but I have a strong bias towards answering in the form of documentation.

I prefer collaboration, and would love to find ways to work together if you have a similar project.

I prefer indentation with tabs for improved accessibility. But, I'd rather you use the system you want and make a PR than hesitate because of whitespace.

By participating in this project you agree to abide by the Contributor Code of Conduct.