Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for customization of the preview name #478

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions BlueprintUI/Sources/BlueprintView/ElementPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,34 +191,28 @@ extension ElementPreview {
for element: Element
) -> AnyView {

let formattedName: String = {
if name.isEmpty == false {
return " – " + name
} else {
return ""
}
}()
let formattedName: String? = name.isEmpty ? nil : name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure, and fine keeping this in, but should we just allow empty string here? We could document that it maps to .previewDisplayName() so consumers know it matches that behavior.
What happens if you do provide empty string?

Copy link
Contributor Author

@nsillik nsillik Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you pass in an empty string you get the SwiftUI default, if you pass in a name it shows the name. I could have made this trinary (String?) and have nil give you the swiftUI default, empty string keep the same behavior ("size that fits" etc.), and a non-empty string override the preview name.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I kinda like the idea of having the name match the type of previewDisplayName but probably not worth the churn. Fine keeping as is!


switch self {
case .device(let device):
return AnyView(
constrained(element: element)
.previewDevice(.init(rawValue: device.rawValue))
.previewDisplayName(device.rawValue + formattedName)
.previewDisplayName(formattedName)
)

case .fixed(let width, let height):
return AnyView(
constrained(element: element)
.previewLayout(.fixed(width: width, height: height))
.previewDisplayName("Fixed Size: (\(width), \(height))" + formattedName)
.previewDisplayName(formattedName)
)

case .thatFits(let padding):
return AnyView(
constrained(element: element)
.previewLayout(.sizeThatFits)
.previewDisplayName("Size That Fits" + formattedName)
.previewDisplayName(formattedName)
.padding(.all, padding)
)
}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- The behavior of `name` of `ElementPreview` has been change, affecting the SwiftUI `previewName`. Instead of including device or size information (i.e. `sizeThatFits - \(name)`), it now either defaults to the Xcode default if given an empty string, and shows _only_ the `name` if `name` is non-empty.
- Updated minimum deployment target from iOS 14 to iOS 15.

### Deprecated
Expand Down
3 changes: 3 additions & 0 deletions SampleApp/Sources/XcodePreviewDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct TestingView_Preview: PreviewProvider {
ElementPreview(with: .thatFits(padding: 5)) {
TestElement()
}
ElementPreview(named: "with a name", with: .thatFits(padding: 5)) {
TestElement()
}
}
}

Expand Down
Loading