Drawing when app is in active

3 views
Skip to first unread message

Jonathan Guy

unread,
Mar 24, 2012, 6:30:40 AM3/24/12
to coco...@lists.apple.com
Hi all
This is must be the most simple a puzzling problem I've had. Take a new app, create a custom view class with a drawrect of

- (void)drawRect:(NSRect)dirtyRect
{
if ([NSApp isActive]) {
[[NSColor redColor] set];
}
else {
[[NSColor blueColor] set];
}
NSRectFill([self bounds]);
}

pretty simple stuff you would think but if I drop this custom view onto the main window and run it up the view draw a blue square??? how bizarre. If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here? I can only think the whole view is being clipped as the system doesn't think it needs to be redrawn as the very first drawrect the app is showing as inactive but then subsequently drawrect gets called again with the app in an active state so it initially draws blue but then is supposed to draw red but that's not happening.
Any help would be appreciated.
Thanks
_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com

This email sent to cocoa-dev-ga...@googlegroups.com

Graham Cox

unread,
Apr 1, 2012, 3:26:01 AM4/1/12
to Jonathan Guy, coco...@lists.apple.com

On 24/03/2012, at 9:30 PM, Jonathan Guy wrote:

> Hi all
> This is must be the most simple a puzzling problem I've had. Take a new app, create a custom view class with a drawrect of
>
> - (void)drawRect:(NSRect)dirtyRect
> {
> if ([NSApp isActive]) {
> [[NSColor redColor] set];
> }
> else {
> [[NSColor blueColor] set];
> }
> NSRectFill([self bounds]);
> }
>
> pretty simple stuff you would think but if I drop this custom view onto the main window and run it up the view draw a blue square??? how bizarre. If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here? I can only think the whole view is being clipped as the system doesn't think it needs to be redrawn as the very first drawrect the app is showing as inactive but then subsequently drawrect gets called again with the app in an active state so it initially draws blue but then is supposed to draw red but that's not happening.
> Any help would be appreciated.
> Thanks


The problem is, I think, that the view is not automatically invalidated when the app is inactivated or activated. Therefore what you see is whatever it was in its previous state. You need to listen for the activation/inactivation notification, and invalidate the view.

--Graham

Jens Alfke

unread,
Apr 2, 2012, 12:47:47 AM4/2/12
to Jonathan Guy, coco...@lists.apple.com

On Mar 24, 2012, at 3:30 AM, Jonathan Guy wrote:

> If I resize the window it suddenly draws red but deactivating a reactivating the app is not redrawing the view with the correct color. What is going on here?

Activating/deactivating an app does not force all of its views to redraw! That would be really expensive. If you want your view to change appearance when this happens, you'll have to listen for the right notification and call -setNeedsDisplay:. That's what controls that update their appearance when active/inactive do.

Also, you probably don't want the appearance to be based on whether the app is active or inactive; that would be rather weird and nonstandard. Instead, base it on whether the window is key.

—Jens

Reply all
Reply to author
Forward
0 new messages