Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加第三方截图 App 支持: Longshot #253

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://cdn.cocoapods.org/'
install! 'cocoapods', :generate_multiple_pod_projects => true
inhibit_all_warnings!
platform :osx, '10.15'
platform :osx, '11.0'
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

Expand All @@ -14,7 +14,7 @@ post_install do |installer|
installer.generated_projects.each do |project|
project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.15'
config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '11.0'
end
end
end
Expand Down
17 changes: 7 additions & 10 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
PODS:
- libminipng (0.5.6)
- WCDB.swift (2.0.2.5):
- WCDB.swift/no-arc (= 2.0.2.5)
- WCDBOptimizedSQLCipher (~> 1.4.1)
- WCDB.swift/no-arc (2.0.2.5):
- WCDBOptimizedSQLCipher (~> 1.4.1)
- WCDBOptimizedSQLCipher (1.4.1)
- WCDB.swift (2.1.7):
- WCDBOptimizedSQLCipher (= 1.4.5)
- WCDBOptimizedSQLCipher (1.4.5)

DEPENDENCIES:
- libminipng
Expand All @@ -19,9 +16,9 @@ SPEC REPOS:

SPEC CHECKSUMS:
libminipng: a44c35d06b9d54d6640acdf97f4500c034748abb
WCDB.swift: 41d30d0e113cb2032947e34998f9eedc5c8cbd71
WCDBOptimizedSQLCipher: 880ec8aa6cda6b27e4c72b30bf0cc01771deda6f
WCDB.swift: 30acc4e15818d08f2ccc23ce86bec6e5776eb218
WCDBOptimizedSQLCipher: a7e8fe818985dd401c39eb1bcc985bc4d92b2d06

PODFILE CHECKSUM: 578e68ce0a8889942b52298dac45365f59c42985
PODFILE CHECKSUM: fde829835db9a104290fb31af3810cb8b291f4c5

COCOAPODS: 1.14.2
COCOAPODS: 1.15.2
103 changes: 58 additions & 45 deletions uPic.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion uPic.xcodeproj/xcshareddata/xcschemes/uPic.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
35 changes: 18 additions & 17 deletions uPic.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"originHash" : "514b67057fc540806048159de4fca077afcd3f38b7321469c99153ae88ea4300",
"pins" : [
{
"identity" : "alamofire",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Alamofire/Alamofire",
"state" : {
"revision" : "bc268c28fb170f494de9e9927c371b8342979ece",
"version" : "5.7.1"
"revision" : "f455c2975872ccd2d9c81594c658af65716e9b9a",
"version" : "5.9.1"
}
},
{
Expand All @@ -23,17 +24,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/CocoaLumberjack/CocoaLumberjack.git",
"state" : {
"revision" : "0188d31089b5881a269e01777be74c7316924346",
"version" : "3.8.0"
"revision" : "4b8714a7fb84d42393314ce897127b3939885ec3",
"version" : "3.8.5"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift",
"state" : {
"revision" : "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version" : "1.7.2"
"revision" : "678d442c6f7828def400a70ae15968aef67ef52d",
"version" : "1.8.3"
}
},
{
Expand All @@ -50,17 +51,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Kingfisher",
"state" : {
"revision" : "3f6992b5cd3143e83b02300ea59c400d4cf0747a",
"version" : "7.8.0"
"revision" : "2ef543ee21d63734e1c004ad6c870255e8716c50",
"version" : "7.12.0"
}
},
{
"identity" : "launchatlogin",
"identity" : "launchatlogin-legacy",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/LaunchAtLogin",
"location" : "https://github.com/sindresorhus/LaunchAtLogin-Legacy",
"state" : {
"revision" : "7ad6331f9c38953eb1ce8737758e18f7607e984a",
"version" : "5.0.0"
"revision" : "9a894d799269cb591037f9f9cb0961510d4dca81",
"version" : "5.0.2"
}
},
{
Expand All @@ -77,8 +78,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SnapKit/SnapKit",
"state" : {
"revision" : "f222cbdf325885926566172f6f5f06af95473158",
"version" : "5.6.0"
"revision" : "2842e6e84e82eb9a8dac0100ca90d9444b0307f4",
"version" : "5.7.1"
}
},
{
Expand Down Expand Up @@ -185,8 +186,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SwiftyJSON/SwiftyJSON",
"state" : {
"revision" : "b3dcd7dbd0d488e1a7077cb33b00f2083e382f07",
"version" : "5.0.1"
"revision" : "af76cf3ef710b6ca5f8c05f3a31307d44a3c5828",
"version" : "5.0.2"
}
},
{
Expand All @@ -208,5 +209,5 @@
}
}
],
"version" : 2
"version" : 3
}
80 changes: 42 additions & 38 deletions uPic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import ScriptingBridge
import MASShortcut
import LaunchAtLogin


@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

Expand Down Expand Up @@ -113,16 +112,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Finder context menu file upload listener
@objc func uploadFilesFromFinderMenu(notification: Notification) {
let pathStr = notification.object as? String ?? ""
Logger.shared.verbose("收到来自 Finder Menu 的上传请求-\(pathStr)")
Logger.shared.verbose("收到来自 Finder Menu 的上传请求: \(pathStr)")
uploadFilesFromPaths(pathStr)
}

