Skip to content

Commit

Permalink
rearrange the list of layouts #887
Browse files Browse the repository at this point in the history
  • Loading branch information
evergreenCode authored and ianyh committed Feb 8, 2020
1 parent 73b44b2 commit e809a0a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Amethyst/Preferences/GeneralPreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class GeneralPreferencesViewController: NSViewController, NSTableViewDataSource,

layoutsTableView?.dataSource = self
layoutsTableView?.delegate = self
layoutsTableView?.registerForDraggedTypes([.string])

}

override func viewWillAppear() {
Expand Down Expand Up @@ -93,4 +95,43 @@ class GeneralPreferencesViewController: NSViewController, NSTableViewDataSource,

return LayoutManager<AXWindow>.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)
}
}

0 comments on commit e809a0a

Please sign in to comment.