.../Source/Components/Popup/PopupContainerView.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // PopupContainerView.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 31/3/18. |
6 | | // Copyright © 2018-2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Cocoa |
10 | | |
11 | | class PopupContainerView: CutBoxBaseView { |
12 | | var contentInset: CGFloat = 1 |
13 | | |
14 | | var contentView: CutBoxBaseView? { |
15 | 179 | didSet { |
16 | 179 | removeConstraints(constraints) |
17 | 179 | guard let contentView = contentView |
18 | 179 | else { return } |
19 | 179 | |
20 | 179 | contentView.translatesAutoresizingMaskIntoConstraints = false |
21 | 179 | addSubview(contentView) |
22 | 358 | ["H:|-0-[contentView]-0-|", "V:|-0-[contentView]-0-|"].forEach { |
23 | 358 | addConstraints(NSLayoutConstraint.constraints(withVisualFormat: $0, |
24 | 358 | options: .directionLeadingToTrailing, |
25 | 358 | metrics: nil, |
26 | 358 | views: ["contentView": contentView])) |
27 | 358 | } |
28 | 179 | } |
29 | | } |
30 | | |
31 | 179 | var superviewConstraints = [NSLayoutConstraint]() |
32 | | |
33 | 179 | func setup() { |
34 | 179 | self.translatesAutoresizingMaskIntoConstraints = false |
35 | 179 | resetConstraints() |
36 | 179 | } |
37 | | |
38 | 184 | func resetConstraints() { |
39 | 184 | guard let superview = superview as? PopupBackgroundView |
40 | 184 | else { return } |
41 | 184 | |
42 | 184 | superview.removeConstraints(superviewConstraints) |
43 | 184 | |
44 | 184 | let horizontalFormat = "H:|-\(contentInset)-[containerView]-\(contentInset)-|" |
45 | 184 | let verticalFormat = "V:|-\(contentInset)-[containerView]-\(contentInset)-|" |
46 | 184 | |
47 | 184 | let horizontalConstraints = NSLayoutConstraint |
48 | 184 | .constraints(withVisualFormat: horizontalFormat, |
49 | 184 | options: .directionLeadingToTrailing, |
50 | 184 | metrics: nil, |
51 | 184 | views: ["containerView": self]) |
52 | 184 | |
53 | 184 | let verticalConstraints = NSLayoutConstraint |
54 | 184 | .constraints(withVisualFormat: verticalFormat, |
55 | 184 | options: .directionLeadingToTrailing, |
56 | 184 | metrics: nil, |
57 | 184 | views: ["containerView": self]) |
58 | 184 | |
59 | 184 | self.superviewConstraints = horizontalConstraints + verticalConstraints |
60 | 184 | superview.addConstraints(superviewConstraints) |
61 | 184 | } |
62 | | } |