// // DropDownButton.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 25.05.2021. // Copyright © 2021 Privado LLC. All rights reserved. // final class DropDownButton: NSButton { // swiftlint:disable nesting enum Constants { enum Font { enum Name { static let title = PrivadoConstants.Font.semiBold } enum Size { static let title: CGFloat = 10 } } enum Color { static let text = NSColor(red: 122, green: 134, blue: 190) static let activeText = NSColor.white static let background = NSColor(red: 45, green: 51, blue: 82) } enum Geometry { static let height: CGFloat = 37 static let iconSize = CGSize(width: 8, height: 5) } enum Image { static let arrowUp = "countryArrowUp" static let arrowDown = "countryArrowDown" } } // swiftlint:enable nesting private let text: String private var isActive: Bool private var arrowUp: Bool // MARK: - Init init(title: String, isActive: Bool = false, arrowUp: Bool = true) { self.text = title self.isActive = isActive self.arrowUp = arrowUp super.init(frame: .zero) self.configureUI() } required init?(coder: NSCoder) { nil } // MARK: - Interface func set(isActive: Bool) { self.isActive = isActive self.updateUI() } func set(arrowUp: Bool) { self.arrowUp = arrowUp self.updateUI() } // MARK: - Private private func configureUI() { self.translatesAutoresizingMaskIntoConstraints = false self.isBordered = false self.wantsLayer = true self.layer?.backgroundColor = .clear self.updateUI() } private func updateUI() { let icon = NSImage(imageLiteralResourceName: self.arrowUp ? Constants.Image.arrowUp : Constants.Image.arrowDown) let font = NSFont(name: Constants.Font.Name.title, size: Constants.Font.Size.title) ?? .systemFont(ofSize: Constants.Font.Size.title) let attributedTitle = NSMutableAttributedString( string: self.text, attributes: [.font: font, .foregroundColor: self.isActive ? Constants.Color.activeText : Constants.Color.text] ) let textAttachment = NSTextAttachment() textAttachment.bounds = CGRect(x: 0, y: (font.capHeight - Constants.Geometry.iconSize.height).rounded() / 2, width: Constants.Geometry.iconSize.width, height: Constants.Geometry.iconSize.height) textAttachment.image = icon attributedTitle.append(NSAttributedString(string: " ")) attributedTitle.append(NSAttributedString(attachment: textAttachment)) self.attributedTitle = attributedTitle } }