// // SegmentCell.swift.swift // PrivadoVPN // // Created by Murad Shabanov on 03.02.2022. // Copyright © 2022 Privado LLC. All rights reserved. // import Foundation import AppKit final class SegmentCell: View { // swiftlint:disable nesting private enum Constants { enum Color { static let title = NSColor.white static let date = NSColor.white static let line = NSColor(red: 21, green: 25, blue: 53) } enum Font { enum Name { static let title = PrivadoConstants.Font.semiBold static let date = PrivadoConstants.Font.semiBold } enum Size { static let title: CGFloat = 13.5 static let date: CGFloat = 12 } } enum Geometry { static let titleTop: CGFloat = 15 static let titleBottom: CGFloat = 15 static let titleLeading: CGFloat = 20 static let lineHeight: CGFloat = 1 } } // swiftlint:enable nesting private let titleField = NSTextField(frame: .zero) private let line = View() private var isSelected: Bool? // MARK: - Interface func update(with title: String) { self.titleField.stringValue = title } func cellIsSelected(_ selected: Bool) { self.isSelected = selected self.titleField.textColor = selected ? .white : NSColor(red: 124, green: 134, blue: 186) } // MARK: - Lifecycle View override func addSubviews() { self.addSubview(self.titleField) self.addSubview(self.line) self.viewBackgroundColor = .clear self.isHoverEnabled = true // titleField self.titleField.translatesAutoresizingMaskIntoConstraints = false self.titleField.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal) self.titleField.isEditable = false self.titleField.isSelectable = false self.titleField.usesSingleLineMode = true self.titleField.isBezeled = false self.titleField.drawsBackground = false self.titleField.focusRingType = .none self.titleField.alignment = .left self.titleField.textColor = Constants.Color.title self.titleField.textColor = NSColor(red: 124, green: 134, blue: 186) self.titleField.font = NSFont(name: Constants.Font.Name.title, size: Constants.Font.Size.title) // line self.line.translatesAutoresizingMaskIntoConstraints = false self.line.viewBackgroundColor = Constants.Color.line } override func addConstraints() { NSLayoutConstraint.activate([ // self.titleField.topAnchor.constraint(equalTo: self.topAnchor, constant: Constants.Geometry.titleTop), self.titleField.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: Constants.Geometry.titleLeading), self.titleField.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5), self.titleField.bottomAnchor.constraint(equalTo: self.line.topAnchor, constant: -Constants.Geometry.titleBottom), // self.line.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: Constants.Geometry.titleLeading), self.line.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -Constants.Geometry.titleLeading), self.line.heightAnchor.constraint(equalToConstant: Constants.Geometry.lineHeight), self.line.bottomAnchor.constraint(equalTo: self.bottomAnchor) ]) } override func updateTrackingAreas() { super.updateTrackingAreas() for trackingArea in self.trackingAreas { self.removeTrackingArea(trackingArea) } let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways] let trackingArea = NSTrackingArea(rect: NSRect(x: 0, y: 0, width: 160, height: 55), options: options, owner: self, userInfo: nil) self.addTrackingArea(trackingArea) } override func onMouseHover(_ hover: Bool) { guard let isSelected = self.isSelected, !isSelected else { return } self.titleField.textColor = hover ? .white : NSColor(red: 124, green: 134, blue: 186) } }