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

发布于 2024-11-02 20:18:12 字数 765 浏览 2 评论 0原文

我正在研究符号弹出按钮的详细信息,类似于 Xcode 3 编辑器窗口顶部的按钮。

我的控制器对象是显示 NSPopUpButton 时显示的 NSMenu 的委托。我实现了两个方法 numberOfItemsInMenu:menu:updateItem:atIndex:shouldCancel: 在菜单即将显示之前填充菜单。

但是,每次用户更改编辑器 NSTextView 中的选择或更改文本时,所选 NSMenuItem 的标题和图像都必须更改;就像 Xcode 的情况一样。

我遇到的问题是,当用户单击 NSPopUpButton 以显示 NSMenu、所选的 NSMenuItem 以及应该显示的项目时所选择的项目不匹配,因为菜单还没有正确数量的项目。

我想知道是否有一种方法可以控制当用户单击打开菜单时最初突出显示和跟踪哪个 NSMenuItem 。就目前情况而言,它始终突出显示并跟踪第一个项目,或者,如果用户之前选择了一个项目,则该项目将突出显示并跟踪。

我尝试尽我所能解释,但这里有一张图片说明了我的问题:

http://imgur.com/izGvh

我希望当用户打开菜单时突出显示的项目与所选项目相同。有什么想法吗?

I'm working on the details of a symbols pop up button, similar to what Xcode 3 at the top of its editor window.

My controller object is the delegate of the NSMenu that is shown when the NSPopUpButton is shown. I implement the two methods numberOfItemsInMenu: and menu:updateItem:atIndex:shouldCancel: to populate the menu right before it's about to be displayed.

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; just as is the case with 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.

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

翻了热茶 2024-11-09 20:18:12

我会使用 NSPopupbutton - 它可以做你想做的事。也许你甚至隐藏它?

I would use a NSPopupbutton - it can do what you want. maybe you even hide it?

无戏配角 2024-11-09 20:18:12

我不太确定我是否理解您的问题,但如果我们可以向这些菜单项添加标签。
例如

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

我们可以使用选择任何菜单项
[_popUp selectItemWithTag:_selectedItem];

I am not very sure that I understood your problem but If we can add tags to these menu Items.
e.g.

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

we can select any Menu item using
[_popUp selectItemWithTag: _selectedItem];

情何以堪。 2024-11-09 20:18:12

只是一个提示,不是完整的解决方案。

尝试在显示菜单后立即将 NSMouseMoved 事件发布到您的应用程序。
主要问题是检测要突出显示的项目的位置。
只是一个起点。

Just a hint not a full solution.

Try to post an NSMouseMoved event to your app right after the menu shown.
Main problem is here to detect the position of the item you want to be highlighted.
Just a starting point.

情绪操控生活 2024-11-09 20:18:12

您是否尝试过此 NSMenuDelegate 方法:处理突出显示

– menu:willHighlightItem:

NSMenuDelegate 协议参考

此外,您还可以将 NSMenuItem 索引存储在某个变量中,以跟踪所选项目以供以后使用。

Have you tried this NSMenuDelegate method:Handling Highlighting

– menu:willHighlightItem:

NSMenuDelegate Protocol Reference

Also you can store the NSMenuItem index in some var to keep track of the selected item for later use.

白衬杉格子梦 2024-11-09 20:18:12

我不确定,您要移动鼠标选择突出显示(蓝色的东西)还是复选标记?

一般来说,复选标记就是您要更改的内容。您可以使用 -setState: 方法从 validateMenuItem: 方法执行此操作(或者在任何其他时间,如果您的项目设置为不参与验证)。

蓝色突出显示是用户键盘输入或鼠标位置的指示器,您不应该弄乱它。更改它没有任何意义,因为当用户移动鼠标时,即使移动一个像素,它也会被更改回来。

也就是说,您可以设置NSPopUpButtonselectedItem,这将导致整个菜单移动,因此selectedItem code> 位于鼠标下方(假设鼠标刚刚单击了弹出按钮)。

如果在弹出按钮决定选择哪个项目后调用菜单委托方法,则无法使用它。相反,您可以从 NSPopUpButtonWillPopUpNotification 处理程序设置弹出按钮的 selectedItem 和菜单。

如果这也被调用得太晚了,您可能必须继承 NSPopupButtonCell 并在 -attachPopUpWithFrame:inView: 的重写中执行此操作(我认为这是应该的地方当您在选择弹出按钮时不单击而只按空格键时也可以工作)。

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.

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).

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文