-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoundation+Additions.swift
159 lines (108 loc) · 4.1 KB
/
Foundation+Additions.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
148
149
150
151
152
153
154
155
156
157
158
159
//
// Foundation+Additions.swift
// weborganizer
//
// Created by Bruno Vandekerkhove on 26/04/16.
// Copyright © 2016 Koray Birand. All rights reserved.
//
import Foundation
// MARK: NSRect Extension -
extension NSRect {
func bottomLeft() -> NSPoint {
return NSMakePoint(self.origin.x, self.origin.y)
}
func bottomRight() -> NSPoint {
return NSMakePoint(self.origin.x + self.size.width, self.origin.y)
}
func topRight() -> NSPoint {
return NSMakePoint(self.origin.x + self.size.width, self.origin.y + self.size.height)
}
func topLeft() -> NSPoint {
return NSMakePoint(self.origin.x, self.origin.y + self.size.height)
}
static func handlerRectForPoint(_ point: NSPoint) -> NSRect {
let size = CGFloat(8.5)
return NSMakeRect(point.x - size/2, point.y - size/2, size, size)
}
mutating func constraintToSize(_ size: NSSize) {
var rect = NSZeroRect
rect.size = size
self.constraintToRect(rect)
}
mutating func constraintToRect(_ rect: NSRect) {
var topright = NSMakePoint(self.origin.x + self.size.width, self.origin.y + self.size.height)
topright.constraintToRect(rect)
self.origin.constraintToRect(rect)
self.size = NSMakeSize(topright.x - self.origin.x, topright.y - self.origin.y)
}
mutating func moveBy(_ dx: CGFloat, dy: CGFloat) {
self.origin.x += dx
self.origin.y += dy
}
mutating func moveBy(_ dx: CGFloat, dy: CGFloat, inFrame frame: NSRect) {
var newOrigin = self.origin
newOrigin.x += dx
newOrigin.y += dy
newOrigin.constraintToRect(frame)
if newOrigin.x + self.size.width > frame.origin.x + frame.size.width {
newOrigin.x = frame.origin.x + frame.size.width - self.size.width
}
if newOrigin.y + self.size.height > frame.origin.y + frame.size.height {
newOrigin.y = frame.origin.y + frame.size.height - self.size.height
}
self.origin = newOrigin
}
func ratioOfFrame(_ frame: NSRect) -> NSRect {
var innerRect = self
innerRect.constraintToRect(frame)
innerRect.origin.x -= frame.origin.x
innerRect.origin.y -= frame.origin.y
return NSMakeRect(innerRect.origin.x / frame.size.width,
innerRect.origin.y / frame.size.height,
innerRect.size.width / frame.size.width,
innerRect.size.height / frame.size.height)
}
}
// MARK: NSPoint Extension -
extension NSPoint {
mutating func constraintToSize(_ size: NSSize) {
var rect = NSZeroRect
rect.size = size
self.constraintToRect(rect)
}
mutating func constraintToRect(_ rect: NSRect) {
if self.x > rect.origin.x + rect.size.width {
self.x = rect.origin.x + rect.size.width
}
if self.y > rect.origin.y + rect.size.height {
self.y = rect.origin.y + rect.size.height
}
if self.x < rect.origin.x {
self.x = rect.origin.x
}
if self.y < rect.origin.y {
self.y = rect.origin.y
}
}
func bufferRect(_ size: Float?=nil) -> NSRect {
var bufferSize = CGFloat(8.5)
if size != nil {
bufferSize = CGFloat(size!)
}
return NSMakeRect(self.x - bufferSize/2, self.y - bufferSize/2, bufferSize, bufferSize)
}
}
// MARK: Float Extension -
extension Float {
mutating func roundToPlaces(_ places:Int) -> Float {
let divisor = pow(10.0, Double(places))
return Float((Double(self) * divisor).rounded() / divisor)
}
}
// MARK: Double Extension -
extension Double {
mutating func roundToPlaces(_ places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}