Skip to content

Instantly share code, notes, and snippets.

@eonil
Last active October 31, 2021 12:35
Show Gist options
  • Save eonil/1aa12ecacf1a4b2566fe to your computer and use it in GitHub Desktop.
Save eonil/1aa12ecacf1a4b2566fe to your computer and use it in GitHub Desktop.
Cocoa Note

Cocoa Note (including AppKit)

  • NSLayoutManager.setTemporaryAttributes(:forCharacterRange:) is far faster than NSTextStorage.setAttributes(:range:). On My iMac, former took about 2 seconds, and latter took about 17 seconds in optimised build of test suit.

  • Anyway, modifying NSTextStorage also took 2 seconds when wrapped by beginEditing/endEditing pair. So this performance drop is mainly due to inefficient layout refresh. You will get same performance if you can suppress the layout refreshes.

NSWindow

  • You should not kill the window object while func windowWillClose(notification: NSNotification) is being called. It will cause program crash. Just keep it alive. You can try to remove some subviews from it to clean up stuffs.

NSTableView

  • If you want a cell (including view-based table-view, NSTableCellView in view-based table-view) to be edited with a small delay after selecting it (just like you're renaming a file in Finder), use this code.

      	outlineView.doubleAction			=	"dummyDoubleAction:"
    

This makes the table-view to wait for double-clicking timing, and the small delay will be introduced effectively.

  • If you want to get notified when the node label editing starts, see here.

  • NSTableView.sizeLastColumnToFit doesn seem to work if there's no row. Try applying this after you add some row.

NSTextField

  • How to clear focus ring?

    let	tf	=	getTextField() as NSTextField
    tf.window!.makeFirstResponder(nil)
    
  • How to clear focus ring with Escape key?

    @objc
    override func cancelOperation(sender: AnyObject?) {
    	self.window!.makeFirstResponder(nil)
    }
    

System Icons

OS X provides a lot of default icons. These icons are accessible via several methods.

Here're related informations.

Typographics

Drag & Drop

Note that drag-drop support could be overriden and can be different by view classes. For example, NSOutlineView uses completely different approach to support per-item drag-and-drop.

  • Extremly simple example for bare bone NSViews

  • NSOutlineView

    You should start with implementing this data-source method at first.

    @objc
    func outlineView(outlineView: NSOutlineView, pasteboardWriterForItem item: AnyObject?) -> NSPasteboardWriting! {
    	let	m	=	NSPasteboardItem()
    	return	m
    }
    

    You'll see the outlne-view will drag nodes immediately. Now start to add extra supports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment