Skip to content

Commit

Permalink
Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
WCByrne committed Sep 7, 2017
1 parent 7447217 commit d53b5ac
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 74 deletions.
22 changes: 18 additions & 4 deletions CollectionView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0820;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Noun Project";
TargetAttributes = {
DE9071E81CAC7FD800AD0E37 = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
Expand Down Expand Up @@ -294,14 +294,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -343,14 +349,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -398,7 +410,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -422,7 +435,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
8 changes: 4 additions & 4 deletions CollectionView/ChangeSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public struct Edit<T: Hashable> : CustomStringConvertible, Hashable {
switch self.operation {
case let .move(origin):
return "Edit: Move \(origin) to \(self.index)"
case let .substitution(target):
return "Edit: Replace item at \(self.index) - Target: \(target)"
case .substitution:
return "Edit: Replace item at \(self.index)"
case .insertion:
return "Edit: Insert at \(self.index)"
case .deletion:
Expand Down Expand Up @@ -702,7 +702,7 @@ public struct ChangeSet<T: Collection> where T.Iterator.Element: Hashable, T.Ind
- parameter edits: An array of `Edit` elements to be reduced.
- returns: An array of `Edit` elements.
*/
private static func reducedEdits<T: Equatable>(_ edits: [Edit<T>]) -> [Edit<T>] {
private static func reducedEdits<T>(_ edits: [Edit<T>]) -> [Edit<T>] {
return edits.reduce([Edit<T>]()) { (edits, edit) in
var reducedEdits = edits
if let (move, index) = move(from: edit, in: reducedEdits), case let .move(origin) = move.operation {
Expand Down Expand Up @@ -735,7 +735,7 @@ public struct ChangeSet<T: Collection> where T.Iterator.Element: Hashable, T.Ind
- returns: An optional tuple consisting of the `.move` `Edit` that corresponds to the given deletion or insertion and an opposite match in `edits`, and the index of the match – if one was found.
*/
private func move<T: Equatable>(from deletionOrInsertion: Edit<T>, `in` edits: [Edit<T>]) -> (move: Edit<T>, index: Int)? {
private func move<T>(from deletionOrInsertion: Edit<T>, `in` edits: [Edit<T>]) -> (move: Edit<T>, index: Int)? {

switch deletionOrInsertion.operation {

Expand Down
10 changes: 5 additions & 5 deletions CollectionView/ClipView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ open class ClipView : NSClipView {

override open func viewWillMove(toWindow newWindow: NSWindow?) {
if (self.window != nil) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSWindowDidChangeScreen, object: self.window)
NotificationCenter.default.removeObserver(self, name: NSWindow.didChangeScreenNotification, object: self.window)
}
super.viewWillMove(toWindow: newWindow)
if (newWindow != nil) {
NotificationCenter.default.addObserver(self, selector: #selector(ClipView.updateCVDisplay(_:)), name: NSNotification.Name.NSWindowDidChangeScreen, object: newWindow)
NotificationCenter.default.addObserver(self, selector: #selector(ClipView.updateCVDisplay(_:)), name: NSWindow.didChangeScreenNotification, object: newWindow)
}
}

Expand All @@ -92,13 +92,13 @@ open class ClipView : NSClipView {



func updateCVDisplay(_ note: Notification) {
@objc func updateCVDisplay(_ note: Notification) {

guard self._displayLink != nil else { return }

if let screen = self.window?.screen {
let screenDictionary = screen.deviceDescription
let screenID = screenDictionary["NSScreenNumber"] as! NSNumber
let screenID = screenDictionary[NSDeviceDescriptionKey("NSScreenNumber")] as! NSNumber
let displayID = screenID.uint32Value
CVDisplayLinkSetCurrentCGDisplay(displayLink, displayID)
}
Expand Down Expand Up @@ -189,7 +189,7 @@ open class ClipView : NSClipView {
// }
// Make this call so that we can force an update of the scroller positions.
self.scrollView?.reflectScrolledClipView(self)
NotificationCenter.default.post(name: Notification.Name.NSScrollViewDidLiveScroll, object: self.scrollView)
NotificationCenter.default.post(name: NSScrollView.didLiveScrollNotification, object: self.scrollView)
}

//.postNotificationName(NSScrollViewDidLiveScrollNotification, object: self, userInfo: nil)
Expand Down
43 changes: 22 additions & 21 deletions CollectionView/CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class CollectionView : ScrollView, NSDraggingSource {


/// The object that acts as the delegate to the collection view
public weak var delegate : CollectionViewDelegate?
@objc public weak var delegate : CollectionViewDelegate?

/// The object that provides data for the collection view
public weak var dataSource : CollectionViewDataSource?
Expand Down Expand Up @@ -84,9 +84,9 @@ open class CollectionView : ScrollView, NSDraggingSource {
self.hasVerticalScroller = true
self.scrollsDynamically = true

NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.didScroll(_:)), name: NSNotification.Name.NSScrollViewDidLiveScroll, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.willBeginScroll(_:)), name: NSNotification.Name.NSScrollViewWillStartLiveScroll, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.didEndScroll(_:)), name: NSNotification.Name.NSScrollViewDidEndLiveScroll, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.didScroll(_:)), name: NSScrollView.didLiveScrollNotification, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.willBeginScroll(_:)), name: NSScrollView.willStartLiveScrollNotification, object: self)
NotificationCenter.default.addObserver(self, selector: #selector(CollectionView.didEndScroll(_:)), name: NSScrollView.didEndLiveScrollNotification, object: self)

self.addSubview(_floatingSupplementaryView, positioned: .above, relativeTo: self.clipView!)
self._floatingSupplementaryView.wantsLayer = true
Expand Down Expand Up @@ -114,7 +114,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
}
}

open override var scrollerStyle: NSScrollerStyle {
open override var scrollerStyle: NSScroller.Style {
didSet {
// log.debug("Scroller Style changed")
self.reloadLayout(false)
Expand Down Expand Up @@ -211,9 +211,10 @@ open class CollectionView : ScrollView, NSDraggingSource {
internal var _registeredSupplementaryViewKinds = Set<String>()
private func _firstObjectOfClass(_ aClass: AnyClass, inNib: NSNib) -> NSView? {
var foundObject: AnyObject? = nil
var topLevelObjects = NSArray()
if inNib.instantiate(withOwner: self, topLevelObjects: &topLevelObjects) {
for obj in topLevelObjects {
var topLevelObjects : NSArray?

if inNib.instantiate(withOwner: self, topLevelObjects: &topLevelObjects), let objects = topLevelObjects {
for obj in objects {
if let o = obj as? NSView, o.isKind(of: aClass) {
foundObject = o
break
Expand Down Expand Up @@ -727,7 +728,7 @@ open class CollectionView : ScrollView, NSDraggingSource {

// MARK: - Scroll Handling
/*-------------------------------------------------------------------------------*/
override open class func isCompatibleWithResponsiveScrolling() -> Bool { return true }
override open class var isCompatibleWithResponsiveScrolling: Bool { return true }

public var isScrollEnabled : Bool {
set { self.clipView?.scrollEnabled = newValue }
Expand Down Expand Up @@ -786,7 +787,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
public private(set) var peakScrollVelocity = CGPoint.zero


final func didScroll(_ notification: Notification) {
@objc final func didScroll(_ notification: Notification) {
let rect = _preperationRect
self.contentDocumentView.prepareRect(rect)

Expand All @@ -802,22 +803,22 @@ open class CollectionView : ScrollView, NSDraggingSource {
self.delegate?.collectionViewDidScroll?(self)
}

final func willBeginScroll(_ notification: Notification) {
@objc final func willBeginScroll(_ notification: Notification) {
self.isScrolling = true
self.delegate?.collectionViewWillBeginScrolling?(self)
self._previousOffset = self.contentVisibleRect.origin
self.peakScrollVelocity = CGPoint.zero
self.scrollVelocity = CGPoint.zero
}

final func didEndScroll(_ notification: Notification) {
@objc final func didEndScroll(_ notification: Notification) {
self.isScrolling = false

self.delegate?.collectionViewDidEndScrolling?(self, animated: true)
self.scrollVelocity = CGPoint.zero
self.peakScrollVelocity = CGPoint.zero

if trackSectionHover && NSApp.isActive, let point = self.window?.convertFromScreen(NSRect(origin: NSEvent.mouseLocation(), size: CGSize.zero)).origin {
if trackSectionHover && NSApp.isActive, let point = self.window?.convertFromScreen(NSRect(origin: NSEvent.mouseLocation, size: CGSize.zero)).origin {
let loc = self.contentDocumentView.convert(point, from: nil)
self.delegate?.collectionView?(self, mouseMovedToSection: indexPathForSection(at: loc))
}
Expand Down Expand Up @@ -1530,7 +1531,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
self.removeTrackingArea(ta)
}
if trackSectionHover {
_trackingArea = NSTrackingArea(rect: self.bounds, options: [NSTrackingAreaOptions.activeInActiveApp, NSTrackingAreaOptions.mouseEnteredAndExited, NSTrackingAreaOptions.mouseMoved], owner: self, userInfo: nil)
_trackingArea = NSTrackingArea(rect: self.bounds, options: [NSTrackingArea.Options.activeInActiveApp, NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.mouseMoved], owner: self, userInfo: nil)
self.addTrackingArea(_trackingArea!)
}
}
Expand Down Expand Up @@ -1621,7 +1622,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
self._deselectAllItems(true, notify: true)
}

if theEvent.modifierFlags.contains(NSEventModifierFlags.control) {
if theEvent.modifierFlags.contains(NSEvent.ModifierFlags.control) {
self.rightMouseDown(with: theEvent)
return
}
Expand All @@ -1635,10 +1636,10 @@ open class CollectionView : ScrollView, NSDraggingSource {

guard ip == mouseDownIP else { return }

if allowsMultipleSelection && theEvent.modifierFlags.contains(NSEventModifierFlags.shift) {
if allowsMultipleSelection && theEvent.modifierFlags.contains(NSEvent.ModifierFlags.shift) {
self._selectItem(at: ip, atScrollPosition: .nearest, animated: true, selectionType: .extending)
}
else if allowsMultipleSelection && theEvent.modifierFlags.contains(NSEventModifierFlags.command) {
else if allowsMultipleSelection && theEvent.modifierFlags.contains(NSEvent.ModifierFlags.command) {
if self._selectedIndexPaths.contains(ip) {
if self._selectedIndexPaths.count == 1 { return }
self._deselectItem(at: ip, animated: true, notifyDelegate: true)
Expand Down Expand Up @@ -1701,7 +1702,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
else {
lastEventTime = nil
}
let extend = selectionMode == .multi || theEvent.modifierFlags.contains(NSEventModifierFlags.shift)
let extend = selectionMode == .multi || theEvent.modifierFlags.contains(NSEvent.ModifierFlags.shift)
if theEvent.keyCode == 123 { self.moveSelectionLeft(extend) }
else if theEvent.keyCode == 124 { self.moveSelectionRight(extend) }
else if theEvent.keyCode == 125 { self.moveSelectionDown(extend) }
Expand Down Expand Up @@ -2562,7 +2563,7 @@ open class CollectionView : ScrollView, NSDraggingSource {

// image = NSImage(data: cell.dataWithPDF(inside: cell.bounds))
}
let comp = NSDraggingImageComponent(key: NSDraggingImageComponentIconKey)
let comp = NSDraggingImageComponent(key: NSDraggingItem.ImageComponentKey.icon)
comp.contents = image
comp.frame = CGRect(origin: CGPoint.zero, size: frame.size)
return [comp]
Expand Down Expand Up @@ -2623,7 +2624,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
invalidateAutoscroll()
self.interactionDelegate?.collectionView?(self, dragExited: sender)
}
open override func draggingEnded(_ sender: NSDraggingInfo?) {
open override func draggingEnded(_ sender: NSDraggingInfo) {
self.interactionDelegate?.collectionView?(self, dragEnded: sender)
}
open override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
Expand Down Expand Up @@ -2651,7 +2652,7 @@ open class CollectionView : ScrollView, NSDraggingSource {
autoscrollTimer = nil
}

func autoscrollTimer(_ sender: Timer) {
@objc func autoscrollTimer(_ sender: Timer) {
if let p = (sender.userInfo as? [String:Any])?["point"] as? CGPoint {
autoScroll(to: p)
}
Expand Down
20 changes: 10 additions & 10 deletions CollectionView/CollectionViewCells.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ open class CollectionReusableView : NSView {
override public init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.onSetNeedsDisplay
self.layerContentsRedrawPolicy = NSView.LayerContentsRedrawPolicy.onSetNeedsDisplay
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
self.wantsLayer = true
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.onSetNeedsDisplay
self.layerContentsRedrawPolicy = NSView.LayerContentsRedrawPolicy.onSetNeedsDisplay
}


Expand Down Expand Up @@ -114,7 +114,7 @@ open class CollectionReusableView : NSView {
if let c = self.backgroundColor {
NSGraphicsContext.saveGraphicsState()
c.setFill()
NSRectFill(dirtyRect)
dirtyRect.fill()
NSGraphicsContext.restoreGraphicsState()
}
super.draw(dirtyRect)
Expand All @@ -126,14 +126,14 @@ open class CollectionReusableView : NSView {
/*-------------------------------------------------------------------------------*/

fileprivate var wantsTracking = false
open var trackingOptions = [NSTrackingAreaOptions.mouseEnteredAndExited, NSTrackingAreaOptions.activeInKeyWindow, .inVisibleRect, .enabledDuringMouseDrag]
open var trackingOptions = [NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.activeInKeyWindow, NSTrackingArea.Options.inVisibleRect, NSTrackingArea.Options.enabledDuringMouseDrag]
var _trackingArea : NSTrackingArea?
open var trackMouseMoved : Bool = false {
didSet {
if trackMouseMoved == oldValue { return }
let idx = trackingOptions.index(of: .mouseMoved)
let idx = trackingOptions.index(of: NSTrackingArea.Options.mouseMoved)
if trackMouseMoved && idx == nil {
trackingOptions.append(.mouseMoved)
trackingOptions.append(NSTrackingArea.Options.mouseMoved)
}
else if !trackMouseMoved, let i = idx {
trackingOptions.remove(at: i)
Expand All @@ -159,7 +159,7 @@ open class CollectionReusableView : NSView {
super.updateTrackingAreas()
if let ta = self._trackingArea { self.removeTrackingArea(ta) }
if self.wantsTracking == false { return }
_trackingArea = NSTrackingArea(rect: self.bounds, options: NSTrackingAreaOptions(trackingOptions), owner: self, userInfo: nil)
_trackingArea = NSTrackingArea(rect: self.bounds, options: NSTrackingArea.Options(trackingOptions), owner: self, userInfo: nil)
self.addTrackingArea(_trackingArea!)
}

Expand Down Expand Up @@ -233,13 +233,13 @@ open class CollectionViewCell : CollectionReusableView {

// Validate self and the event
guard let cv = self.collectionView, let ip = self.attributes?.indexPath else { return }
guard theEvent.type == NSEventType.mouseEntered && (theEvent.trackingArea?.owner as? CollectionViewCell) == self else { return }
guard theEvent.type == NSEvent.EventType.mouseEntered && (theEvent.trackingArea?.owner as? CollectionViewCell) == self else { return }

// Make sure the event is inside self

guard self.highlighted == false else { return }
guard let window = self.window else { return }
let mLoc = window.convertFromScreen(NSRect(origin: NSEvent.mouseLocation(), size: CGSize.zero)).origin
let mLoc = window.convertFromScreen(NSRect(origin: NSEvent.mouseLocation, size: CGSize.zero)).origin
if !self.bounds.contains(self.convert(mLoc, from: nil)) { return }

// Ignore the event if an interaction enabled view is over this cell
Expand All @@ -254,7 +254,7 @@ open class CollectionViewCell : CollectionReusableView {
override open func mouseExited(with theEvent: NSEvent) {
super.mouseExited(with: theEvent)
guard self.highlighted == true else { return }
guard theEvent.type == NSEventType.mouseExited && (theEvent.trackingArea?.owner as? CollectionViewCell) == self else { return }
guard theEvent.type == NSEvent.EventType.mouseExited && (theEvent.trackingArea?.owner as? CollectionViewCell) == self else { return }
self.setHighlighted(false, animated: true)
}

Expand Down
Loading

0 comments on commit d53b5ac

Please sign in to comment.