Unified · Split
| @@ -52,6 +52,19 @@ extension NSEvent.ModifierFlags { · 51 unmodified lines | ||
| 52 | 52 | public static let optionRight: NSEvent.ModifierFlags = .init(rawValue: UInt(NX_DEVICERALTKEYMASK)) |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | +extension String { | |
| 56 | + func isUrl() -> Bool { | |
| 57 | + guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$", | |
| 58 | + options: .caseInsensitive) else { | |
| 59 | + return false | |
| 60 | + } | |
| 61 | + let isUrl = regex.numberOfMatches(in: self, | |
| 62 | + options: [], | |
| 63 | + range: NSRange(location: 0, length: self.count)) | |
| 64 | + return isUrl > 0 | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 55 | 68 | extension mp_keymap { |
| 56 | 69 | init(_ f: Int, _ t: Int32) { |
| 57 | 70 | self.init(from: Int32(f), to: t) |
| @@ -65,17 +65,6 @@ class View: NSView, CALayerDelegate { · 64 unmodified lines | ||
| 65 | 65 | return [] |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | − func isURL(_ str: String) -> Bool { | |
| 69 | − guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$", | |
| 70 | − options: .caseInsensitive) else { | |
| 71 | − return false | |
| 72 | − } | |
| 73 | − let isURL = regex.numberOfMatches(in: str, | |
| 74 | − options: [], | |
| 75 | − range: NSRange(location: 0, length: str.count)) | |
| 76 | − return isURL > 0 | |
| 77 | − } | |
| 78 | − | |
| 79 | 68 | override func performDragOperation(_ sender: NSDraggingInfo) -> Bool { |
| 80 | 69 | let pb = sender.draggingPasteboard |
| 81 | 70 | guard let types = pb.types else { return false } |
| @@ -89,7 +78,7 @@ class View: NSView, CALayerDelegate { · 7 unmodified lines | ||
| 89 | 78 | files = str.components(separatedBy: "\n").compactMap { |
| 90 | 79 | let url = $0.trimmingCharacters(in: .whitespacesAndNewlines) |
| 91 | 80 | let path = (url as NSString).expandingTildeInPath |
| 92 | − if isURL(url) { return url } | |
| 81 | + if url.isUrl() { return url } | |
| 93 | 82 | if path.starts(with: "/") { return path } |
| 94 | 83 | return nil |
| 95 | 84 | } |