Skip to content

Commit

Permalink
CanvasView: optimize rendering (#64)
Browse files Browse the repository at this point in the history
- Only re-draw elements that are inside the dirtyRect.

  previously for each dirty rect I re-rendered the whole set of concepts.

- Mark view as opaque

  so the OS doesn’t need to render views under.
  • Loading branch information
fespinoza authored Jul 12, 2017
1 parent ab1120b commit e0793b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
23 changes: 14 additions & 9 deletions LinkedIdeas/CanvasView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import Cocoa

public protocol CanvasViewDataSource {
var drawableElements: [DrawableElement] { get }

func drawableElements(forRect: NSRect) -> [DrawableElement]
}

public class CanvasView: NSView {

public override var isOpaque: Bool { return true }
public var dataSource: CanvasViewDataSource?

var selectFromPoint: NSPoint?
Expand All @@ -34,20 +37,23 @@ public class CanvasView: NSView {

override public func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
drawBackground()
drawBackground(dirtyRect)
drawElements(inRect: dirtyRect)
drawSelectionRect()
drawLinkConstructionArrow()
}

if let dataSource = dataSource {
for element in dataSource.drawableElements { element.draw() }
func drawElements(inRect dirtyRect: NSRect) {
guard let dataSource = dataSource else {
return
}

drawSelectionRect()

drawLinkConstructionArrow()
dataSource.drawableElements(forRect: dirtyRect).forEach({ $0.draw() })
}

func drawBackground() {
func drawBackground(_ dirtyRect: NSRect) {
NSColor.white.set()
NSRectFill(bounds)
NSRectFill(dirtyRect)
}

func drawSelectionRect() {
Expand All @@ -57,7 +63,6 @@ public class CanvasView: NSView {

let borderColor = NSColor(red: 0, green: 0, blue: 1, alpha: 1)
let backgroundColor = NSColor(red: 0, green: 0, blue: 1, alpha: 0.5)

let bezierPath = NSBezierPath(rect: selectionRect)

borderColor.set()
Expand Down
4 changes: 4 additions & 0 deletions LinkedIdeas/CanvasViewController+CanvasViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ extension CanvasViewController: CanvasViewDataSource {

return elements
}

func drawableElements(forRect containerRect: NSRect) -> [DrawableElement] {
return drawableElements.filter({ containerRect.intersects($0.drawingBounds) })
}
}
2 changes: 1 addition & 1 deletion LinkedIdeas/CanvasViewController+DraggingActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ extension CanvasViewController {
dragCount = 0
canvasView.selectFromPoint = nil
canvasView.selectToPoint = nil
canvasView.needsDisplay = true
reRenderCanvasView()
}
}
4 changes: 4 additions & 0 deletions LinkedIdeas/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ extension Document: LinkedIdeasDocument {
}

extension Document: CanvasViewDataSource {
public func drawableElements(forRect: NSRect) -> [DrawableElement] {
return drawableElements
}

public var drawableElements: [DrawableElement] {
var elements: [DrawableElement] = []

Expand Down

0 comments on commit e0793b8

Please sign in to comment.