Skip to content

Commit

Permalink
fixed #2 - added support for drag on dock icon to open image
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkaessner committed Oct 12, 2018
1 parent afba282 commit 68c6c38
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Touch Bar Preview/Touch Bar Preview/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
return true
}

func application(_ sender: NSApplication, openFile filename: String) -> Bool {

NotificationCenter.default.post(name: Notification.Name("dropFileOnDock"), object: filename)

return true
}

// MARK: - Menu Links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
<constraint firstItem="5Ir-lx-pbs" firstAttribute="centerX" secondItem="Bhq-fs-c55" secondAttribute="centerX" id="AWz-Mq-NC4"/>
</constraints>
<connections>
<outlet property="dropIconView" destination="5Ir-lx-pbs" id="Ii1-YF-fW7"/>
<outlet property="dropIconView" destination="5Ir-lx-pbs" id="jJP-g2-eaP"/>
</connections>
</customView>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="SjC-ae-fzs" userLabel="Bottom Window Bar">
Expand Down Expand Up @@ -442,6 +442,7 @@
<outlet property="bottomBarAlertImageWidth" destination="7C4-oz-oBo" id="iSa-eg-Kfr"/>
<outlet property="bottomBarInfoLable" destination="If8-xj-QtX" id="2hI-Hy-arD"/>
<outlet property="dropDestinationView" destination="Bhq-fs-c55" id="TNP-Ar-82b"/>
<outlet property="dropIconView" destination="5Ir-lx-pbs" id="zBP-g6-SOt"/>
<outlet property="imagePreviewView" destination="b3k-xi-eRB" id="STQ-cU-HTl"/>
</connections>
</viewController>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class DropDestinationView: NSView {

func setup() {
self.registerForDraggedTypes([NSPasteboard.PasteboardType(kUTTypeURL as String)])

NotificationCenter.default.addObserver(self, selector: #selector(hideDragAndDropIcon), name: NSNotification.Name("hideDragAndDropIcon"), object: nil)
}


Expand Down Expand Up @@ -144,7 +146,7 @@ class DropDestinationView: NSView {
isReceivingDrag = false

// hide the drop icon as we don't need this anymore
dropIconView.isHidden = true
hideDragAndDropIcon()

let pasteBoard = draggingInfo.draggingPasteboard()

Expand All @@ -157,4 +159,8 @@ class DropDestinationView: NSView {

}

@objc func hideDragAndDropIcon() {
dropIconView.isHidden = true
}

}
16 changes: 16 additions & 0 deletions Touch Bar Preview/Touch Bar Preview/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Images</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
<string>public.png</string>
</array>
</dict>
</array>
</dict>
</plist>
18 changes: 18 additions & 0 deletions Touch Bar Preview/Touch Bar Preview/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,31 @@ class ViewController: NSViewController {

// "hide" alert icon in bottom bar
bottomBarAlertImageWidth.constant = 0.0

NotificationCenter.default.addObserver(self, selector: #selector(handleDockIconDrop), name: NSNotification.Name("dropFileOnDock"), object: nil)
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}

@objc func handleDockIconDrop(notification: Notification) {

let fileName = notification.object as! String
let urlString = fileName.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? fileName
let url = URL(string: "file://\(urlString)")

if (url != nil) {
processImageURLs([url!])

// hide the drag and drop icon
NotificationCenter.default.post(name: NSNotification.Name("hideDragAndDropIcon"), object: nil)
}else {
print("could not import image from icon drag")
}
}

}

Expand Down

0 comments on commit 68c6c38

Please sign in to comment.