在NSToolBarItem的默认视图中添加一个提示框矩形。

huangapple go评论121阅读模式
英文:

Add a tooltip rectangle to NSToolBarItem's default view

问题

我有一些NSToolBarItems用于在我的应用程序中执行撤销/重做操作。我希望它们在工具提示中显示撤销/重做操作的名称。

我可以通过toolTip属性指定NSToolBarItem的工具提示。但我不确定何时应该设置此属性。我尚未找到一种在撤销管理器记录新的撤销或重做操作时通知的方法。

最好的方法是在用户悬停在工具栏项上时动态返回工具提示字符串。但这需要向我无法访问的视图添加工具提示矩形。实际上,NSToolBarItemview属性默认返回nil。如果我将view属性设置为我的自定义视图,它就不会返回nil,但默认情况下,NSToolBarItem使用NSButton的私有子类作为视图。这个子类实现了NSButton没有的一些良好行为。所以我宁愿使用默认视图。

是否有一种方法可以向不使用自定义视图的NSToolBarItemview添加工具提示矩形?换句话说,我如何访问这个视图?

英文:

I have some NSToolBarItems to allow undo/redo in my app. I want them to show the name of undo/redo actions in their tooltip.

I can specify the tooltip of an NSToolBarItem vial its toolTip property. But I'm not sure when this property should be set. I haven't found a way to be notified when the undo manager has recorded a new action to undo or redo.

It would be better to dynamically return the tooltip string when the user hovers the toolbar item. But this requires adding a tooltip rectangle to a view that I cannot access. Indeed, the view property of NSToolBarItem returns nil by default. It does not return nil if I set the view property with my own view, but a NSToolBarItem uses a private subclass of NSButton as view, by default. This subclass implements nice behaviours that NSButton doesn't. So I'd rather use the default view.

Is there a way to add a tooltip rectangle to an NSToolBarItem's view that doesn't use a custom view? IOW, how can I access this view?

答案1

得分: 1

不需要破解 NSToolbarItem。请使用您的撤销管理器上的 NSUndoManagerCheckpointNotification 通知。每次调用它时,您可以更新两个工具栏项目的工具提示。

[NSNotificationCenter.defaultCenter addObserverForName:NSUndoManagerCheckpointNotification object:self.undoManager queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
    NSString *undoTitle = self.undoManager.undoMenuItemTitle;
    NSString *redoTitle = self.undoManager.redoMenuItemTitle;
    // 更新工具栏项目的工具提示
}];

您还可以通过在块中使用 note.object 访问相关的撤销管理器。

英文:

You don't need to hack NSToolbarItem. Use the NSUndoManagerCheckpointNotification notification on your undo manager. Every time it is called you can update the tooltip of the two toolbar bar items.

[NSNotificationCenter.defaultCenter addObserverForName:NSUndoManagerCheckpointNotification object:self.undoManager queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
    NSString *undoTitle = self.undoManager.undoMenuItemTitle;
    NSString *redoTitle = self.undoManager.redoMenuItemTitle;
    // Update the toolbar items' tooltips
}];

You can also access the relevant undo manager via note.object in the block.

huangapple
  • 本文由 发表于 2023年6月12日 00:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定