From 3042c19dc5d67005222b7b422494805bcf2d7f18 Mon Sep 17 00:00:00 2001 From: Takuma Matsushita <46811718+takumatt@users.noreply.github.com> Date: Tue, 9 Apr 2024 01:19:25 +0900 Subject: [PATCH] Add frame modifier for size modification (#116) --- Development/Demo/Components/Mocks.swift | 12 ++++------ Sources/LayoutSpecBuilders/Modifiers.swift | 27 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Development/Demo/Components/Mocks.swift b/Development/Demo/Components/Mocks.swift index 2fb98bb..b451365 100644 --- a/Development/Demo/Components/Mocks.swift +++ b/Development/Demo/Components/Mocks.swift @@ -65,8 +65,7 @@ enum Mocks { AnyDisplayNode { _, _ in LayoutSpec { shape - .height(16) - .width(120) + .frame(width: 120, height: 16) .padding(2) } } @@ -85,8 +84,7 @@ enum Mocks { AnyDisplayNode { _, _ in LayoutSpec { shape - .height(6) - .width(20) + .frame(width: 20, height: 6) .padding(2) } } @@ -107,8 +105,7 @@ enum Mocks { AnyDisplayNode { _, _ in LayoutSpec { shape - .width(28) - .height(28) + .frame(width: 28, height: 28) .padding(2) } } @@ -132,8 +129,7 @@ enum Mocks { LayoutSpec { VStackLayout(spacing: 2) { shapes - .width(120) - .height(16) + .frame(width: 120, height: 16) } } } diff --git a/Sources/LayoutSpecBuilders/Modifiers.swift b/Sources/LayoutSpecBuilders/Modifiers.swift index f8bfef5..864e688 100644 --- a/Sources/LayoutSpecBuilders/Modifiers.swift +++ b/Sources/LayoutSpecBuilders/Modifiers.swift @@ -426,6 +426,33 @@ extension _ASLayoutElementType { public func spacingBefore(_ spacing: CGFloat) -> ModifiedContent { modifier(SpacingModifier(after: nil, before: spacing)) } + + public func frame(width: CGFloat? = nil, height: CGFloat? = nil) -> ModifiedContent { + let width: ASDimension? = if let width { .init(unit: .points, value: width) } else { nil } + let height: ASDimension? = if let height { .init(unit: .points, value: height) } else { nil } + return modifier(SizeModifier( + width: width, + height: height + )) + } + + public func frame(minWidth: CGFloat? = nil, minHeight: CGFloat? = nil) -> ModifiedContent { + let minWidth: ASDimension? = if let minWidth { .init(unit: .points, value: minWidth) } else { nil } + let minHeight: ASDimension? = if let minHeight { .init(unit: .points, value: minHeight) } else { nil } + return modifier(MinSizeModifier( + minWidth: minWidth, + minHeight: minHeight + )) + } + + public func frame(maxWidth: CGFloat? = nil, maxHeight: CGFloat? = nil) -> ModifiedContent { + let maxWidth: ASDimension? = if let maxWidth { .init(unit: .points, value: maxWidth) } else { nil } + let maxHeight: ASDimension? = if let maxHeight { .init(unit: .points, value: maxHeight) } else { nil } + return modifier(MaxSizeModifier( + maxWidth: maxWidth, + maxHeight: maxHeight + )) + } }