应用 window 在所有 windows 之上,包括其他应用 windows
App window on top of all windows including others app windows
我喜欢让我的 NSWindow 位于所有应用程序的首位,包括其他应用程序 windows。这怎么可能?
这就是我的自定义 NSWindow 的样子?
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask,
backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: NSWindow.StyleMask.borderless,
backing: NSWindow.BackingStoreType.buffered, defer: false)
// Manage window appearance and size
// this window manages video view controller
self.backgroundColor = NSColor.clear
self.alphaValue = CGFloat(AppSingleton.shared.getVideoOpacity()) / 100
self.isOpaque = false
self.collectionBehavior = .transient
}
这就是我通过按钮操作打开 NSWindowController 的方式
func openPlayerWindow(url: URL) {
let storyboard:NSStoryboard? = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
let playerWindowController = storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SIPlayerWindowController")) as! PlayerWindowController
if let playerWindow = playerWindowController.window {
playerWindowController.showWindow(nil)
playerWindow.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
let videoPlayerController = playerWindow.contentViewController as! VideoPlayerViewController
videoPlayerController.videoFile = url
}
}
PlayerView.swift
import Cocoa
import AVFoundation
class PlayerView: NSView {
override func hitTest(_ point: NSPoint) -> NSView? {
return nil;
}
}
这个PlayerView在这个func中立即填充了CorePlayer。
VideoViewController.swift
func playVideo(videoFile: URL) {
corePlayer.view().translatesAutoresizingMaskIntoConstraints = false
view.addSubview(corePlayer.view())
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
corePlayer.playURL(videoFile.absoluteURL)
}
尝试使用这个
self.windowController?.showWindow(nil)
self.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
我找到了解决办法
self.ignoresMouseEvents = true;
我喜欢让我的 NSWindow 位于所有应用程序的首位,包括其他应用程序 windows。这怎么可能?
这就是我的自定义 NSWindow 的样子?
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask,
backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: NSWindow.StyleMask.borderless,
backing: NSWindow.BackingStoreType.buffered, defer: false)
// Manage window appearance and size
// this window manages video view controller
self.backgroundColor = NSColor.clear
self.alphaValue = CGFloat(AppSingleton.shared.getVideoOpacity()) / 100
self.isOpaque = false
self.collectionBehavior = .transient
}
这就是我通过按钮操作打开 NSWindowController 的方式
func openPlayerWindow(url: URL) {
let storyboard:NSStoryboard? = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
let playerWindowController = storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SIPlayerWindowController")) as! PlayerWindowController
if let playerWindow = playerWindowController.window {
playerWindowController.showWindow(nil)
playerWindow.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
let videoPlayerController = playerWindow.contentViewController as! VideoPlayerViewController
videoPlayerController.videoFile = url
}
}
PlayerView.swift
import Cocoa
import AVFoundation
class PlayerView: NSView {
override func hitTest(_ point: NSPoint) -> NSView? {
return nil;
}
}
这个PlayerView在这个func中立即填充了CorePlayer。 VideoViewController.swift
func playVideo(videoFile: URL) {
corePlayer.view().translatesAutoresizingMaskIntoConstraints = false
view.addSubview(corePlayer.view())
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint.init(item: corePlayer.view(), attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
corePlayer.playURL(videoFile.absoluteURL)
}
尝试使用这个
self.windowController?.showWindow(nil)
self.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
我找到了解决办法
self.ignoresMouseEvents = true;