Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Swift 5 with Xcode 11.5 #24

Open
wants to merge 2 commits into
base: m@ster
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions LoveLiver-osx/AVKitExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ extension AVAssetImageGenerator {

extension AVPlayerItem {
var naturalSize: CGSize? {
return asset.tracks(withMediaType: AVMediaTypeVideo).first?.naturalSize
return asset.tracks(withMediaType: .video).first?.naturalSize
}

var minFrameDuration: CMTime? {
return asset.tracks(withMediaType: AVMediaTypeVideo).first?.minFrameDuration
return asset.tracks(withMediaType: .video).first?.minFrameDuration
}
}
4 changes: 2 additions & 2 deletions LoveLiver-osx/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return true
}

@objc fileprivate func openDocument(_ sender: AnyObject?) {
NSDocumentController.shared().openDocument(sender)
@objc private func openDocument(_ sender: Any?) {
NSDocumentController.shared.openDocument(sender)
}
}

49 changes: 26 additions & 23 deletions LoveLiver-osx/LivePhotoSandboxViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import Ikemen
private let livePhotoDuration: TimeInterval = 3
private let outputDir = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Pictures/LoveLiver")

fileprivate extension NSTouchBarItemIdentifier {
static let overview = NSTouchBarItemIdentifier("jp.mzp.loveliver.overview")
@available (OSX 10.12.2, *)
fileprivate extension NSTouchBarItem.Identifier {
static let overview = NSTouchBarItem.Identifier("jp.mzp.loveliver.overview")
}

private func label() -> NSTextField {
Expand All @@ -26,7 +27,7 @@ private func label() -> NSTextField {
tf.isEditable = false
tf.drawsBackground = false
tf.textColor = NSColor.gray
tf.font = NSFont.monospacedDigitSystemFont(ofSize: 12, weight: NSFontWeightRegular)
tf.font = NSFont.monospacedDigitSystemFont(ofSize: 12, weight: .regular)
}
}

Expand All @@ -46,7 +47,7 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
b.target = self
b.action = #selector(self.close)
}
var closeAction: ((Void) -> Void)?
var closeAction: (() -> Void)?

