1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 11:35:35 +03:00

GH-296 Implement some more drops

This commit is contained in:
Tae Won Ha 2016-11-17 22:13:31 +01:00
parent 177a57f9e9
commit cf9fd3796c
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 65 additions and 1 deletions

View File

@ -145,7 +145,7 @@ extension Workspace {
let openToolAfterwards = tool.isSelected
self.bars[tool.location]?.remove(tool: tool)
tool.bar?.remove(tool: tool)
self.bars[barLoc]?.append(tool: tool)
if openToolAfterwards {

View File

@ -108,6 +108,7 @@ class WorkspaceBar: NSView, WorkspaceToolDelegate {
}
func append(tool: WorkspaceTool) {
tool.bar = self
tool.delegate = self
tool.location = self.location
self.tools.append(tool)
@ -120,11 +121,26 @@ class WorkspaceBar: NSView, WorkspaceToolDelegate {
self.relayout()
}
func insert(tool: WorkspaceTool, at idx: Int) {
tool.bar = self
tool.delegate = self
tool.location = self.location
self.tools.insert(tool, at: idx)
if self.isOpen {
self.selectedTool?.isSelected = false
self.selectedTool = tool
}
self.relayout()
}
func remove(tool: WorkspaceTool) {
guard let idx = self.tools.index(of: tool) else {
return
}
tool.bar = nil
tool.delegate = nil
self.tools.remove(at: idx)
@ -196,6 +212,53 @@ extension WorkspaceBar {
self.endDrag()
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let toolButton = sender.draggingSource() as? WorkspaceToolButton else {
return false
}
guard let tool = toolButton.tool else {
return false
}
guard let draggedOnToolIdx = self.draggedOnToolIdx else {
// This means:
// 1. the dragged tool is from this bar and is dropped at the same spot
// 2. the dragged tool is from this bar and is dropped at the end of the bar
// 3. the dragged tool is dropped at the end of the bar
guard self.tools.filter({ $0.uuid == tool.uuid }).isEmpty else {
// 1 and 2
NSLog("doing nothing")
return false
}
// 3
NSLog("append at the end")
return false
}
// If we are here, the dragged tool is dropped somewhere in the middle and
// 1. is not from this bar
// 2. is from this bar
guard let toolIdx = self.tools.index(of: tool) else {
// 1.
tool.bar?.remove(tool: tool)
self.insert(tool: tool, at: draggedOnToolIdx)
return true
}
// 2.
if draggedOnToolIdx > toolIdx {
(toolIdx..<draggedOnToolIdx).forEach { swap(&self.tools[$0], &self.tools[$0 + 1]) }
} else {
(draggedOnToolIdx..<toolIdx).reversed().forEach { swap(&self.tools[$0 + 1], &self.tools[$0]) }
}
return true
}
fileprivate func endDrag() {
self.isDragOngoing = false
self.draggedOnToolIdx = nil

View File

@ -41,6 +41,7 @@ class WorkspaceTool: Hashable {
}
weak var delegate: WorkspaceToolDelegate?
weak var bar: WorkspaceBar?
let minimumDimension: CGFloat
var dimension: CGFloat