Sin descripción

FeatureIcons.swift 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import Cocoa
  2. enum FeatureIconKind {
  3. case scanFile
  4. case printText
  5. case printContacts
  6. case printWebsite
  7. case drawPrint
  8. case ocrFile
  9. }
  10. final class FeatureIconView: NSView {
  11. private let kind: FeatureIconKind
  12. init(kind: FeatureIconKind) {
  13. self.kind = kind
  14. super.init(frame: .zero)
  15. translatesAutoresizingMaskIntoConstraints = false
  16. }
  17. @available(*, unavailable)
  18. required init?(coder: NSCoder) { nil }
  19. override var isFlipped: Bool { true }
  20. override var isOpaque: Bool { false }
  21. override func draw(_ dirtyRect: NSRect) {
  22. super.draw(dirtyRect)
  23. guard let context = NSGraphicsContext.current?.cgContext else { return }
  24. switch kind {
  25. case .scanFile: drawScanFile(in: context)
  26. case .printText: drawPrintText(in: context)
  27. case .printContacts: drawPrintContacts(in: context)
  28. case .printWebsite: drawPrintWebsite(in: context)
  29. case .drawPrint: drawDrawPrint(in: context)
  30. case .ocrFile: drawOCRFile(in: context)
  31. }
  32. }
  33. // MARK: - Scan File
  34. private func drawScanFile(in context: CGContext) {
  35. let s = min(bounds.width, bounds.height)
  36. let ox = (bounds.width - s) / 2
  37. let oy = (bounds.height - s) / 2
  38. let bodyRect = CGRect(x: ox + s * 0.08, y: oy + s * 0.30, width: s * 0.84, height: s * 0.52)
  39. drawSoftShadow(in: context, rect: CGRect(x: bodyRect.minX, y: bodyRect.maxY - s * 0.03, width: bodyRect.width, height: s * 0.05), radius: s * 0.025)
  40. let blueTop = NSColor(red: 0.38, green: 0.66, blue: 1.0, alpha: 1)
  41. let blueBottom = NSColor(red: 0.18, green: 0.44, blue: 0.94, alpha: 1)
  42. fillGradient(in: context, path: roundedRect(bodyRect, radius: s * 0.06), colors: [blueTop.cgColor, blueBottom.cgColor], start: CGPoint(x: bodyRect.midX, y: bodyRect.minY), end: CGPoint(x: bodyRect.midX, y: bodyRect.maxY))
  43. let glassRect = CGRect(x: bodyRect.minX + s * 0.08, y: bodyRect.minY + s * 0.06, width: bodyRect.width * 0.84, height: bodyRect.height * 0.52)
  44. context.setFillColor(NSColor.white.withAlphaComponent(0.25).cgColor)
  45. context.addPath(roundedRect(glassRect, radius: s * 0.03))
  46. context.fillPath()
  47. let lidRect = CGRect(x: ox + s * 0.04, y: oy + s * 0.14, width: s * 0.92, height: s * 0.20)
  48. fillGradient(in: context, path: roundedRect(lidRect, radius: s * 0.04), colors: [NSColor(red: 0.55, green: 0.76, blue: 1.0, alpha: 1).cgColor, blueTop.cgColor], start: CGPoint(x: lidRect.midX, y: lidRect.minY), end: CGPoint(x: lidRect.midX, y: lidRect.maxY))
  49. let paperRect = CGRect(x: ox + s * 0.28, y: oy + s * 0.02, width: s * 0.44, height: s * 0.22)
  50. context.setFillColor(NSColor.white.cgColor)
  51. context.addPath(roundedRect(paperRect, radius: s * 0.02))
  52. context.fillPath()
  53. for i in 0..<3 {
  54. let lineY = paperRect.minY + s * 0.06 + CGFloat(i) * s * 0.045
  55. let lineW = paperRect.width * (0.85 - CGFloat(i) * 0.1)
  56. context.setFillColor(NSColor(red: 0.72, green: 0.82, blue: 0.98, alpha: 1).cgColor)
  57. context.addPath(roundedRect(CGRect(x: paperRect.minX + s * 0.05, y: lineY, width: lineW, height: s * 0.025), radius: s * 0.01))
  58. context.fillPath()
  59. }
  60. let lightRect = CGRect(x: bodyRect.maxX - s * 0.14, y: bodyRect.minY + s * 0.08, width: s * 0.06, height: s * 0.06)
  61. context.setFillColor(NSColor(red: 0.30, green: 0.90, blue: 0.55, alpha: 1).cgColor)
  62. context.fillEllipse(in: lightRect)
  63. }
  64. // MARK: - Print Text
  65. private func drawPrintText(in context: CGContext) {
  66. let s = min(bounds.width, bounds.height)
  67. let ox = (bounds.width - s) / 2
  68. let oy = (bounds.height - s) / 2
  69. let frameRect = CGRect(x: ox + s * 0.14, y: oy + s * 0.14, width: s * 0.72, height: s * 0.72)
  70. drawSoftShadow(in: context, rect: frameRect.insetBy(dx: s * 0.04, dy: -s * 0.02), radius: s * 0.03)
  71. context.setStrokeColor(NSColor(red: 0.62, green: 0.44, blue: 0.96, alpha: 1).cgColor)
  72. context.setLineWidth(s * 0.025)
  73. context.setLineDash(phase: 0, lengths: [s * 0.04, s * 0.04])
  74. context.addPath(roundedRect(frameRect, radius: s * 0.04))
  75. context.strokePath()
  76. context.setLineDash(phase: 0, lengths: [])
  77. let purple = NSColor(red: 0.55, green: 0.36, blue: 0.96, alpha: 1)
  78. let font = NSFont.systemFont(ofSize: s * 0.42, weight: .bold)
  79. let attrs: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: purple]
  80. let text = "T" as NSString
  81. let textSize = text.size(withAttributes: attrs)
  82. text.draw(at: CGPoint(x: frameRect.midX - textSize.width / 2, y: frameRect.midY - textSize.height / 2), withAttributes: attrs)
  83. }
  84. // MARK: - Print Contacts
  85. private func drawPrintContacts(in context: CGContext) {
  86. let s = min(bounds.width, bounds.height)
  87. let ox = (bounds.width - s) / 2
  88. let oy = (bounds.height - s) / 2
  89. let bookRect = CGRect(x: ox + s * 0.12, y: oy + s * 0.18, width: s * 0.62, height: s * 0.68)
  90. drawSoftShadow(in: context, rect: CGRect(x: bookRect.minX, y: bookRect.maxY - s * 0.03, width: bookRect.width, height: s * 0.05), radius: s * 0.025)
  91. let greenTop = NSColor(red: 0.42, green: 0.86, blue: 0.52, alpha: 1)
  92. let greenBottom = NSColor(red: 0.18, green: 0.68, blue: 0.38, alpha: 1)
  93. fillGradient(in: context, path: roundedRect(bookRect, radius: s * 0.05), colors: [greenTop.cgColor, greenBottom.cgColor], start: CGPoint(x: bookRect.midX, y: bookRect.minY), end: CGPoint(x: bookRect.midX, y: bookRect.maxY))
  94. let spineRect = CGRect(x: bookRect.minX, y: bookRect.minY, width: s * 0.08, height: bookRect.height)
  95. context.setFillColor(NSColor(red: 0.12, green: 0.52, blue: 0.30, alpha: 1).cgColor)
  96. context.addPath(roundedRect(spineRect, radius: s * 0.02, topLeft: s * 0.05, topRight: 0, bottomLeft: s * 0.05, bottomRight: 0))
  97. context.fillPath()
  98. let tabColors: [NSColor] = [
  99. NSColor(red: 0.96, green: 0.55, blue: 0.55, alpha: 1),
  100. NSColor(red: 0.55, green: 0.72, blue: 0.96, alpha: 1),
  101. NSColor(red: 0.96, green: 0.82, blue: 0.40, alpha: 1),
  102. ]
  103. for (i, color) in tabColors.enumerated() {
  104. let tabRect = CGRect(x: bookRect.maxX - s * 0.02, y: bookRect.minY + s * 0.10 + CGFloat(i) * s * 0.14, width: s * 0.10, height: s * 0.10)
  105. context.setFillColor(color.cgColor)
  106. context.addPath(roundedRect(tabRect, radius: s * 0.02, topLeft: 0, topRight: s * 0.02, bottomLeft: 0, bottomRight: s * 0.02))
  107. context.fillPath()
  108. }
  109. let personCenter = CGPoint(x: bookRect.midX - s * 0.02, y: bookRect.midY + s * 0.04)
  110. context.setFillColor(NSColor.white.cgColor)
  111. context.fillEllipse(in: CGRect(x: personCenter.x - s * 0.10, y: personCenter.y - s * 0.16, width: s * 0.20, height: s * 0.20))
  112. context.fillEllipse(in: CGRect(x: personCenter.x - s * 0.14, y: personCenter.y + s * 0.02, width: s * 0.28, height: s * 0.22))
  113. }
  114. // MARK: - Print Website
  115. private func drawPrintWebsite(in context: CGContext) {
  116. let s = min(bounds.width, bounds.height)
  117. let ox = (bounds.width - s) / 2
  118. let oy = (bounds.height - s) / 2
  119. let globeRect = CGRect(x: ox + s * 0.10, y: oy + s * 0.14, width: s * 0.58, height: s * 0.58)
  120. drawSoftShadow(in: context, rect: globeRect.insetBy(dx: 0, dy: -s * 0.03), radius: s * 0.03)
  121. let blueTop = NSColor(red: 0.40, green: 0.70, blue: 1.0, alpha: 1)
  122. let blueBottom = NSColor(red: 0.16, green: 0.44, blue: 0.92, alpha: 1)
  123. fillGradient(in: context, path: CGPath(ellipseIn: globeRect, transform: nil), colors: [blueTop.cgColor, blueBottom.cgColor], start: CGPoint(x: globeRect.midX, y: globeRect.minY), end: CGPoint(x: globeRect.midX, y: globeRect.maxY))
  124. context.setStrokeColor(NSColor.white.withAlphaComponent(0.55).cgColor)
  125. context.setLineWidth(s * 0.018)
  126. context.strokeEllipse(in: globeRect.insetBy(dx: globeRect.width * 0.15, dy: 0))
  127. context.strokeEllipse(in: globeRect.insetBy(dx: 0, dy: globeRect.height * 0.22))
  128. context.move(to: CGPoint(x: globeRect.minX, y: globeRect.midY))
  129. context.addLine(to: CGPoint(x: globeRect.maxX, y: globeRect.midY))
  130. context.strokePath()
  131. let cursorSize = s * 0.30
  132. let cursorOrigin = CGPoint(x: ox + s * 0.52, y: oy + s * 0.38)
  133. context.setFillColor(NSColor.white.cgColor)
  134. let cursor = CGMutablePath()
  135. cursor.move(to: cursorOrigin)
  136. cursor.addLine(to: CGPoint(x: cursorOrigin.x, y: cursorOrigin.y + cursorSize))
  137. cursor.addLine(to: CGPoint(x: cursorOrigin.x + cursorSize * 0.35, y: cursorOrigin.y + cursorSize * 0.72))
  138. cursor.closeSubpath()
  139. context.addPath(cursor)
  140. context.fillPath()
  141. context.setStrokeColor(NSColor(red: 0.30, green: 0.50, blue: 0.90, alpha: 1).cgColor)
  142. context.setLineWidth(s * 0.02)
  143. context.addPath(cursor)
  144. context.strokePath()
  145. }
  146. // MARK: - Draw & Print
  147. private func drawDrawPrint(in context: CGContext) {
  148. let s = min(bounds.width, bounds.height)
  149. let ox = (bounds.width - s) / 2
  150. let oy = (bounds.height - s) / 2
  151. let paletteRect = CGRect(x: ox + s * 0.08, y: oy + s * 0.30, width: s * 0.72, height: s * 0.48)
  152. drawSoftShadow(in: context, rect: CGRect(x: paletteRect.minX, y: paletteRect.maxY - s * 0.02, width: paletteRect.width, height: s * 0.04), radius: s * 0.025)
  153. let woodTop = NSColor(red: 0.96, green: 0.78, blue: 0.48, alpha: 1)
  154. let woodBottom = NSColor(red: 0.86, green: 0.58, blue: 0.28, alpha: 1)
  155. let palettePath = paletteShape(in: paletteRect)
  156. fillGradient(in: context, path: palettePath, colors: [woodTop.cgColor, woodBottom.cgColor], start: CGPoint(x: paletteRect.midX, y: paletteRect.minY), end: CGPoint(x: paletteRect.midX, y: paletteRect.maxY))
  157. let paintColors: [NSColor] = [
  158. NSColor(red: 0.96, green: 0.35, blue: 0.35, alpha: 1),
  159. NSColor(red: 0.35, green: 0.65, blue: 0.96, alpha: 1),
  160. NSColor(red: 0.35, green: 0.82, blue: 0.45, alpha: 1),
  161. NSColor(red: 0.96, green: 0.78, blue: 0.25, alpha: 1),
  162. NSColor(red: 0.72, green: 0.40, blue: 0.96, alpha: 1),
  163. ]
  164. let thumbCenter = CGPoint(x: paletteRect.minX + paletteRect.width * 0.18, y: paletteRect.midY)
  165. for (i, color) in paintColors.enumerated() {
  166. let angle = CGFloat(i) * .pi * 0.32 - .pi * 0.5
  167. let radius = paletteRect.width * 0.28
  168. let dotCenter = CGPoint(x: thumbCenter.x + cos(angle) * radius, y: thumbCenter.y + sin(angle) * radius * 0.55)
  169. let dotSize = s * 0.09
  170. context.setFillColor(color.cgColor)
  171. context.fillEllipse(in: CGRect(x: dotCenter.x - dotSize / 2, y: dotCenter.y - dotSize / 2, width: dotSize, height: dotSize))
  172. }
  173. context.saveGState()
  174. context.translateBy(x: ox + s * 0.58, y: oy + s * 0.08)
  175. context.rotate(by: -.pi / 5)
  176. let brushRect = CGRect(x: 0, y: 0, width: s * 0.10, height: s * 0.42)
  177. fillGradient(in: context, path: roundedRect(brushRect, radius: s * 0.02), colors: [NSColor(red: 0.72, green: 0.48, blue: 0.28, alpha: 1).cgColor, NSColor(red: 0.52, green: 0.32, blue: 0.18, alpha: 1).cgColor], start: CGPoint(x: brushRect.midX, y: brushRect.minY), end: CGPoint(x: brushRect.midX, y: brushRect.maxY))
  178. let bristleRect = CGRect(x: brushRect.midX - s * 0.05, y: brushRect.maxY - s * 0.02, width: s * 0.10, height: s * 0.10)
  179. context.setFillColor(NSColor(red: 0.96, green: 0.55, blue: 0.30, alpha: 1).cgColor)
  180. context.fillEllipse(in: bristleRect)
  181. context.restoreGState()
  182. }
  183. private func paletteShape(in rect: CGRect) -> CGPath {
  184. let path = CGMutablePath()
  185. path.addEllipse(in: rect)
  186. let thumb = CGRect(x: rect.minX - rect.width * 0.08, y: rect.midY - rect.height * 0.18, width: rect.width * 0.22, height: rect.height * 0.36)
  187. path.addEllipse(in: thumb)
  188. return path
  189. }
  190. // MARK: - OCR File
  191. private func drawOCRFile(in context: CGContext) {
  192. let s = min(bounds.width, bounds.height)
  193. let ox = (bounds.width - s) / 2
  194. let oy = (bounds.height - s) / 2
  195. let docRect = CGRect(x: ox + s * 0.18, y: oy + s * 0.10, width: s * 0.56, height: s * 0.72)
  196. drawSoftShadow(in: context, rect: CGRect(x: docRect.minX, y: docRect.maxY - s * 0.03, width: docRect.width, height: s * 0.05), radius: s * 0.025)
  197. context.setFillColor(NSColor.white.cgColor)
  198. context.addPath(roundedRect(docRect, radius: s * 0.04))
  199. context.fillPath()
  200. let purpleTop = NSColor(red: 0.62, green: 0.44, blue: 0.96, alpha: 1)
  201. let purpleBottom = NSColor(red: 0.45, green: 0.28, blue: 0.86, alpha: 1)
  202. let headerRect = CGRect(x: docRect.minX, y: docRect.minY, width: docRect.width, height: s * 0.18)
  203. fillGradient(in: context, path: roundedRect(headerRect, radius: s * 0.04, topLeft: s * 0.04, topRight: s * 0.04, bottomLeft: 0, bottomRight: 0), colors: [purpleTop.cgColor, purpleBottom.cgColor], start: CGPoint(x: headerRect.midX, y: headerRect.minY), end: CGPoint(x: headerRect.midX, y: headerRect.maxY))
  204. let font = NSFont.systemFont(ofSize: s * 0.14, weight: .bold)
  205. let attrs: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: NSColor.white]
  206. let text = "OCR" as NSString
  207. let textSize = text.size(withAttributes: attrs)
  208. text.draw(at: CGPoint(x: docRect.midX - textSize.width / 2, y: docRect.minY + s * 0.04), withAttributes: attrs)
  209. let lineColor = NSColor(red: 0.78, green: 0.72, blue: 0.96, alpha: 1)
  210. for i in 0..<4 {
  211. let lineY = docRect.minY + s * 0.24 + CGFloat(i) * s * 0.10
  212. let lineW = docRect.width * (0.80 - CGFloat(i) * 0.08)
  213. context.setFillColor(lineColor.cgColor)
  214. context.addPath(roundedRect(CGRect(x: docRect.minX + s * 0.08, y: lineY, width: lineW, height: s * 0.035), radius: s * 0.012))
  215. context.fillPath()
  216. }
  217. }
  218. // MARK: - Helpers
  219. private func drawSoftShadow(in context: CGContext, rect: CGRect, radius: CGFloat) {
  220. context.saveGState()
  221. context.setFillColor(NSColor.black.withAlphaComponent(0.10).cgColor)
  222. context.addPath(roundedRect(rect.offsetBy(dx: 0, dy: radius * 0.2), radius: radius))
  223. context.fillPath()
  224. context.restoreGState()
  225. }
  226. private func fillGradient(in context: CGContext, path: CGPath, colors: [CGColor], start: CGPoint, end: CGPoint) {
  227. context.saveGState()
  228. context.addPath(path)
  229. context.clip()
  230. guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as CFArray, locations: [0, 1]) else {
  231. context.restoreGState()
  232. return
  233. }
  234. context.drawLinearGradient(gradient, start: start, end: end, options: [])
  235. context.restoreGState()
  236. }
  237. private func roundedRect(_ rect: CGRect, radius: CGFloat) -> CGPath {
  238. roundedRect(rect, radius: radius, topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius)
  239. }
  240. private func roundedRect(_ rect: CGRect, radius: CGFloat, topLeft: CGFloat, topRight: CGFloat, bottomLeft: CGFloat, bottomRight: CGFloat) -> CGPath {
  241. let path = CGMutablePath()
  242. path.move(to: CGPoint(x: rect.minX + topLeft, y: rect.maxY))
  243. path.addLine(to: CGPoint(x: rect.maxX - topRight, y: rect.maxY))
  244. path.addQuadCurve(to: CGPoint(x: rect.maxX, y: rect.maxY - topRight), control: CGPoint(x: rect.maxX, y: rect.maxY))
  245. path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY + bottomRight))
  246. path.addQuadCurve(to: CGPoint(x: rect.maxX - bottomRight, y: rect.minY), control: CGPoint(x: rect.maxX, y: rect.minY))
  247. path.addLine(to: CGPoint(x: rect.minX + bottomLeft, y: rect.minY))
  248. path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.minY + bottomLeft), control: CGPoint(x: rect.minX, y: rect.minY))
  249. path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY - topLeft))
  250. path.addQuadCurve(to: CGPoint(x: rect.minX + topLeft, y: rect.maxY), control: CGPoint(x: rect.minX, y: rect.maxY))
  251. path.closeSubpath()
  252. return path
  253. }
  254. }