pommedoro/Sources/Pommedoro/EdgeGradientView.swift

26 lines
1.4 KiB
Swift

import AppKit
class EdgeGradientView: NSView {
var intensity: CGFloat = 0.4
var color: NSColor = Settings.shared.gradientColor
override func draw(_ dirtyRect: NSRect) {
guard let ctx = NSGraphicsContext.current?.cgContext else { return }
let w = bounds.width, h = bounds.height, edge: CGFloat = 220
let colors = [color.withAlphaComponent(intensity).cgColor, color.withAlphaComponent(0.0).cgColor] as CFArray
guard let grad = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0, 1]) else { return }
ctx.saveGState(); ctx.clip(to: CGRect(x: 0, y: h - edge, width: w, height: edge))
ctx.drawLinearGradient(grad, start: CGPoint(x: w/2, y: h), end: CGPoint(x: w/2, y: h - edge), options: []); ctx.restoreGState()
ctx.saveGState(); ctx.clip(to: CGRect(x: 0, y: 0, width: w, height: edge))
ctx.drawLinearGradient(grad, start: CGPoint(x: w/2, y: 0), end: CGPoint(x: w/2, y: edge), options: []); ctx.restoreGState()
ctx.saveGState(); ctx.clip(to: CGRect(x: 0, y: 0, width: edge, height: h))
ctx.drawLinearGradient(grad, start: CGPoint(x: 0, y: h/2), end: CGPoint(x: edge, y: h/2), options: []); ctx.restoreGState()
ctx.saveGState(); ctx.clip(to: CGRect(x: w - edge, y: 0, width: edge, height: h))
ctx.drawLinearGradient(grad, start: CGPoint(x: w, y: h/2), end: CGPoint(x: w - edge, y: h/2), options: []); ctx.restoreGState()
}
}