Fill NSView with NSColor

Hi,

Seems like this should be straight-forward, but alas, I’m having no luck. I am trying to fill a NSView with a NSColor using something like following:


set aColor to current application's NSColor's selectedMenuItemColor()

current application's NSColor's setFill_(aColor) -- this doesn't work

set aRect to customView's |frame|()
tell current application to NSRectFill(aRect)

Is this a syntax issue or am I going about this in the wrong way? Thanks in advance!

Hi,

simplest solution: enable CoreAnimation Layer in Interface Builder and


set aColor to current application's NSColor's selectedMenuItemColor()
customView's setBackgroundColor:aColor

This worked great. Thanks Stefan!

On an somewhat-related note, while this did successfully fill my NSView with the “Blue” highlight color as I had defined in System Preferences>General>Appearance, when I changed the preference to “Graphite” and re-ran my application, the NSView filled with a the blue color again. I checked in a color picker window (Developer Palette) and selectedMenuItemColor has been changed to the Graphite color there.

Any ideas why selectedMenuItemColor would still be ‘referencing’ the blue color?

Update:

I couldn’t ever figure out why selectedMenuItemColor would only return the Blue color, so I tried to implement something like the following, but get no highlighting whatsoever from:


-- statusItem is a NSStatusItem and is implemented using:
--set statusItem to current application's NSStatusBar's systemStatusBar()'s statusItemWithLength_(current application's NSVariableStatusItemLength)

-- statusView is a custom NSView and is implemented using:
-- statusItem's setView_(statusView)

statusItem's drawStatusBarBackgroundInRect_withHighlight_((statusView's |frame|()), true) -- also tried |bounds|()
statusItem's drawStatusBarBackgroundInRect_withHighlight_((statusItem's  view()'s |frame|()), true) -- also tried |bounds|()

The end goal here is to highlight a NSStatusItem app, and respect the user’s color choice preferences. Thanks in advance

drawStatusBarBackgroundInRect is normally used in the drawRect method of an NSView subclass.

But then you have to implement the code to manage the highlight and menu open behavior by yourself

Hi Stefan,

Thanks for the information. I had been using the following tutorial as an aid in trying to accomplish the highlighting:
http://undefinedvalue.com/2009/07/07/adding-custom-view-nsstatusitem

NSStatusItem doesn’t seem to directly have a bounds property, but rather its view does. I assumed using the bounds/frame of the view would be sufficient for drawStatusBarBackgroundInRect, but of course this isn’t working. Is there a need to draw a ‘new’ NSRect? Is that what drawRect is accomplishing? I’ve read in other forums that one should not attempt to call drawRect directly, but rather call setNeedsDisplay. I of course had no luck trying this.

I thought maybe part of the issue was that getting the bounds/frame of statusView wasn’t returning the screen position origins (0,0 was returned) in addition to height and width values, but modifying the script to get the origins didn’t solve anything:


        set aRect to statusItem's valueForKey_("window")
        set aRect to aRect's |frame|() 
        statusItem's drawStatusBarBackgroundInRect_withHighlight_(aRect, true)

The Apple documentation doesn’t seem to indicate that anything special needs to happen to get this to execute correctly,