大佬教程收集整理的这篇文章主要介绍了按 NSMenuItem 时打开并将新窗口置于最前面,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个作为代理 (lsuielement) 运行的 macOS 菜单应用程序。
我需要它有一个配套的设置窗口。 NSMenu中有一个“设置”NSMenuItem,要求是打开一个实际的窗口,按下时将它带到最前面。
窗口是 SwiftUI 驱动的。这是它的工作原理:
response.data?.let { brastlewarkTown ->
hereIwantTheObject = brastlewarkTown
Log.i(tag,"onCreateVIEw: ${hereIwantTheObject!!.namE}")
}
// main is an NSMenu
main.addItem(
withtitle: "SetTings",action: #SELEctor(AppDelegate.openSetTings),keyEquivalent: "")
@objc func openSetTings() {
let detailVIEw = SetTingsWindow(); // Swift UI vIEw
let controller = DetailWindowController(rootVIEw: detailVIEw) // See below
controller.window?.title = "SetTings";
controller.showWindow(nil)
NSApp.activate(ignoringOtherApps: truE)
}
当前的行为是窗口打开,但它总是在当前处于前台的其他窗口之后。
我需要它在前台。
上面代码中的class DetailWindowController<RootVIEw : VIEw>: NSWindowController {
convenIEnce init(rootVIEw: RootVIEw) {
let hosTingController = NSHosTingController(rootVIEw: rootVIEw.frame(wIDth: 400,height: 500))
let window = NSWindow(contentVIEwController: hosTingController)
window.setContentSize(NSSize(wIDth: 400,height: 500))
self.init(window: window)
}
}
试图实现这一点,但没有奏效。
任何帮助都会很棒。非常感谢。
最后想通了。我需要这 3 个。激活策略应该得到更好的管理,但它的价值在这里是有效的。
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: truE)
setTingsWindow!.window?.orderFrontRegardless()
在上下文中
@objc func openSetTings() {
if(setTingsWindow == nil) {
let detailView = ActionWindow();
setTingsWindow = DetailWindowController(rootView: detailView)
setTingsWindow!.window?.title = "Cloud Brains - SetTings";
setTingsWindow!.showWindow(nil)
}
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: truE)
setTingsWindow!.window?.orderFrontRegardless()
}
以上是大佬教程为你收集整理的按 NSMenuItem 时打开并将新窗口置于最前面全部内容,希望文章能够帮你解决按 NSMenuItem 时打开并将新窗口置于最前面所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。