gpt4 book ai didi

Swift StatusBarItem,使按钮支持拖放

转载 作者:行者123 更新时间:2023-12-03 17:00:56 25 4
gpt4 key购买 nike

我知道有很多与此类似的问题,但它们都非常过时,基本上我有一个 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 并向按钮添加 subview ,但似乎没有任何作用,有人知道如何使其工作吗?

最佳答案

这可以在我的系统上使用 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()

关于Swift StatusBarItem,使按钮支持拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61687206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com