Skip to content

Commit

Permalink
Meet SwiftLint requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelhanneken committed Mar 22, 2016
1 parent 28209f7 commit 48d7d89
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 441 deletions.
17 changes: 17 additions & 0 deletions Icns Composer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
20FA7A5A1B14E9D900E7B8E7 /* Frameworks */,
20FA7A5B1B14E9D900E7B8E7 /* Resources */,
8EA095AC1B583BFC00DA6A2E /* CopyFiles */,
8E4CB2331CA196F4001D52C4 /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -187,6 +188,22 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
8E4CB2331CA196F4001D52C4 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint autocorrect\n swiftlint\nelse\n echo \"Warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
20FA7A591B14E9D900E7B8E7 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
50 changes: 25 additions & 25 deletions Icns Composer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

/// Handle the window via MainWindowController.
var mainWindowController: MainWindowController?


func applicationDidFinishLaunching(aNotification: NSNotification) {
// Create a new WindowController instance.
let mainWindowController = MainWindowController()

// Display the associated window on screen.
mainWindowController.showWindow(self)

// Point the instance variable to the WindowController object.
self.mainWindowController = mainWindowController
}

// Reopen mainWindow, when the user clicks on the dock icon.
func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if let mainWindowController = self.mainWindowController {
mainWindowController.showWindow(self)
}
return true
}
/// Handle the window via MainWindowController.
var mainWindowController: MainWindowController?


func applicationDidFinishLaunching(aNotification: NSNotification) {
// Create a new WindowController instance.
let mainWindowController = MainWindowController()

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
// Display the associated window on screen.
mainWindowController.showWindow(self)

// Point the instance variable to the WindowController object.
self.mainWindowController = mainWindowController
}

// Reopen mainWindow, when the user clicks on the dock icon.
func applicationShouldHandleReopen(sender: NSApplication,
hasVisibleWindows flag: Bool) -> Bool {
if let mainWindowController = self.mainWindowController {
mainWindowController.showWindow(self)
}
}
return true
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
178 changes: 91 additions & 87 deletions Icns Composer/DragDropImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,94 +29,98 @@
import Cocoa

class DragDropImageView: NSImageView, NSDraggingSource {

/// Holds the last mouse down event, to track the drag distance.
var mouseDownEvent: NSEvent?



override init(frame frameRect: NSRect) {
super.init(frame: frameRect)

// Assure editable is set to true, to enable drop capabilities.
self.editable = true
}

required init?(coder: NSCoder) {
super.init(coder: coder)

// Assure editable is set to true, to enable drop capabilities.
self.editable = true
}

override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}



// MARK: - NSDraggingSource

// Since we only want to copy/delete the current image we register ourselfes
// for .Copy and .Delete operations.
func draggingSession(session: NSDraggingSession, sourceOperationMaskForDraggingContext context: NSDraggingContext) -> NSDragOperation {
return NSDragOperation.Copy.union(.Delete)
}

// Clear the ImageView on delete operation; e.g. the image gets
// dropped on the trash can in the dock.
func draggingSession(session: NSDraggingSession, endedAtPoint screenPoint: NSPoint, operation: NSDragOperation) {
if operation == .Delete {
self.image = nil
}

/// Holds the last mouse down event, to track the drag distance.
var mouseDownEvent: NSEvent?



override init(frame frameRect: NSRect) {
super.init(frame: frameRect)

// Assure editable is set to true, to enable drop capabilities.
self.editable = true
}

required init?(coder: NSCoder) {
super.init(coder: coder)

// Assure editable is set to true, to enable drop capabilities.
self.editable = true
}

override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}



// MARK: - NSDraggingSource

// Since we only want to copy/delete the current image we register ourselfes
// for .Copy and .Delete operations.
func draggingSession(session: NSDraggingSession, sourceOperationMaskForDraggingContext
context: NSDraggingContext) -> NSDragOperation {
return NSDragOperation.Copy.union(.Delete)
}

// Clear the ImageView on delete operation; e.g. the image gets
// dropped on the trash can in the dock.
func draggingSession(session: NSDraggingSession, endedAtPoint screenPoint: NSPoint,
operation: NSDragOperation) {
if operation == .Delete {
self.image = nil
}

// Track mouse down events and safe the to the poperty.
override func mouseDown(theEvent: NSEvent) {
self.mouseDownEvent = theEvent
}

// Track mouse down events and safe the to the poperty.
override func mouseDown(theEvent: NSEvent) {
self.mouseDownEvent = theEvent
}

// Track mouse dragged events to handle dragging sessions.
override func mouseDragged(theEvent: NSEvent) {
// Calculate the drag distance...
let mouseDown = self.mouseDownEvent!.locationInWindow
let dragPoint = theEvent.locationInWindow
let dragDistance = hypot(mouseDown.x - dragPoint.x, mouseDown.y - dragPoint.y)

// ...to cancel the dragging session in case of accidental drag.
if dragDistance < 3 {
return
}

// Track mouse dragged events to handle dragging sessions.
override func mouseDragged(theEvent: NSEvent) {
// Calculate the drag distance...
let mouseDown = self.mouseDownEvent!.locationInWindow
let dragPoint = theEvent.locationInWindow
let dragDistance = hypot(mouseDown.x - dragPoint.x, mouseDown.y - dragPoint.y)

// ...to cancel the dragging session in case of accidental drag.
if dragDistance < 3 {
return
}

// Unwrap the image property
if let image = self.image {
// Do some math to properly resize the given image.
let size = NSSize(width: log10(image.size.width) * 30, height: log10(image.size.height) * 30)
let img = image.copyWithSize(size)!

// Create a new NSDraggingItem with the image as content.
let draggingItem = NSDraggingItem(pasteboardWriter: image)
// Calculate the mouseDown location from the window's coordinate system to the
// ImageView's coordinate system, to use it as origin for the dragging frame.
let draggingFrameOrigin = convertPoint(mouseDown, fromView: nil)
// Build the dragging frame and offset it by half the image size on each axis
// to center the mouse cursor within the dragging frame.
let draggingFrame = NSRect(origin: draggingFrameOrigin, size: img.size).offsetBy(dx: -img.size.width / 2, dy: -img.size.height / 2)

// Assign the dragging frame to the draggingFrame property of our dragging item.
draggingItem.draggingFrame = draggingFrame

// Provide the components of the dragging image.
draggingItem.imageComponentsProvider = {
let component = NSDraggingImageComponent(key: NSDraggingImageComponentIconKey)

component.contents = image
component.frame = NSRect(origin: NSPoint(), size: draggingFrame.size)
return [component]
}

// Begin actual dragging session. Woohow!
beginDraggingSessionWithItems([draggingItem], event: mouseDownEvent!, source: self)
}

// Unwrap the image property
if let image = self.image {
// Do some math to properly resize the given image.
let size = NSSize(width: log10(image.size.width) * 30,
height: log10(image.size.height) * 30)
let img = image.copyWithSize(size)!

// Create a new NSDraggingItem with the image as content.
let draggingItem = NSDraggingItem(pasteboardWriter: image)
// Calculate the mouseDown location from the window's coordinate system to the
// ImageView's coordinate system, to use it as origin for the dragging frame.
let draggingFrameOrigin = convertPoint(mouseDown, fromView: nil)
// Build the dragging frame and offset it by half the image size on each axis
// to center the mouse cursor within the dragging frame.
let draggingFrame = NSRect(origin: draggingFrameOrigin, size: img.size)
.offsetBy(dx: -img.size.width / 2, dy: -img.size.height / 2)

// Assign the dragging frame to the draggingFrame property of our dragging item.
draggingItem.draggingFrame = draggingFrame

// Provide the components of the dragging image.
draggingItem.imageComponentsProvider = {
let component = NSDraggingImageComponent(key: NSDraggingImageComponentIconKey)

component.contents = image
component.frame = NSRect(origin: NSPoint(), size: draggingFrame.size)
return [component]
}

// Begin actual dragging session. Woohow!
beginDraggingSessionWithItems([draggingItem], event: mouseDownEvent!, source: self)
}
}
}
Loading

0 comments on commit 48d7d89

Please sign in to comment.