Files

111 lines
2.9 KiB
Swift

//
// RoundedRectangleView.swift
// PrivadoVPN
//
// Created by Murad Shabanov on 05.08.2021.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Cocoa
final class RoundedRectangleView: NSView {
private let configuration: Configuration
init(frame frameRect: NSRect, configuration: Configuration) {
self.configuration = configuration
super.init(frame: frameRect)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let backgroundRect = NSMakeRect(NSMinX(bounds), NSMinY(bounds), NSWidth(bounds), NSHeight(bounds))
_ = BezierPath(rect: backgroundRect, roundedCorners: [.bottomLeft, .bottomRight], cornerRadius: 10.0)
}
override var frame: NSRect {
didSet {
setNeedsDisplay(frame)
}
}
}
public typealias BezierPath = NSBezierPath
public enum Corners {
case topLeft
case bottomLeft
case topRight
case bottomRight
}
public enum PathSide {
case top
case bottom
}
public extension BezierPath {
convenience init(rect: CGRect, roundedCorners: [Corners], cornerRadius: CGFloat) {
self.init()
let corners = roundedCorners
let maxX: CGFloat = rect.size.width
let minX: CGFloat = 0
let maxY: CGFloat = rect.size.height
let minY: CGFloat = 0
let bottomRightCorner = CGPoint(x: maxX, y: minY)
move(to: bottomRightCorner)
if corners.contains(.bottomRight) {
line(to: CGPoint(x: maxX - cornerRadius, y: minY))
curve(to: CGPoint(x: maxX, y: minY + cornerRadius), controlPoint1: bottomRightCorner, controlPoint2: bottomRightCorner)
}
else {
line(to: bottomRightCorner)
}
let topRightCorner = CGPoint(x: maxX, y: maxY)
if corners.contains(.topRight) {
line(to: CGPoint(x: maxX, y: maxY - cornerRadius))
curve(to: CGPoint(x: maxX - cornerRadius, y: maxY), controlPoint1: topRightCorner, controlPoint2: topRightCorner)
}
else {
line(to: topRightCorner)
}
let topLeftCorner = CGPoint(x: minX, y: maxY)
move(to: topLeftCorner)
let bottomLeftCorner = CGPoint(x: minX, y: minY)
if corners.contains(.bottomLeft) {
line(to: CGPoint(x: minX, y: minY + cornerRadius))
curve(to: CGPoint(x: minX + cornerRadius, y: minY), controlPoint1: bottomLeftCorner, controlPoint2: bottomLeftCorner)
}
else {
line(to: bottomLeftCorner)
}
line(to: CGPoint(x: maxX - cornerRadius, y: minY))
self.lineWidth = 1.5
NSColor(red: 130, green: 136, blue: 216).setStroke()
self.stroke()
}
}