在连接的显示器上打开 NSWIndow / NSView
Opening an NSWIndow / NSView on a connected display
环境: macOS Big Sur 11.5.2 / Swift 5.4
目标平台: macOS
我在搜索如何在连接的显示器上打开新的 NSWindow / NSView 时遇到 this discussion。
我将 obj-c 代码转换为 Swift,但我无法让它工作。我在 IBAction 中设置了该方法,并将该操作链接到我放置在标题为“显示外部显示”的视图中的按钮。
我做错了什么?
import Cocoa
class ViewController: NSViewController {
var externalDisplay: NSScreen?
var fullScreenWindow: NSWindow?
var fullScreenView: NSView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func showExternalDisplayButtonClicked(sender: Any?) {
// Open a window on the external display if present
if NSScreen.screens.count > 1 {
externalDisplay = NSScreen.screens.last
let externalDisplayRect = externalDisplay!.frame
fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: .borderless, backing: .buffered, defer: true, screen: externalDisplay)
fullScreenWindow!.level = .normal
fullScreenWindow!.isOpaque = false
fullScreenWindow!.hidesOnDeactivate = false
fullScreenWindow!.backgroundColor = .red
let viewRect = NSRect(x: 0, y: 0, width: externalDisplay!.frame.width, height: externalDisplay!.frame.height)
fullScreenView = NSView(frame: viewRect)
fullScreenWindow!.contentView = fullScreenView
fullScreenWindow!.makeKeyAndOrderFront(self)
}
}
}
为什么要制作styleMask: .borderless
?文档说无边界 window 不能成为 key 或 main 所以 makeKeyAndOrderFront
失败。
如果你像这样制作一个正常的window:
let mask: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: mask, backing: .buffered, defer: true, screen: externalDisplay)
它在最后一个屏幕上打开。
环境: macOS Big Sur 11.5.2 / Swift 5.4
目标平台: macOS
我在搜索如何在连接的显示器上打开新的 NSWindow / NSView 时遇到 this discussion。
我将 obj-c 代码转换为 Swift,但我无法让它工作。我在 IBAction 中设置了该方法,并将该操作链接到我放置在标题为“显示外部显示”的视图中的按钮。
我做错了什么?
import Cocoa
class ViewController: NSViewController {
var externalDisplay: NSScreen?
var fullScreenWindow: NSWindow?
var fullScreenView: NSView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func showExternalDisplayButtonClicked(sender: Any?) {
// Open a window on the external display if present
if NSScreen.screens.count > 1 {
externalDisplay = NSScreen.screens.last
let externalDisplayRect = externalDisplay!.frame
fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: .borderless, backing: .buffered, defer: true, screen: externalDisplay)
fullScreenWindow!.level = .normal
fullScreenWindow!.isOpaque = false
fullScreenWindow!.hidesOnDeactivate = false
fullScreenWindow!.backgroundColor = .red
let viewRect = NSRect(x: 0, y: 0, width: externalDisplay!.frame.width, height: externalDisplay!.frame.height)
fullScreenView = NSView(frame: viewRect)
fullScreenWindow!.contentView = fullScreenView
fullScreenWindow!.makeKeyAndOrderFront(self)
}
}
}
为什么要制作styleMask: .borderless
?文档说无边界 window 不能成为 key 或 main 所以 makeKeyAndOrderFront
失败。
如果你像这样制作一个正常的window:
let mask: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: mask, backing: .buffered, defer: true, screen: externalDisplay)
它在最后一个屏幕上打开。