141 lines
5.0 KiB
Swift
141 lines
5.0 KiB
Swift
//
|
|
// HyperlinkTextField.swift
|
|
// Cyberlock
|
|
//
|
|
// Created by Juraldinio on 8/31/19.
|
|
// Copyright © 2019 Omicronmedia. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import AppKit
|
|
|
|
protocol HyperlinkTextFieldDelegate: AnyObject {
|
|
func hyperlinkClicked(target: HyperlinkTextField)
|
|
}
|
|
|
|
final class HyperlinkTextField: NSTextField {
|
|
|
|
public var hoveredColor: NSColor?
|
|
public var link: String?
|
|
public var normalColor: NSColor?
|
|
public var underline: Bool?
|
|
public var isHighlitedWithUnderline: Bool = false
|
|
|
|
public var linkIsEnabled: Bool = true {
|
|
didSet {
|
|
self.isEnabled = self.linkIsEnabled
|
|
self.alphaValue = self.linkIsEnabled ? 1 : 0.5
|
|
}
|
|
}
|
|
|
|
private struct Constants {
|
|
static let linkColor = NSColor(calibratedRed: 173 / 255, green: 179 / 255, blue: 210 / 255, alpha: 0.8)
|
|
}
|
|
|
|
private var trackingArea: NSTrackingArea?
|
|
|
|
weak var hyperlinkDelegate: HyperlinkTextFieldDelegate?
|
|
|
|
|
|
// MARK: - Initializers
|
|
|
|
init(frame frameRect: NSRect, font: NSFont?, alignment: NSTextAlignment = .left) {
|
|
super.init(frame: frameRect)
|
|
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
self.isBezeled = false
|
|
self.drawsBackground = false
|
|
self.isEditable = false
|
|
self.isSelectable = false
|
|
self.focusRingType = .none
|
|
self.usesSingleLineMode = false
|
|
self.alignment = alignment
|
|
if let font = font {
|
|
self.font = font
|
|
}
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func layout() {
|
|
super.layout()
|
|
|
|
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)
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
func show(hyper link: String, underline: Bool = false, with url: URL?, using color: NSColor? = nil) {
|
|
self.underline = underline
|
|
self.link = link
|
|
self.normalColor = color
|
|
let textAttributes: [NSAttributedString.Key: Any] = [
|
|
NSAttributedString.Key.foregroundColor: color ?? Constants.linkColor,
|
|
NSAttributedString.Key.underlineStyle: underline ? NSUnderlineStyle.single.rawValue : NSUnderlineStyle(rawValue: 0).rawValue
|
|
]
|
|
|
|
self.attributedStringValue = NSMutableAttributedString(string: link, attributes: textAttributes)
|
|
}
|
|
|
|
override func mouseDown(with event: NSEvent) {
|
|
guard self.isEnabled else { return }
|
|
self.hyperlinkDelegate?.hyperlinkClicked(target: self)
|
|
}
|
|
|
|
/// Always display a pointing-hand cursor
|
|
override func resetCursorRects() {
|
|
super.resetCursorRects()
|
|
self.addCursorRect(self.bounds, cursor: NSCursor.pointingHand)
|
|
}
|
|
|
|
override func mouseEntered(with event: NSEvent) {
|
|
super.mouseEntered(with: event)
|
|
|
|
if self.isHighlitedWithUnderline {
|
|
let textAttributes: [NSAttributedString.Key: Any] = [
|
|
NSAttributedString.Key.foregroundColor: self.hoveredColor ?? Constants.linkColor,
|
|
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
|
|
]
|
|
self.attributedStringValue = NSMutableAttributedString(string: link ?? "", attributes: textAttributes)
|
|
return
|
|
}
|
|
|
|
let textAttributes: [NSAttributedString.Key: Any] = [
|
|
NSAttributedString.Key.foregroundColor: self.hoveredColor ?? Constants.linkColor,
|
|
NSAttributedString.Key.underlineStyle: self.underline ?? false ? NSUnderlineStyle.single.rawValue : NSUnderlineStyle(rawValue: 0).rawValue
|
|
]
|
|
|
|
self.attributedStringValue = NSMutableAttributedString(string: link ?? "", attributes: textAttributes)
|
|
}
|
|
|
|
override func mouseExited(with event: NSEvent) {
|
|
super.mouseEntered(with: event)
|
|
|
|
if self.isHighlitedWithUnderline {
|
|
let textAttributes: [NSAttributedString.Key: Any] = [
|
|
NSAttributedString.Key.foregroundColor: self.normalColor ?? Constants.linkColor,
|
|
NSAttributedString.Key.underlineStyle: NSUnderlineStyle(rawValue: 0).rawValue
|
|
]
|
|
|
|
self.attributedStringValue = NSMutableAttributedString(string: link ?? "", attributes: textAttributes)
|
|
return
|
|
}
|
|
|
|
let textAttributes: [NSAttributedString.Key: Any] = [
|
|
NSAttributedString.Key.foregroundColor: self.normalColor ?? Constants.linkColor,
|
|
NSAttributedString.Key.underlineStyle: self.underline ?? false ? NSUnderlineStyle.single.rawValue : NSUnderlineStyle(rawValue: 0).rawValue
|
|
]
|
|
|
|
self.attributedStringValue = NSMutableAttributedString(string: link ?? "", attributes: textAttributes)
|
|
}
|
|
|
|
}
|