NSStatusItem自定义视图NSDraggingDestination仅在单击项目后才有效

 mobiledu2502876027 发布于 2022-12-31 20:03

我有一个NSStatusItem,其自定义视图实现了NSDraggingDestination协议.但是,只有在单击/激活状态项至少一次时,拖放才有效.无论如何,我希望自定义视图接受drop.

一个展示问题的gif: GIF.

上例中的代码(或Xcode Project):

import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        let vc = StatusItemViewController()
    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }

}

class StatusItemView: NSView, NSDraggingDestination {
    public var image = NSImage(named: "NSQuickLookTemplate")
    public var action: Selector?
    public var target: AnyObject?

    private var statusItem: NSStatusItem

    init(statusItem: NSStatusItem) {
        let itemWidth = statusItem.length
        let itemHeight = NSStatusBar.systemStatusBar().thickness
        self.statusItem = statusItem
        super.init(frame: NSRect(x: 0.0, y: 0.0, width: itemWidth, height: itemHeight))

        var types = [NSURLPboardType, NSStringPboardType] as [String]
        for type in NSImage.imagePasteboardTypes() {
            types.append(type as String)
        }
        self.registerForDraggedTypes(types)
        statusItem.view = self
    }

    override func drawRect(dirtyRect: NSRect) {
        let iconSize = image.size
        let bounds = self.bounds
        let iconX = round((bounds.width - iconSize.width)/2)
        let iconY = round((bounds.height - iconSize.height)/2)
        image.drawAtPoint(NSPoint(x: iconX, y: iconY), fromRect: NSRect(), operation: NSCompositingOperation.CompositeOverlay, fraction: 1.0)
    }

    override func mouseDown(theEvent: NSEvent!) {
        if let _action = self.action {
            NSApp.sendAction(_action, to: self.target, from: self)
        }
    }

    override func performDragOperation(sender: NSDraggingInfo!) -> Bool {
        println("Performed")
        return true
    }

    override func draggingEntered(sender: NSDraggingInfo!) -> NSDragOperation {
        return NSDragOperation.Copy
    }

    override func draggingUpdated(sender: NSDraggingInfo!) -> NSDragOperation {
        println("Dragging")
        return NSDragOperation.Copy
    }

    override func draggingExited(sender: NSDraggingInfo!) {
        println("Exit")
    }
}

class StatusItemViewController: NSObject {
    private var statusBarItem: NSStatusItem?
    private var statusItemView: StatusItemView!

    init() {
        self.statusBarItem = NSStatusBar.systemStatusBar().statusItemWithLength(24)
        super.init()
        if let item = statusBarItem {
            statusItemView = StatusItemView(statusItem: item)
            statusItemView.target = self
            statusItemView.action = "doAction:"
        } else {
            println("Unable to create status item, terminating self")
            NSApplication.sharedApplication().terminate(nil)
        }
    }

    deinit {
        NSStatusBar.systemStatusBar().removeStatusItem(statusBarItem)
        statusBarItem = nil
        statusItemView = nil
    }

    func doAction(sender: AnyObject!) {
        println("Here is where one would perform the action")
    }
}

也许这是一个错误,或许我只是遗漏了一些东西.为什么会发生这种情况,我能做些什么来阻止这种行为?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有