From 6899b3ac7d679582be00921f7ca90fbdfceef937 Mon Sep 17 00:00:00 2001 From: xinnjie Date: Tue, 9 Sep 2025 22:32:16 +0800 Subject: [PATCH] feat: support mac os --- Package.swift | 3 ++- Sources/gitdiff/Core/PlatformColors.swift | 31 +++++++++++++++++++++++ Sources/gitdiff/Views/DiffFileView.swift | 2 +- Sources/gitdiff/Views/DiffLineView.swift | 2 +- Sources/gitdiff/Views/DiffRenderer.swift | 4 +-- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 Sources/gitdiff/Core/PlatformColors.swift diff --git a/Package.swift b/Package.swift index f200f90..203105d 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,8 @@ import PackageDescription let package = Package( name: "gitdiff", platforms: [ - .iOS(.v15) + .iOS(.v15), + .macOS(.v13) ], products: [ .library( diff --git a/Sources/gitdiff/Core/PlatformColors.swift b/Sources/gitdiff/Core/PlatformColors.swift new file mode 100644 index 0000000..94e05ef --- /dev/null +++ b/Sources/gitdiff/Core/PlatformColors.swift @@ -0,0 +1,31 @@ +import SwiftUI + +#if os(iOS) || os(tvOS) || os(watchOS) + import UIKit +#endif +#if os(macOS) + import AppKit +#endif + +/// Cross-platform system background color wrapper +enum PlatformColors { + static var background: Color { + #if os(iOS) || os(tvOS) || os(watchOS) + return Color(UIColor.systemBackground) + #elseif os(macOS) + if #available(macOS 10.15, *) { + // There is no NSColor.systemBackground; windowBackgroundColor is closest for content areas + return Color(NSColor.windowBackgroundColor) + } else { + return Color.white + } + #else + return Color.white + #endif + } +} + +extension Color { + /// A cross-platform background color similar to system background. + public static var appBackground: Color { PlatformColors.background } +} diff --git a/Sources/gitdiff/Views/DiffFileView.swift b/Sources/gitdiff/Views/DiffFileView.swift index cf12407..ad4de11 100644 --- a/Sources/gitdiff/Views/DiffFileView.swift +++ b/Sources/gitdiff/Views/DiffFileView.swift @@ -110,5 +110,5 @@ struct DiffFileView: View { DiffFileView(file: sampleFile) .padding() - .background(Color(.systemBackground)) + .background(Color.appBackground) } diff --git a/Sources/gitdiff/Views/DiffLineView.swift b/Sources/gitdiff/Views/DiffLineView.swift index 0fffed3..923f8cf 100644 --- a/Sources/gitdiff/Views/DiffLineView.swift +++ b/Sources/gitdiff/Views/DiffLineView.swift @@ -142,5 +142,5 @@ struct DiffLineView: View { ) ) } - .background(Color(.systemBackground)) + .background(Color.appBackground) } diff --git a/Sources/gitdiff/Views/DiffRenderer.swift b/Sources/gitdiff/Views/DiffRenderer.swift index bac5a91..64ade4e 100644 --- a/Sources/gitdiff/Views/DiffRenderer.swift +++ b/Sources/gitdiff/Views/DiffRenderer.swift @@ -60,7 +60,7 @@ public struct DiffRenderer: View { .padding() } } - .background(Color(.systemBackground)) + .background(Color.appBackground) } } @@ -103,7 +103,7 @@ public struct DiffRenderer: View { // With dark theme DiffRenderer(diffText: text) - .diffTheme(.vsCodeDark) + .diffTheme(.dark) // With custom configuration DiffRenderer(diffText: text)