NSWindow: alternative to -setOpaque:NO - objective-c

I want to have a window which is like QuickTime X window. An all opaque window with rounded corners.
I've obtained it implementing a custom borderless NSWindow with:
[window setOpaque:NO];
[window setBackgroundColor: [NSColor clearColor]];
and a custom NSView with:
- (void)drawRect:(NSRect)rect
{
NSBezierPath* thePath = [NSBezierPath bezierPath];
[thePath appendBezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[thePath fill];
}
It works as expected but the window becomes noticeably slow when it gets resized fast.
I've identified that this slowdown is given by -setOpaque:NO; if I remove that line, the window can be resized fast again but corners are obviously no more rounded.
Is there a way to avoid using -setOpaque:NO and still be able to have rounded corners? Maybe one can have a window which is all opaque except for the corners?
The view is a NSOpenGLView so I can leverage on OpenGL if it may helps.

See this Apple developer example: https://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html

In Quartz/Core Graphics, opaque rects are faster to composite than non-opaque rects.
But an opaque window means you need to draw a rectangle and anything you didn't fill would be black.
If you want a custom window that is not a sharp-edged rectangle, you must set the window to be non-opaque.
Anyway, this is almost certainly, not your main bottleneck.
First thing is to stop doing so much stuff in drawRect:
Make that NSBezierPath a property.
Only replace it when the rect is actually changing size.
Only do that in viewWillDraw: or some earlier point.
Maybe one of the view or window resize NSNotifications or a delegate method.
In drawRect: you should do only drawing really, as much as possible.
You should only redraw what you need to, as much as possible.
This is still small, and probably not your bottleneck.
You'll need to examine the drawing in ALL of the views in your window.
Your window is really the root CGContext (drawing context) that you get access to on OS X. Everything in your context is all of your subviews.
Every subview is a potential drawing bottleneck.
An OpenGL view drawing during live resize of the window sounds like a prime candidate.
You might throttle frame rates or something during live resize of the view or the window.
That should get it a little snappier.
Beyond that, you'll notice from NSWindow and NSView classes that not drawing during live resize IS a performance win.
See the NSView Class documentation and specifically the methods noted under the section Managing Live Resize
inLiveResize
preservesContentDuringLiveResize
getRectsExposedDuringLiveResize:count:
rectPreservedDuringLiveResize
viewWillStartLiveResize
viewDidEndLiveResize
Those last two look like a great place to sandwich some reduction in drawing,

Try using the setAlphaValue method of NSWindow.

Related

Identifying correct window frame size for filling background color

I am developing in Cocoa, and I am currently having problems with filling the background of a NSWindowController.
I understand that subclassing is the way forward if you want to customise your cocoa app. So I created a custom NSView named whiteView and added this view as a subview to my windowController's contentView; however, there are some issues with completely filling the background of the window. Can anyone explain how I can have the color cover the complete surface area of the window's frame pls. Thank you
These are the results that I have so far.
1) This is the window when I leave it as it is, notice the white color only having covered half of the window.
2)Here is the same window again when I adjust the window far to the right and bottom. The white screen seems to stretch enough so that it covers the elements.
This is how I create the custom view
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
[[NSColor whiteColor] set];
NSRectFill([self bounds]);
}
And this how I achieve plaster the view onto my window.
WhiteView *whiteBackgroundView = [[WhiteView alloc] initWithFrame:self.window.frame];
[self.window.contentView addSubview:whiteBackgroundView positioned:NSWindowBelow relativeTo:self.window.contentView];
What do I need to do to correctly allow for my window's background to be fully covered in white?
First, the simple solution is to use -[NSWindow setBackgroundColor:] to just set the window's background color. No need for a view.
If you're still interested in how to fix the view-based approach, probably what's wrong is that you haven't set the autoresizing mask of the view to make it follow the changes in the window size. For example, you could do [whiteBackgroundView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable].
However, you could also set the whiteBackgroundView as the window's contentView rather than as a subview of it. The window's content view is always kept at the size necessary to fill the window's content rect. All of the other views of your window would be subviews of the white background view. In my opinion, this is better than making it a sibling that just happens to be at the back. Using relative ordering among siblings views to achieve a particular rendering order is a hack.
Finally, there's no reason to invoke super's implementation in your -drawRect: if the superclass is NSView itself. NSView doesn't do any drawing in its -drawRect:. Also, your subclass takes over full responsibility for the entire drawn contents of its bounds, so you'd overdraw whatever super had drawn, anyway. (Also, you need only fill dirtyRect rather than [self bounds].)
While you're at it, since your class fills its bounds, you should override -isOpaque to return YES for optimization.
Update: regarding the frame of the view: if it's not going to be the window's content view, then you want to set its frame to be its prospective superview's bounds. So, you should have used self.window.contentView.bounds if you wanted whiteBackgroundView to fill the content view.
More generally, if you want the content rect of a window, you would do [window contentRectForFrameRect:window.frame]. But if a view is going to be a window's content view, there's no need to set its frame to anything in particular. It will be resized automatically.
Update 2:
To transfer the view hierarchy from the original content view to the new content view (when you're making the white background view the content view):
NSArray* subviews = [self.window.contentView.subviews copy];
[subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[whiteBackgroundView setSubviews:subviews];
[subviews release];
(Written for manual retain-release. If using ARC, just drop the -release invocation.)
Regarding the frame to use, as mentioned in the first update: keep in mind that the view's frame should be expressed in the coordinate system of its superview. So, as I said, self.window.contentView.bounds would work if you're putting the new view into the content view. The window's frame and content rect are in screen coordinates. They would be completely incorrect for positioning a view.

How to draw half-transparent NSTableRowView?

I want to customize drawing of floating group row background and can't do it. Basically I want a partially transparent background (which always keeps the same look when scrolling or not), but something alters the appearance of my subclass of NSTableRowView when the table view is not scrolling (though when scrolling it looks as intended).
Here's the code and the image explaining the issue.
- (void)drawBackgroundInRect:(NSRect)dirtyRect
{
[[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set];
[[NSBezierPath bezierPathWithRect:dirtyRect] fill];
}
For table cell view I tried to use NSTableCellView, NSView and simple NSTextField (this is the one you see on the image), but the result is always the same.
I was able to fix it by turning on Core Animation layer on the header cell view and its table view.
If you're not doing anything other than filling the background of the row with a color, you should be setting the backgroundColor property of NSTableRowView instead of overriding -drawBackgroundInRect:.
If you do need this override for further customization, I noticed that you're forgetting to call [super drawBackgroundInRect:dirtyRect] in your implementation, which is likely responsible for the behaviour you're seeing.

NSWindow: place part of image outside window

I've a Cocoa application with a NSWindow with the style NSBorderlessWindowMask (without titlebar). I would like to place a image in the window but a part of the image should be places outside of the window.
How can this be done?
Here are two articles I found related to what you want to do.
Cocoa With Love Example
parmanoir.com example
The gist is to subclass NSWindow to make it a borderless transparent window, then make a sub view that draws your custom shape and make it the windows content view.
From the look of the sample the shadow should still apply.
From Cocoa With Love:
The shadow behind the window is drawn automatically for whatever shape we draw. Any part of the window that is left completely clear will not receive mouse clicks (they will fall through the window).
to draw the border do something like this in your view class display method:
NSBezierPath* border = [NSBezierPath bezierPathWithRect:self.frame];
[border setLineWidth: 1.0];
[[NSColor windowFrameColor] set];
[border stroke];
If you don't have a custom view class then do [view lockFocus]; before doing that path and replace self with your view instance. after drawing be sure to do [view unlockFocus];
An important message from the docs concerning lockFocus:
Hiding or miniaturizing a one-shot window causes the backing store for that window to be released. If you don’t use the standard display mechanism to draw, you should use lockFocusIfCanDraw rather than lockFocus if there is a chance of drawing while the window is either miniaturized or hidden.
Another way to do this that for sure would keep the shadow would be to use two windows and make one a child to the other.
You will not ever be able to draw outside of a window for various reasons, not the least of which being your process needs to own or have permission for what it draws to (many other reasons too).

Cause UIScrollView zoomToRect: to only display a portion

I have a UIScrollView subclass that contains a UIImageView. When the user double taps on the zone I want to zoom to that CGRect (calculated from CGPoint) and "hide" the rest of the UIImageView. The final effect is similar in Mavel.app and The Walking Dead.app when you are reading a comic.
Until now I got:
-(void)presentRect:(CGRect)rect {
self.bounds = originalFrame; //1
[UIView beginAnimations:#"navigateToPos" context:nil];
[self zoomToRect:rect animated:NO];
self.bounds = rect;
[UIView commitAnimations];
}
This works, but "zoomToRect" needs the whole bounds of the UIScrollView and when I restart it (line 1), it gives an undesired effect.
I am stuck with this. I don't know if I need a new approach or need to use another property of UIScrollView.
Any help is appreciated. Thanks.
I downloaded this apps to see it. I'm almost sure they didn't even use uiscrollview. The way the pages moves make me think about that. And uiscrollview should be used only when you really need it, because it have a lot issues when you're customizing. (tiled content, zooming and others problems).
Slide to a next page only 50px aprox., the page goes to the next, in uiscrollview it didn't happen because it works diff.
As I said before, probably it's made using 4 bars (as you correct me) or it can be done with a view wrapping the uiimage and working as a mask. You only need calculate the size of the zoom square and move it to center, and resize the wrapper view to fit this new size. The substitute for the uiscrollview for pages can be a simple image gallery, but where is images you put this "comic-view".
I've solved using other properies from UIScrollView simulating the desired effect.

How to implement a custom Focus Ring in drawRect for NSTextField or NSTextVew

I want to draw a custom focus ring for my NSTextView subclass (which doesn't have a focus ring by default). I managed to implement it by overriding the parent NSScrollView drawRect and adding this code:
- (void)drawRect:(NSRect)dirtyRect {
if (focused) {
NSSetFocusRingStyle(NSFocusRingOnly);
NSRectFill(dirtyRect);
}
[super drawRect:dirtyRect];
}
However, I want to draw my own, custom focus ring. I have searched and searched for examples of this, and tried messing around and writing it myself, to no avail. The biggest issue I have is the fact that it will get cropped to the NSScrollView/NSTextView frame, no matter how I do it.
Thanks.
Updating this answer for 10.7+:
Now you should override drawFocusRingMask to render (simply drawing a shape; the system will take care of color/style), and override focusRingMaskBounds to hint at its boundaries. Also, call noteFocusRingMaskChanged if you change the shape in some way that the system could not figure out on its own.
(Below is the previous answer, requiring older APIs:)
In the Carbon framework there are HIThemeBeginFocus() and HIThemeEndFocus(), which allow you to cause any series of drawings (such as a rectangle or shape) to have an automatic "focused" appearance. Requires Mac OS X 10.5 or later.
This uses Core Graphics directly. To find the CG context from a drawRect: method in Cocoa, you'd do something like:
NSGraphicsContext* contextMgr = [NSGraphicsContext currentContext];
CGContextRef drawingContext = (CGContextRef)[contextMgr graphicsPort];
As far as avoiding clipping, one option is to use a parent view (such as an NSBox that has no border) to give extra padding. Perform the custom drawing at an inset location in the parent view that won't be clipped; in other words, give the illusion that the view is a bit smaller than its actual rectangle.