From 03cefc509984a47264eb2e2867db04c0f59c3ae0 Mon Sep 17 00:00:00 2001 From: Charlie Qiu Date: Sat, 9 Jul 2016 15:10:05 +0800 Subject: [PATCH] Support drag & drop reorder the server profiles. --- .../PreferencesWindowController.swift | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/ShadowsocksX-NG/PreferencesWindowController.swift b/ShadowsocksX-NG/PreferencesWindowController.swift index 45fc92a..6cff2ce 100644 --- a/ShadowsocksX-NG/PreferencesWindowController.swift +++ b/ShadowsocksX-NG/PreferencesWindowController.swift @@ -26,6 +26,8 @@ class PreferencesWindowController: NSWindowController @IBOutlet weak var copyURLBtn: NSButton! + let tableViewDragType: String = "ss.server.profile.data" + var defaults: NSUserDefaults! var profileMgr: ServerProfileManager! @@ -56,6 +58,10 @@ class PreferencesWindowController: NSWindowController updateProfileBoxVisible() } + override func awakeFromNib() { + profilesTableView.registerForDraggedTypes([tableViewDragType]) + } + @IBAction func addProfile(sender: NSButton) { if editingProfile != nil && !editingProfile.isValid(){ return @@ -203,6 +209,58 @@ class PreferencesWindowController: NSWindowController return "" } + // Drag & Drop reorder rows + + func tableView(tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? { + let item = NSPasteboardItem() + item.setString(String(row), forType: tableViewDragType) + return item + } + + func tableView(tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int + , proposedDropOperation dropOperation: NSTableViewDropOperation) -> NSDragOperation { + if dropOperation == .Above { + return .Move + } + return .None + } + + func tableView(tableView: NSTableView, acceptDrop info: NSDraggingInfo + , row: Int, dropOperation: NSTableViewDropOperation) -> Bool { + if let mgr = profileMgr { + var oldIndexes = [Int]() + info.enumerateDraggingItemsWithOptions([], forView: tableView, classes: [NSPasteboardItem.self], searchOptions: [:]) { + if let str = ($0.0.item as! NSPasteboardItem).stringForType(self.tableViewDragType), index = Int(str) { + oldIndexes.append(index) + } + } + + var oldIndexOffset = 0 + var newIndexOffset = 0 + + // For simplicity, the code below uses `tableView.moveRowAtIndex` to move rows around directly. + // You may want to move rows in your content array and then call `tableView.reloadData()` instead. + tableView.beginUpdates() + for oldIndex in oldIndexes { + if oldIndex < row { + let o = mgr.profiles.removeAtIndex(oldIndex + oldIndexOffset) + mgr.profiles.insert(o, atIndex:row - 1) + tableView.moveRowAtIndex(oldIndex + oldIndexOffset, toIndex: row - 1) + oldIndexOffset -= 1 + } else { + let o = mgr.profiles.removeAtIndex(oldIndex) + mgr.profiles.insert(o, atIndex:row + newIndexOffset) + tableView.moveRowAtIndex(oldIndex, toIndex: row + newIndexOffset) + newIndexOffset += 1 + } + } + tableView.endUpdates() + + return true + } + return false + } + //-------------------------------------------------- // For NSTableViewDelegate