QuickStartIcons.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import Cocoa
  2. enum QuickStartIconKind {
  3. case photos
  4. case files
  5. case importFile
  6. }
  7. final class QuickStartIconView: NSView {
  8. private let kind: QuickStartIconKind
  9. init(kind: QuickStartIconKind) {
  10. self.kind = kind
  11. super.init(frame: .zero)
  12. translatesAutoresizingMaskIntoConstraints = false
  13. }
  14. @available(*, unavailable)
  15. required init?(coder: NSCoder) { nil }
  16. override var isFlipped: Bool { true }
  17. override var isOpaque: Bool { false }
  18. override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. guard let context = NSGraphicsContext.current?.cgContext else { return }
  21. switch kind {
  22. case .photos:
  23. drawPhotosIcon(in: context)
  24. case .files:
  25. drawFilesIcon(in: context)
  26. case .importFile:
  27. drawImportIcon(in: context)
  28. }
  29. }
  30. // MARK: - Photos
  31. private func drawPhotosIcon(in context: CGContext) {
  32. let s = min(bounds.width, bounds.height)
  33. let ox = (bounds.width - s) / 2
  34. let oy = (bounds.height - s) / 2
  35. drawSoftShadow(in: context, rect: CGRect(x: ox + s * 0.30, y: oy + s * 0.52, width: s * 0.50, height: s * 0.08), radius: s * 0.04)
  36. context.saveGState()
  37. context.translateBy(x: ox + s * 0.62, y: oy + s * 0.30)
  38. context.rotate(by: .pi / 7.5)
  39. context.translateBy(x: -(s * 0.24), y: -(s * 0.28))
  40. let backRect = CGRect(x: 0, y: 0, width: s * 0.48, height: s * 0.56)
  41. let backPath = roundedRect(backRect, radius: s * 0.085)
  42. context.setFillColor(NSColor.white.withAlphaComponent(0.92).cgColor)
  43. context.addPath(backPath)
  44. context.fillPath()
  45. context.setStrokeColor(NSColor.white.cgColor)
  46. context.setLineWidth(s * 0.014)
  47. context.addPath(backPath)
  48. context.strokePath()
  49. drawMountainScene(
  50. in: context,
  51. rect: CGRect(x: backRect.minX + s * 0.05, y: backRect.minY + s * 0.07, width: backRect.width - s * 0.10, height: backRect.height - s * 0.12),
  52. sunColor: NSColor(red: 0.62, green: 0.86, blue: 0.95, alpha: 1),
  53. mountainColor: NSColor(red: 0.38, green: 0.80, blue: 0.90, alpha: 1)
  54. )
  55. context.restoreGState()
  56. let frontRect = CGRect(x: ox + s * 0.20, y: oy + s * 0.22, width: s * 0.50, height: s * 0.58)
  57. let frontPath = roundedRect(frontRect, radius: s * 0.085)
  58. let blueTop = NSColor(red: 0.34, green: 0.62, blue: 1.0, alpha: 1)
  59. let blueBottom = NSColor(red: 0.16, green: 0.40, blue: 0.94, alpha: 1)
  60. fillGradient(in: context, path: frontPath, colors: [blueTop.cgColor, blueBottom.cgColor], start: CGPoint(x: frontRect.midX, y: frontRect.minY), end: CGPoint(x: frontRect.midX, y: frontRect.maxY))
  61. drawMountainScene(
  62. in: context,
  63. rect: CGRect(x: frontRect.minX + s * 0.05, y: frontRect.minY + s * 0.07, width: frontRect.width - s * 0.10, height: frontRect.height - s * 0.12),
  64. sunColor: .white,
  65. mountainColor: .white
  66. )
  67. context.setStrokeColor(NSColor.white.withAlphaComponent(0.4).cgColor)
  68. context.setLineWidth(s * 0.01)
  69. context.addPath(frontPath)
  70. context.strokePath()
  71. }
  72. private func drawMountainScene(in context: CGContext, rect: CGRect, sunColor: NSColor, mountainColor: NSColor) {
  73. let sunRadius = rect.width * 0.09
  74. context.setFillColor(sunColor.cgColor)
  75. context.fillEllipse(in: CGRect(x: rect.minX + rect.width * 0.10, y: rect.minY + rect.height * 0.08, width: sunRadius * 2, height: sunRadius * 2))
  76. let baseY = rect.maxY - rect.height * 0.10
  77. context.setFillColor(mountainColor.cgColor)
  78. let leftPeak = mountainPath(
  79. baseY: baseY,
  80. peakX: rect.minX + rect.width * 0.22,
  81. peakY: rect.minY + rect.height * 0.48,
  82. leftX: rect.minX,
  83. rightX: rect.minX + rect.width * 0.48
  84. )
  85. context.addPath(leftPeak)
  86. context.fillPath()
  87. let rightPeak = mountainPath(
  88. baseY: baseY,
  89. peakX: rect.minX + rect.width * 0.72,
  90. peakY: rect.minY + rect.height * 0.62,
  91. leftX: rect.minX + rect.width * 0.34,
  92. rightX: rect.maxX
  93. )
  94. context.addPath(rightPeak)
  95. context.fillPath()
  96. }
  97. // MARK: - Files
  98. private func drawFilesIcon(in context: CGContext) {
  99. let s = min(bounds.width, bounds.height)
  100. let ox = (bounds.width - s) / 2
  101. let oy = (bounds.height - s) / 2
  102. let folderRect = CGRect(x: ox + s * 0.10, y: oy + s * 0.28, width: s * 0.78, height: s * 0.52)
  103. drawSoftShadow(in: context, rect: CGRect(x: folderRect.minX, y: folderRect.maxY - s * 0.04, width: folderRect.width, height: s * 0.06), radius: s * 0.03)
  104. let backGreen = NSColor(red: 0.42, green: 0.80, blue: 0.36, alpha: 1)
  105. let frontGreen = NSColor(red: 0.62, green: 0.92, blue: 0.44, alpha: 1)
  106. let tabRect = CGRect(x: folderRect.minX + s * 0.02, y: folderRect.minY - s * 0.08, width: s * 0.30, height: s * 0.10)
  107. context.setFillColor(backGreen.cgColor)
  108. context.addPath(roundedRect(tabRect, radius: s * 0.025, topLeft: s * 0.025, topRight: s * 0.025, bottomLeft: 0, bottomRight: 0))
  109. context.fillPath()
  110. let backBody = CGRect(x: folderRect.minX, y: folderRect.minY, width: folderRect.width, height: folderRect.height)
  111. fillGradient(in: context, path: roundedRect(backBody, radius: s * 0.055), colors: [backGreen.cgColor, NSColor(red: 0.24, green: 0.66, blue: 0.28, alpha: 1).cgColor], start: CGPoint(x: backBody.midX, y: backBody.minY), end: CGPoint(x: backBody.midX, y: backBody.maxY))
  112. let docBack = CGRect(x: folderRect.minX + s * 0.14, y: folderRect.minY - s * 0.18, width: s * 0.32, height: s * 0.40)
  113. context.setFillColor(NSColor.white.withAlphaComponent(0.85).cgColor)
  114. context.addPath(roundedRect(docBack, radius: s * 0.02))
  115. context.fillPath()
  116. let docFront = CGRect(x: folderRect.minX + s * 0.22, y: folderRect.minY - s * 0.28, width: s * 0.40, height: s * 0.50)
  117. context.setFillColor(NSColor.white.cgColor)
  118. context.addPath(roundedRect(docFront, radius: s * 0.03))
  119. context.fillPath()
  120. let header = CGRect(x: docFront.minX, y: docFront.minY, width: docFront.width, height: s * 0.12)
  121. context.setFillColor(NSColor(red: 0.20, green: 0.48, blue: 0.96, alpha: 1).cgColor)
  122. context.addPath(roundedRect(header, radius: s * 0.03, topLeft: s * 0.03, topRight: s * 0.03, bottomLeft: 0, bottomRight: 0))
  123. context.fillPath()
  124. let lineColor = NSColor(red: 0.66, green: 0.74, blue: 0.98, alpha: 1)
  125. for (index, widthFactor) in [0.78, 0.62, 0.70].enumerated() {
  126. let lineY = docFront.minY + s * 0.18 + CGFloat(index) * s * 0.075
  127. let lineRect = CGRect(x: docFront.minX + s * 0.06, y: lineY, width: docFront.width * widthFactor - s * 0.08, height: s * 0.038)
  128. context.setFillColor(lineColor.cgColor)
  129. context.addPath(roundedRect(lineRect, radius: s * 0.014))
  130. context.fillPath()
  131. }
  132. let frontFlap = CGRect(x: folderRect.minX, y: folderRect.minY + folderRect.height * 0.38, width: folderRect.width, height: folderRect.height * 0.62)
  133. fillGradient(in: context, path: roundedRect(frontFlap, radius: s * 0.055, topLeft: 0, topRight: 0, bottomLeft: s * 0.055, bottomRight: s * 0.055), colors: [frontGreen.cgColor, NSColor(red: 0.38, green: 0.76, blue: 0.32, alpha: 1).cgColor], start: CGPoint(x: frontFlap.midX, y: frontFlap.minY), end: CGPoint(x: frontFlap.midX, y: frontFlap.maxY))
  134. }
  135. // MARK: - Import
  136. private func drawImportIcon(in context: CGContext) {
  137. let s = min(bounds.width, bounds.height)
  138. let ox = (bounds.width - s) / 2
  139. let oy = (bounds.height - s) / 2
  140. let folderRect = CGRect(x: ox + s * 0.08, y: oy + s * 0.30, width: s * 0.74, height: s * 0.46)
  141. drawSoftShadow(in: context, rect: CGRect(x: folderRect.minX, y: folderRect.maxY - s * 0.03, width: folderRect.width, height: s * 0.05), radius: s * 0.025)
  142. let orangeTop = NSColor(red: 1.0, green: 0.84, blue: 0.40, alpha: 1)
  143. let orangeBottom = NSColor(red: 0.95, green: 0.60, blue: 0.16, alpha: 1)
  144. let tabRect = CGRect(x: folderRect.minX + s * 0.02, y: folderRect.minY - s * 0.07, width: s * 0.28, height: s * 0.09)
  145. context.setFillColor(orangeBottom.cgColor)
  146. context.addPath(roundedRect(tabRect, radius: s * 0.022, topLeft: s * 0.022, topRight: s * 0.022, bottomLeft: 0, bottomRight: 0))
  147. context.fillPath()
  148. fillGradient(in: context, path: roundedRect(folderRect, radius: s * 0.048), colors: [orangeTop.cgColor, orangeBottom.cgColor], start: CGPoint(x: folderRect.midX, y: folderRect.minY), end: CGPoint(x: folderRect.midX, y: folderRect.maxY))
  149. let paper = CGRect(x: folderRect.minX + s * 0.12, y: folderRect.minY + s * 0.08, width: folderRect.width * 0.76, height: folderRect.height * 0.38)
  150. context.setFillColor(NSColor.white.withAlphaComponent(0.9).cgColor)
  151. context.addPath(roundedRect(paper, radius: s * 0.018))
  152. context.fillPath()
  153. let frontFlap = CGRect(x: folderRect.minX, y: folderRect.minY + folderRect.height * 0.42, width: folderRect.width, height: folderRect.height * 0.58)
  154. fillGradient(in: context, path: roundedRect(frontFlap, radius: s * 0.048, topLeft: 0, topRight: 0, bottomLeft: s * 0.048, bottomRight: s * 0.048), colors: [NSColor(red: 1.0, green: 0.90, blue: 0.50, alpha: 1).cgColor, orangeBottom.cgColor], start: CGPoint(x: frontFlap.midX, y: frontFlap.minY), end: CGPoint(x: frontFlap.midX, y: frontFlap.maxY))
  155. let badgeRadius = s * 0.17
  156. let badgeCenter = CGPoint(x: folderRect.midX, y: folderRect.maxY - s * 0.02)
  157. drawSoftShadow(in: context, rect: CGRect(x: badgeCenter.x - badgeRadius, y: badgeCenter.y - badgeRadius * 0.4, width: badgeRadius * 2, height: badgeRadius * 0.5), radius: s * 0.03)
  158. let badgeRect = CGRect(x: badgeCenter.x - badgeRadius, y: badgeCenter.y - badgeRadius, width: badgeRadius * 2, height: badgeRadius * 2)
  159. let badgeColors = [NSColor(red: 0.66, green: 0.78, blue: 1.0, alpha: 1).cgColor, NSColor(red: 0.40, green: 0.56, blue: 0.96, alpha: 1).cgColor]
  160. fillGradient(in: context, path: CGPath(ellipseIn: badgeRect, transform: nil), colors: badgeColors, start: CGPoint(x: badgeRect.midX, y: badgeRect.minY), end: CGPoint(x: badgeRect.midX, y: badgeRect.maxY))
  161. drawCloudUpload(in: context, center: badgeCenter, size: badgeRadius * 1.15)
  162. }
  163. private func drawCloudUpload(in context: CGContext, center: CGPoint, size: CGFloat) {
  164. let cloudColor = NSColor.white
  165. let arrowColor = NSColor(red: 0.28, green: 0.42, blue: 0.86, alpha: 1)
  166. let w = size
  167. let h = size * 0.62
  168. let cloudRect = CGRect(x: center.x - w / 2, y: center.y - h / 2 + size * 0.04, width: w, height: h)
  169. let cloud = cloudPath(in: cloudRect)
  170. context.setFillColor(cloudColor.cgColor)
  171. context.addPath(cloud)
  172. context.fillPath()
  173. let arrowWidth = size * 0.14
  174. let arrowHeight = size * 0.28
  175. let arrowTop = center.y + size * 0.02
  176. let arrow = CGMutablePath()
  177. arrow.move(to: CGPoint(x: center.x, y: arrowTop + arrowHeight))
  178. arrow.addLine(to: CGPoint(x: center.x - arrowWidth, y: arrowTop + arrowHeight * 0.45))
  179. arrow.addLine(to: CGPoint(x: center.x - arrowWidth * 0.45, y: arrowTop + arrowHeight * 0.45))
  180. arrow.addLine(to: CGPoint(x: center.x - arrowWidth * 0.45, y: arrowTop))
  181. arrow.addLine(to: CGPoint(x: center.x + arrowWidth * 0.45, y: arrowTop))
  182. arrow.addLine(to: CGPoint(x: center.x + arrowWidth * 0.45, y: arrowTop + arrowHeight * 0.45))
  183. arrow.addLine(to: CGPoint(x: center.x + arrowWidth, y: arrowTop + arrowHeight * 0.45))
  184. arrow.closeSubpath()
  185. context.setFillColor(arrowColor.cgColor)
  186. context.addPath(arrow)
  187. context.fillPath()
  188. }
  189. // MARK: - Helpers
  190. private func drawSoftShadow(in context: CGContext, rect: CGRect, radius: CGFloat) {
  191. context.saveGState()
  192. context.setFillColor(NSColor.black.withAlphaComponent(0.12).cgColor)
  193. context.addPath(roundedRect(rect.offsetBy(dx: 0, dy: -radius * 0.3), radius: radius))
  194. context.fillPath()
  195. context.restoreGState()
  196. }
  197. private func fillGradient(in context: CGContext, path: CGPath, colors: [CGColor], start: CGPoint, end: CGPoint) {
  198. context.saveGState()
  199. context.addPath(path)
  200. context.clip()
  201. guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as CFArray, locations: [0, 1]) else {
  202. context.restoreGState()
  203. return
  204. }
  205. context.drawLinearGradient(gradient, start: start, end: end, options: [])
  206. context.restoreGState()
  207. }
  208. private func roundedRect(_ rect: CGRect, radius: CGFloat) -> CGPath {
  209. roundedRect(rect, radius: radius, topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius)
  210. }
  211. private func roundedRect(_ rect: CGRect, radius: CGFloat, topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat) -> CGPath {
  212. let path = CGMutablePath()
  213. path.move(to: CGPoint(x: rect.minX + topLeft, y: rect.maxY))
  214. path.addLine(to: CGPoint(x: rect.maxX - topRight, y: rect.maxY))
  215. path.addQuadCurve(to: CGPoint(x: rect.maxX, y: rect.maxY - topRight), control: CGPoint(x: rect.maxX, y: rect.maxY))
  216. path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY + bottomRight))
  217. path.addQuadCurve(to: CGPoint(x: rect.maxX - bottomRight, y: rect.minY), control: CGPoint(x: rect.maxX, y: rect.minY))
  218. path.addLine(to: CGPoint(x: rect.minX + bottomLeft, y: rect.minY))
  219. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.minY + bottomLeft), control: CGPoint(x: rect.minX, y: rect.minY))
  220. path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY - topLeft))
  221. path.addQuadCurve(to: CGPoint(x: rect.minX + topLeft, y: rect.maxY), control: CGPoint(x: rect.minX, y: rect.maxY))
  222. path.closeSubpath()
  223. return path
  224. }
  225. private func mountainPath(baseY: CGFloat, peakX: CGFloat, peakY: CGFloat, leftX: CGFloat, rightX: CGFloat) -> CGPath {
  226. let path = CGMutablePath()
  227. path.move(to: CGPoint(x: leftX, y: baseY))
  228. path.addLine(to: CGPoint(x: peakX, y: peakY))
  229. path.addLine(to: CGPoint(x: rightX, y: baseY))
  230. path.closeSubpath()
  231. return path
  232. }
  233. private func cloudPath(in rect: CGRect) -> CGPath {
  234. let path = CGMutablePath()
  235. let r = rect.height * 0.38
  236. path.addEllipse(in: CGRect(x: rect.minX + rect.width * 0.08, y: rect.minY + rect.height * 0.18, width: r * 2, height: r * 2))
  237. path.addEllipse(in: CGRect(x: rect.minX + rect.width * 0.30, y: rect.minY + rect.height * 0.30, width: r * 2.1, height: r * 2.1))
  238. path.addEllipse(in: CGRect(x: rect.minX + rect.width * 0.52, y: rect.minY + rect.height * 0.16, width: r * 1.9, height: r * 1.9))
  239. path.addRect(CGRect(x: rect.minX + rect.width * 0.12, y: rect.minY + rect.height * 0.18, width: rect.width * 0.76, height: rect.height * 0.42))
  240. return path
  241. }
  242. }