Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement