133 lines
4.5 KiB
Swift
133 lines
4.5 KiB
Swift
//
|
|
// BottomItemCell.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Murad Shabanov on 16.03.2022.
|
|
// Copyright © 2022 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class BottomItemCell: View {
|
|
|
|
// swiftlint:disable nesting
|
|
private enum Constants {
|
|
|
|
enum Color {
|
|
|
|
}
|
|
|
|
enum Geometry {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// swiftlint:enable nesting
|
|
private var line: View?
|
|
private var iconView: NSImageView?
|
|
|
|
// MARK: - Interface
|
|
|
|
func update(with imageName: String, lineIsVisible: Bool, type: SideMenuItemType) {
|
|
|
|
if lineIsVisible {
|
|
let line = View()
|
|
line.translatesAutoresizingMaskIntoConstraints = false
|
|
line.viewBackgroundColor = NSColor(red: 130, green: 136, blue: 216)
|
|
self.addSubview(line)
|
|
self.line = line
|
|
|
|
NSLayoutConstraint.activate([
|
|
line.heightAnchor.constraint(equalToConstant: 1),
|
|
line.widthAnchor.constraint(equalToConstant: 40),
|
|
line.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10),
|
|
line.topAnchor.constraint(equalTo: self.topAnchor)
|
|
])
|
|
}
|
|
|
|
if let image = NSImage(named: imageName) {
|
|
let tintedImage = image.imageWithTintColor(tintColor: NSColor(red: 147, green: 149, blue: 178))
|
|
let imageView = NSImageView(image: tintedImage)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
self.addSubview(imageView)
|
|
self.iconView = imageView
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageView.heightAnchor.constraint(equalToConstant: 20),
|
|
imageView.widthAnchor.constraint(equalToConstant: 20),
|
|
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20),
|
|
imageView.centerYAnchor.constraint(equalTo: self.centerYAnchor)
|
|
])
|
|
}
|
|
|
|
if case let .notification(unread: unread) = type {
|
|
guard let imageView = iconView else { return }
|
|
if unread {
|
|
let circle = BellCircleView(frame: .zero)
|
|
self.addSubview(circle)
|
|
NSLayoutConstraint.activate([
|
|
circle.heightAnchor.constraint(equalToConstant: 5),
|
|
circle.widthAnchor.constraint(equalToConstant: 5),
|
|
circle.bottomAnchor.constraint(equalTo: imageView.topAnchor, constant: 8),
|
|
circle.trailingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: -4.5)
|
|
])
|
|
}
|
|
}
|
|
}
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
super.init(frame: frameRect)
|
|
}
|
|
|
|
init(frame frameRect: NSRect, lineIsVisible: Bool) {
|
|
super.init(frame: frameRect)
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
// MARK: - Lifecycle View
|
|
|
|
override func addSubviews() {
|
|
self.viewBackgroundColor = .clear
|
|
}
|
|
|
|
override func addConstraints() {
|
|
if let superView = self.superview {
|
|
NSLayoutConstraint.activate([
|
|
self.leadingAnchor.constraint(equalTo: superView.leadingAnchor),
|
|
self.trailingAnchor.constraint(equalTo: superView.trailingAnchor),
|
|
self.topAnchor.constraint(equalTo: superView.topAnchor),
|
|
self.bottomAnchor.constraint(equalTo: superView.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: 60, height: 50), options: options, owner: self, userInfo: nil)
|
|
self.addTrackingArea(trackingArea)
|
|
}
|
|
|
|
override func onMouseHover(_ hover: Bool) {
|
|
if let icon = self.iconView, let image = icon.image {
|
|
image.isTemplate = true
|
|
icon.image = hover ? image.imageWithTintColor(tintColor: .white) : image.imageWithTintColor(tintColor: NSColor(red: 147, green: 149, blue: 178))
|
|
}
|
|
}
|
|
|
|
override func mouseDown(with event: NSEvent) {
|
|
super.mouseDown(with: event)
|
|
print("mouse down cell")
|
|
}
|
|
}
|