From 1b902ee28472d285f45e385102fdb127c6a1f029 Mon Sep 17 00:00:00 2001 From: Vyrtsev Mikhail Date: Wed, 13 Sep 2023 00:15:02 +0300 Subject: [PATCH] Fix previews rendering --- MiddleMe/ContentView.swift | 14 ++++++++------ MiddleMe/MiddleMeApp.swift | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/MiddleMe/ContentView.swift b/MiddleMe/ContentView.swift index d77a8d2..e6c9cbc 100644 --- a/MiddleMe/ContentView.swift +++ b/MiddleMe/ContentView.swift @@ -38,12 +38,14 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - let s1 = Settings() - let s2 = Settings() - let _ = {s2.permissionGranted = true}() - - ContentView().environmentObject(s1) - ContentView().environmentObject(s2) + ContentView().environmentObject(makeSettings(false)) + ContentView().environmentObject(makeSettings(true)) + } + + static func makeSettings(_ granted: Bool) -> Settings { + let settings = Settings() + settings.permissionGranted = granted + return settings } } diff --git a/MiddleMe/MiddleMeApp.swift b/MiddleMe/MiddleMeApp.swift index 6a43266..17a2e49 100644 --- a/MiddleMe/MiddleMeApp.swift +++ b/MiddleMe/MiddleMeApp.swift @@ -16,15 +16,23 @@ struct MiddleMeApp: App { private let settings = Settings() private let handler = GlobalEventMonitor() + private let isPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" + #if DEBUG + private let isDebug = true + #else + private let isDebug = true + #endif init() { + if isPreview { return } + handler.start() handler.register() appLogger.info("Registering login item") - #if !DEBUG - try? SMAppService.mainApp.register() - #endif + if !isPreview && !isDebug { + try? SMAppService.mainApp.register() + } NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: .main, using: self.handleTerminate) } @@ -41,6 +49,8 @@ struct MiddleMeApp: App { settings.checkAccess() } .onAppear { + if isPreview { return } + appLogger.info("Making app active") NSApplication.shared.activate(ignoringOtherApps: true) } @@ -50,9 +60,9 @@ struct MiddleMeApp: App { private func handleTerminate(notification: Notification) { if(appDelegate.activeExit) { appLogger.info("Unregistering login item") - #if !DEBUG - try? SMAppService.mainApp.unregister() - #endif + if !isPreview && !isDebug { + try? SMAppService.mainApp.unregister() + } } } }