Files

101 lines
3.3 KiB
Swift

//
// MenuTableViewCell.swift
// PrivadoVPN
//
// Created by Murad Shabanov on 22.06.2021.
// Copyright © 2021 Privado LLC. All rights reserved.
//
import Foundation
final class MenuTableViewCell: View {
// swiftlint:disable nesting
private enum Constants {
enum Color {
static let highlitedBackground = NSColor(red: 3, green: 15, blue: 72)
static let text = NSColor(red: 122, green: 134, blue: 190)
}
enum Font {
enum Size {
static let text: CGFloat = 13
}
}
}
// swiftlint:enable nesting
override init() {
super.init(frame: .zero)
self.isHoverEnabled = true
}
private let textField = NSTextField()
private let backgroungView = RoundedView()
private var trackingArea: NSTrackingArea?
override func addSubviews() {
self.viewBackgroundColor = .clear
self.textField.translatesAutoresizingMaskIntoConstraints = false
self.textField.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
self.textField.isEditable = false
self.textField.isSelectable = false
self.textField.usesSingleLineMode = false
self.textField.isBezeled = false
self.textField.drawsBackground = false
self.textField.focusRingType = .none
self.textField.alignment = .left
self.textField.font = NSFont(name: PrivadoConstants.Font.medium, size: Constants.Font.Size.text)
self.textField.textColor = Constants.Color.text
self.backgroungView.addSubview(self.textField)
self.addSubview(self.backgroungView)
}
override func addConstraints() {
NSLayoutConstraint.activate([
self.backgroungView.topAnchor.constraint(equalTo: self.topAnchor),
self.backgroungView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.backgroungView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.backgroungView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
self.textField.centerYAnchor.constraint(equalTo: self.backgroungView.centerYAnchor),
self.textField.leadingAnchor.constraint(equalTo: self.backgroungView.leadingAnchor, constant: 20)
])
}
func updateText(text: String) {
self.textField.stringValue = text
}
override func hitTest(_ point: NSPoint) -> NSView? { nil }
override func mouseEntered(with theEvent: NSEvent) {
self.backgroungView.viewBackgroundColor = Constants.Color.highlitedBackground
self.textField.textColor = .white
}
override func mouseExited(with theEvent: NSEvent) {
self.backgroungView.viewBackgroundColor = .clear
self.textField.textColor = Constants.Color.text
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
if let trackingArea = self.trackingArea {
self.removeTrackingArea(trackingArea)
}
let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways]
let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
}
}