[英]NSClickGestureRecognizer not working on NSStatusItem
Trying to recognize a right click on a NSStatusItem
I got a suggestion ( Thanks to Zoff Dino ) to use a NSClickGestureRecognizer
for that.试图识别右键点击一个
NSStatusItem
我有一个建议(感谢佐夫迪诺)使用NSClickGestureRecognizer
了点。 But for some bizarre reason it isn't working as it should be.但由于一些奇怪的原因,它没有按预期工作。 I am able to recognize a left click (
buttonMask = 0x1
) but not a right-click ( buttonMask = 0x2
).我能够识别左键单击(
buttonMask = 0x1
)但不能识别右键单击( buttonMask = 0x2
)。 This is how I would like it to work but it isn't:这就是我希望它工作的方式,但事实并非如此:
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
if let button = statusItem.button {
// Add right click functionality
let gesture = NSClickGestureRecognizer()
gesture.buttonMask = 0x2 // right mouse
gesture.target = self
gesture.action = "rightClickAction:"
button.addGestureRecognizer(gesture)
}}
func rightClickAction(sender: NSGestureRecognizer) {
if let button = sender.view as? NSButton {
NSLog("rightClick")
}
}
UPDATE:
更新:
I still did not manage to gets to work.
我还是没能去上班。 Somehow it doesn't react on a right click (but changing the code on a left click) does.
不知何故,它不会对右键单击做出反应(但在左键单击时更改代码)会。 I guess some really simple issues are occurring that seem to block it from working.
我想一些非常简单的问题似乎会阻止它工作。 Even stranger is the fact that
gesture.buttonMask = 0x1
works on the left click.更奇怪的是,
gesture.buttonMask = 0x1
在左键单击时起作用。
An alternative solution rather than NSClickGestureRecognizer
is to attach a custom view to the status bar and handle the event from there.替代
NSClickGestureRecognizer
的替代解决方案是将自定义视图附加到状态栏并从那里处理事件。
The small disadvantage is you have to take care of the drawing and menu delegate methods.一个小缺点是您必须处理绘图和菜单委托方法。
Here a simple example:这里有一个简单的例子:
Create a file StatusItemView
a subclass of NSView
创建一个文件
StatusItemView
作为NSView
的子NSView
import Cocoa
class StatusItemView: NSView, NSMenuDelegate {
//MARK: - Variables
weak var statusItem : NSStatusItem!
var menuVisible = false
var image : NSImage! {
didSet {
if image != nil {
statusItem.length = image.size.width
needsDisplay = true
}
}
}
//MARK: - Override functions
override func mouseDown(theEvent: NSEvent) {
if let hasMenu = menu {
hasMenu.delegate = self
statusItem.popUpStatusItemMenu(hasMenu)
needsDisplay = true
}
}
override func rightMouseDown(theEvent: NSEvent) {
Swift.print(theEvent)
}
//MARK: - NSMenuDelegate
func menuWillOpen(menu: NSMenu) {
menuVisible = true
needsDisplay = true
}
func menuDidClose(menu: NSMenu) {
menuVisible = false
menu.delegate = nil
needsDisplay = true
}
//MARK: - DrawRect
override func drawRect(dirtyRect: NSRect) {
statusItem.drawStatusBarBackgroundInRect(bounds, withHighlight:menuVisible)
let origin = NSMakePoint(2.0, 3.0) // adjust origin if necessary
image?.drawAtPoint(origin, fromRect: dirtyRect, operation: .CompositeSourceOver, fraction: 1.0)
}
}
In AppDelegate
you need a reference to the custom menu and an instance variable for the NSStatusItem
instance在
AppDelegate
您需要对自定义菜单的引用和NSStatusItem
实例的实例变量
@IBOutlet weak var menu : NSMenu!
var statusItem : NSStatusItem!
In applicationDidFinishLaunching
create the view and attach it to the status item.在
applicationDidFinishLaunching
创建视图并将其附加到状态项。 Be aware to set the image of the view after attaching it to make sure the width is considered.请注意在附加视图后设置视图的图像以确保考虑宽度。
func applicationDidFinishLaunching(aNotification: NSNotification) {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1) // NSVariableStatusItemLength)
let statusItemView = StatusItemView(frame: NSRect(x: 0.0, y: 0.0, width: statusItem.length, height: 22.0))
statusItemView.statusItem = statusItem;
statusItemView.menu = menu
statusItem.view = statusItemView
statusItemView.image = NSImage(named: NSImageNameStatusAvailable)
}
The special case control-click to trigger the right-click function is not implemented.未实现特殊情况 control-click 触发右键单击功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.