Skip to content

Commit

Permalink
Upgrade to Swift 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 11, 2019
1 parent 3ffbbaf commit 948d255
Show file tree
Hide file tree
Showing 26 changed files with 507 additions and 522 deletions.
2 changes: 2 additions & 0 deletions Gifski.xcodeproj/project.pbxproj
Expand Up @@ -483,6 +483,7 @@
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_COMPILATION_MODE = singlefile;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
VALID_ARCHS = x86_64;
};
name = Debug;
Expand Down Expand Up @@ -534,6 +535,7 @@
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 5.0;
VALID_ARCHS = x86_64;
};
name = Release;
Expand Down
29 changes: 13 additions & 16 deletions Gifski.xcodeproj/xcshareddata/xcschemes/Gifski.xcscheme
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1100"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,8 +26,17 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E3AE62821E5CD2F300035A2F"
BuildableName = "Gifski.app"
BlueprintName = "Gifski"
ReferencedContainer = "container:Gifski.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -40,17 +49,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E3AE62821E5CD2F300035A2F"
BuildableName = "Gifski.app"
BlueprintName = "Gifski"
ReferencedContainer = "container:Gifski.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -63,6 +61,7 @@
debugDocumentVersioning = "YES"
stopOnEveryThreadSanitizerIssue = "YES"
stopOnEveryMainThreadCheckerIssue = "YES"
migratedStopOnEveryIssue = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
Expand All @@ -82,8 +81,6 @@
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
4 changes: 1 addition & 3 deletions Gifski/AppDelegate.swift
Expand Up @@ -50,9 +50,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true }

func application(_ application: NSApplication, willPresentError error: Error) -> Error {
Crashlytics.recordNonFatalError(error: error)
Expand Down
18 changes: 7 additions & 11 deletions Gifski/ConversionCompletedViewController.swift
Expand Up @@ -42,9 +42,9 @@ final class ConversionCompletedViewController: NSViewController {
super.viewDidAppear()

// This is needed for Quick Look to work.
self.view.window?.makeFirstResponder(self)
view.window?.makeFirstResponder(self)

if #available(macOS 10.14, *), defaults[.successfulConversionsCount] == 5 {
if #available(macOS 10.14, *), Defaults[.successfulConversionsCount] == 5 {
SKStoreReviewController.requestReview()
}

Expand Down Expand Up @@ -160,9 +160,7 @@ extension ConversionCompletedViewController: QLPreviewPanelDataSource {
panel.toggle()
}

override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool {
return true
}
override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool { true }

override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
panel.delegate = self
Expand All @@ -174,22 +172,20 @@ extension ConversionCompletedViewController: QLPreviewPanelDataSource {
panel.delegate = nil
}

func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return 1
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int { 1 }

func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
return gifUrl as NSURL
gifUrl as NSURL
}
}

extension ConversionCompletedViewController: QLPreviewPanelDelegate {
func previewPanel(_ panel: QLPreviewPanel!, sourceFrameOnScreenFor item: QLPreviewItem!) -> CGRect {
return draggableFile.imageView?.boundsInScreenCoordinates ?? .zero
draggableFile.imageView?.boundsInScreenCoordinates ?? .zero
}

func previewPanel(_ panel: QLPreviewPanel!, transitionImageFor item: QLPreviewItem!, contentRect: UnsafeMutablePointer<CGRect>!) -> Any! {
return draggableFile.image
draggableFile.image
}
}

