Skip to content

Commit

Permalink
Fix Button Height (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
mackoj authored Jul 21, 2023
1 parent d1cf6ed commit 865654b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
26 changes: 26 additions & 0 deletions Showcase/ShowcaseSnapshotTests/NonRegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import XCTest
import SnapshotTesting
import PreviewSnapshotsTesting
import UIKit
import SwiftUI

@testable import Vitamin
@testable import VitaminSwiftUI
@testable import Showcase

final class NonRegressionUIKitTests: XCTestCase {
Expand Down Expand Up @@ -42,4 +44,28 @@ final class NonRegressionUIKitTests: XCTestCase {
deleteButton.frame = CGRect(origin: .zero, size: CGSize(width: 320, height: 80))
assertSnapshot(matching: deleteButton, as: .image)
}

func testButtonAlignement() throws {
@State var bt1: CGSize = .zero
@State var bt2: CGSize = .zero
let container = HStack {
VitaminButton(
text: "Modifier le profil",
style: .tertiary,
size: .medium,
iconType: .leading(image: UIImage(systemName: "person")!, renderingMode: .template),
action: {})
.fixedSize()
.readSize(onChange: { bt1 = $0 })
VitaminButton(
style: .tertiary,
size: .medium,
iconType: .alone(image: UIImage(systemName: "gear")!, renderingMode: .template),
action: {})
.fixedSize()
.readSize(onChange: { bt2 = $0 })
}.padding()
assertSnapshot(matching: UIHostingController(rootView: container), as: .recursiveDescription(on: .iPhone12))
assertSnapshot(matching: UIHostingController(rootView: container), as: .image)
}
}
21 changes: 21 additions & 0 deletions Showcase/ShowcaseSnapshotTests/ReadSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import SwiftUI

extension View {
/// Le but de cet helper est de simplifier l'utilisation du geometry reader
/// - Parameter onChange: what to do on size update
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: ReadSizePreferenceKey.self, value: geometryProxy.size)
}
)
.onPreferenceChange(ReadSizePreferenceKey.self, perform: onChange)
}
}

struct ReadSizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero

