diff --git a/Amethyst/Preferences/GeneralPreferencesViewController.swift b/Amethyst/Preferences/GeneralPreferencesViewController.swift index 52dc7a47..f6e38a6f 100644 --- a/Amethyst/Preferences/GeneralPreferencesViewController.swift +++ b/Amethyst/Preferences/GeneralPreferencesViewController.swift @@ -20,6 +20,8 @@ class GeneralPreferencesViewController: NSViewController, NSTableViewDataSource, layoutsTableView?.dataSource = self layoutsTableView?.delegate = self + layoutsTableView?.registerForDraggedTypes([.string]) + } override func viewWillAppear() { @@ -93,4 +95,43 @@ class GeneralPreferencesViewController: NSViewController, NSTableViewDataSource, return LayoutManager.layoutNameForKey(layoutKeys[row]) } + + + + func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool { + if let dragData = info.draggingPasteboard.data(forType: .string), + let rowString = String(bytes: dragData, encoding: .utf8), + let oldRow = Int(rowString){ + + layoutKeys.move(from: oldRow, to: oldRow < row ? row-1 : row) + UserConfiguration.shared.setLayoutKeys(self.layoutKeys) + layoutsTableView?.reloadData() + return true + } + return false + } + + + + func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation { + if dropOperation == .above { + return .move + } + return [] + + } + + func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? { + let item = NSPasteboardItem() + item.setString(String(row), forType: .string) + return item + } +} + +extension Array{ + mutating func move(from oldIndex: Index, to newIndex: Index) { + if oldIndex == newIndex { return } + if abs(newIndex - oldIndex) == 1 { return self.swapAt(oldIndex, newIndex) } + self.insert(self.remove(at: oldIndex), at: newIndex) + } }