Expand Down
2 changes: 1 addition & 1 deletion Gifski/ConversionViewController.swift
Expand Up @@ -86,7 +86,7 @@ final class ConversionViewController: NSViewController {
let gifUrl = self.generateTempGifUrl(for: conversion.video)
try result.get().write(to: gifUrl, options: .atomic)
try? gifUrl.setMetadata(key: .itemCreator, value: "\(App.name) \(App.version)")
defaults[.successfulConversionsCount] += 1
Defaults[.successfulConversionsCount] += 1

self.didComplete(conversion: conversion, gifUrl: gifUrl)
} catch Gifski.Error.cancelled {
Expand Down
47 changes: 24 additions & 23 deletions Gifski/CustomButton.swift
Expand Up @@ -24,7 +24,6 @@ final class HoverView: NSView {
*/
final class TrackingArea {
private weak var view: NSView?

private let rect: CGRect
private let options: NSTrackingArea.Options
private var trackingArea: NSTrackingArea?
Expand All @@ -34,15 +33,19 @@ final class TrackingArea {
- view: The view to add tracking to.
- rect: The area inside the view to track. Defaults to the whole view (`view.bounds`).
*/
init(for view: NSView, rect: CGRect? = nil, options: NSTrackingArea.Options = []) {
init(
for view: NSView,
rect: CGRect? = nil,
options: NSTrackingArea.Options = []
) {
self.view = view
self.rect = rect ?? view.bounds
self.options = options
}

/**
Updates the tracking area.
This should be called in your `NSView#updateTrackingAreas()` method.
- Note: This should be called in your `NSView#updateTrackingAreas()` method.
*/
func update() {
if let oldTrackingArea = trackingArea {
Expand Down Expand Up @@ -74,7 +77,7 @@ final class AnimationDelegate: NSObject, CAAnimationDelegate {

extension CALayer {
// TODO: Find a way to use a strongly-typed KeyPath here.
// TODO: Accept NSColor instead of CGColor.
// TODO: Accept `NSColor` instead of `CGColor`.
func animate(color: CGColor, keyPath: String, duration: Double) {
guard (value(forKey: keyPath) as! CGColor?) != color else {
return
Expand All @@ -91,7 +94,11 @@ extension CALayer {
}
}

func add(_ animation: CAAnimation, forKey key: String?, completion: @escaping ((Bool) -> Void)) {
func add(
_ animation: CAAnimation,
forKey key: String?,
completion: @escaping ((Bool) -> Void)
) {
let animationDelegate = AnimationDelegate()
animationDelegate.didStopHandler = completion
animation.delegate = animationDelegate
Expand All @@ -100,13 +107,13 @@ extension CALayer {
}

extension CGPoint {
func rounded(_ rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> CGPoint {
return CGPoint(x: x.rounded(rule), y: y.rounded(rule))
func rounded(_ rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> Self {
Self(x: x.rounded(rule), y: y.rounded(rule))
}
}

extension CGRect {
func roundedOrigin(_ rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> CGRect {
func roundedOrigin(_ rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> Self {
var rect = self
rect.origin = rect.origin.rounded(rule)
return rect
Expand All @@ -116,7 +123,7 @@ extension CGRect {
extension CGSize {
/// Returns a CGRect with `self` centered in it.
func centered(in rect: CGRect) -> CGRect {
return CGRect(
CGRect(
x: (rect.width - width) / 2,
y: (rect.height - height) / 2,
width: width,
Expand All @@ -125,24 +132,22 @@ extension CGSize {
}
}

// TODO: Add padding option
// TODO: Add padding option.
@IBDesignable
open class CustomButton: NSButton {
private let titleLayer = CATextLayer()
private var isMouseDown = false

static func circularButton(title: String, radius: Double, center: CGPoint) -> CustomButton {
return with(CustomButton()) {
with(CustomButton()) {
$0.title = title
$0.frame = CGRect(x: Double(center.x) - radius, y: Double(center.y) - radius, width: radius * 2, height: radius * 2)
$0.cornerRadius = radius
$0.font = NSFont.systemFont(ofSize: CGFloat(radius * 2 / 3))
}
}

override open var wantsUpdateLayer: Bool {
return true
}
override open var wantsUpdateLayer: Bool { true }

@IBInspectable override open var title: String {
didSet {
Expand Down Expand Up @@ -272,7 +277,7 @@ open class CustomButton: NSButton {
setup()
}

// Ensure the button doesn't draw its default contents
// Ensure the button doesn't draw its default contents.
override open func draw(_ dirtyRect: CGRect) {}
override open func drawFocusRingMask() {}

Expand Down Expand Up @@ -324,16 +329,14 @@ open class CustomButton: NSButton {
///
/// - Parameter keyPath: The key path that specifies the color related property.
subscript(colorGenerator keyPath: KeyPath<CustomButton, NSColor>) -> ColorGenerator? {
get {
return colorGenerators[keyPath]
}
get { colorGenerators[keyPath] }
set {
colorGenerators[keyPath] = newValue
}
}

private func color(for keyPath: KeyPath<CustomButton, NSColor>) -> NSColor {
return colorGenerators[keyPath]?() ?? self[keyPath: keyPath]
colorGenerators[keyPath]?() ?? self[keyPath: keyPath]
}

override open func updateLayer() {
Expand Down Expand Up @@ -381,7 +384,7 @@ open class CustomButton: NSButton {
}

override open func hitTest(_ point: CGPoint) -> NSView? {
return isEnabled ? super.hitTest(point) : nil
isEnabled ? super.hitTest(point) : nil
}

override open func mouseDown(with event: NSEvent) {
Expand Down Expand Up @@ -412,7 +415,5 @@ open class CustomButton: NSButton {
}

extension CustomButton: NSViewLayerContentScaleDelegate {
public func layer(_ layer: CALayer, shouldInheritContentsScale newScale: CGFloat, from window: NSWindow) -> Bool {
return true
}
public func layer(_ layer: CALayer, shouldInheritContentsScale newScale: CGFloat, from window: NSWindow) -> Bool { true }
}
8 changes: 2 additions & 6 deletions Gifski/DraggableFile.swift
Expand Up @@ -19,9 +19,7 @@ final class DraggableFile: NSImageView {
}
}

var imageView: NSView? {
return subviews.first
}
var imageView: NSView? { subviews.first }

override init(frame: CGRect) {
super.init(frame: frame)
Expand Down Expand Up @@ -57,7 +55,5 @@ final class DraggableFile: NSImageView {
}

extension DraggableFile: NSDraggingSource {
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return .copy
}
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation { .copy }
}

0 comments on commit 948d255

Please sign in to comment.