-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAppKit.swift
147 lines (109 loc) · 4.36 KB
/
AppKit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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
struct _NSCursorMirror : _MirrorType {
var _value: NSCursor
init(_ v: NSCursor) { _value = v }
var value: Any { return _value }
var valueType: Any.Type { return (_value as Any).dynamicType }
var objectIdentifier: ObjectIdentifier? { return .None }
var count: Int { return 0 }
subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String { return "" }
var quickLookObject: PlaygroundQuickLook? {
return .Some(.Image(_value.image))
}
var disposition : _MirrorDisposition { return .Aggregate }
}
extension NSCursor : _Reflectable {
public func _getMirror() -> _MirrorType {
return _NSCursorMirror(self)
}
}
struct _NSViewMirror : _MirrorType {
static var _views = NSMutableSet()
var _v : NSView
init(_ v : NSView) { _v = v }
var value: Any { get { return _v } }
var valueType: Any.Type { get { return (_v as Any).dynamicType } }
var objectIdentifier: ObjectIdentifier? { get { return .None } }
var count: Int { get { return 0 } }
subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String { get { return "" } }
var quickLookObject: PlaygroundQuickLook? { get {
// adapted from the Xcode QuickLooks implementation
var result: PlaygroundQuickLook? = nil
// 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
// nil, which is probably a safer option than crashing
// FIXME: is there a way to say "cacheDisplayInRect butDoNotRedrawEvenIfISaidSo"?
switch _NSViewMirror._views.member(_v) {
case nil:
_NSViewMirror._views.addObject(_v)
let bounds = _v.bounds
if let b = _v.bitmapImageRepForCachingDisplayInRect(bounds) {
_v.cacheDisplayInRect(bounds, toBitmapImageRep: b)
result = .Some(.View(b))
}
_NSViewMirror._views.removeObject(_v)
default: ()
}
return result
} }
var disposition : _MirrorDisposition { get { return .Aggregate } }
}
extension NSView : _Reflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _NSViewMirror(self)
}
}
// 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.genericRGBColorSpace())
}
}
// 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