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

Aligned Additions #42

Merged
merged 1 commit into from
Dec 17, 2019
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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ matrix:
xcode_destination: platform=iOS Simulator,OS=9.3,name=iPhone 4s
before_install:
- cd SampleApp
- gem update --system
- gem install bundler
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These lines aren't needed – they're already on Travis.

- bundle install
- bundle exec pod repo update
- bundle exec pod install
Expand Down
16 changes: 14 additions & 2 deletions BlueprintUI/Sources/Layout/Aligned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import UIKit

/// Aligns a content element within itself. The vertical and horizontal alignment may be set independently.
///
/// The size of the content element is determined by calling `measure(in:)` on
/// the content element – even if that size is larger than the wrapping element.
/// When using alignment mode `.fill`, the content is scaled to the width or height of the `Aligned` element.
///
/// For other modes, the size of the content element is determined by calling `measure(in:)`
/// on the content element – even if that size is larger than the wrapping element.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@watt not going to change it here, but this bit "even if that size is larger than the wrapping element." seems a bit weird to me – is there a use case for this, or should we change the implementation to cap the size?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I copied this from Centered. I think it's reasonable behavior, as it limits the responsibility of this element and favors composition (also a reason that I have mixed feelings about adding fill here instead of introducing a Fill element). If you want to cap the size of an element, composition with ConstrainedSize seems like a more scalable solution.

///
public struct Aligned: Element {
/// The possible vertical alignment values.
Expand All @@ -14,6 +16,8 @@ public struct Aligned: Element {
case center
/// Aligns the content to the bottom edge of the containing element.
case bottom
/// The content fills the full vertical height of the containing element.
case fill
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should update the docs on this type if we add this, to something like:

For alignments other than fill, the size of the content element is determined by calling measure(in:) on the content element – even if that size is larger than the wrapping element.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}

/// The possible horizontal alignment values.
Expand All @@ -26,6 +30,8 @@ public struct Aligned: Element {
/// Aligns the content to the trailing edge of the containing element.
/// In left-to-right languages, this is the right edge.
case trailing
/// The content fills the full horizontal width of the containing element.
case fill
}

/// The content element to be aligned.
Expand Down Expand Up @@ -80,6 +86,9 @@ public struct Aligned: Element {
attributes.frame.origin.y = (size.height - contentSize.height) / 2.0
case .bottom:
attributes.frame.origin.y = size.height - contentSize.height
case .fill:
attributes.frame.origin.y = 0
attributes.frame.size.height = size.height
}

switch horizontalAlignment {
Expand All @@ -89,6 +98,9 @@ public struct Aligned: Element {
attributes.frame.origin.x = (size.width - contentSize.width) / 2.0
case .trailing:
attributes.frame.origin.x = size.width - contentSize.width
case .fill:
attributes.frame.origin.x = 0
attributes.frame.size.width = size.width
}

// TODO: screen-scale round here once that lands
Expand Down
20 changes: 20 additions & 0 deletions BlueprintUI/Tests/AlignedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class AlignedTests: XCTestCase {
XCTAssertEqual(frame.maxX, 5000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_horizontalFill() {
let children = childLayoutResultNodesAligned(horizontally: .fill)

XCTAssertEqual(children.count, 1)
let frame = children[0].layoutAttributes.frame
XCTAssertEqual(frame.minX, 0.0)
XCTAssertEqual(frame.maxX, 5000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_verticalTop() {
let children = childLayoutResultNodesAligned(vertically: .top)
Expand Down Expand Up @@ -86,6 +96,16 @@ class AlignedTests: XCTestCase {
XCTAssertEqual(frame.maxY, 6000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_verticalFill() {
let children = childLayoutResultNodesAligned(vertically: .fill)

XCTAssertEqual(children.count, 1)
let frame = children[0].layoutAttributes.frame
XCTAssertEqual(frame.minY, 0)
XCTAssertEqual(frame.maxY, 6000)
XCTAssertTrue(children[0].element is TestElement)
}
}

private struct TestElement: Element {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,8 +36,18 @@
ReferencedContainer = "container:SampleApp.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F0DBD15A7E34A7177B40DF27414BFF9"
BuildableName = "BlueprintUI-Unit-Tests.xctest"
BlueprintName = "BlueprintUI-Unit-Tests"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -61,8 +69,6 @@
ReferencedContainer = "container:SampleApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down