static func reduce(value _: inout CGSize, nextValue _: () -> CGSize) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<_TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentGVS_6HStackGVS_9TupleViewTGS1_GS1_GS1_V14VitaminSwiftUI13VitaminButtonVS_16_FixedSizeLayout_GVS_19_BackgroundModifierGVS_14GeometryReaderGS1_VS_5ColorGVS_26_PreferenceWritingModifierV21ShowcaseSnapshotTests21ReadSizePreferenceKey_____GVS_25_PreferenceActionModifierS12___GS1_GS1_GS1_S5_S6__GS7_GS8_GS1_S9_GS10_S12______GS13_S12______VS_14_PaddingLayout__; frame = (0 0; 390 844); autoresize = W+H; gestureRecognizers = <NSArray>; backgroundColor = <UIDynamicSystemColor; name = systemBackgroundColor>; layer = <CALayer>>
| <_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView; frame = (67 406; 188 52); anchorPoint = (0, 0); autoresizesSubviews = NO; backgroundColor = UIExtendedSRGBColorSpace 0.905882 0.952941 0.976471 1; layer = <CALayer>>
| <SwiftUI._UIGraphicsView; frame = (87 422; 20 20); anchorPoint = (0, 0); autoresizesSubviews = NO; layer = <SwiftUI.ImageLayer>>
| <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView; frame = (117 422.667; 118 19); anchorPoint = (0, 0); opaque = NO; autoresizesSubviews = NO; layer = <_TtCOCV7SwiftUI11DisplayList11ViewUpdater8PlatformP33_65A81BD07F0108B0485D2E15DE104A7514CGDrawingLayer>>
| <_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView; frame = (263 406; 60 52); anchorPoint = (0, 0); autoresizesSubviews = NO; backgroundColor = UIExtendedSRGBColorSpace 0.905882 0.952941 0.976471 1; layer = <CALayer>>
| <SwiftUI._UIGraphicsView; frame = (283 422; 20 20); anchorPoint = (0, 0); autoresizesSubviews = NO; layer = <SwiftUI.ImageLayer>>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion Showcase/Vitamin Showcase.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 52;
objects = {

/* Begin PBXBuildFile section */
30F95E44292FBA0500D6CEB7 /* TextFieldsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30F95E43292FBA0500D6CEB7 /* TextFieldsView.swift */; };
3D54BF912A44392900D77578 /* NonRegressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D54BF902A44392900D77578 /* NonRegressionTests.swift */; };
3D5771182A4B2AF2006F0689 /* ReadSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5771172A4B2AF2006F0689 /* ReadSize.swift */; };
3D9863192A01689A0071CC8C /* PreviewSnapshots in Frameworks */ = {isa = PBXBuildFile; productRef = 3D9863182A01689A0071CC8C /* PreviewSnapshots */; };
3D98631B2A01689A0071CC8C /* PreviewSnapshotsTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 3D98631A2A01689A0071CC8C /* PreviewSnapshotsTesting */; };
3D98631D2A0168DE0071CC8C /* ShowcaseSnapshotUIKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D98631C2A0168DE0071CC8C /* ShowcaseSnapshotUIKitTests.swift */; };
Expand Down Expand Up @@ -113,6 +114,7 @@
/* Begin PBXFileReference section */
30F95E43292FBA0500D6CEB7 /* TextFieldsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldsView.swift; sourceTree = "<group>"; };
3D54BF902A44392900D77578 /* NonRegressionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonRegressionTests.swift; sourceTree = "<group>"; };
3D5771172A4B2AF2006F0689 /* ReadSize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadSize.swift; sourceTree = "<group>"; };
3D98630E2A01687E0071CC8C /* ShowcaseSnapshotTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ShowcaseSnapshotTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3D98631C2A0168DE0071CC8C /* ShowcaseSnapshotUIKitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShowcaseSnapshotUIKitTests.swift; sourceTree = "<group>"; };
3D98631E2A0168FA0071CC8C /* ShowcaseSnapshotSwiftUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowcaseSnapshotSwiftUITests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -230,6 +232,7 @@
3D98630F2A01687E0071CC8C /* ShowcaseSnapshotTests */ = {
isa = PBXGroup;
children = (
3D5771172A4B2AF2006F0689 /* ReadSize.swift */,
3D98631C2A0168DE0071CC8C /* ShowcaseSnapshotUIKitTests.swift */,
3D54BF902A44392900D77578 /* NonRegressionTests.swift */,
3D98631E2A0168FA0071CC8C /* ShowcaseSnapshotSwiftUITests.swift */,
Expand Down Expand Up @@ -1043,6 +1046,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3D5771182A4B2AF2006F0689 /* ReadSize.swift in Sources */,
3D54BF912A44392900D77578 /* NonRegressionTests.swift in Sources */,
3D98631F2A0168FA0071CC8C /* ShowcaseSnapshotSwiftUITests.swift in Sources */,
3D98631D2A0168DE0071CC8C /* ShowcaseSnapshotUIKitTests.swift in Sources */,
Expand Down
23 changes: 4 additions & 19 deletions Sources/VitaminCore/Components/Button/VitaminButtonSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,23 @@ public extension VitaminButtonSize {
}

func horizontalInset(iconType: VitaminButtonIconType) -> CGFloat {
if case .alone = iconType {
return 12
}

switch self {
case .medium: return 20
case .large: return 40
}
}

func verticalInset(iconType: VitaminButtonIconType) -> CGFloat {
if case .alone = iconType {
return 12
}

switch self {
case .medium: return 16
case .large: return 20
}
}

func defaultIconSize(iconType: VitaminButtonIconType) -> CGFloat {
if case .alone = iconType {
switch self {
case .medium: return 24
case .large: return 32
}
} else {
switch self {
case .medium: return 20
case .large: return 24
}
}
switch self {
case .medium: return 20
case .large: return 24
}
}
}

0 comments on commit 865654b

Please sign in to comment.