Files
swift-mirror/stdlib/public/SDK/AppKit/AppKit.swift
Jordan Rose bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00

91 lines
3.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
@_exported import AppKit
extension NSCursor : CustomPlaygroundQuickLookable {
public var customPlaygroundQuickLook: PlaygroundQuickLook {
return .image(image)
}
}
internal struct _NSViewQuickLookState {
static var views = Set<NSView>()
}
extension NSView : CustomPlaygroundQuickLookable {
public var customPlaygroundQuickLook: PlaygroundQuickLook {
// if you set NSView.needsDisplay, you can get yourself in a recursive scenario where the same view
// could need to draw itself in order to get a QLObject for itself, which in turn if your code was
// instrumented to log on-draw, would cause yourself to get back here and so on and so forth
// until you run out of stack and crash
// This code checks that we aren't trying to log the same view recursively - and if so just returns
// an empty view, which is probably a safer option than crashing
// FIXME: is there a way to say "cacheDisplayInRect butDoNotRedrawEvenIfISaidSo"?
if _NSViewQuickLookState.views.contains(self) {
return .view(NSImage())
} else {
_NSViewQuickLookState.views.insert(self)
let result: PlaygroundQuickLook
if let b = bitmapImageRepForCachingDisplay(in: bounds) {
cacheDisplay(in: bounds, to: b)
result = .view(b)
} else {
result = .view(NSImage())
}
_NSViewQuickLookState.views.remove(self)
return result
}
}
}
// Overlays for variadics.
public extension NSGradient {
convenience init?(colorsAndLocations objects: (NSColor, CGFloat)...) {
self.init(
colors: objects.map { $0.0 },
atLocations: objects.map { $0.1 },
colorSpace: NSColorSpace.genericRGB())
}
}
// Fix the ARGV type of NSApplicationMain, which nonsensically takes
// argv as a const char**.
@_silgen_name("NSApplicationMain")
public func NSApplicationMain(
_ argc: Int32, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
) -> Int32
extension NSColor : _ColorLiteralConvertible {
public required convenience init(colorLiteralRed red: Float, green: Float,
blue: Float, alpha: Float) {
self.init(srgbRed: CGFloat(red), green: CGFloat(green),
blue: CGFloat(blue), alpha: CGFloat(alpha))
}
}
public typealias _ColorLiteralType = NSColor
extension NSImage : _ImageLiteralConvertible {
private convenience init!(failableImageLiteral name: String) {
self.init(named: name)
}
public required convenience init(imageLiteral name: String) {
self.init(failableImageLiteral: name)
}
}
public typealias _ImageLiteralType = NSImage