29 lines
553 B
Swift
29 lines
553 B
Swift
import Foundation
|
|
|
|
extension NSSize {
|
|
|
|
func scaledDown(to desiredWidth: CGFloat) -> NSSize {
|
|
if width == desiredWidth {
|
|
return self
|
|
}
|
|
|
|
if width < desiredWidth {
|
|
// Don't scale larger
|
|
return self
|
|
}
|
|
|
|
let height = (height * desiredWidth / width).rounded(.down)
|
|
return NSSize(width: desiredWidth, height: height)
|
|
}
|
|
}
|
|
|
|
extension NSSize {
|
|
|
|
var ratio: CGFloat {
|
|
guard height != 0 else {
|
|
return 0
|
|
}
|
|
return width / height
|
|
}
|
|
}
|