FeatureIcons.swift 15 KB

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