@objc func handleGetURLEvent(event: NSAppleEventDescriptor!, withReplyEvent: NSAppleEventDescriptor!) {
if let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue{
Logger.shared.verbose("收到来自 URLScheme 的上传请求-\(urlString)")
Logger.shared.verbose("收到来自 URLScheme 的上传请求: \(urlString)")
URLSchemeExt.shared.handleURL(urlString)
} else {
Logger.shared.warn("收到来自 URLScheme 的上传请求-无效参数")
Logger.shared.warn("收到来自 URLScheme 的上传请求: 无效参数")
}
}
}
Expand Down Expand Up @@ -178,13 +177,7 @@ extension AppDelegate {
.rightMouseUp, .rightMouseDown])

// 注册拖拽文件格式支持。使其支持浏览器拖拽的URL、tiff。以及Safari 有些情况(例如,百度搜图,在默认搜索列表。不进入详情时)下拖拽的时候获取到的是图片URL字符串
if #available(OSX 10.13, *) {
button.window?.registerForDraggedTypes([.URL, .fileURL, .string, .html])
} else {
// Fallback on earlier versions
button.window?.registerForDraggedTypes([.png, .tiff, .pdf, .string, .html])
}

button.window?.registerForDraggedTypes([.URL, .fileURL, .string, .html])
}
}

Expand Down Expand Up @@ -345,34 +338,45 @@ extension AppDelegate {
return
}

// 截图权限检测
Logger.shared.verbose("检查屏幕录制权限")
let hasPermission = ScreenUtil.screeningRecordPermissionCheck()
if !hasPermission {
Logger.shared.warn("无截图权限,申请截图权限并弹出帮助界面、跳转到设置界面")
// 无截图权限,申请截图权限并弹出帮助界面、跳转到设置界面
ScreenUtil.requestRecordScreenPermissions()
if ScreenUtil.getScreenshotApp() == 0 {
Logger.shared.info("使用 macOS 自带截图工具截图")

_ = WindowManager.shared.showWindow(storyboard: "ScreenshotAuthorizationHelp", withIdentifier: "screenshotAuthorizationHelpWindowController")
return
}
Logger.shared.verbose("屏幕录制权限已获取, 开始截图")

let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-i", "-c"]
task.launch()
task.waitUntilExit()

Logger.shared.info("截图上传格式:\(NSPasteboard.general.types?.first?.rawValue ?? "")")
if (NSPasteboard.general.types?.first == NSPasteboard.PasteboardType.png) {
Logger.shared.info("截图上传 PNG")
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.png)
self.uploadFiles([imgData!])
} else if (NSPasteboard.general.types?.first == NSPasteboard.PasteboardType.jpeg) {
Logger.shared.info("截图上传 JPEG")
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.jpeg)
self.uploadFiles([imgData!])
// 截图权限检测
Logger.shared.verbose("检查屏幕录制权限")
let hasPermission = ScreenUtil.screeningRecordPermissionCheck()
if !hasPermission {
Logger.shared.warn("无截图权限,申请截图权限并弹出帮助界面、跳转到设置界面")
// 无截图权限,申请截图权限并弹出帮助界面、跳转到设置界面
ScreenUtil.requestRecordScreenPermissions()

_ = WindowManager.shared.showWindow(storyboard: "ScreenshotAuthorizationHelp", withIdentifier: "screenshotAuthorizationHelpWindowController")
return
}
Logger.shared.verbose("屏幕录制权限已获取, 开始截图")

let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-i", "-c"]
task.launch()
task.waitUntilExit()

Logger.shared.info("截图上传格式:\(NSPasteboard.general.types?.first?.rawValue ?? "")")
if (NSPasteboard.general.types?.first == NSPasteboard.PasteboardType.png) {
Logger.shared.info("截图上传 PNG")
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.png)
self.uploadFiles([imgData!])
} else if (NSPasteboard.general.types?.first == NSPasteboard.PasteboardType.jpeg) {
Logger.shared.info("截图上传 JPEG")
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.jpeg)
self.uploadFiles([imgData!])
}

} else {
Logger.shared.info("使用 Longshot 截图")
guard let url = URL(string: "longshot://x-callback-url/snip?func=start&channel=clipboard&type=data&x-source=uPic&x-success=uPic://x-callback-url/acceptSnip?x-source=longshot&x-error=uPic://x-callback-url/snipError?x-source=longshot&errorMessage=message") else {
return
}
NSWorkspace.shared.open(url)
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions uPic/Assets.xcassets/LongShot.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "LongShot.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions uPic/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="23094" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23094"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down
16 changes: 2 additions & 14 deletions uPic/Basic/PasteboardType+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,11 @@ extension NSPasteboard.PasteboardType {
// MARK: 剪切板扩展,让 10.13 以前的版本也支持 FileUrl 类型

static let backwardsCompatibleFileURL: NSPasteboard.PasteboardType = {

if #available(OSX 10.13, *) {
return NSPasteboard.PasteboardType.fileURL
} else {
return NSPasteboard.PasteboardType(kUTTypeFileURL as String)
}

return NSPasteboard.PasteboardType.fileURL
} ()

static let backwardsCompatibleURL: NSPasteboard.PasteboardType = {

if #available(OSX 10.13, *) {
return NSPasteboard.PasteboardType.URL
} else {
return NSPasteboard.PasteboardType(kUTTypeURL as String)
}

return NSPasteboard.PasteboardType.URL
} ()

static let gif: NSPasteboard.PasteboardType = kUTType(kUTTypeGIF)
Expand Down
Loading