.../Source/Components/ValidIndicatorTextField.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // ValidIndicatorTextField.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 12/8/23. |
6 | | // Copyright © 2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Cocoa |
10 | | import RxSwift |
11 | | |
12 | | /// Setting isValid updates the |
13 | | /// field border color (green/red) |
14 | | /// |
15 | | /// Validation logic handled externally |
16 | | class ValidIndicatorTextField: TextFieldKeyUpRxStream { |
17 | | var isValid: Bool = false { |
18 | 98 | didSet { |
19 | 98 | updateBorderColor() |
20 | 98 | } |
21 | | } |
22 | | |
23 | 2 | override init(frame frameRect: NSRect) { |
24 | 2 | super.init(frame: frameRect) |
25 | 2 | } |
26 | | |
27 | 48 | required init?(coder: NSCoder) { |
28 | 48 | super.init(coder: coder) |
29 | 48 | self.cell?.focusRingType = .none |
30 | 48 | } |
31 | | |
32 | 0 | override func draw(_ dirtyRect: NSRect) { |
33 | 0 | super.draw(dirtyRect) |
34 | 0 |
|
35 | 0 | if let fieldLayer = self.layer { |
36 | 0 | if stringValue.isEmpty { |
37 | 0 | fieldLayer.borderColor = NSColor |
38 | 0 | .white |
39 | 0 | .withAlphaComponent(0.15).cgColor |
40 | 0 | } |
41 | 0 | fieldLayer.borderWidth = 1 |
42 | 0 | fieldLayer.cornerRadius = 0 |
43 | 0 | fieldLayer.backgroundColor = NSColor |
44 | 0 | .black |
45 | 0 | .withAlphaComponent(0.25).cgColor |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | 98 | private func updateBorderColor() { |
50 | 98 | let green = NSColor.green.withAlphaComponent(0.15).cgColor |
51 | 98 | let red = NSColor.red.withAlphaComponent(0.15).cgColor |
52 | 98 | |
53 | 98 | if let fieldLayer = self.layer { |
54 | 2 | fieldLayer.borderColor = isValid ? green : red |
55 | 2 | fieldLayer.borderWidth = 1 |
56 | 2 | fieldLayer.cornerRadius = 0 |
57 | 98 | } |
58 | 98 | } |
59 | | } |