From 6c3eaaf6470c493256b779b5b1b3598a2a7c886f Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 15 Nov 2024 19:59:31 -0500 Subject: [PATCH 1/5] macos: support linkable AboutView properties This allows us to enrich the build's commit property as a GitHub link. This change also displays the property values using a monospaced font, which I think looks a little nicer (especially the commit SHA). --- macos/Sources/Features/About/AboutView.swift | 74 +++++++++++--------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/macos/Sources/Features/About/AboutView.swift b/macos/Sources/Features/About/AboutView.swift index 71fe9c252..90792d1cf 100644 --- a/macos/Sources/Features/About/AboutView.swift +++ b/macos/Sources/Features/About/AboutView.swift @@ -3,7 +3,7 @@ import SwiftUI struct AboutView: View { @Environment(\.openURL) var openURL - private let githubLink = URL(string: "https://github.com/ghostty-org/ghostty") + private let githubURL = URL(string: "https://github.com/ghostty-org/ghostty") /// Read the commit from the bundle. private var build: String? { Bundle.main.infoDictionary?["CFBundleVersion"] as? String } @@ -11,25 +11,6 @@ struct AboutView: View { private var version: String? { Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String } private var copyright: String? { Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String } - private var properties: [KeyValue] { - let list: [KeyValue] = [ - .init(key: "Version", value: version), - .init(key: "Build", value: build), - .init(key: "Commit", value: commit == "" ? nil : commit) - ] - - return list.compactMap { - guard let value = $0.value else { return nil } - return .init(key: $0.key, value: value) - } - } - - private struct KeyValue: Identifiable { - var id = UUID() - public let key: LocalizedStringResource - public let value: Value - } - #if os(macOS) // This creates a background style similar to the Apple "About My Mac" Window private struct VisualEffectBackground: NSViewRepresentable { @@ -80,27 +61,23 @@ struct AboutView: View { .opacity(0.8) } .textSelection(.enabled) + VStack(spacing: 2) { - ForEach(properties) { item in - HStack(spacing: 4) { - Text(item.key) - .frame(width: 126, alignment: .trailing) - .padding(.trailing, 2) - Text(item.value) - .frame(width: 125, alignment: .leading) - .padding(.leading, 2) - .tint(.secondary) - .opacity(0.8) - } - .font(.callout) - .textSelection(.enabled) - .frame(maxWidth: .infinity) + if let version { + propertyRow("Version", text: version) + } + if let build { + propertyRow("Build", text: build) + } + if let commit, commit != "", + let url = githubURL?.appendingPathComponent("/commits/\(commit)") { + propertyRow("Commit", text: commit, url: url) } } .frame(maxWidth: .infinity) HStack(spacing: 8) { - if let url = githubLink { + if let url = githubURL { Button("GitHub") { openURL(url) } @@ -127,6 +104,33 @@ struct AboutView: View { .background(VisualEffectBackground(material: .underWindowBackground).ignoresSafeArea()) #endif } + + private func propertyRow(_ label: LocalizedStringResource, text: String, url: URL? = nil) -> some View { + HStack(spacing: 4) { + Text(label) + .frame(width: 126, alignment: .trailing) + .padding(.trailing, 2) + if let url { + Link(destination: url) { + propertyText(text) + } + } else { + propertyText(text) + } + } + .font(.callout) + .textSelection(.enabled) + .frame(maxWidth: .infinity) + } + + private func propertyText(_ text: String) -> some View { + Text(text) + .frame(width: 125, alignment: .leading) + .padding(.leading, 2) + .tint(.secondary) + .opacity(0.8) + .monospaced() + } } struct AboutView_Previews: PreviewProvider { From 1807eabcb8b924315aaaca7520973ff0c5e83081 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 15 Nov 2024 22:29:18 -0500 Subject: [PATCH 2/5] macos: prefer private View structs to functions --- macos/Sources/Features/About/AboutView.swift | 66 ++++++++++++-------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/macos/Sources/Features/About/AboutView.swift b/macos/Sources/Features/About/AboutView.swift index 90792d1cf..dcadeaa96 100644 --- a/macos/Sources/Features/About/AboutView.swift +++ b/macos/Sources/Features/About/AboutView.swift @@ -64,14 +64,14 @@ struct AboutView: View { VStack(spacing: 2) { if let version { - propertyRow("Version", text: version) + PropertyRow(label: "Version", text: version) } if let build { - propertyRow("Build", text: build) + PropertyRow(label: "Build", text: build) } if let commit, commit != "", let url = githubURL?.appendingPathComponent("/commits/\(commit)") { - propertyRow("Commit", text: commit, url: url) + PropertyRow(label: "Commit", text: commit, url: url) } } .frame(maxWidth: .infinity) @@ -105,31 +105,47 @@ struct AboutView: View { #endif } - private func propertyRow(_ label: LocalizedStringResource, text: String, url: URL? = nil) -> some View { - HStack(spacing: 4) { - Text(label) - .frame(width: 126, alignment: .trailing) - .padding(.trailing, 2) - if let url { - Link(destination: url) { - propertyText(text) - } - } else { - propertyText(text) - } + private struct PropertyRow: View { + let label: String + let text: String + let url: URL? + + init(label: String, text: String, url: URL? = nil) { + self.label = label + self.text = text + self.url = url + } + + var body: some View { + HStack(spacing: 4) { + Text(label) + .frame(width: 126, alignment: .trailing) + .padding(.trailing, 2) + if let url { + Link(destination: url) { + PropertyText(text: text) + } + } else { + PropertyText(text: text) + } + } + .font(.callout) + .textSelection(.enabled) + .frame(maxWidth: .infinity) } - .font(.callout) - .textSelection(.enabled) - .frame(maxWidth: .infinity) } - private func propertyText(_ text: String) -> some View { - Text(text) - .frame(width: 125, alignment: .leading) - .padding(.leading, 2) - .tint(.secondary) - .opacity(0.8) - .monospaced() + private struct PropertyText: View { + let text: String + + var body: some View { + Text(text) + .frame(width: 125, alignment: .leading) + .padding(.leading, 2) + .tint(.secondary) + .opacity(0.8) + .monospaced() + } } } From 2db43158a8b73aa90e8ec1988623006da57126d1 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 15 Nov 2024 22:36:11 -0500 Subject: [PATCH 3/5] macos: formatting --- macos/Sources/Features/About/AboutView.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/macos/Sources/Features/About/AboutView.swift b/macos/Sources/Features/About/AboutView.swift index dcadeaa96..0b0dcc75f 100644 --- a/macos/Sources/Features/About/AboutView.swift +++ b/macos/Sources/Features/About/AboutView.swift @@ -118,9 +118,9 @@ struct AboutView: View { var body: some View { HStack(spacing: 4) { - Text(label) - .frame(width: 126, alignment: .trailing) - .padding(.trailing, 2) + Text(label) + .frame(width: 126, alignment: .trailing) + .padding(.trailing, 2) if let url { Link(destination: url) { PropertyText(text: text) From de60382824a6e5ff5e31fa6592b8f56ccbf63b02 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 15 Nov 2024 22:43:21 -0500 Subject: [PATCH 4/5] macos: further simplify using a @ViewBuilder --- macos/Sources/Features/About/AboutView.swift | 26 +++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/macos/Sources/Features/About/AboutView.swift b/macos/Sources/Features/About/AboutView.swift index 0b0dcc75f..efe1f31bb 100644 --- a/macos/Sources/Features/About/AboutView.swift +++ b/macos/Sources/Features/About/AboutView.swift @@ -116,6 +116,15 @@ struct AboutView: View { self.url = url } + @ViewBuilder private var textView: some View { + Text(text) + .frame(width: 125, alignment: .leading) + .padding(.leading, 2) + .tint(.secondary) + .opacity(0.8) + .monospaced() + } + var body: some View { HStack(spacing: 4) { Text(label) @@ -123,10 +132,10 @@ struct AboutView: View { .padding(.trailing, 2) if let url { Link(destination: url) { - PropertyText(text: text) + textView } } else { - PropertyText(text: text) + textView } } .font(.callout) @@ -134,19 +143,6 @@ struct AboutView: View { .frame(maxWidth: .infinity) } } - - private struct PropertyText: View { - let text: String - - var body: some View { - Text(text) - .frame(width: 125, alignment: .leading) - .padding(.leading, 2) - .tint(.secondary) - .opacity(0.8) - .monospaced() - } - } } struct AboutView_Previews: PreviewProvider { From 8a74b59a7eca30b4359e52f58e9a000d9dbea5fc Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 15 Nov 2024 22:45:44 -0500 Subject: [PATCH 5/5] macos: struct attributes can be private --- macos/Sources/Features/About/AboutView.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/macos/Sources/Features/About/AboutView.swift b/macos/Sources/Features/About/AboutView.swift index efe1f31bb..d9372aa15 100644 --- a/macos/Sources/Features/About/AboutView.swift +++ b/macos/Sources/Features/About/AboutView.swift @@ -106,9 +106,9 @@ struct AboutView: View { } private struct PropertyRow: View { - let label: String - let text: String - let url: URL? + private let label: String + private let text: String + private let url: URL? init(label: String, text: String, url: URL? = nil) { self.label = label