I'm trying to use NSView's getRectsBeingDrawn
and I'm not too sure as to how to cast the vars that it needs.
The definition of it is:
func getRectsBeingDrawn(rects: UnsafeMutablePointer<UnsafePointer<NSRect>>, count: UnsafeMutablePointer<Int>)
I've defined both rects
and count
as:
var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = nil
var count: UnsafeMutablePointer<Int> = nil
which seems to work. The debugger doesn't come up with anything handy for seeing what's at the pointers so that's no help.
rect
should point to an array of NSRect after getRectsBeingDrawn
is called, but it seems to return nothing. Also how do I get the array and int which rects and count are pointing to?
I'm trying to get a piece of code from the Apple sample "TableViewPlayground" to work in swift.
- (void)drawSelectionInRect:(NSRect)dirtyRect {
NSColor *primaryColor = [[NSColor alternateSelectedControlColor] colorWithAlphaComponent:0.5];
NSColor *secondarySelectedControlColor = [[NSColor secondarySelectedControlColor] colorWithAlphaComponent:0.5];
// Implement our own custom alpha drawing
switch (self.selectionHighlightStyle) {
case NSTableViewSelectionHighlightStyleRegular: {
if (self.selected) {
if (self.emphasized) {
[primaryColor set];
} else {
[secondarySelectedControlColor set];
}
NSRect bounds = self.bounds;
const NSRect *rects = NULL;
NSInteger count = 0;
[self getRectsBeingDrawn:&rects count:&count];
for (NSInteger i = 0; i < count; i++) {
NSRect rect = NSIntersectionRect(bounds, rects[i]);
NSRectFillUsingOperation(rect, NSCompositeSourceOver);
}
}
break;
}
default: {
// Do super's drawing
[super drawSelectionInRect:dirtyRect];
break;
}
}
}
I've managed to suss this out... I wasn't allocating the memory properly for rects
and count
What I had was:
var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = nil
var count: UnsafeMutablePointer<Int> = nil
What ultimately fixed the issue was:
var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1)
var count: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.alloc(1)
Hope this helps someone else :)
edit If anyone is interested, here's what the swift code for the above turned out to be:
override func drawSelectionInRect(dirtyRect: NSRect) {
var primaryColor = NSColor.alternateSelectedControlColor().colorWithAlphaComponent(0.5)
var secondarySelectedControlColor = NSColor.secondarySelectedControlColor().colorWithAlphaComponent(0.5)
switch (self.selectionHighlightStyle){
case .Regular:
if (self.selected){
if self.emphasized{
primaryColor.set()
}else{
secondarySelectedControlColor.set()
}
let bounds = self.bounds
var rects: UnsafeMutablePointer<UnsafePointer<NSRect>> = UnsafeMutablePointer<UnsafePointer<NSRect>>.alloc(1)
var count: UnsafeMutablePointer<Int> = UnsafeMutablePointer<Int>.alloc(1)
self.getRectsBeingDrawn(rects, count: count)
let rectArrayPtr = UnsafeMutableBufferPointer(start: rects, count: count.memory)
var base = rectArrayPtr.baseAddress
for var i = 0; i < count.memory; i++ {
var currentRect = base.memory
let rect = NSIntersectionRect(bounds, currentRect.memory)
NSRectFillUsingOperation(rect, .CompositeSourceOver)
base.successor()
}
}
default:
super.drawSelectionInRect(dirtyRect)
}
}
edit
As per a further comment, the same has been achieved here with a less verbose approach (thanks to Paul Patterson below):
http://stackoverflow.com/questions/32536855/using-getrectsbeingdrawn-with-swift
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments