Skip to content

Instantly share code, notes, and snippets.

@helje5
Last active March 22, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helje5/48728983951ab3362af43b967c554475 to your computer and use it in GitHub Desktop.
Save helje5/48728983951ab3362af43b967c554475 to your computer and use it in GitHub Desktop.
Dock gets stuck on drag
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"
]
@helje5
Copy link
Author

helje5 commented Mar 20, 2019

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)

@helje5
Copy link
Author

helje5 commented Mar 22, 2019

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