Skip to content

Commit

Permalink
Allow for customization of the preview name (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsillik committed Feb 21, 2024
1 parent fe83ed4 commit e27debd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
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

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

0 comments on commit e27debd

Please sign in to comment.