简体   繁体   English

有没有办法控制打开菜单时最初突出显示的NSMenuItem项?

[英]Is there a way to control the NSMenuItem item that is initially highlighted when opening a menu?

I'm working on the details of a symbols pop up button, similar to what Xcode 3 at the top of its editor window. 我正在研究符号弹出按钮的细节,类似于编辑器窗口顶部的Xcode 3。

My controller object is the delegate of the NSMenu that is shown when the NSPopUpButton is shown. 我的控制器对象是的代表NSMenu的时被示出NSPopUpButton被示出。 I implement the two methods numberOfItemsInMenu: and menu:updateItem:atIndex:shouldCancel: to populate the menu right before it's about to be displayed. 我实现了两个方法numberOfItemsInMenu:menu:updateItem:atIndex:shouldCancel:在即将显示之前填充菜单。

However, the title and image of the selected NSMenuItem have to change each time the user changes the selection in the editor NSTextView , or makes changes to the text; 但是,每次用户在编辑器NSTextView更改选择时,所选NSMenuItem的标题和图像都必须更改,或者对文本进行更改; just as is the case with Xcode. 就像Xcode的情况一样。

The problem I'm having is when the user goes to click on the NSPopUpButton to display the NSMenu , the selected NSMenuItem and the item that should be selected do not match up, since the menu doesn't have the proper number of items yet. 我遇到的问题是当用户点击NSPopUpButton以显示NSMenu ,所选的NSMenuItem和应该选择的项目不匹配,因为菜单还没有正确数量的项目。

I'm wondering if there is a way to control which NSMenuItem is initially highlighted and tracked when the user clicks to open the menu. 我想知道是否有办法控制当用户点击打开菜单时最初突出显示和跟踪的NSMenuItem As it stands, the first item it always highlighted and tracked or, if the user had previously selected a item, that item is highlighted and tracked. 目前,它始终突出显示和跟踪的第一个项目,或者,如果用户先前已选择项目,则突出显示并跟踪该项目。

I tried explaining as best I could, but here is a image illustrating my problem: 我尝试尽可能地解释,但这是一个说明我的问题的图像:

http://imgur.com/izGvh

I want the highlighted item to be the same as the selected item when the user opens the menu. 当用户打开菜单时,我希望突出显示的项目与所选项目相同。 Any ideas? 有任何想法吗?

I would use a NSPopupbutton - it can do what you want. 我会使用NSPopup按钮 - 它可以做你想要的。 maybe you even hide it? 也许你甚至隐藏它?

I am not very sure that I understood your problem but If we can add tags to these menu Items. 我不是很确定我理解你的问题但是如果我们可以为这些菜单项添加标签。 eg 例如

[mMenuItemOne setTag : 1];
[mMenuItemTwo setTag : 2];
[mMenuItemThree setTag : 3];
[mMenuItemFour setTag : 4];}

we can select any Menu item using [_popUp selectItemWithTag: _selectedItem]; 我们可以使用[_popUp selectItemWithTag:_selectedItem]选择任何菜单项;

Have you tried this NSMenuDelegate method: Handling Highlighting 您是否尝试过此NSMenuDelegate方法: 处理突出显示

– menu:willHighlightItem:

NSMenuDelegate Protocol Reference NSMenuDelegate协议参考

Also you can store the NSMenuItem index in some var to keep track of the selected item for later use. 您还可以将NSMenuItem索引存储在某些var中,以跟踪所选项目以供以后使用。

Just a hint not a full solution. 只是暗示不是一个完整的解决方案。

Try to post an NSMouseMoved event to your app right after the menu shown. 尝试在显示的菜单后立即将NSMouseMoved事件发布到您的应用。 Main problem is here to detect the position of the item you want to be highlighted. 主要问题是检测要突出显示的项目的位置。 Just a starting point. 只是一个起点。

I'm not sure, do you want to move the mouse selection highlight (the blue stuff) or the checkmark? 我不确定,你想移动鼠标选择高亮(蓝色的东西)还是复选标记?

In general, the checkmark is the thing you want to change. 通常,复选标记是您要更改的内容。 You can do so from your validateMenuItem: method (or at any other time if your item is set not to take part in validation) using the -setState: method. 您可以使用-setState:方法从validateMenuItem:方法(或在任何其他时间,如果您的项目设置为不参与验证)执行此-setState:

The blue highlight is an indicator of the user's keyboard input or mouse location, and you should not mess with it. 蓝色突出显示是用户键盘输入或鼠标位置的指示器,您不应该弄乱它。 Changing it does not make any sense, as it would be changed back the moment the user moves the mouse even a single pixel. 改变它没有任何意义,因为它会在用户移动鼠标甚至单个像素时改变。

That said, you can set the selectedItem of the NSPopUpButton , which will cause the entire menu to be moved so the selectedItem is under the mouse (assuming the mouse just clicked the popup button). 这就是说,你可以设置selectedItem中的NSPopUpButton ,这将导致移动的整个菜单,因此selectedItem是鼠标下(假设鼠标刚刚点击弹出的按钮)。

If your menu delegate method is called after the pop up button has decided what item to select, you can't use it. 如果在弹出按钮确定要选择的项目后调用菜单委托方法,则无法使用它。 Instead, you could probably set the selectedItem and menu of the popup button from a NSPopUpButtonWillPopUpNotification handler. 相反,您可以从NSPopUpButtonWillPopUpNotification处理程序设置弹出按钮的selectedItem和菜单。

If that is also called too late, you'll probably have to subclass NSPopupButtonCell and do it in an override of -attachPopUpWithFrame:inView: (I think that's the spot that should also work when you don't click and just hit the space key while the popup button is selected). 如果这也被称为太晚了,你可能必须NSPopupButtonCell ,并在-attachPopUpWithFrame:inView:的覆盖中-attachPopUpWithFrame:inView:我认为当你不点击并且只是点击空格键时这也应该有效选择弹出按钮时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM