Add open from clipboard

This commit is contained in:
Arkadiusz Fal 2022-11-10 21:47:27 +01:00
parent 4a26ef2839
commit 9eb8228fcf
3 changed files with 103 additions and 34 deletions

View File

@ -1,5 +1,11 @@
#if canImport(AppKit)
import AppKit
#endif
import Foundation import Foundation
import Logging import Logging
#if canImport(UIKit)
import UIKit
#endif
struct OpenVideosModel { struct OpenVideosModel {
enum PlaybackMode: String, CaseIterable { enum PlaybackMode: String, CaseIterable {
@ -42,6 +48,24 @@ struct OpenVideosModel {
} }
} }
var urlsFromClipboard: [URL] {
#if os(iOS)
if let pasteboard = UIPasteboard.general.string {
return urlsFrom(pasteboard)
}
#elseif os(macOS)
if let pasteboard = NSPasteboard.general.string(forType: .string) {
return urlsFrom(pasteboard)
}
#endif
return []
}
func openURLsFromClipboard(removeQueueItems: Bool = false, playbackMode: OpenVideosModel.PlaybackMode) {
openURLs(urlsFromClipboard, removeQueueItems: removeQueueItems, playbackMode: playbackMode)
}
func openURLs(_ urls: [URL], removeQueueItems: Bool, playbackMode: OpenVideosModel.PlaybackMode) { func openURLs(_ urls: [URL], removeQueueItems: Bool, playbackMode: OpenVideosModel.PlaybackMode) {
logger.info("opening \(urls.count) urls") logger.info("opening \(urls.count) urls")
urls.forEach { logger.info("\($0.absoluteString)") } urls.forEach { logger.info("\($0.absoluteString)") }
@ -102,6 +126,10 @@ struct OpenVideosModel {
} }
} }
func urlsFrom(_ string: String) -> [URL] {
string.split(whereSeparator: \.isNewline).compactMap { URL(string: String($0)) }
}
var canOpenVideosByID: Bool { var canOpenVideosByID: Bool {
guard let app = player.accounts.current?.app else { return false } guard let app = player.accounts.current?.app else { return false }
return !player.accounts.isEmpty && app.supportsOpeningVideosByID return !player.accounts.isEmpty && app.supportsOpeningVideosByID

View File

@ -82,6 +82,7 @@ struct OpenURLHandler {
player.hide() player.hide()
navigation.presentingChannel = false navigation.presentingChannel = false
navigation.presentingPlaylist = false navigation.presentingPlaylist = false
navigation.presentingOpenVideos = false
} }
private func urlByReplacingYatteeProtocol(_ url: URL, with urlProtocol: String = "https") -> URL! { private func urlByReplacingYatteeProtocol(_ url: URL, with urlProtocol: String = "https") -> URL! {

View File

@ -74,44 +74,16 @@ struct OpenVideosView: View {
HStack { HStack {
Group { Group {
Button { openURLsButton
openURLs(urlsToOpenFromText)
} label: {
HStack {
Image(systemName: "network")
Text("Open URLs")
.fontWeight(.bold)
.padding(.vertical, 10)
}
.padding(.horizontal, 20)
}
.disabled(urlsToOpenFromText.isEmpty)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
Spacer() Spacer()
Button { openFromClipboardButton
presentingFileImporter = true
} label: {
HStack {
Image(systemName: "folder")
Text("Open Files")
.fontWeight(.bold)
.padding(.vertical, 10)
}
.padding(.horizontal, 20)
}
} }
.foregroundColor(.accentColor)
.background(
RoundedRectangle(cornerRadius: 4)
.foregroundColor(Color.accentColor.opacity(0.33))
)
} }
.buttonStyle(.plain) .padding(.bottom, 10)
openFilesButton
Spacer() Spacer()
} }
@ -146,8 +118,76 @@ struct OpenVideosView: View {
#endif #endif
} }
var openURLsButton: some View {
Button {
openURLs(urlsToOpenFromText)
} label: {
HStack {
Image(systemName: "network")
Text("Open URLs")
.fontWeight(.bold)
.padding(.vertical, 10)
}
.padding(.horizontal, 20)
}
.foregroundColor(.accentColor)
.buttonStyle(.plain)
.background(buttonBackground)
.disabled(urlsToOpenFromText.isEmpty)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
}
var openFromClipboardButton: some View {
Button {
OpenVideosModel.shared.openURLsFromClipboard(
removeQueueItems: removeQueueItems,
playbackMode: playbackMode
)
} label: {
HStack {
Image(systemName: "doc.on.clipboard.fill")
Text("Clipboard")
.fontWeight(.bold)
.padding(.vertical, 10)
}
.padding(.horizontal, 20)
}
.foregroundColor(.accentColor)
.buttonStyle(.plain)
.background(buttonBackground)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
}
var openFilesButton: some View {
Button {
presentingFileImporter = true
} label: {
HStack {
Image(systemName: "folder")
Text("Open Files")
.fontWeight(.bold)
.padding(.vertical, 10)
}
.frame(maxWidth: .infinity)
.padding(.horizontal, 20)
}
.foregroundColor(.accentColor)
.buttonStyle(.plain)
.background(buttonBackground)
}
var buttonBackground: some View {
RoundedRectangle(cornerRadius: 4)
.foregroundColor(Color.accentColor.opacity(0.33))
}
var urlsToOpenFromText: [URL] { var urlsToOpenFromText: [URL] {
urlsToOpenText.split(whereSeparator: \.isNewline).compactMap { URL(string: String($0)) } OpenVideosModel.shared.urlsFrom(urlsToOpenText)
} }
func openURLs(_ urls: [URL]) { func openURLs(_ urls: [URL]) {