105 lines
3.1 KiB
Swift
105 lines
3.1 KiB
Swift
//
|
|
// SearchField.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Zhandos Bolatbekov on 31.05.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
protocol SearchFieldOutput: AnyObject {
|
|
func searchFieldQueryChange(text: String)
|
|
func searchFieldCancel()
|
|
}
|
|
|
|
final class SearchField: NSSearchField {
|
|
|
|
// swiftlint:disable nesting
|
|
private enum Constants {
|
|
|
|
enum Color {
|
|
static let queryText = NSColor(red: 124, green: 134, blue: 186)
|
|
static let background = NSColor(rgb: 0x272d4e)
|
|
static let cancelButtonTint = NSColor(rgb: 0x7a86be)
|
|
}
|
|
|
|
enum Font {
|
|
enum Name {
|
|
static let query = PrivadoConstants.Font.regular
|
|
}
|
|
enum Size {
|
|
static let query: CGFloat = 12.0
|
|
}
|
|
}
|
|
|
|
enum Image {
|
|
static let search = "search"
|
|
static let cancel = "search_cancel"
|
|
}
|
|
|
|
enum Geometry {
|
|
static let cancelButtonSize = CGSize(width: 12, height: 12)
|
|
static let cancelButtonRight: CGFloat = 7.5
|
|
static let searchIconSize = CGSize(width: 16, height: 16)
|
|
static let cornerRadius: CGFloat = 5
|
|
}
|
|
}
|
|
// swiftlint:enable nesting
|
|
|
|
private weak var output: SearchFieldOutput?
|
|
|
|
// MARK: - Init
|
|
|
|
init(output: SearchFieldOutput?) {
|
|
self.output = output
|
|
|
|
super.init(frame: .zero)
|
|
|
|
self.configureUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) { nil }
|
|
|
|
// MARK: - Private
|
|
|
|
private func configureUI() {
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
self.textColor = Constants.Color.queryText
|
|
self.sendsSearchStringImmediately = false
|
|
self.sendsWholeSearchString = false
|
|
self.usesSingleLineMode = false
|
|
self.action = #selector(self.onQueryChange(sender:))
|
|
self.font = NSFont(name: Constants.Font.Name.query, size: Constants.Font.Size.query)
|
|
self.focusRingType = .none
|
|
// self.isBordered = false // creates bug (after tap cancel)
|
|
self.wantsLayer = true
|
|
self.layer?.cornerRadius = Constants.Geometry.cornerRadius
|
|
self.layer?.backgroundColor = Constants.Color.background.cgColor
|
|
}
|
|
|
|
private func createCancelButton() -> NSButton {
|
|
let button = NSButton()
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
button.isBordered = false
|
|
button.wantsLayer = true
|
|
button.layer?.backgroundColor = .clear
|
|
button.image = NSImage(imageLiteralResourceName: Constants.Image.cancel)
|
|
.imageWithTintColor(tintColor: Constants.Color.cancelButtonTint)
|
|
.resize(to: Constants.Geometry.cancelButtonSize)
|
|
button.action = #selector(self.onCancelTap)
|
|
button.target = self
|
|
return button
|
|
}
|
|
|
|
@objc
|
|
private func onQueryChange(sender: NSSearchField) {
|
|
self.output?.searchFieldQueryChange(text: sender.stringValue)
|
|
}
|
|
|
|
@objc
|
|
private func onCancelTap() {
|
|
self.output?.searchFieldCancel()
|
|
}
|
|
}
|