Skip to content

Commit

Permalink
Improve wrapping of single-line texts in preview
Browse files Browse the repository at this point in the history
Better than used to be, though not perfect and very hard to do
for such a basic problem with SwiftUI.

Fixes #880
  • Loading branch information
p0deje committed Nov 23, 2024
1 parent 45a4ad2 commit c5cbd24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Maccy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -248,6 +249,7 @@
DA555F0E2CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/PinsSettings.strings; sourceTree = "<group>"; };
DA555F0F2CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/PreviewItemView.strings; sourceTree = "<group>"; };
DA555F102CF108EA009608BD /* sl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sl; path = sl.lproj/Localizable.strings; sourceTree = "<group>"; };
DA555F112CF155A2009608BD /* WrappingTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WrappingTextView.swift; sourceTree = "<group>"; };
DA5E627F2C39E53F00F4C710 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/PreviewItemView.strings; sourceTree = "<group>"; };
DA5E62812C39E54A00F4C710 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/PreviewItemView.strings; sourceTree = "<group>"; };
DA5E62822C39E54B00F4C710 /* bs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = bs; path = bs.lproj/PreviewItemView.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -598,6 +600,7 @@
DA1969132C3F11D600258481 /* PreviewItemView.swift */,
DA1969172C3F327500258481 /* SearchFieldView.swift */,
DAA072D02C4089D3006DDFD2 /* VisualEffectView.swift */,
DA555F112CF155A2009608BD /* WrappingTextView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
8 changes: 4 additions & 4 deletions Maccy/Views/PreviewItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -63,7 +63,7 @@ struct PreviewItemView: View {
}
}
.controlSize(.small)
.frame(maxWidth: 800)
.frame(maxWidth: maxWidth)
.padding()
}
}
27 changes: 27 additions & 0 deletions Maccy/Views/WrappingTextView.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c5cbd24

Please sign in to comment.