Skip to content

Instantly share code, notes, and snippets.

@fhefh2015
Created November 6, 2021 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhefh2015/d928d9269aade94bdd1d9f788ec90701 to your computer and use it in GitHub Desktop.
Save fhefh2015/d928d9269aade94bdd1d9f788ec90701 to your computer and use it in GitHub Desktop.
Continuity Camera Demo
import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let myWindow = NSWindow()
let style: NSWindow.StyleMask = [.closable, .miniaturizable, .titled]
myWindow.isOpaque = true
myWindow.styleMask.insert(style)
myWindow.backingType = .buffered
myWindow.contentViewController = TestViewController()
myWindow.setFrame(NSRect(x: 100, y: 100, width: 200, height: 400), display: false)
let myWindowController = TestWindowController(window: myWindow)
// myWindowController.contentViewController = TestViewController()
myWindowController.window = myWindow
myWindowController.window?.title = "MyWindow"
myWindowController.window?.center()
myWindowController.showWindow(self)
myWindowController.window?.makeKeyAndOrderFront(self)
myWindowController.window?.makeFirstResponder(myWindowController.window?.contentView)
NSApp.activate(ignoringOtherApps: true)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
import Cocoa
class TestViewController: NSViewController,NSServicesMenuRequestor {
var continuityCameraMenu = NSMenu()
lazy var preview:NSImageView = {
let imageView = NSImageView(frame: NSRect(x: 10, y: 10, width: 100, height: 100))
imageView.image?.size = CGSize(width: 50, height: 50)
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
setUI()
}
override func loadView() {
view = NSView()
}
func test()->NSMenu {
print("2")
return continuityCameraMenu
}
func setUI() {
let button = NSButton(title: "load", target: self, action: #selector(click(_:)))
button.frame = NSRect(x: 180, y: 100, width: 50, height: 20)
view.addSubview(button)
view.addSubview(preview)
print("1")
// continuityCameraMenu.identifier = NSUserInterfaceItemIdentifier("NSUserInterfaceItemIdentifier")
}
override func validRequestor(forSendType sendType: NSPasteboard.PasteboardType?, returnType: NSPasteboard.PasteboardType?) -> Any? {
print("validRequestor")
if let pasteboardType = returnType,
NSImage.imageTypes.contains(pasteboardType.rawValue)
{
return self
} else {
return super.validRequestor(forSendType: sendType, returnType: returnType)
}
}
func readSelection(from pasteboard: NSPasteboard) -> Bool {
guard pasteboard.canReadItem(withDataConformingToTypes: NSImage.imageTypes) else { return false }
guard let image = NSImage(pasteboard: pasteboard) else { return false }
print("readSelection image: ", image)
preview.image = image
return true
}
@objc func click(_ sender:NSButton) {
print("click")
guard let event = NSApplication.shared.currentEvent else { return }
// AppKit uses the Responder Chain to figure out where to insert the Continuity Camera menu items.
// So making ourselves `firstResponder` here is important.
self.view.window?.makeFirstResponder(self)
// self.view.window.
print("continuityCameraMenu: ", continuityCameraMenu)
NSMenu.popUpContextMenu(continuityCameraMenu, with: event, for: sender)
print("continuityCameraMenu: ", continuityCameraMenu)
}
}
import Cocoa
class TestWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
print("TestWindowController")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment