Skip to content

Commit 65db8ec

Browse files
committed
Fix layout bugs
1 parent 22c7902 commit 65db8ec

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

Sources/GateEngine/UI/Label.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,13 @@ public final class Label: View {
161161
let yOffset: Float
162162
switch textAlignment {
163163
case .leading:
164-
xOffset = 4 * self.interfaceScale
165-
yOffset = (rect.height / 2) - ((size.height / 2) * self.interfaceScale)
164+
xOffset = self.marginInsets.leading * self.interfaceScale
166165
case .centered:
167166
xOffset = (rect.width / 2) - ((size.width / 2) * self.interfaceScale)
168-
yOffset = (rect.height / 2) - ((size.height / 2) * self.interfaceScale)
169167
case .trailing:
170-
xOffset = rect.width - (size.width * self.interfaceScale) - (4 * self.interfaceScale)
171-
yOffset = rect.height - (size.height * self.interfaceScale) - (4 * self.interfaceScale)
168+
xOffset = .maximum(0, rect.width - (size.width * self.interfaceScale) - (self.marginInsets.trailing * self.interfaceScale))
172169
}
170+
yOffset = (rect.height / 2) - ((size.height / 2) * self.interfaceScale)
173171

174172
canvas.insert(
175173
DrawCommand(

Sources/GateEngine/UI/Layout.swift

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,14 @@ public struct Layout {
313313
if let sourceX = resolveX(for: view) {
314314
if let trailing = view.layoutConstraints.horizontalPositions.first(where: {$0.source == view.trailingAnchor}) {
315315
if let targetView = trailing.target?.view {
316-
if trailing.target === targetView.trailingAnchor {
317-
if let targetTrailing = relativeResolvedTrailing(forTarget: targetView) {
318-
computed = Value.Computed(value: targetTrailing - sourceX.value + trailing.constant)
319-
}
320-
}else if trailing.target === targetView.leadingAnchor {
316+
if trailing.target === targetView.leadingAnchor {
321317
if let targetLeading = relativeResolvedLeading(forTarget: targetView) {
322318
computed = Value.Computed(value: targetLeading - sourceX.value + trailing.constant)
323319
}
320+
}else if trailing.target === targetView.trailingAnchor {
321+
if let targetTrailing = relativeResolvedTrailing(forTarget: targetView) {
322+
computed = Value.Computed(value: targetTrailing - sourceX.value + trailing.constant)
323+
}
324324
}
325325
}
326326
}
@@ -370,6 +370,25 @@ public struct Layout {
370370
}
371371
view.layoutConstraints.resolvedFrame.height.currentlyBeingResolved = true
372372

373+
// Determine the local bottom offset between the view and the target coordinate spaces
374+
func relativeResolvedTop(forTarget targetView: View) -> Float? {
375+
if view.superView === targetView {// Target is the supreview
376+
return 0
377+
}else if targetView.superView === view {// Target is a subview
378+
if let targetY = resolveY(for: targetView) {
379+
return targetY.value
380+
}
381+
}else if view.superView === targetView.superView {// Target is a sibling
382+
if let targetY = resolveY(for: targetView) {
383+
return targetY.value
384+
}
385+
}else{
386+
//TODO: The view is in another coordinate space
387+
fatalError("Layout cannot yet constrain views between coordinate spaces.")
388+
}
389+
return nil
390+
}
391+
373392
// Determine the local bottom offset between the view and the target coordinate spaces
374393
func relativeResolvedBottom(forTarget targetView: View) -> Float? {
375394
if view.superView === targetView {// Target is the supreview
@@ -419,7 +438,11 @@ public struct Layout {
419438
if let sourceY = resolveY(for: view) {
420439
if let bottom = view.layoutConstraints.verticalPositions.first(where: {$0.source == view.bottomAnchor}) {
421440
if let targetView = bottom.target?.view {
422-
if bottom.target === targetView.bottomAnchor {
441+
if bottom.target === targetView.topAnchor {
442+
if let targetTop = relativeResolvedTop(forTarget: targetView) {
443+
computed = Value.Computed(value: targetTop + bottom.constant)
444+
}
445+
}else if bottom.target === targetView.bottomAnchor {
423446
if let targetBottom = relativeResolvedBottom(forTarget: targetView) {
424447
computed = Value.Computed(value: targetBottom - sourceY.value + bottom.constant)
425448
}
@@ -688,26 +711,22 @@ extension Layout {
688711
}
689712

690713
public struct Constraints {
691-
@usableFromInline
692-
internal var horizontalPositions: [Constraint<Layout.Horizontal, Layout.Location>] = [] {
714+
public internal(set) var horizontalPositions: [Constraint<Layout.Horizontal, Layout.Location>] = [] {
693715
didSet {
694716
needsSorting = true
695717
}
696718
}
697-
@usableFromInline
698-
internal var verticalPositions: [Constraint<Layout.Vertical, Layout.Location>] = [] {
719+
public internal(set) var verticalPositions: [Constraint<Layout.Vertical, Layout.Location>] = [] {
699720
didSet {
700721
needsSorting = true
701722
}
702723
}
703-
@usableFromInline
704-
internal var horizontalSizes: [Constraint<Layout.Horizontal, Layout.Size>] = [] {
724+
public internal(set) var horizontalSizes: [Constraint<Layout.Horizontal, Layout.Size>] = [] {
705725
didSet {
706726
needsSorting = true
707727
}
708728
}
709-
@usableFromInline
710-
internal var verticalSizes: [Constraint<Layout.Vertical, Layout.Size>] = [] {
729+
public internal(set) var verticalSizes: [Constraint<Layout.Vertical, Layout.Size>] = [] {
711730
didSet {
712731
needsSorting = true
713732
}
@@ -835,11 +854,9 @@ extension Layout {
835854

836855
@MainActor
837856
extension Layout.Anchor where D == Layout.Horizontal, A == Layout.Location {
838-
@inlinable
839857
public func constrain(to target: Layout.Anchor<Layout.Horizontal, Layout.Location>, priority: Layout.Priority = .default) {
840858
self.constrain(0, from: target, priority: priority)
841859
}
842-
@inlinable
843860
public func constrain(_ constant: Float, from target: Layout.Anchor<Layout.Horizontal, Layout.Location>, priority: Layout.Priority = .default) {
844861
self.view.layoutConstraints.horizontalPositions.append(
845862
Layout.Constraint(source: self, target: target, constant: constant, multiplier: 1, priority: priority)
@@ -849,11 +866,9 @@ extension Layout.Anchor where D == Layout.Horizontal, A == Layout.Location {
849866

850867
@MainActor
851868
extension Layout.Anchor where D == Layout.Vertical, A == Layout.Location {
852-
@inlinable
853869
public func constrain(to target: Layout.Anchor<Layout.Vertical, Layout.Location>, priority: Layout.Priority = .default) {
854870
self.constrain(0, from: target, priority: priority)
855871
}
856-
@inlinable
857872
public func constrain(_ constant: Float, from target: Layout.Anchor<Layout.Vertical, Layout.Location>, priority: Layout.Priority = .default) {
858873
self.view.layoutConstraints.verticalPositions.append(
859874
Layout.Constraint(source: self, target: target, constant: constant, multiplier: 1, priority: priority)
@@ -863,13 +878,11 @@ extension Layout.Anchor where D == Layout.Vertical, A == Layout.Location {
863878

864879
@MainActor
865880
extension Layout.Anchor where D == Layout.Horizontal, A == Layout.Size {
866-
@inlinable
867881
public func constrain(to target: Layout.Anchor<Layout.Horizontal, Layout.Size>, multiplier: Float = 1, adding constant: Float = 0, priority: Layout.Priority = .default) {
868882
self.view.layoutConstraints.horizontalSizes.append(
869883
Layout.Constraint(source: self, target: target, constant: constant, multiplier: multiplier, priority: priority)
870884
)
871885
}
872-
@inlinable
873886
public func constrain(to constant: Float, priority: Layout.Priority = .default) {
874887
self.view.layoutConstraints.horizontalSizes.append(
875888
Layout.Constraint(source: self, target: nil, constant: constant, multiplier: 1, priority: priority)
@@ -879,13 +892,11 @@ extension Layout.Anchor where D == Layout.Horizontal, A == Layout.Size {
879892

880893
@MainActor
881894
extension Layout.Anchor where D == Layout.Vertical, A == Layout.Size {
882-
@inlinable
883895
public func constrain(to target: Layout.Anchor<Layout.Vertical, Layout.Size>, multiplier: Float = 1, adding constant: Float = 0, priority: Layout.Priority = .default) {
884896
self.view.layoutConstraints.verticalSizes.append(
885897
Layout.Constraint(source: self, target: target, constant: constant, multiplier: multiplier, priority: priority)
886898
)
887899
}
888-
@inlinable
889900
public func constrain(to constant: Float, priority: Layout.Priority = .default) {
890901
self.view.layoutConstraints.verticalSizes.append(
891902
Layout.Constraint(source: self, target: nil, constant: constant, multiplier: 1, priority: priority)

0 commit comments

Comments
 (0)