From 691a3305e751af22e7e5b1104688fc58e840af2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Tue, 21 Nov 2023 00:58:40 +0100 Subject: [PATCH 1/3] make links in description clickable Adds clickable links to macOS. I it is a naive approach, but it works. Probably closes #51 --- .../Video Details/VideoDescription.swift | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Shared/Player/Video Details/VideoDescription.swift b/Shared/Player/Video Details/VideoDescription.swift index b704a26d..56bf9450 100644 --- a/Shared/Player/Video Details/VideoDescription.swift +++ b/Shared/Player/Video Details/VideoDescription.swift @@ -61,7 +61,7 @@ struct VideoDescription: View { #if !os(iOS) Group { if #available(macOS 12, *) { - Text(description) + DescriptionWithLinks(description: description, detailsSize: detailsSize) .frame(maxWidth: .infinity, alignment: .leading) .lineLimit(shouldExpand ? 500 : Self.collapsedLines) #if !os(tvOS) @@ -79,6 +79,33 @@ struct VideoDescription: View { #endif } + // If possibe convert URLs to clickable links + #if os(macOS) + @available(macOS 12, *) + struct DescriptionWithLinks: View { + let description: String + let detailsSize: CGSize? + let separators = CharacterSet(charactersIn: " \n") + + var formattedString: AttributedString { + var attrString = AttributedString(description) + let words = description.unicodeScalars.split(whereSeparator: separators.contains).map(String.init) + words.forEach { word in + if (word.hasPrefix("https://") || word.hasPrefix("http://")), let url = URL(string: String(word)) { + if let range = attrString.range(of: word) { + attrString[range].link = url + } + } + } + return attrString + } + + var body: some View { + Text(formattedString) + } + } + #endif + @ViewBuilder var keywords: some View { if showKeywords { ScrollView(.horizontal, showsIndicators: showScrollIndicators) { From ed69780d523d8df096083e9085450836d52125c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Tue, 21 Nov 2023 01:13:01 +0100 Subject: [PATCH 2/3] removed superfluous braces --- Shared/Player/Video Details/VideoDescription.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shared/Player/Video Details/VideoDescription.swift b/Shared/Player/Video Details/VideoDescription.swift index 56bf9450..1bd12c08 100644 --- a/Shared/Player/Video Details/VideoDescription.swift +++ b/Shared/Player/Video Details/VideoDescription.swift @@ -91,7 +91,7 @@ struct VideoDescription: View { var attrString = AttributedString(description) let words = description.unicodeScalars.split(whereSeparator: separators.contains).map(String.init) words.forEach { word in - if (word.hasPrefix("https://") || word.hasPrefix("http://")), let url = URL(string: String(word)) { + if word.hasPrefix("https://") || word.hasPrefix("http://"), let url = URL(string: String(word)) { if let range = attrString.range(of: word) { attrString[range].link = url } From 5a5f5a8696209fdad135ac3e414fd6132f671375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Wed, 22 Nov 2023 10:21:21 +0100 Subject: [PATCH 3/3] code formatting --- .../Video Details/VideoDescription.swift | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Shared/Player/Video Details/VideoDescription.swift b/Shared/Player/Video Details/VideoDescription.swift index 1bd12c08..c0b82bf2 100644 --- a/Shared/Player/Video Details/VideoDescription.swift +++ b/Shared/Player/Video Details/VideoDescription.swift @@ -81,29 +81,29 @@ struct VideoDescription: View { // If possibe convert URLs to clickable links #if os(macOS) - @available(macOS 12, *) - struct DescriptionWithLinks: View { - let description: String - let detailsSize: CGSize? - let separators = CharacterSet(charactersIn: " \n") + @available(macOS 12, *) + struct DescriptionWithLinks: View { + let description: String + let detailsSize: CGSize? + let separators = CharacterSet(charactersIn: " \n") - var formattedString: AttributedString { - var attrString = AttributedString(description) - let words = description.unicodeScalars.split(whereSeparator: separators.contains).map(String.init) - words.forEach { word in - if word.hasPrefix("https://") || word.hasPrefix("http://"), let url = URL(string: String(word)) { - if let range = attrString.range(of: word) { - attrString[range].link = url + var formattedString: AttributedString { + var attrString = AttributedString(description) + let words = description.unicodeScalars.split(whereSeparator: separators.contains).map(String.init) + words.forEach { word in + if word.hasPrefix("https://") || word.hasPrefix("http://"), let url = URL(string: String(word)) { + if let range = attrString.range(of: word) { + attrString[range].link = url + } } } + return attrString } - return attrString - } - var body: some View { + var body: some View { Text(formattedString) + } } - } #endif @ViewBuilder var keywords: some View {