diff --git a/Maccy.xcodeproj/project.pbxproj b/Maccy.xcodeproj/project.pbxproj index 80407494..92cb5843 100644 --- a/Maccy.xcodeproj/project.pbxproj +++ b/Maccy.xcodeproj/project.pbxproj @@ -66,6 +66,7 @@ DA49EE7928B594DC002752E0 /* NSRunningApplication+WindowFrame.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA49EE7828B594DC002752E0 /* NSRunningApplication+WindowFrame.swift */; }; DA5154D82564102900C01004 /* Sauce in Frameworks */ = {isa = PBXBuildFile; productRef = DA5154D72564102900C01004 /* Sauce */; }; DA555F082CF0F994009608BD /* ApplicationImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA555F072CF0F98F009608BD /* ApplicationImageCache.swift */; }; + DA555F122CF155A8009608BD /* WrappingTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA555F112CF155A2009608BD /* WrappingTextView.swift */; }; DA5E62802C39E53F00F4C710 /* PreviewItemView.strings in Resources */ = {isa = PBXBuildFile; fileRef = DA5E627E2C39E53F00F4C710 /* PreviewItemView.strings */; }; DA5F46512020E9FB00425C11 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F464F2020E4DF00425C11 /* Carbon.framework */; }; DA6373981E4AB9BB00263391 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DA6373971E4AB9BB00263391 /* Assets.xcassets */; }; @@ -248,6 +249,7 @@ DA555F0E2CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/PinsSettings.strings; sourceTree = ""; }; DA555F0F2CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/PreviewItemView.strings; sourceTree = ""; }; DA555F102CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/Localizable.strings; sourceTree = ""; }; + DA555F112CF155A2009608BD /* WrappingTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WrappingTextView.swift; sourceTree = ""; }; DA5E627F2C39E53F00F4C710 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/PreviewItemView.strings; sourceTree = ""; }; DA5E62812C39E54A00F4C710 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/PreviewItemView.strings; sourceTree = ""; }; DA5E62822C39E54B00F4C710 /* bs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = bs; path = bs.lproj/PreviewItemView.strings; sourceTree = ""; }; @@ -598,6 +600,7 @@ DA1969132C3F11D600258481 /* PreviewItemView.swift */, DA1969172C3F327500258481 /* SearchFieldView.swift */, DAA072D02C4089D3006DDFD2 /* VisualEffectView.swift */, + DA555F112CF155A2009608BD /* WrappingTextView.swift */, ); path = Views; sourceTree = ""; @@ -983,6 +986,7 @@ DA19691F2C3F5F0600258481 /* KeyHandlingView.swift in Sources */, DA13D7DC2C1A223E00FA9E23 /* HistoryItemAppEntity.swift in Sources */, DAA072D12C4089D3006DDFD2 /* VisualEffectView.swift in Sources */, + DA555F122CF155A8009608BD /* WrappingTextView.swift in Sources */, DAC929D7297A0E8B00814F19 /* NSPasteboard.PasteboardType+Types.swift in Sources */, DA9C3C472C20B91D0056795D /* String+Identifiable.swift in Sources */, DAA365E72C4C57E600A394F8 /* KeyEquivalent+Keys.swift in Sources */, diff --git a/Maccy/Views/PreviewItemView.swift b/Maccy/Views/PreviewItemView.swift index e4a3dcb9..64282fd8 100644 --- a/Maccy/Views/PreviewItemView.swift +++ b/Maccy/Views/PreviewItemView.swift @@ -4,6 +4,8 @@ import SwiftUI struct PreviewItemView: View { var item: HistoryItemDecorator + private let maxWidth: CGFloat = 800 + var body: some View { VStack(alignment: .leading, spacing: 0) { if let image = item.previewImage { @@ -12,9 +14,7 @@ struct PreviewItemView: View { .aspectRatio(contentMode: .fit) .clipShape(.rect(cornerRadius: 5)) } else { - Text(item.text) - .controlSize(.regular) - .lineLimit(100) + WrappingTextView(text: item.text, maxWidth: maxWidth) } Divider() @@ -63,7 +63,7 @@ struct PreviewItemView: View { } } .controlSize(.small) - .frame(maxWidth: 800) + .frame(maxWidth: maxWidth) .padding() } } diff --git a/Maccy/Views/WrappingTextView.swift b/Maccy/Views/WrappingTextView.swift new file mode 100644 index 00000000..729e1288 --- /dev/null +++ b/Maccy/Views/WrappingTextView.swift @@ -0,0 +1,27 @@ +import SwiftUI + +// A text view that properly wraps single-line content without extra horizontal or vertical spaces. +// https://www.reddit.com/r/SwiftUI/comments/1gx1w6v/how_to_wrap_a_text_inside_a_macos_popover/ +struct WrappingTextView: View { + let text: String + let maxWidth: CGFloat + + private let charHeight: CGFloat = 10 + private let charWidth: CGFloat = 6 + + private var approxWidth: CGFloat { + let width = CGFloat(text.count) * charWidth + return min(max(width, 50), maxWidth) + } + + private var approxHeight: CGFloat { + let width = CGFloat(text.count) * charWidth + return width / min(max(width, 50), maxWidth) * charHeight * 3 + } + + var body: some View { + Text(text) + .frame(idealWidth: approxWidth, maxHeight: approxHeight, alignment: .leading) + .font(.body) + } +}