Re: Set focus on NSTextField in an NSMenuItem
Re: Set focus on NSTextField in an NSMenuItem
- Subject: Re: Set focus on NSTextField in an NSMenuItem
- From: Dragan Milić via Cocoa-dev <email@hidden>
- Date: Wed, 19 Oct 2022 21:45:57 +0200
> On 19. 10. 2022., at 03:52, Eric Schlegel wrote:
>
>
> I can tell you that the Help menu does itself use makeFirstResponder: on the
> text view…
Eric, thanks for detailed explanation and willingness to help. Your message
triggered me to play with the issue a bit more and I figured out what the
problem is. But I also realised I’ve come to some wrong conclusions before, so
I want to correct myself as well so that nobody here is left confused by them.
I’ll do that first…
> On 18. 10. 2022., at 18:43, Dragan Milić wrote:
>
> This issue isn’t related to the menu being attached to the NSStatusItem, it
> also happens in the main and/or contextual menu within the application. I’d
> eventually want to set the focus on the text field programmatically each time
> the menu appears, but that’s not possible either.
This was something I observed quite some time ago and I admin I haven’t
confirmed it again before I sent my initial message here. God knows what I did
wrong at that time, but I can say my above statement is completely false!
Placing NSTextFiend inside NSMenuItem works fine in both main and contextual
menus in the application. The text field gets focus when the menu opens,
sending -[NSWindow makeFirstResponder:] is not even necessary.
Now, my problem is that the application is defined as LSUIElement=YES (or its
activation policy is programmatically set to
NSApplicationActivationPolicyAccessory), meaning it doesn’t show its icon in
the Dock, it doesn’t show its main menu, nor can it be made active with
alt+tab. As known, clicking the status item of any application and making its
status item menu visible, doesn’t really make the application active, and in my
case that affect functionality of the text item. So, when I click on the status
item and show the status item menu, the application is not really active and
hence is text field (kind of) disabled, not being able to receive events. If I
set application’s activation policy to NSApplicationActivationPolicyRegular and
make it active (clicking on its icon in the Dock or alt+tabbing) before I click
its status item, the status item menu appears with the text field active and
focused, ready to be typed into.
I haven’t find the complete solution yet. Sending -[NSApp
activateIgnoringOtherApps:YES] does the job partially; clicking the status item
and opening its menu results in the application being activated, but that
automatically closes the menu immediately upon being open. But the application
stays active, so clicking the status item again finally opens the menu with the
text field active and having focus. If anyone have any idea how to solve this,
I’d be thankful to hear it.
-- Dragan
_______________________________________________
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