Skip navigation.
 
mlRe: Set focus on NSView in an NSMenuItem
FROM : Lieven Dekeyser
DATE : Fri Feb 15 13:55:47 2008

On 06 Feb 2008, at 11:38, Pierre Bernard wrote:

> Hi Ron!
>
> It appears that both work. I guess it depends on whether you add the 
> semi-colon to the selector or not.
>
> The timer was only a wild idea on how to work around the original 
> problem with the window's field editor. I got the timer to work, but 
> it does not solve the original problem.


I had the same problem and I solved it more or less with the following 
approach:

- use a custom view for the NSStatusItem. Using the standard setMenu 
approach, all sorts of redrawing and focus issues occur.
- when a mouse click occurs in the NSStatusItem's view, I bring the 
application to front using [NSApp activateIgnoringOtherApps:YES] 
(because the application isn't necessarily the frontmost application 
when the NSStatusItem is clicked) and start a timer to fire after 0.01 
seconds
- In the timer action, I pop up the menu, which causes the delegate 
method menuWillOpen to be called.
- When the menu is being opened, I call [[self window] 
makeFirstResponder:textField] in the viewDidMoveToWindow method of the 
NSMenuItem's view (a custom view with a text field in it)
- in menuWillOpen and menuDidClose I redraw the NSStatusItem's view 
using -drawStatusBarBackgroundInRect:withHighlight:

So to recap, I needed:
- A custom view for the NSStatusItem which fakes the standard view
- A custom view for the NSMenuItem that tries to make the text field 
the first responder
- All sorts of dirty hacks just to make sure the menu isn't popped up 
before the view is able to get focus

As I said, this solves the problem "more or less". There are still 
redrawing issues, especially when the menu is longer than the screen 
height. But at least the view gets focus. And I think I might be able 
to solve the redrawing issues by calling needsDisplay after every 
event in the textfield, but still..

Surely, there must be a better way to do this? I'm not sure these 
hacks will continue to work..

Apple?

--Lieven

Related mailsAuthorDate
mlSet focus on NSView in an NSMenuItem Hendrik Holtmann Jan 4, 15:20
mlRe: Set focus on NSView in an NSMenuItem Peter Ammon Jan 4, 21:30
mlRe: Set focus on NSView in an NSMenuItem Pierre Bernard Feb 6, 00:43
mlRe: Set focus on NSView in an NSMenuItem Pierre Bernard Feb 6, 09:37
mlRe: Set focus on NSView in an NSMenuItem Ron Fleckner Feb 6, 11:33
mlRe: Set focus on NSView in an NSMenuItem Pierre Bernard Feb 6, 11:38
mlRe: Set focus on NSView in an NSMenuItem Lieven Dekeyser Feb 15, 13:55