我知道有一堆类似的问题,但它们都非常过时,基本上我有一个statusBarItem,我想让它对拖放文件做出响应,下面是我尝试过的一些代码:
extension NSStatusBarButton {
open override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
true
}
open override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("dragging entered")
return .copy
}
open override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
print("Something was dragged!")
return true
}
open override func draggingEnded(_ sender: NSDraggingInfo) {
print("Draging ended")
}
}
class DragAndDropButton: NSStatusBarButton {
open override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
true
}
open override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("dragging entered")
return .copy
}
open override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
print("Something was dragged!")
return true
}
open override func draggingEnded(_ sender: NSDraggingInfo) {
print("Draging ended")
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
var popover: NSPopover!
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let popoverView = PopoverView()
popover = NSPopover()
popover.contentSize = NSSize(width: AppConstants.popoverWidth, height: AppConstants.popoverHeight)
popover.animates = true
popover.behavior = .transient
popover.contentViewController = NSHostingController(rootView: popoverView)
statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
let dropSubview = DragAndDropButton()
dropSubview.registerForDraggedTypes([.png])
dropSubview.image = NSImage(named: "CognitionSmall")
statusBarItem.button = drop
// statusBarItem.view = dropSubview
// statusBarItem.title = "Test"
if let button = self.statusBarItem.button {
// button.view
// button.registerForDraggedTypes([.multipleTextSelection])
button.addSubview(dropSubview)
button.imagePosition = NSControl.ImagePosition.imageLeft
button.image = NSImage(named: "CognitionSmall")
button.action = #selector(togglePopover(_:))
// button.window!.delegate = self
// button.addSubview(cognitoSubview)
// button.performDragOperation = #selector(onDrag(_:))
}
}如你所见,我试图扩展和覆盖默认的NSStatusBarButton行为,并用我自己的NSView替换它,并为按钮添加一个子视图,但似乎什么都不起作用,有人知道如何使其工作吗?
发布于 2020-05-09 13:33:23
事实证明,我对NSStatusBarButton所做的扩展很好,除了我没有正确测试它之外,如果你声明更多的注册类型,它就可以正常工作:
extension NSStatusBarButton {
open override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
true
}
open override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("dragging entered")
return .copy
}
open override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
print("Something was dragged!")
return true
}
open override func draggingEnded(_ sender: NSDraggingInfo) {
print("Draging ended")
}
}
... more initialization code
button.registerForDraggedTypes([.fileURL, .multipleTextSelection, .png])发布于 2020-05-09 06:03:24
这在我使用CommandLine的系统上可以工作(还没有在Xcode中尝试过)
import Cocoa
class CustomView:NSView {
var filePaths : [String] = []
override init(frame frameRect: NSRect) {
super.init(frame:frameRect)
self.registerForDraggedTypes([NSPasteboard.PasteboardType.fileURL])
}
required init?(coder aDecoder : NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: NSRect) {
super.draw(rect)
let bkgrnd = NSBezierPath(rect:rect)
NSColor.red.set()
bkgrnd.fill()
NSColor.black.set()
let str = String("DND")
let font = NSFont.init(name: "Menlo", size:14.0)
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
let attrText = NSAttributedString(string:str, attributes: [.paragraphStyle: style, .font:font!])
attrText.draw(in: rect)
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("draggingEntered")
return .copy
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
print("draggingUpdated")
return .copy
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let board = sender.draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "NSFilenamesPboardType")) as? NSArray
var draggedFilePath : String
draggedFilePath = board![0] as! String
print("draggedFilePath = \(draggedFilePath)")
return true
}
}
// **** main.swift **** //
let app = NSApplication.shared
app.setActivationPolicy(NSApplication.ActivationPolicy.regular)
let statusItem = NSStatusBar.system.statusItem(withLength: 100)
var view : CustomView!
view = CustomView(frame:NSMakeRect(0, 0, 100, 40))
statusItem.button?.addSubview(view)
app.run()发布于 2020-05-10 00:43:18
@Oscar -我修改了你的原始帖子,可以确认以下代码将通过使用扩展向NSStatusBarButton添加拖放功能,并且它消除了对NSView子视图的需要。谢谢。
import Cocoa
extension NSStatusBarButton {
open override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
true
}
open override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
print("Dragging entered.")
return .copy
}
open override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
print("Something was dragged!")
return true
}
open override func draggingEnded(_ sender: NSDraggingInfo) {
print("Dragging ended.")
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
var statusItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
statusItem = NSStatusBar.system.statusItem(withLength: 100)
statusItem.button!.title = "DND Test"
statusItem.button!.registerForDraggedTypes([.fileURL])
}
}
let appDelegate = AppDelegate()
// **** main.swift **** //
let app = NSApplication.shared
app.setActivationPolicy(.regular)
app.delegate = appDelegate
app.activate(ignoringOtherApps:true)
app.run()如果文件另存为statusItem.swift,则可以使用以下命令从终端运行该文件:
swiftc statusItem.swift -framework Cocoa -o statusItem && ./statusItem
https://stackoverflow.com/questions/61687206
复制相似问题