gpt4 book ai didi

swift - 未打开的 NSWindow isVisible 属性设置为 true

转载 作者:行者123 更新时间:2023-11-30 12:50:53 24 4
gpt4 key购买 nike

我正在尝试使用 NSWindowisVisible 属性来检测 NSWindow 是打开还是关闭,但它没有像我那样工作预期的。例如,我重写了 NSWindowControllerloadWindow 方法(我需要在扩展屏幕上显示全屏网页):

override func loadWindow() {
self.contentController = WKUserContentController();

guard let contentController = self.contentController else {
return
}

let config = WKWebViewConfiguration()
config.userContentController = contentController


let externalScreens = NSScreen.externalScreens()
let screen = externalScreens.count == 0 ? NSScreen.main()! : externalScreens[0]

window = KeyWindow(
contentRect: NSRect(x: 0, y: 0, width: screen.frame.width, height: screen.frame.height),
styleMask: NSBorderlessWindowMask,
backing: NSBackingStoreType.buffered,
defer: false,
screen: screen
)

if let w = window {
w.level = Int(CGShieldingWindowLevel())
w.backgroundColor = NSColor.black
w.makeKeyAndOrderFront(self)
w.makeFirstResponder(self)

webView = WKWebView(frame: w.frame, configuration: config)
w.contentView = webView!

debugPrint("Window is visible = \(w.isVisible)")
}
}

键窗口:

import Foundation
import AppKit

class KeyWindow : NSWindow {
override var canBecomeKey: Bool {
return true
}
}

但是 debugPrint 显示 isVisible 属性设置为 true,尽管窗口尚未打开(尚未调用 Controller 的 self.showWindow(self) 方法) .

如何可靠地确定窗口是否打开(显示在屏幕上)?

最佳答案

在你的代码中

if let w = window {
w.level = Int(CGShieldingWindowLevel())
w.backgroundColor = NSColor.black
w.makeKeyAndOrderFront(self)
w.makeFirstResponder(self)

webView = WKWebView(frame: w.frame, configuration: config)
w.contentView = webView!

debugPrint("Window is visible = \(w.isVisible)")
}

您正在调用w.makeKeyAndOrderFront(self)检查前根据Apple documentation :

func makeKeyAndOrderFront(Any?) Moves the window to the front of the screen list, within its level, and makes it the key window; that is, it shows the window.

所以从技术上来说 isVisible正如广告所宣传的那样:) isVisible

The value of this property is true when the window is onscreen (even if it’s obscured by other windows); otherwise, false.

您的窗口应该在屏幕上 - 尽管您已将其级别设置为 CGShieldingWindowLevel它可能只是因为调用而看不见 w.makeKeyAndOrderFront(self)您可以尝试调用func orderFrontRegardless()

Apple Doc

在这种情况下,它将立即显示窗口 - 但我认为这是另一个 SO 问题

关于swift - 未打开的 NSWindow isVisible 属性设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998908/

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