Re: PDFView focus ring
Re: PDFView focus ring
- Subject: Re: PDFView focus ring
- From: Graham Cox <email@hidden>
- Date: Sun, 05 Feb 2012 11:17:04 +1100
On 04/02/2012, at 8:06 PM, Martin Hewitson wrote:
> Thank you all for your responses. I need to support 10.6.8, so in end I chose two different approaches for the textview and pdfview.
>
> For the textview, I handle drawing a thin rect around the textview's bounds when it is first responder. I just handle the change in
> -becomeFirstResponder and -resignFirstResponder.
>
> For the pdfview, I embedded it in an NSView which receives notifications when the pdfview becomes or resigns first responder. Then it draws a thin rect around its bounds. The pdfview is then 1 pixel smaller on all sides that this container view.
>
> I'm not really happy with either of these solutions, but nothing else seemed to work so far. The end result is visually not too bad. The thing I like least is that I need this 1 pixel gap in the container view for the pdfview. Because when the pdfview doesn't have focus, I have to decide to draw something there. For now I toggle between stroke colors (dark grey and focus ring color).
Sounds pretty complicated.
The way I've done this is to handle the focus ring in the view's -drawRect method. You need to focus on the superview so you can draw outside your frame, but that's not a problem - just do it after you've drawn the rest of your normal content. You can detect whether the view has the keyboard focus by using:
[[self window] firstResponder] == self;
The code to draw the focus ring doesn't need to outset the frame by any amount - the special case of -fill that uses the focus ring style manages that trick for you, so the code is:
// we have the focus, draw the focus ring and use appropriate active colours
[[self superview] lockFocus];
NSRect fr = [self frame];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:fr] fill];
[[self superview] unlockFocus];
The only other thing I've found necessary is to track whether the focus ring needs an update or not by using -setKeyboardFocusRingNeedsDisplayInRect:[self bounds] which I call from -becomeFirstResponder and -resignFirstResponder.
I'm not sure if this is considered the "correct" way to do all that (in pre-Lion code), but it has always worked well for me, and the advantage of it is that it doesn't require that you have any special outer view to support the focus ring.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden