Files

127 lines
3.7 KiB
Swift

//
// RoundedSecuredTextField.swift
// Cyberlock
//
// Created by Jura on 8/31/19.
// Copyright © 2019 Omicronmedia. All rights reserved.
//
import Foundation
import AppKit
final class RoundedSecuredTextField: NSSecureTextField, NSTextFieldDelegate {
typealias EnterHandlerClosure = () -> Bool
private struct Constants {
static let cornerRadius: CGFloat = 13
static let placeholderColor = NSColor(calibratedRed: 173 / 255, green: 179 / 255, blue: 210 / 255, alpha: 0.5)
static let textColor = NSColor(calibratedRed: 173 / 255, green: 179 / 255, blue: 210 / 255, alpha: 1.0)
}
// MARK: - Properties
var enterHandler: EnterHandlerClosure?
var coloredPlaceHolder: String? {
get {
return self.placeholderAttributedString?.string
}
set {
if let value = newValue
, let font = self.font {
let attrs = [NSAttributedString.Key.foregroundColor: Constants.placeholderColor,
NSAttributedString.Key.font: NSFont.systemFont(ofSize: font.pointSize)]
self.placeholderAttributedString = NSAttributedString(string: value, attributes: attrs)
} else {
self.placeholderAttributedString = nil
}
}
}
// MARK: - Initializers
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.isBezeled = false
self.drawsBackground = false
self.translatesAutoresizingMaskIntoConstraints = false
self.focusRingType = .none
self.textColor = Constants.textColor
self.delegate = self
if let font = self.font {
self.font = NSFont(name: font.fontName, size: 12.0)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Methods
func hightlight(with color: NSColor?) {
guard let borderColor = color else {
self.layer?.borderWidth = 0.0
return
}
self.layer?.borderColor = borderColor.cgColor
self.layer?.borderWidth = 1.0
self.layer?.cornerRadius = Constants.cornerRadius
}
// MARK: - Lifecycle
override class var cellClass: AnyClass? {
get {
return RoundedSecuredTextFieldCell.self
}
// swiftlint:disable unused_setter_value
set { }
// swiftlint:enable unused_setter_value
}
override func viewDidMoveToWindow() {
if let textView = self.window?.fieldEditor(true, for: self) as? NSTextView {
textView.insertionPointColor = Constants.textColor
}
}
override func draw(_ dirtyRect: NSRect) {
let size = self.bounds.size
let blackOutlineFrame = NSRect(x: 0, y: 0, width: size.width, height: size.height - 1.0)
let color = NSColor(calibratedRed: 45 / 255, green: 51 / 255, blue: 82 / 255, alpha: 1.0)
let gradient = NSGradient(colors: [color])
let path = NSBezierPath(roundedRect: blackOutlineFrame, xRadius: 13, yRadius: 13)
gradient?.draw(in: path, angle: 90)
super.draw(dirtyRect)
}
// MARK: - NSTextFieldDelegate
func controlTextDidChange(_ obj: Notification) {
self.hightlight(with: nil)
}
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if commandSelector == #selector(NSResponder.insertNewline(_:)) {
guard let handler = self.enterHandler else { return true }
return handler()
}
return false
}
}