On 4 Sep 2011, at 10:39, Quincey Morris wrote:

> On Sep 4, 2011, at 00:40 , Antonio Nunes wrote:
> 
>> I have a control that needs to be highlighted when the font panel is 
>> visible. This turns out not to be an easy task. I can't find any 
>> notifications that will be triggered whenever the font panel is shown or is 
>> hidden.
> 
> Why can't you use '[[NSFontPanel sharedFontPanel] isVisible]' (possibly 
> checking first with '[NSFontPanel sharedFontPanelExists]' so that you don't 
> unnecessarily create it)?

Well, that sent me into a direction that I'm happy with:
User KVO to observe changes to the "visible" property of the font panel.

====

1. When my control gets created it sets itself up as an observer  for the 
NSWindowDidUpdateNotification for only the font panel object:
…
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(windowDidUpdate:) name:NSWindowDidUpdateNotification 
object:[NSFontPanel sharedFontPanel]];
…

2. When the font panel gets shown for the first time the selector is called, 
and we set ourselves up as an observer for the "visible" property. We also stop 
observing the
NSWindowDidUpdateNotification, since we no longer need it.

- (void)startObservingFontPanelVisibility
{
        [[NSFontPanel sharedFontPanel] addObserver:self
                                        forKeyPath:@"visible"
                                        options:NSKeyValueObservingOptionNew
                                        context:nil];
}


- (void)windowDidUpdate:(NSNotification *)notification
{       
        [self startObservingFontPanelVisibility];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                
name:NSWindowDidUpdateNotification
                                                        object:[NSFontPanel 
sharedFontPanel]];
}


3. When the visibility of the font panel changes, we now get called and can 
change the highlight of the control:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context
{
        [self setSelected:[[NSFontPanel sharedFontPanel] isVisible] 
forSegment:2];
}

====

The only aspect that I'd like to improve is that the font panel gets created 
when the first document gets created, even though the user may never actually 
use it. I'd like to avoid creating the font panel unnecessarily but a 
satisfactory method to do that hasn't yet occurred to me. I could do something 
like observe the NSWindowDidUpdateNotification without narrowing it down to the 
font panel object and then:

        if ([NSFontPanel sharedFontPanelExists] && notification.object == 
[NSFontPanel sharedFontPanel]) {
                [self startObservingFontPanelVisibility];
                [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                
                                name:NSWindowDidUpdateNotification
                                                                                
                          object:[NSFontPanel sharedFontPanel]];
        }

but that would entail that the method is called constantly when any of the 
app's windows is updated (until the font panel gets shown for the first time, 
if at all), which I think is a worse solution that what I did instead.

-António

-----------------------------------------
Forgiveness is not an occasional act;
it is a permanent attitude.

--Martin Luther King, Jr
-----------------------------------------




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to