1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 00:34:26 +03:00

GH-296 Detect drag on buttons

This commit is contained in:
Tae Won Ha 2016-11-16 21:06:23 +01:00
parent c3679c9503
commit dfdb3fb91b
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 78 additions and 6 deletions

View File

@ -40,6 +40,9 @@ class Workspace: NSView, WorkspaceBarDelegate {
fileprivate var tools = [WorkspaceTool]()
fileprivate var isDragOngoing = false
fileprivate var draggedOnBarLocation: WorkspaceBarLocation?
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@ -93,22 +96,28 @@ class Workspace: NSView, WorkspaceBarDelegate {
extension Workspace {
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
// NSLog("\(#function): \(sender.draggingSource())")
self.isDragOngoing = true
return .move
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
let loc = self.convert(sender.draggingLocation(), from: nil)
if let barLoc = self.bar(inLocation: loc) {
NSLog("\(barLoc)")
}
self.draggedOnBarLocation = self.bar(inLocation: loc)
return .move
}
override func draggingExited(_ sender: NSDraggingInfo?) {
self.endDrag()
}
override func draggingEnded(_ sender: NSDraggingInfo?) {
// NSLog("\(#function): \(sender)")
self.endDrag()
}
fileprivate func endDrag() {
self.isDragOngoing = false
self.draggedOnBarLocation = nil
}
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {

View File

@ -25,6 +25,9 @@ class WorkspaceBar: NSView, WorkspaceToolDelegate {
fileprivate var layoutConstraints = [NSLayoutConstraint]()
fileprivate var isDragOngoing = false
fileprivate var draggedOnBarButton: WorkspaceToolButton?
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@ -51,6 +54,8 @@ class WorkspaceBar: NSView, WorkspaceToolDelegate {
super.init(frame: CGRect.zero)
self.configureForAutoLayout()
self.register(forDraggedTypes: [WorkspaceToolButton.toolUti])
self.wantsLayer = true
self.layer!.backgroundColor = NSColor.windowBackgroundColor.cgColor
}
@ -115,6 +120,54 @@ class WorkspaceBar: NSView, WorkspaceToolDelegate {
}
}
// MARK: - NSDraggingDestination
extension WorkspaceBar {
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
self.isDragOngoing = true
return .move
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
let loc = self.convert(sender.draggingLocation(), from: nil)
let currentDraggedOnButton = self.tools
.map { $0.button }
.reduce(nil) { (result, button) -> WorkspaceToolButton? in
if result != nil {
return result
}
if button.frame.contains(loc) {
return button
}
return nil
}
if currentDraggedOnButton == self.draggedOnBarButton {
return .move
}
self.draggedOnBarButton = currentDraggedOnButton
self.relayout()
return .move
}
override func draggingEnded(_ sender: NSDraggingInfo?) {
self.endDrag()
}
override func draggingExited(_ sender: NSDraggingInfo?) {
self.endDrag()
}
fileprivate func endDrag() {
self.isDragOngoing = false
self.draggedOnBarButton = nil
}
}
// MARK: - NSView
extension WorkspaceBar {
@ -446,6 +499,8 @@ extension WorkspaceBar {
}
fileprivate func layoutButtons() {
NSLog("\(#function)")
guard let firstTool = self.tools.first else {
return
}

View File

@ -20,6 +20,14 @@ class WorkspaceToolButton: NSView, NSDraggingSource, NSPasteboardItemDataProvide
// MARK: - API
static let toolUti = "com.qvacua.vimr.tool"
static func ==(left: WorkspaceToolButton, right: WorkspaceToolButton) -> Bool {
guard let leftTool = left.tool, let rightTool = right.tool else {
return false
}
return leftTool == rightTool
}
var location = WorkspaceBarLocation.top
var isSelected: Bool {
return self.tool?.isSelected ?? false