What’s New in AppKit for macOS

Written by Kuba Suder

Description: Learn about the latest APIs in AppKit and associated frameworks. Get an overview of the enhancements coming in macOS Catalina to help you save time, take advantage of the latest hardware, and add polish to your application.

New frameworks

AppKit is no longer the only framework to build Mac apps with - you also have an option now to use UIKit via Catalyst and the new SwiftUI framework (although you will probably still need to use at least a little bit of AppKit in both cases).

Colors

  • new system colors: NSColor.systemTeal and systemIndigo (dynamic colors, specific color values depend on light/dark appearance)
  • NSColor uses tagged pointers - this means that color data is now stored in the pointer itself and allocating NSColor objects becomes very cheap
  • NSColorSampler - a magnifier tool for picking a color from somewhere on the screen
  • new NSColor initializer that lets you return different colors based on appearance:
let color = NSColor(name: "userWidgetColor") { appearance in
  switch appearance.bestMatch(from: [.aqua, .darkAqua]) {
  case .darkAqua:
    return darkUserWidgetColor
  case .aqua, .default:
    return lightUserWidgetColor
  }
}

Screens

Extended dynamic range:

Computer screens are very bright these days and most of the time aren't used at full brightness. This means that at lower brightness levels we can use the monitor's capability to show brighter pixels than what 100% white means at that brightness level, and provide an extended dynamic range, i.e. color values of more than 1.0.

This feature has been available in macOS since 10.11:

New APIs in 10.15:

Finding the current screen in Metal:

Previously, in order to find the MTLDevice for the current screen in Metal you had to do something like this:

let preferredDevice = CGDirectDisplayCopyCurrentMetalDevice(
    self.window?.screen?.deviceDescription["NSScreenNumber"]
)

In 10.15, there's a new property CAMetalLayer.preferredDevice that returns the MTLDevice for the current screen

Text & Fonts

NSTextView.usesAdaptiveColorMappingForDarkAppearance

  • automatically updates colors for light/dark appearance
  • enable this if it's more important for rich text to be readable regardless of appearance than to show the document in an original unchanged form

Almost all of the NSText* related classes support secure coding now.

Spell checking:

  • NSSpellChecker - old API for managing spell checking (since OS X 10.0!)
  • NSTextCheckingController - new API built as a successor of NSSpellChecker
    • used in UIKit, WebKit and AppKit
    • you can add support for it to any view by implementing the NSTextCheckingClient protocol
    • also does grammar checks, data detection, autocorrection

Font descriptors:

NSFontDescriptor.withDesign(.monospaced / .rounded / .serif)

  • lets you choose a different variant of the same font, if available

Scaling fonts between macOS & iOS:

If your app opens rich text documents created on another platform, you will notice that fonts at the same point size will look different, because Mac and iOS devices use very different screen densities. There are now new NSAttributedString APIs that let you scale fonts in documents coming from the other platform to a more appropriate size:

Text hyphenation:

Previously, you could configure hyphenation for each paragraph using NSParagraphStyle. Now, this can be enabled for the whole container using NSLayoutManager.usesDefaultHyphenation.

Toolbars

NSToolbarItem.isBordered - an easier way to make standard toolbar buttons:

  • previously, you had to manually create an NSButton, configure it appropriately and add it as a custom view to the toolbar item
  • now, you can just create an NSToolbarItem with an image and set isBordered = true
  • needs to be done in code though, no setting on the storyboard yet
  • this also allows you to make use of NSToolbarItem's built-in functionality like automatic enabling/disabling

NSToolbarItem.title:

  • allows you to make toolbar items that are buttons with a text label instead of an icon
  • note: this is different than the item label, which appears below the button

NSToolbarItemGroup:

  • new convenience initializers that create a group with given item labels/icons in one line
  • can display items as a segmented control which collapses into a popup or pulldown menu when there isn't enough space (use the new initializers for this)

NSMenuToolbarItem:

  • a button toolbar item that shows a pulldown menu
  • the menu can be any NSMenu, with separators, nested menus etc.

Touch Bar

