From 5341b4450d19f2c2356533ef84713e93b4900f19 Mon Sep 17 00:00:00 2001 From: Gnohz Gniy <0x00eeee@gmail.com> Date: Wed, 17 Apr 2024 19:31:23 +0800 Subject: [PATCH] SWIFT_STRICT_CONCURRENCY = complete --- TextGrabber2.xcodeproj/project.pbxproj | 8 ++++++++ TextGrabber2/Sources/App.swift | 13 ++++++++++--- .../Sources/Extensions/NSPasteboard+Extension.swift | 1 + TextGrabber2/Sources/Recognizer.swift | 2 +- TextGrabber2/Sources/Unchecked.swift | 12 ++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 TextGrabber2/Sources/Unchecked.swift diff --git a/TextGrabber2.xcodeproj/project.pbxproj b/TextGrabber2.xcodeproj/project.pbxproj index da1c9b1..d3df37a 100644 --- a/TextGrabber2.xcodeproj/project.pbxproj +++ b/TextGrabber2.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 8737A24A2BBEC27700042E83 /* Services in Resources */ = {isa = PBXBuildFile; fileRef = 8737A2492BBEC27700042E83 /* Services */; }; 8737A24C2BBECB8800042E83 /* Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8737A24B2BBECB8800042E83 /* Services.swift */; }; + 874D776C2BCF6DF90093662D /* Unchecked.swift in Sources */ = {isa = PBXBuildFile; fileRef = 874D776B2BCF6DF90093662D /* Unchecked.swift */; }; 8772FAE92BAC743D00DBEAA0 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 8772FAE82BAC743D00DBEAA0 /* Localizable.xcstrings */; }; 87E192C32BAADE1200A87A4E /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87E192C22BAADE1200A87A4E /* App.swift */; }; 87E192C72BAADE1300A87A4E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 87E192C62BAADE1300A87A4E /* Assets.xcassets */; }; @@ -32,6 +33,7 @@ /* Begin PBXFileReference section */ 8737A2492BBEC27700042E83 /* Services */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Services; sourceTree = ""; }; 8737A24B2BBECB8800042E83 /* Services.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Services.swift; sourceTree = ""; }; + 874D776B2BCF6DF90093662D /* Unchecked.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Unchecked.swift; sourceTree = ""; }; 8772FAE82BAC743D00DBEAA0 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; 87E192BF2BAADE1200A87A4E /* TextGrabber2.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TextGrabber2.app; sourceTree = BUILT_PRODUCTS_DIR; }; 87E192C22BAADE1200A87A4E /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; @@ -139,6 +141,7 @@ 87E192F02BABB6EA00A87A4E /* Recognizer.swift */, 87E192E82BAAFF0200A87A4E /* Resources.swift */, 8737A24B2BBECB8800042E83 /* Services.swift */, + 874D776B2BCF6DF90093662D /* Unchecked.swift */, ); path = Sources; sourceTree = ""; @@ -224,6 +227,7 @@ 87E192FD2BAC1BD600A87A4E /* NSWindow+Extension.swift in Sources */, 87E192D52BAAE12600A87A4E /* Bundle+Extension.swift in Sources */, 87E192FB2BAC044E00A87A4E /* NSObject+Extension.swift in Sources */, + 874D776C2BCF6DF90093662D /* Unchecked.swift in Sources */, 87E192DE2BAAE38700A87A4E /* NSMenuItem+Extension.swift in Sources */, 87E192E52BAAFB1800A87A4E /* NSWorkspace+Extension.swift in Sources */, 87E192D82BAAE17200A87A4E /* main.swift in Sources */, @@ -286,6 +290,7 @@ "DEBUG=1", "$(inherited)", ); + GCC_TREAT_WARNINGS_AS_ERRORS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -301,6 +306,7 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_STRICT_CONCURRENCY = complete; }; name = Debug; }; @@ -345,6 +351,7 @@ ENABLE_USER_SCRIPT_SANDBOXING = YES; GCC_C_LANGUAGE_STANDARD = gnu17; GCC_NO_COMMON_BLOCKS = YES; + GCC_TREAT_WARNINGS_AS_ERRORS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -358,6 +365,7 @@ SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_STRICT_CONCURRENCY = complete; }; name = Release; }; diff --git a/TextGrabber2/Sources/App.swift b/TextGrabber2/Sources/App.swift index e0924de..519989f 100644 --- a/TextGrabber2/Sources/App.swift +++ b/TextGrabber2/Sources/App.swift @@ -8,6 +8,7 @@ import AppKit import ServiceManagement +@MainActor final class App: NSObject, NSApplicationDelegate { private var currentResult: Recognizer.ResultData? private var pasteboardObserver: Timer? @@ -163,11 +164,17 @@ extension App: NSMenuDelegate { // For an edge case, we can capture the screen while the menu is shown. let timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in - guard let self, NSPasteboard.general.changeCount != self.pasteboardChangeCount else { + guard let self else { return } - DispatchQueue.main.async(execute: startDetection) + DispatchQueue.main.async { + guard NSPasteboard.general.changeCount != self.pasteboardChangeCount else { + return + } + + self.startDetection() + } } pasteboardObserver = timer @@ -196,7 +203,7 @@ private extension App { statusItem.menu?.removeItems { $0 is ResultItem } } - @MainActor func startDetection() { + func startDetection() { guard let menu = statusItem.menu else { return Logger.assertFail("Missing menu to proceed") } diff --git a/TextGrabber2/Sources/Extensions/NSPasteboard+Extension.swift b/TextGrabber2/Sources/Extensions/NSPasteboard+Extension.swift index 9abff6b..5e66217 100644 --- a/TextGrabber2/Sources/Extensions/NSPasteboard+Extension.swift +++ b/TextGrabber2/Sources/Extensions/NSPasteboard+Extension.swift @@ -39,6 +39,7 @@ extension NSPasteboard { return (readObjects(forClasses: [NSImage.self]) as? [NSImage])?.first } + @MainActor func saveImageAsFile() { if #available(macOS 14.0, *) { NSApp.activate() diff --git a/TextGrabber2/Sources/Recognizer.swift b/TextGrabber2/Sources/Recognizer.swift index 36f55da..600635f 100644 --- a/TextGrabber2/Sources/Recognizer.swift +++ b/TextGrabber2/Sources/Recognizer.swift @@ -6,7 +6,7 @@ // import AppKit -import Vision +@preconcurrency import Vision /** https://developer.apple.com/documentation/vision/recognizing_text_in_images diff --git a/TextGrabber2/Sources/Unchecked.swift b/TextGrabber2/Sources/Unchecked.swift new file mode 100644 index 0000000..79edb93 --- /dev/null +++ b/TextGrabber2/Sources/Unchecked.swift @@ -0,0 +1,12 @@ +// +// Unchecked.swift +// TextGrabber2 +// +// Created by cyan on 2024/4/17. +// + +import AppKit +import os.log + +extension NSMenuItem: @unchecked Sendable {} +extension os.Logger: @unchecked Sendable {}