-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Koray Birand
authored and
Koray Birand
committed
Sep 16, 2018
0 parents
commit 1586582
Showing
35 changed files
with
2,655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// IntegerFormatter.swift | ||
// weborganizer | ||
// | ||
// Created by Bruno Vandekerkhove on 26/04/16. | ||
// Copyright © 2016 Koray Birand. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool { | ||
switch (lhs, rhs) { | ||
case let (l?, r?): | ||
return l < r | ||
case (nil, _?): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool { | ||
switch (lhs, rhs) { | ||
case let (l?, r?): | ||
return l > r | ||
default: | ||
return rhs < lhs | ||
} | ||
} | ||
|
||
|
||
// MARK: Integer Formatter - | ||
|
||
class IntegerFormatter: NumberFormatter { | ||
|
||
override func isPartialStringValid(_ partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>?, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool { | ||
|
||
if partialString.count > 0 { | ||
|
||
if !(partialString.isNumeric()) | ||
|| (partialString as NSString).longLongValue > (self.maximum?.int64Value)! | ||
|| (partialString as NSString).longLongValue < (self.minimum?.int64Value)! { | ||
return false | ||
} | ||
|
||
} | ||
|
||
return true | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// NSBezierPath+Additions.swift | ||
// weborganizer | ||
// | ||
// Created by Bruno Vandekerkhove on 26/04/16. | ||
// Copyright © 2016 Koray Birand. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
// MARK: Bezier Path Extension - | ||
// From https://gist.github.com/juliensagot/9749c3a1df28c38fb9f9 | ||
|
||
extension NSBezierPath { | ||
|
||
var CGPath: CGPath { | ||
get { | ||
return self.transformToCGPath() | ||
} | ||
} | ||
|
||
/// Transform this NSBezierPath into a CGPathRef | ||
fileprivate func transformToCGPath() -> CGPath { | ||
|
||
// Create path | ||
let path = CGMutablePath() | ||
let points = UnsafeMutablePointer<NSPoint>.allocate(capacity: 3) | ||
let numElements = self.elementCount | ||
|
||
if numElements > 0 { | ||
|
||
var didClosePath = true | ||
|
||
for index in 0..<numElements { | ||
|
||
let pathType = self.element(at: index, associatedPoints: points) | ||
|
||
switch pathType { | ||
|
||
case .moveToBezierPathElement: | ||
path.move(to: CGPoint(x: points[0].x, y: points[0].y)) | ||
case .lineToBezierPathElement: | ||
path.addLine(to: CGPoint(x: points[0].x, y: points[0].y)) | ||
didClosePath = false | ||
case .curveToBezierPathElement: | ||
path.addCurve(to: CGPoint(x: points[2].x, y: points[2].y), control1: CGPoint(x: points[0].x, y: points[0].y), control2: CGPoint(x: points[1].x, y: points[1].y)) | ||
didClosePath = false | ||
case .closePathBezierPathElement: | ||
path.closeSubpath() | ||
didClosePath = true | ||
} | ||
} | ||
|
||
if !didClosePath { | ||
path.closeSubpath() | ||
} | ||
|
||
} | ||
|
||
points.deallocate() | ||
return path | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// NSImageView+Additions.swift | ||
// weborganizer | ||
// | ||
// Created by Bruno Vandekerkhove on 26/04/16. | ||
// Copyright © 2016 Koray Birand. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
// MARK: NSImageView Extension - | ||
// From http://stackoverflow.com/questions/7929761/get-the-actual-display-image-size-on-nsimageview | ||
|
||
extension NSImageView { | ||
|
||
func calculateImageSize() -> NSSize { | ||
|
||
if self.image != nil { | ||
|
||
let imageSize = self.image!.size | ||
let frameSize = self.frame.size | ||
|
||
switch self.imageScaling { | ||
case .scaleProportionallyUpOrDown: | ||
|
||
if imageSize.width > frameSize.width | ||
|| imageSize.height > frameSize.height { // Width or height too big for frame (has priority) | ||
|
||
let dx = frameSize.width / imageSize.width | ||
let dy = frameSize.height / imageSize.height | ||
let minDelta = min(dx, dy) | ||
|
||
return NSMakeSize(imageSize.width * minDelta, imageSize.height * minDelta) | ||
|
||
} | ||
else { // Width and height too small | ||
|
||
let dx = frameSize.width / imageSize.width | ||
let dy = frameSize.height / imageSize.height | ||
let minDelta = min(dx, dy) | ||
|
||
return NSMakeSize(imageSize.width * minDelta, imageSize.height * minDelta) | ||
|
||
} | ||
|
||
case .scaleAxesIndependently: | ||
return frameSize // To be implemented | ||
case .scaleProportionallyDown: | ||
return frameSize // To be implemented | ||
case .scaleNone: | ||
return frameSize | ||
} | ||
|
||
} | ||
|
||
return NSZeroSize | ||
|
||
} | ||
|
||
func calculateImageFrame() -> NSRect { | ||
|
||
if self.image != nil { | ||
|
||
let imageSize = self.calculateImageSize() | ||
var innerRect = NSZeroRect | ||
innerRect.size = imageSize | ||
|
||
return NSRectCenteredInsideRect(innerRect, outerRect:self.frame) | ||
|
||
} | ||
|
||
return NSZeroRect | ||
|
||
} | ||
|
||
fileprivate func NSRectCenteredInsideRect(_ inner: NSRect, outerRect: NSRect) -> NSRect { | ||
|
||
return NSMakeRect((outerRect.size.width - inner.size.width) / 2.0, | ||
(outerRect.size.height - inner.size.height) / 2.0, | ||
inner.size.width, | ||
inner.size.height); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// String+Additions.swift | ||
// weborganizer | ||
// | ||
// Created by Bruno Vandekerkhove on 26/04/16. | ||
// Copyright © 2016 Koray Birand. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
// MARK: NSString Extension - | ||
|
||
extension NSString { | ||
|
||
func isNumeric() -> Bool { | ||
|
||
return removeNumeric().length == 0 | ||
|
||
} | ||
|
||
fileprivate func removeNumeric() -> NSString { | ||
|
||
let charactersToRemove = CharacterSet.decimalDigits | ||
return self.components(separatedBy: charactersToRemove).joined(separator: "") as NSString | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.