SwiftUI macos NSWindow 实例

hil*_*ark 8 macos nswindow ios swift swiftui

使用xcode 12.3和swift 5.3与SwiftUI App生命周期构建一个macOS应用程序,什么是最好的方式来访问和更改的外观和行为NSWindow?

编辑:我真正追求的是NSWindow实例。

我添加了一个AppDelegate,但据我了解它NSWindow很可能是nil,因此无法修改,只需在此处创建一个类似于AppKit App Delegate生命周期方法的方法,就会在启动时出现两个窗口。

一种解决方案是防止默认窗口出现,并将其全部留给applicationDidFinishLaunching方法,但不确定这是可能的或明智的。

该WindowStyle协议看起来是一种可能的解决方案,但不确定CustomWindowStyle在此阶段如何最好地利用它,以及是否提供对NSWindow实例的访问以进行细粒度控制。

class AppDelegate: NSObject, NSApplicationDelegate {        
    func applicationDidFinishLaunching(_ aNotification: Notification) {
      // In AppKit simply create the NSWindow and modify style.
      // In SwiftUI creating an NSWindow and styling results in 2 windows, 
      // one styled and the other default.
    }
}

@main
struct testApp: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate : AppDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 12

有趣的方法@hillmark。

我还通过使用NSApplicationDelegateAdaptor.

我相信下面的代码只能帮助您处理单窗口 MacOS SwiftUI 应用程序 - 多窗口应用程序可能需要处理所有窗口,而不是只处理第一个窗口。

我也不确定我的方法是否有任何警告。

目前,这准确地解决了我的问题,但我也会检查你的方法。

import SwiftUI

final class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        if let window = NSApplication.shared.windows.first {
            window.titleVisibility = .hidden
            window.titlebarAppearsTransparent = true
            window.isOpaque = false
            window.backgroundColor = NSColor.clear
        }
    }
}

@main
struct AlreadyMacOSApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
    var body: some Scene {
        WindowGroup {
            NewLayoutTest()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


hil*_*ark 6

虽然我不完全确定这是否是正确的方法,但基于对这个问题的回答:https : //stackoverflow.com/a/63439982/792406我已经能够访问该NSWindow实例并修改其外观。

为了便于参考,这里的基础上所提供的原始代码的工作示例Asperi使用xcode 12.3,swift 5.3以及SwiftUI应用程序生命周期。

@main
struct testApp: App {    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class Store {
    var window: NSWindow
    
    init(window: NSWindow) {
        self.window = window
        self.window.isOpaque = false
        self.window.backgroundColor = NSColor.clear
    }
}

struct ContentView: View {
    @State private var window: NSWindow?
    var body: some View {
        VStack {
            Text("Loading...")
            if nil != window {
                MainView(store: Store(window: window!))
            }
        }.background(WindowAccessor(window: $window))
    }
}

struct MainView: View {

    let store: Store
    
    var body: some View {
        VStack {
            Text("MainView with Window: \(store.window)")
        }.frame(width: 400, height: 400)
    }
}

struct WindowAccessor: NSViewRepresentable {
    @Binding var window: NSWindow?
    
    func makeNSView(context: Context) -> NSView {
        let view = NSView()
        DispatchQueue.main.async {
            self.window = view.window
        }
        return view
    }
    
    func updateNSView(_ nsView: NSView, context: Context) {}
}
Run Code Online (Sandbox Code Playgroud)