1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Add convenience initializer to NSPasteboardItem

This commit is contained in:
Daniel Barber 2019-06-28 14:58:38 -04:00
parent 1af2c939a2
commit dc60049243
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
4 changed files with 50 additions and 29 deletions

View File

@ -40,6 +40,7 @@
E435E3E2221CD4E200184CFC /* NSFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = E435E3E1221CD4E200184CFC /* NSFont.swift */; };
E435E3E4221CD75D00184CFC /* NSImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E435E3E3221CD75D00184CFC /* NSImage.swift */; };
E439109822640213002982E9 /* SongNotifierService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E439109722640213002982E9 /* SongNotifierService.swift */; };
E43AC1F122C68E6A001E483C /* NSPasteboardItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43AC1F022C68E6A001E483C /* NSPasteboardItem.swift */; };
E43B67AA22909793007DCF55 /* AlbumDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43B67A822909793007DCF55 /* AlbumDetailView.swift */; };
E43B67AB22909793007DCF55 /* AlbumDetailView.xib in Resources */ = {isa = PBXBuildFile; fileRef = E43B67A922909793007DCF55 /* AlbumDetailView.xib */; };
E43B67AD229194CD007DCF55 /* AlbumTracksDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E43B67AC229194CD007DCF55 /* AlbumTracksDataSource.swift */; };
@ -260,6 +261,7 @@
E435E3E1221CD4E200184CFC /* NSFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSFont.swift; sourceTree = "<group>"; };
E435E3E3221CD75D00184CFC /* NSImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSImage.swift; sourceTree = "<group>"; };
E439109722640213002982E9 /* SongNotifierService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongNotifierService.swift; sourceTree = "<group>"; };
E43AC1F022C68E6A001E483C /* NSPasteboardItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSPasteboardItem.swift; sourceTree = "<group>"; };
E43B67A822909793007DCF55 /* AlbumDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumDetailView.swift; sourceTree = "<group>"; };
E43B67A922909793007DCF55 /* AlbumDetailView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AlbumDetailView.xib; sourceTree = "<group>"; };
E43B67AC229194CD007DCF55 /* AlbumTracksDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumTracksDataSource.swift; sourceTree = "<group>"; };
@ -460,6 +462,7 @@
E408D3B8220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift */,
E489E39822B85D0400CA8CBD /* NSPasteboard.swift */,
E489E39C22B9CF0000CA8CBD /* NSView.swift */,
E43AC1F022C68E6A001E483C /* NSPasteboardItem.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -979,6 +982,7 @@
E4B11B61226A4C000075461B /* PlayerReducer.swift in Sources */,
E4FF71922276029000D4C412 /* PreferencesActions.swift in Sources */,
E489E39922B85D0400CA8CBD /* NSPasteboard.swift in Sources */,
E43AC1F122C68E6A001E483C /* NSPasteboardItem.swift in Sources */,
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */,
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */,
E47E2FDD2220A6D100F747E6 /* Time.swift in Sources */,

View File

@ -45,20 +45,14 @@ class AlbumTracksDataSource: NSObject, NSTableViewDataSource {
guard let song = albumSongItem.song
else { return nil }
let pasteboardItem = NSPasteboardItem()
let draggedSong = DraggedSong(
type: .albumSongItem(song.mpdSong.uriString),
title: song.title,
artist: song.artist
return NSPasteboardItem(
draggedSong: DraggedSong(
type: .albumSongItem(song.mpdSong.uriString),
title: song.title,
artist: song.artist
),
ofType: .songPasteboardType
)
let encoder = PropertyListEncoder()
let data = try! encoder.encode(draggedSong)
pasteboardItem.setData(data, forType: .songPasteboardType)
return pasteboardItem
}
func numberOfRows(in tableView: NSTableView) -> Int {
@ -73,8 +67,7 @@ class AlbumTracksDataSource: NSObject, NSTableViewDataSource {
searchOptions: [:]
) { draggingItem, index, stop in
guard let item = draggingItem.item as? NSPasteboardItem,
let data = item.data(forType: .songPasteboardType),
let draggedSong = try? PropertyListDecoder().decode(DraggedSong.self, from: data),
let draggedSong = item.draggedSong(forType: .songPasteboardType),
case let (title?, artist?) = (draggedSong.title, draggedSong.artist)
else { return }

View File

@ -43,19 +43,14 @@ class QueueDataSource: NSObject, NSOutlineViewDataSource {
guard let queueItem = item as? QueueItem
else { return nil }
let pasteboardItem = NSPasteboardItem()
let draggedSong = DraggedSong(
type: .queueItem(queueItem.queuePos),
title: queueItem.song.title,
artist: queueItem.song.artist
return NSPasteboardItem(
draggedSong: DraggedSong(
type: .queueItem(queueItem.queuePos),
title: queueItem.song.title,
artist: queueItem.song.artist
),
ofType: .songPasteboardType
)
let data = try! PropertyListEncoder().encode(draggedSong)
pasteboardItem.setData(data, forType: .songPasteboardType)
return pasteboardItem
}
func outlineView(_ outlineView: NSOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
@ -64,8 +59,8 @@ class QueueDataSource: NSObject, NSOutlineViewDataSource {
guard newQueuePos >= 0,
let draggingTypes = info.draggingPasteboard.types,
draggingTypes.contains(.songPasteboardType),
let data = info.draggingPasteboard.data(forType: .songPasteboardType),
let draggedSong = try? PropertyListDecoder().decode(DraggedSong.self, from: data)
let pasteboardItem = info.draggingPasteboard.pasteboardItems?.first,
let draggedSong = pasteboardItem.draggedSong(forType: .songPasteboardType)
else { return [] }
switch draggedSong.type {

View File

@ -0,0 +1,29 @@
//
// NSPasteboardItem.swift
// Persephone
//
// Created by Daniel Barber on 2019/6/28.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import AppKit
extension NSPasteboardItem {
convenience init(draggedSong: DraggedSong, ofType type: NSPasteboard.PasteboardType) {
self.init()
self.setDraggedSong(draggedSong, forType: type)
}
func setDraggedSong(_ draggedSong: DraggedSong, forType type: NSPasteboard.PasteboardType) {
let data = try! PropertyListEncoder().encode(draggedSong)
setData(data, forType: type)
}
func draggedSong(forType type: NSPasteboard.PasteboardType) -> DraggedSong? {
guard let itemData = data(forType: type)
else { return nil }
return try? PropertyListDecoder().decode(DraggedSong.self, from: itemData)
}
}