NSTouchBar.isAutomaticCustomizeTouchBarMenuItemEnabled

  • lets you enable/disable the "Customize Touch Bar…" entry in the Edit menu
  • NSApplication already has such property, but this is useful if you want to avoid referencing NSApplication in the Touch Bar related code, or if you don't have access to it (e.g. Catalyst apps don't)

NSStepperTouchBarItem:

  • a new item type for selecing an item from a list, with left/right arrows

NSSliderTouchBarItem:

Other controls

NSSwitch:

  • a new NSControl that looks like UISwitch on iOS
  • not a replacement for checkbox
  • avoid using it for small things and in large numbers, just one for some general mode switch, a "master toggle", like the Time Machine on/off switch

NSCollectionView:

  • added compositional layout and diffable data source, same as on iOS

Storyboards

Storyboards now let you use custom VC initializers for injecting dependencies:

@IBSegueAction func showFoo(_ coder: NSCoder) -> NSViewController {
    return MyViewController(params...)
}

This lets you use initializers in view controller classes that require passing any data the view controller needs, while still using storyboard segues to connect view controllers on the storyboard.

AutoLayout

Controls like text labels, buttons with titles etc. calculate their intrinsic content size that lets the AutoLayout system scale the whole window layout depending on the text contents of those controls. However, in some scenarios (e.g. inside an NSGridView) you want all controls to have their size set externally, and the control's intrinsic size is ignored. The calculations to determine the intrinsic size are still performed though in such case.

Now you can manually disable them (as an optimization) by setting these to false if you know the result will not be used in the layout calculations:

NSResponder

There was a possible source of bugs before if you captured an instance of a UI-related class in a block in such a way that it could be later deallocated on a background thread:

let label = NSTextField(labelWithString: "...")

dispatch_async(dispatch_get_global_queue(0, 0)) {
    // ...

    dispatch_async(dispatch_get_main_queue()) {
        label.value = "done"
    }
}

If the outer block is released after the inner block (which you have no control over), then at that point the label object is deallocated on the background thread, and this could cause various hard to debug issues. This is now solved in 10.15 - the SDK guarantees that UI objects will be deallocated on the main thread in such scenario.

Privacy & security

  • Recording the screen will now require asking the user for permission
    • this doesn't apply to the new NSColorSampler control mentioned earlier, which runs out of process
    • Open and save panels are also now always out-of-process, even for non-sandboxed apps
    • you may run into problems if you were somehow subclassing or customizing them in a non-standard way

NSWorkspace

Added new asynchronous versions of existing methods for opening one or more URLs or applications. You can configure some aspects of opening using NSWorkspace.OpenConfiguration, e.g.:

  • if the user can control the process of opening a URL/app
  • if the opened app or document is added to the "Recents" menu
  • if the launched app is hidden on launch

iPad & Sidecar

With the Sidecar feature, an iPad can function as an additional screen for the Mac, and also as a drawing tablet.

Tablet drawing events are sent as normal mouse events, with .tabletPoint subtype. They also provide pressure data (although unlike on iOS, you can't register to get retroactive updates for previous pressure).

Switching mode on the Apple Pencil by double-tapping can be handled by another event with event type .changeMode, also an NSResponder in the responder chain can handle that event through a new changeMode(withEvent:) method.

You can also listen to that event outside of the responder chain by using the "local event monitor" API:

let monitorId = NSEvent.addLocalMonitorForEvents(matching: .changeMode) { (event) -> NSEvent? in
    switchTool()
    return event
}

Foundation additions

View geometry:

New data types for specifying view geometry:

These use the terms leading & trailing on the horizontal axis instead of e.g. "minX" or "maxX", so they work better with right-to-left languages.

Formatters:

NSRelativeDateFormatter - allows you to format dates as e.g. "1 month ago" or "last week"

NSListFormatter - formats lists of things, adding commas properly (uses nested formatters from .itemFormatter to format specific items)

Changes in extensions

  • non-UI file provider action extension
  • Network Extensions, DriverKit and Endpoint Security replace old kernel extensions

This note was originally published at mackuba.eu.

Missing anything? Corrections? Contributions are welcome 😃

Related

Written by

Kuba Suder

Kuba Suder

Independent Mac & iOS developer. Sometimes freelancing, mostly working on my own stuff these days.