Skip to content

Commit

Permalink
Refactor to not use newer SwiftUI features
Browse files Browse the repository at this point in the history
  • Loading branch information
mlw committed Jun 14, 2023
1 parent 02ea03f commit b89072a
Showing 1 changed file with 5 additions and 54 deletions.
59 changes: 5 additions & 54 deletions Source/gui/SNTFileAccessMessageWindowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,6 @@ import santa_common_SNTFileAccessEvent
}
}

// This struct helps to make the 2 buttons on the dialog match their width to the label of the longest button.
@available(macOS 13, *)
struct EqualWidthHStack: Layout {
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
let maxSize = maxSize(subviews: subviews)
let spacing = spacing(subviews: subviews)
let totalSpacing = spacing.reduce(0.0, +)

return CGSize(width: maxSize.width * CGFloat(subviews.count) + totalSpacing, height: maxSize.height)
}

func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
let maxSize = maxSize(subviews: subviews)
let spacing = spacing(subviews: subviews)

let sizeProposal = ProposedViewSize(width: maxSize.width, height: maxSize.height)
var x = bounds.minX + maxSize.width / 2

for index in subviews.indices {
subviews[index].place(at: CGPoint(x: x, y: bounds.midY), anchor: .center, proposal: sizeProposal)
x += maxSize.width + spacing[index]
}
}

private func maxSize(subviews: Subviews) -> CGSize {
let subviewSizes = subviews.map { $0.sizeThatFits(.unspecified) }

let maxSize: CGSize = subviewSizes.reduce(.zero, { result, size in
CGSize(width: max(result.width, size.width),
height: max(result.height, size.height))
})
return maxSize
}

private func spacing(subviews: Subviews) -> [CGFloat] {
subviews.indices.map { index in
guard index < subviews.count - 1 else { return 0.0 }
return subviews[index].spacing.distance(to: subviews[index + 1].spacing, along: .horizontal)
}
}
}

@available(macOS 13, *)
struct Property : View {
var lbl: String
Expand All @@ -78,7 +36,7 @@ struct Property : View {
Text(lbl + ":")
.frame(width: width, alignment: .trailing)
.lineLimit(1)
.bold()
.font(.system(size: 12, weight: .bold))
.padding(Edge.Set.horizontal, 10)

Text(val)
Expand Down Expand Up @@ -120,12 +78,7 @@ struct SNTFileAccessMessageWindowView: View {
let event: SNTFileAccessEvent?
let customMsg: NSAttributedString?

enum FocusField: Hashable {
case field
}

@State private var checked = false
@FocusState private var focusedField: FocusField?

var body: some View {
VStack(spacing:20.0) {
Expand All @@ -145,20 +98,18 @@ struct SNTFileAccessMessageWindowView: View {
.font(Font.system(size: 11.0));
}

EqualWidthHStack {
VStack(spacing:15) {
Button(action: openButton, label: {
Text("Open Event Info...").frame(maxWidth:.infinity)
})
Button(action: dismissButton, label: {
Text("Dismiss").frame(maxWidth:.infinity)
})
.keyboardShortcut(.return)
.focused($focusedField, equals: .field)
.onAppear {
self.focusedField = .field
}
}.padding(20.0)
}.frame(width: 220)

Spacer()

}.frame(maxWidth:800.0).fixedSize()
}

Expand Down

0 comments on commit b89072a

Please sign in to comment.