Skip to content

Commit

Permalink
add notifyWhenDone option
Browse files Browse the repository at this point in the history
  • Loading branch information
ku committed Feb 19, 2020
1 parent a8022f0 commit d0554a2
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 47 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ convert selected area into text by Google [Cloud Vision API](https://cloud.googl
# build

```
carthage checkout --use-ssh
carthage build --platform Mac
```

Expand Down
12 changes: 10 additions & 2 deletions screentextcapture.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
8814FA1123FD5A8A00F376D8 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8814FA1023FD5A8A00F376D8 /* Preferences.swift */; };
8889BCE523F9302F0069A82F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8889BCE423F9302F0069A82F /* AppDelegate.swift */; };
8889BCE723F9302F0069A82F /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8889BCE623F9302F0069A82F /* ViewController.swift */; };
8889BCE923F930300069A82F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8889BCE823F930300069A82F /* Assets.xcassets */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
8814FA1023FD5A8A00F376D8 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
8889BCE123F9302F0069A82F /* screentextcapture.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = screentextcapture.app; sourceTree = BUILT_PRODUCTS_DIR; };
8889BCE423F9302F0069A82F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
8889BCE623F9302F0069A82F /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -114,6 +116,7 @@
8889BD0A23F954CF0069A82F /* ScreenCapture.swift */,
8889BD0823F940D80069A82F /* ScreenCapturePermissionManager.swift */,
8889BCE623F9302F0069A82F /* ViewController.swift */,
8814FA1023FD5A8A00F376D8 /* Preferences.swift */,
8889BD0C23F959A90069A82F /* Annotator.swift */,
8889BCE823F930300069A82F /* Assets.xcassets */,
8889BCEA23F930300069A82F /* Main.storyboard */,
Expand Down Expand Up @@ -268,6 +271,7 @@
8889BD0923F940D80069A82F /* ScreenCapturePermissionManager.swift in Sources */,
8889BD0323F93CC80069A82F /* KeyStore.swift in Sources */,
8889BCE523F9302F0069A82F /* AppDelegate.swift in Sources */,
8814FA1123FD5A8A00F376D8 /* Preferences.swift in Sources */,
8889BD0B23F954CF0069A82F /* ScreenCapture.swift in Sources */,
8889BD0723F93DCF0069A82F /* Api.swift in Sources */,
8889BD0523F93DB60069A82F /* CloudVision.swift in Sources */,
Expand Down Expand Up @@ -424,6 +428,7 @@
CODE_SIGN_ENTITLEMENTS = screentextcapture/screentextcapture.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = A36Y9V3HVF;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -435,7 +440,8 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = nu.ido.screentextcapture;
MARKETING_VERSION = 0.0.2;
PRODUCT_BUNDLE_IDENTIFIER = nu.ido.ku.screentextcapture;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
};
Expand All @@ -448,6 +454,7 @@
CODE_SIGN_ENTITLEMENTS = screentextcapture/screentextcapture.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = A36Y9V3HVF;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -459,7 +466,8 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = nu.ido.screentextcapture;
MARKETING_VERSION = 0.0.2;
PRODUCT_BUNDLE_IDENTIFIER = nu.ido.ku.screentextcapture;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
};
Expand Down
29 changes: 19 additions & 10 deletions screentextcapture/Annotator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ class Annotator {
case .failure(let error):
if let error = error as? ApplicationError,
error == .cancelled {
self.exit()
DispatchQueue.main.async {
guard let appDelegate = NSApp.self?.delegate as? AppDelegate else {
self.exit()
return
}
appDelegate.startExitTimer()
}
} else {
self.notify(error: error, id: UUID().uuidString)
}
Expand All @@ -38,29 +44,33 @@ class Annotator {
guard let self = self else { return }
switch result {
case .success(let text):
self.copy(text)
self.notify(text: "Captured successfully")
if self.terminateOnCopy {
self.exit()
}
self.extracted(text: text)
case .failure(let error):
self.notify(error: error, id: Annotator.apiErrorNotificationId)
}
})
}

private func extracted(text: String) {
copy(text)
if Preferences().notifyWhenDone {
notify(text: text)
}
if terminateOnCopy {
exit()
}
}

private func notify(error: Error, id: String) {
let notification = NSUserNotification()
notification.identifier = UUID().uuidString
notification.title = "ScreenTextCapture"
notification.informativeText = error.localizedDescription
notification.soundName = "Sosumi"
notification.hasActionButton = true
notification.actionButtonTitle = "設定"
notification.actionButtonTitle = "Preferences"
notification.hasActionButton = true
notification.otherButtonTitle = "Close"
notification.actionButtonTitle = "Show"
// notification.contentImage = NSImage(contentsOfURL: NSURL(string: "https://placehold.it/300")!)
NSUserNotificationCenter.default.deliver(notification)
}

Expand All @@ -70,7 +80,6 @@ class Annotator {
notification.title = "ScreenTextCapture"
notification.informativeText = text
notification.soundName = NSUserNotificationDefaultSoundName
// notification.contentImage = NSImage(contentsOfURL: NSURL(string: "https://placehold.it/300")!)
NSUserNotificationCenter.default.deliver(notification)
}

Expand Down
35 changes: 23 additions & 12 deletions screentextcapture/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,38 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if let _ = KeyStore().get() {
annotator.capture()
} else {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let mainWC = storyboard.instantiateController(withIdentifier: "MainWindowController") as? NSWindowController else {
fatalError("Error getting main window controller")
}
// optionally store the reference here
// self.mainWindowController = mainWC

mainWC.window?.makeKeyAndOrderFront(nil)
show()
}
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


@IBAction private func preferences(_ sender: Any) {
exitTimer?.invalidate()
show()
}

private func show() {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let mainWC = storyboard.instantiateController(withIdentifier: "MainWindowController") as? NSWindowController else {
fatalError("Error getting main window controller")
}
mainWC.window?.makeKeyAndOrderFront(nil)
}

private var exitTimer: Timer?

func startExitTimer() {
exitTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false, block: { _ in
NSApp.self?.terminate(nil)
})
}
}
extension AppDelegate: NSUserNotificationCenterDelegate {
func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}

func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
print(1)
}
}
Loading

0 comments on commit d0554a2

Please sign in to comment.