forked from square/Blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request square#467 from square/add-tintAdjustmentMode-to-i…
…mage Add `TintAdjustmentMode` to layout attributes
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import CoreGraphics | ||
import UIKit | ||
|
||
/// `TintAdjustmentMode` conditionally modifies the tint adjustment mode of its wrapped element. | ||
/// | ||
/// - Note: When a tint adjustment mode is applied, any elements within the wrapped element will adopt the parent's tint adjustment mode. | ||
public struct TintAdjustmentMode: Element { | ||
public var tintAdjustmentMode: UIView.TintAdjustmentMode | ||
|
||
public var wrappedElement: Element | ||
|
||
public init(_ tintAdjustmentMode: UIView.TintAdjustmentMode, wrapping element: Element) { | ||
self.tintAdjustmentMode = tintAdjustmentMode | ||
wrappedElement = element | ||
} | ||
|
||
public var content: ElementContent { | ||
ElementContent(child: wrappedElement, layout: Layout(tintAdjustmentMode: tintAdjustmentMode)) | ||
} | ||
|
||
public func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription? { | ||
nil | ||
} | ||
|
||
private struct Layout: SingleChildLayout { | ||
var tintAdjustmentMode: UIView.TintAdjustmentMode | ||
|
||
func measure(in constraint: SizeConstraint, child: Measurable) -> CGSize { | ||
child.measure(in: constraint) | ||
} | ||
|
||
func layout(size: CGSize, child: Measurable) -> LayoutAttributes { | ||
var attributes = LayoutAttributes(size: size) | ||
attributes.tintAdjustmentMode = tintAdjustmentMode | ||
return attributes | ||
} | ||
|
||
func sizeThatFits( | ||
proposal: SizeConstraint, | ||
subelement: Subelement, | ||
environment: Environment, | ||
cache: inout Cache | ||
) -> CGSize { | ||
subelement.sizeThatFits(proposal) | ||
} | ||
|
||
func placeSubelement( | ||
in size: CGSize, | ||
subelement: Subelement, | ||
environment: Environment, | ||
cache: inout () | ||
) { | ||
subelement.attributes.tintAdjustmentMode = tintAdjustmentMode | ||
} | ||
} | ||
} | ||
|
||
extension Element { | ||
/// Conditionally modifies the tint adjustment mode of its wrapped element. | ||
/// | ||
/// - Note: When a tint adjustment mode is applied, any elements within the wrapped element will adopt the parent's tint adjustment mode. | ||
public func tintAdjustmentMode(_ tintAdjustmentMode: UIView.TintAdjustmentMode) -> TintAdjustmentMode { | ||
TintAdjustmentMode(tintAdjustmentMode, wrapping: self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import XCTest | ||
@testable import BlueprintUI | ||
|
||
final class TintAdjustmentModeTests: XCTestCase { | ||
func test() throws { | ||
do { | ||
let wrapped = TintAdjustmentMode(.normal, wrapping: TestElement()) | ||
let layout = wrapped.layout(frame: CGRect(origin: .zero, size: .init(width: 10, height: 10))) | ||
if let child = layout.findLayout(of: TestElement.self) { | ||
XCTAssertEqual( | ||
child.layoutAttributes.tintAdjustmentMode, | ||
.normal | ||
) | ||
} else { | ||
XCTFail("TestElement should be a child element") | ||
} | ||
} | ||
} | ||
|
||
func test_convenience() throws { | ||
do { | ||
let wrapped = TestElement().tintAdjustmentMode(.normal) | ||
let layout = wrapped.layout(frame: CGRect(origin: .zero, size: .init(width: 10, height: 10))) | ||
if let child = layout.findLayout(of: TestElement.self) { | ||
XCTAssertEqual( | ||
child.layoutAttributes.tintAdjustmentMode, | ||
.normal | ||
) | ||
} else { | ||
XCTFail("TestElement should be a child element") | ||
} | ||
} | ||
} | ||
|
||
/// A view-backed box to generate a native view node | ||
struct TestElement: Element { | ||
var content: ElementContent { | ||
ElementContent(intrinsicSize: .init(width: 10, height: 10)) | ||
} | ||
|
||
func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription? { | ||
UIView.describe { _ in } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters