Skip to content

Commit 46640c3

Browse files
committed
Improve support for macOS icons
1 parent 35951c5 commit 46640c3

File tree

5 files changed

+85
-19
lines changed

5 files changed

+85
-19
lines changed

Example/Sources/ExampleAppIcon/ExampleAppIconView.swift

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import SwiftUI
22
import AppIconCreator
33

44
public struct ExampleAppIconView: View {
5-
public init() {}
5+
public enum Platform {
6+
case iOS
7+
case macOS
8+
}
9+
10+
public init(_ platform: Platform) {
11+
self.platform = platform
12+
}
13+
14+
var platform: Platform
615

716
public var body: some View {
817
GeometryReader { geometry in
@@ -15,16 +24,44 @@ public struct ExampleAppIconView: View {
1524
startPoint: .top,
1625
endPoint: .bottom
1726
))
18-
.padding(geometry.size.width * 0.15)
27+
.padding(geometry.size.width * 0.18)
1928
.frame(maxWidth: .infinity, maxHeight: .infinity)
29+
.background(
30+
LinearGradient(
31+
gradient: Gradient(colors: [.blue, .purple]),
32+
startPoint: .top,
33+
endPoint: .bottom
34+
)
35+
)
36+
.if(platform == .macOS) {
37+
$0.overlay(
38+
RoundedRectangle(
39+
cornerRadius: geometry.size.width * 0.4,
40+
style: .continuous
41+
)
42+
.strokeBorder(
43+
lineWidth: geometry.size.width * 0.06
44+
)
45+
.overlayMask(
46+
LinearGradient(
47+
gradient: Gradient(colors: [.purple, .blue]),
48+
startPoint: .top,
49+
endPoint: .bottom
50+
)
51+
)
52+
.shadow(
53+
color: Color.black.opacity(0.5),
54+
radius: geometry.size.width * 0.015
55+
)
56+
)
57+
.clipShape(
58+
RoundedRectangle(
59+
cornerRadius: geometry.size.width * 0.4,
60+
style: .continuous
61+
)
62+
)
63+
}
2064
}
21-
.background(
22-
LinearGradient(
23-
gradient: Gradient(colors: [.blue, .purple]),
24-
startPoint: .top,
25-
endPoint: .bottom
26-
)
27-
)
2865
}
2966
}
3067

@@ -37,8 +74,13 @@ extension View {
3774
struct ExampleAppIconView_Preivews: PreviewProvider {
3875
static var previews: some View {
3976
IconPreviews(
40-
icon: ExampleAppIconView(),
77+
icon: ExampleAppIconView(.iOS),
4178
configs: .iOS
4279
)
80+
IconPreviews(
81+
icon: ExampleAppIconView(.macOS),
82+
configs: .macOS,
83+
clip: false
84+
)
4385
}
4486
}

Example/Sources/Export/main.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import AppIconCreator
22
import ExampleAppIcon
33
import Foundation
44

5-
let icon = ExampleAppIconView()
6-
let configs = [IconConfig].iOS
7-
let images = [IconImage].images(for: icon, with: configs)
85
let exportURL = FileManager.default
96
.homeDirectoryForCurrentUser
107
.appendingPathComponent("Desktop")
118
.appendingPathComponent("ExampleAppIcon")
12-
images.forEach { $0.save(to: exportURL) }
9+
10+
[IconImage]
11+
.images(for: ExampleAppIconView(.iOS), with: .iOS)
12+
.forEach { $0.save(to: exportURL) }
13+
14+
[IconImage]
15+
.images(for: ExampleAppIconView(.macOS), with: .macOS)
16+
.forEach { $0.save(to: exportURL) }

Sources/AppIconCreator/IconPreview.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import SwiftUI
33
public struct IconPreview<Icon: View>: View {
44
public var icon: Icon
55
public var config: IconConfig
6+
public var clip: Bool
67

7-
public init(icon: Icon, config: IconConfig) {
8+
public init(icon: Icon, config: IconConfig, clip: Bool = true) {
89
self.icon = icon
910
self.config = config
11+
self.clip = clip
1012
}
1113

1214
public var body: some View {
@@ -29,9 +31,13 @@ public struct IconPreview<Icon: View>: View {
2931
let screenScale = NSScreen.main!.backingScaleFactor
3032
let viewSize = CGFloat(config.size) * CGFloat(scale) / screenScale
3133
return icon
32-
.background(Color.white)
3334
.environment(\.colorScheme, .light)
3435
.frame(width: viewSize, height: viewSize, alignment: .center)
35-
.clipShape(RoundedRectangle(cornerRadius: viewSize * 0.2, style: .continuous))
36+
.if(clip) {
37+
$0.clipShape(RoundedRectangle(
38+
cornerRadius: viewSize * 0.2,
39+
style: .continuous
40+
))
41+
}
3642
}
3743
}

Sources/AppIconCreator/IconPreviews.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import SwiftUI
33
public struct IconPreviews<Icon: View>: View {
44
public var icon: Icon
55
public var configs: [IconConfig]
6+
public var clip: Bool
67

7-
public init(icon: Icon, configs: [IconConfig]) {
8+
public init(icon: Icon, configs: [IconConfig], clip: Bool = true) {
89
self.icon = icon
910
self.configs = configs
11+
self.clip = clip
1012
}
1113

1214
public var body: some View {
1315
VStack(alignment: .leading, spacing: 16) {
1416
ForEach(configs) { config in
15-
IconPreview(icon: icon, config: config)
17+
IconPreview(icon: icon, config: config, clip: clip)
1618
}
1719
}
1820
.fixedSize()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import SwiftUI
2+
3+
public extension View {
4+
@ViewBuilder
5+
func `if`<Content: View>(_ condition: Bool, content: (Self) -> Content) -> some View {
6+
if condition {
7+
content(self)
8+
} else {
9+
self
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)