Last active
March 22, 2019 14:10
-
-
Save helje5/48728983951ab3362af43b967c554475 to your computer and use it in GitHub Desktop.
Dock gets stuck on drag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
@NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
@IBOutlet weak var window : NSWindow! | |
@IBOutlet weak var tableView : NSTableView! | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.allowsEmptySelection = true | |
tableView.allowsColumnResizing = false | |
tableView.allowsMultipleSelection = false | |
tableView.allowsColumnSelection = false | |
tableView.gridStyleMask = [] | |
tableView.gridColor = .clear | |
// This is required to make the labels selectable! | |
tableView.selectionHighlightStyle = .none | |
tableView.columnAutoresizingStyle = .firstColumnOnlyAutoresizingStyle | |
let tc = tableView.tableColumns[0] | |
tc.isEditable = false | |
tc.resizingMask = .autoresizingMask | |
} | |
} | |
let viewID = NSUserInterfaceItemIdentifier(rawValue: "run-view") | |
final class MyTextField : NSTextField { | |
#if false | |
override func draw(_ dirtyRect: NSRect) { | |
// Just doing this makes it work | |
super.draw(dirtyRect) | |
} | |
#endif | |
} | |
extension AppDelegate : NSTableViewDelegate { | |
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { | |
return 32 | |
} | |
func tableView(_ tv: NSTableView, viewFor tc: NSTableColumn?, row: Int) | |
-> NSView? | |
{ | |
let v = (tv.makeView(withIdentifier: viewID, owner: nil) as? NSTextField) | |
?? MyTextField() | |
v.isSelectable = false | |
v.isEditable = false | |
v.stringValue = data[row] | |
v.identifier = viewID | |
return v | |
} | |
func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) | |
-> NSPasteboardWriting? | |
{ | |
return MyPasteboardItem(value: data[row]) | |
} | |
} | |
final class MyPasteboardItem : NSObject, NSPasteboardWriting { | |
let value : String | |
init(value: String) { self.value = value } | |
func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] { | |
return [ .myOwnType, .string ] | |
} | |
func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? { | |
switch type { | |
case .string: return value | |
case .myOwnType: return [ "value": value ] | |
default: return nil | |
} | |
} | |
} | |
extension NSPasteboard.PasteboardType { | |
static let myOwnType = | |
NSPasteboard.PasteboardType(rawValue: "de.zeezide.own-type") | |
} | |
extension AppDelegate : NSTableViewDataSource { | |
func numberOfRows(in tableView: NSTableView) -> Int { return data.count } | |
} | |
fileprivate let data = [ | |
"Hello", | |
"World", | |
"Blub" | |
] |
The solution for the "Dock-hang" is to call setDraggingSourceOperationMask
:
// THIS FIXES IT!
tableView.setDraggingSourceOperationMask([.copy], forLocal: false)
tableView.setDraggingSourceOperationMask([.link], forLocal: true)
The solution for the drawing issue is:
final class MyTextField : NSTextField {
override func draw(_ dirtyRect: NSRect) { // Just doing this makes it work
super.draw(dirtyRect)
}
}
(though presumably that is suboptimal)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The xib is just the default MainMenu.xib with a plain tableview added to the default window (and hooked up to the AppDelegate outlet).
(this also demos the "white label" issue due to the
NSTextLayer
not being captured by NSTableView)