fileprivate func updateButtons() {
exportButton.isEnabled = (exportSession == nil)
Expand Down Expand Up @@ -120,9 +121,9 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
endLabel.stringValue = endTime.stringInmmssSS
}
fileprivate func updateScope() {
let duration = player.currentItem?.duration ?? kCMTimeZero
let duration = player.currentItem?.duration ?? .zero

if CMTimeMaximum(kCMTimeZero, startTime) == kCMTimeZero {
if CMTimeMaximum(.zero, startTime) == .zero {
// scope is clipped by zero. underflow scope start by subtracting from end
scopeRange = CMTimeRange(start: CMTimeSubtract(endTime, CMTime(seconds: livePhotoDuration, preferredTimescale: posterTime.timescale)), end: endTime)
} else if CMTimeMinimum(duration, endTime) == duration {
Expand All @@ -138,23 +139,23 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
let item = AVPlayerItem(asset: asset)

self.baseFilename = baseFilename
posterTime = CMTimeConvertScale(player.currentTime(), max(600, player.currentTime().timescale), .default) // timescale = 1 (too inaccurate) when posterTime = 0
posterTime = CMTimeConvertScale(player.currentTime(), timescale: max(600, player.currentTime().timescale), method: .default) // timescale = 1 (too inaccurate) when posterTime = 0
let duration = item.duration
let offset = CMTime(seconds: livePhotoDuration / 2, preferredTimescale: posterTime.timescale)
startTime = CMTimeMaximum(kCMTimeZero, CMTimeSubtract(posterTime, offset))
startTime = CMTimeMaximum(.zero, CMTimeSubtract(posterTime, offset))
endTime = CMTimeMinimum(CMTimeAdd(posterTime, offset), duration)

self.player = AVPlayer(playerItem: item)

imageGenerator = AVAssetImageGenerator(asset: asset) ※ { g -> Void in
g.requestedTimeToleranceBefore = kCMTimeZero
g.requestedTimeToleranceAfter = kCMTimeZero
g.requestedTimeToleranceBefore = .zero
g.requestedTimeToleranceAfter = .zero
g.maximumSize = CGSize(width: 128 * 2, height: 128 * 2)
}

overview = MovieOverviewControl(player: self.player, playerItem: item)
overview.draggingMode = .scope
overview.imageGeneratorTolerance = kCMTimeZero
overview.imageGeneratorTolerance = .zero

if #available(OSX 10.12.2, *) {
touchBarItemProvider = OverviewTouchBarItemProvider(player: self.player, playerItem: item)
Expand Down Expand Up @@ -261,8 +262,8 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
func onScopeChange(_ overview : MovieOverviewControl) {
guard let s = overview.scopeRange?.start,
let e = overview.scopeRange?.end else { return }
startTime = CMTimeMaximum(kCMTimeZero, s)
endTime = CMTimeMinimum(player.currentItem?.duration ?? kCMTimeZero, e)
startTime = CMTimeMaximum(.zero, s)
endTime = CMTimeMinimum(player.currentItem?.duration ?? .zero, e)

if !((touchBarItemProvider?.dragging ?? false) || self.overview.dragging) {
updateImages()
Expand All @@ -272,8 +273,8 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
@objc fileprivate func export() {
guard let asset = player.currentItem?.asset else { return }
let imageGenerator = AVAssetImageGenerator(asset: asset) ※ {
$0.requestedTimeToleranceBefore = kCMTimeZero
$0.requestedTimeToleranceAfter = kCMTimeZero
$0.requestedTimeToleranceBefore = .zero
$0.requestedTimeToleranceAfter = .zero
}
guard let image = imageGenerator.copyImage(at: posterTime) else { return }
guard let _ = try? FileManager.default.createDirectory(atPath: outputDir.path, withIntermediateDirectories: true, attributes: nil) else { return }
Expand All @@ -297,7 +298,7 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
guard let _ = try? image.tiffRepresentation?.write(to: URL(fileURLWithPath: tmpImagePath), options: [.atomic]) else { return }
// create AVAssetExportSession each time because it cannot be reused after export completion
guard let session = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else { return }
session.outputFileType = "com.apple.quicktime-movie"
session.outputFileType = .mov
session.outputURL = URL(fileURLWithPath: tmpMoviePath)
session.timeRange = CMTimeRange(start: startTime, end: endTime)
session.exportAsynchronously {
Expand All @@ -312,6 +313,8 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {

self.showInFinderAndOpenInPhotos([imagePath, moviePath].map{URL(fileURLWithPath: $0)})
case .cancelled, .exporting, .failed, .unknown, .waiting:
fallthrough
@unknown default:
NSLog("%@", "exportAsynchronouslyWithCompletionHandler = \(session.status)")
}

Expand All @@ -327,30 +330,30 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
}

fileprivate func showInFinderAndOpenInPhotos(_ fileURLs: [URL]) {
NSWorkspace.shared().activateFileViewerSelecting(fileURLs)
NSWorkspace.shared.activateFileViewerSelecting(fileURLs)

// wait until Finder is active or timed out,
// to avoid openURLs overtaking Finder activation
DispatchQueue.global(qos: .default).async {
let start = Date()
while NSWorkspace.shared().frontmostApplication?.bundleIdentifier != "com.apple.finder" && Date().timeIntervalSince(start) < 5 {
while NSWorkspace.shared.frontmostApplication?.bundleIdentifier != "com.apple.finder" && Date().timeIntervalSince(start) < 5 {
Thread.sleep(forTimeInterval: 0.1)
}
NSWorkspace.shared().open(fileURLs, withAppBundleIdentifier: "com.apple.Photos", options: [], additionalEventParamDescriptor: nil, launchIdentifiers: nil)
NSWorkspace.shared.open(fileURLs, withAppBundleIdentifier: "com.apple.Photos", options: [], additionalEventParamDescriptor: nil, launchIdentifiers: nil)
}
}

private func shouldUpdateScopeRange(scopeRange : CMTimeRange?) -> Bool {
guard let scopeRange = scopeRange else { return false }
return CMTimeRangeContainsTime(scopeRange, self.posterTime)
return CMTimeRangeContainsTime(scopeRange, time: self.posterTime)
}

@objc fileprivate func close() {
closeAction?()
}

@objc fileprivate func play() {
player.seek(to: startTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
player.seek(to: startTime, toleranceBefore: .zero, toleranceAfter: .zero)
player.play()
}

Expand All @@ -371,7 +374,7 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
case (is AVPlayer, _):
let stopped = (player.rate == 0)
if stopped {
player.seek(to: posterTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
player.seek(to: posterTime, toleranceBefore: .zero, toleranceAfter: .zero)
}
break
default:
Expand All @@ -380,7 +383,7 @@ class LivePhotoSandboxViewController: NSViewController, NSTouchBarDelegate {
}

@available(OSX 10.12.2, *)
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
if identifier == .overview {
return touchBarItemProvider?.makeTouchbarItem(identifier: identifier)
} else {
Expand Down
2 changes: 1 addition & 1 deletion LoveLiver-osx/MovieDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MovieDocument: NSDocument, NSWindowDelegate {
overviewVC = MovieOverviewViewController(player: player!, playerItem: playerItem!)
overviewWindow = NSWindow(contentViewController: overviewVC!) ※ { w in
w.delegate = self
w.styleMask = NSResizableWindowMask
w.styleMask = .resizable
}
addWindowController(NSWindowController(window: overviewWindow))
mainWindow!.addChildWindow(overviewWindow!, ordered: .above)
Expand Down
2 changes: 1 addition & 1 deletion LoveLiver-osx/MovieDocumentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MovieDocumentViewController: NSViewController {
fileprivate let movieURL: URL
fileprivate let player: AVPlayer
fileprivate let playerItem: AVPlayerItem
var createLivePhotoAction: ((Void) -> Void)?
var createLivePhotoAction: (() -> Void)?

fileprivate let playerView: AVPlayerView = AVPlayerView() ※ { v in
v.controlsStyle = .floating
Expand Down
20 changes: 10 additions & 10 deletions LoveLiver-osx/MovieOverviewControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import NorthLayout

class MovieOverviewControl: NSView {
let player: AVPlayer
var playerTimeObserver: AnyObject?
var playerTimeObserver: Any?
var currentTime: CMTime? {
didSet { updateCurrentTime() }
}
Expand All @@ -26,7 +26,7 @@ class MovieOverviewControl: NSView {
tf.isBezeled = false
tf.isEditable = false
tf.drawsBackground = true
tf.font = NSFont.monospacedDigitSystemFont(ofSize: 12, weight: NSFontWeightRegular)
tf.font = NSFont.monospacedDigitSystemFont(ofSize: 12, weight: .regular)
tf.textColor = NSColor.white
tf.backgroundColor = NSColor.black
}
Expand Down Expand Up @@ -75,7 +75,7 @@ class MovieOverviewControl: NSView {

init(player: AVPlayer, playerItem: AVPlayerItem) {
self.player = player
self.trimRange = CMTimeRange(start: kCMTimeZero, duration: playerItem.duration)
self.trimRange = CMTimeRange(start: .zero, duration: playerItem.duration)

super.init(frame: NSZeroRect)

Expand All @@ -85,8 +85,8 @@ class MovieOverviewControl: NSView {
autolayout("H:|[currentTime]")
autolayout("V:[currentTime]|")

setContentCompressionResistancePriority(NSLayoutPriorityDefaultHigh, for: .vertical)
setContentHuggingPriority(NSLayoutPriorityDefaultHigh, for: .vertical)
setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
setContentHuggingPriority(.defaultHigh, for: .vertical)

// subviews ordering
addSubview(scopeMaskLeftView)
Expand Down Expand Up @@ -159,7 +159,7 @@ class MovieOverviewControl: NSView {

playerTimeObserver = player.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 30), queue: DispatchQueue.main) { [weak self] time in
self?.currentTime = time
} as AnyObject?
}
}

override func viewDidEndLiveResize() {
Expand Down Expand Up @@ -218,7 +218,7 @@ class MovieOverviewControl: NSView {

override func draw(_ dirtyRect: NSRect) {
NSColor.black.setFill()
NSRectFillUsingOperation(dirtyRect, .copy)
dirtyRect.fill(using: .copy)

var x: CGFloat = 0
for t in thumbnails {
Expand Down Expand Up @@ -263,7 +263,7 @@ class MovieOverviewControl: NSView {
fileprivate func seekToMousePosition(_ theEvent: NSEvent) {
let p = convert(theEvent.locationInWindow, from: nil)
let time = CMTimeAdd(CMTime(value: Int64(CGFloat(trimRange.duration.value) * p.x / bounds.width), timescale: trimRange.duration.timescale), trimRange.start)
player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
}

fileprivate func scopeToMousePosition(_ theEvent: NSEvent) {
Expand All @@ -273,8 +273,8 @@ class MovieOverviewControl: NSView {
let p = convert(theEvent.locationInWindow, from: nil)

let distance = Int32(p.x - mouseDownLocation.x)
let start = CMTimeAdd(s.start, CMTimeMultiply(minFrameDuration, distance))
let end = CMTimeAdd(s.end, CMTimeMultiply(minFrameDuration, distance))
let start = CMTimeAdd(s.start, CMTimeMultiply(minFrameDuration, multiplier: distance))
let end = CMTimeAdd(s.end, CMTimeMultiply(minFrameDuration, multiplier: distance))
let newScopeRange = CMTimeRange(start: start, end: end)

if shouldUpdateScopeRange?(newScopeRange) == true {
Expand Down
4 changes: 2 additions & 2 deletions LoveLiver-osx/MovieOverviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MovieOverviewViewController: NSViewController {

init(player: AVPlayer, playerItem: AVPlayerItem) {
self.overview = MovieOverviewControl(player: player, playerItem: playerItem)
super.init(nibName: nil, bundle: nil)!
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
Expand All @@ -34,7 +34,7 @@ class MovieOverviewViewController: NSViewController {
}

func movieDidLoad(_ videoSize: CGSize) {
overview.currentTime = kCMTimeZero
overview.currentTime = .zero
overview.reload()
}
}
14 changes: 7 additions & 7 deletions LoveLiver-osx/OverviewTouchBarItemProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ protocol OverviewTouchBarItemProviderType : class {
var dragging : Bool { get }

@available(OSX 10.12.2, *)
func makeTouchbarItem(identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem
func makeTouchbarItem(identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem
}

@available(OSX 10.12.2, *)
class OverviewTouchBarItemProvider : NSViewController, OverviewTouchBarItemProviderType {
var shouldUpdateScopeRange: ((_ newValue: CMTimeRange?) -> Bool)?
var onScopeChange: ((_ overview : MovieOverviewControl) -> Void)?
var trimRange : CMTimeRange = kCMTimeRangeZero {
var trimRange : CMTimeRange = .zero {
didSet {
overview.trimRange = trimRange
}
}
var scopeRange : CMTimeRange = kCMTimeRangeZero {
var scopeRange : CMTimeRange = .zero {
didSet {
overview.scopeRange = scopeRange
}
Expand All @@ -45,16 +45,16 @@ class OverviewTouchBarItemProvider : NSViewController, OverviewTouchBarItemProvi
init(player: AVPlayer, playerItem: AVPlayerItem) {
self.overview = MovieOverviewControl(player: player, playerItem: playerItem)
self.overview.draggingMode = .scope
self.overview.imageGeneratorTolerance = kCMTimeZero
super.init(nibName: nil, bundle: nil)!
self.overview.imageGeneratorTolerance = .zero
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


func makeTouchbarItem(identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem {
func makeTouchbarItem(identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem {
return NSCustomTouchBarItem(identifier: identifier) ※ { item in
item.viewController = self
}
Expand Down Expand Up @@ -96,7 +96,7 @@ class OverviewTouchBarItemProvider : NSViewController, OverviewTouchBarItemProvi

private func seekToTouchPosition(_ touch: NSTouch) {
let trimRange = overview.trimRange
let duration = overview.scopeRange?.duration ?? kCMTimeZero
let duration = overview.scopeRange?.duration ?? .zero

let p = view.convert(touch.location(in: view), from: nil)

Expand Down
Loading