|
| 1 | +// |
| 2 | +// BaseWidget.swift |
| 3 | +// swift-cross-ui |
| 4 | +// |
| 5 | +// Created by William Baker on 1/10/25. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +public class BaseWidget: UIView { |
| 11 | + private var leftConstraint: NSLayoutConstraint? |
| 12 | + private var topConstraint: NSLayoutConstraint? |
| 13 | + private var widthConstraint: NSLayoutConstraint? |
| 14 | + private var heightConstraint: NSLayoutConstraint? |
| 15 | + |
| 16 | + internal var x = 0 { |
| 17 | + didSet { |
| 18 | + if x != oldValue { |
| 19 | + updateLeftConstraint() |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + internal var y = 0 { |
| 25 | + didSet { |
| 26 | + if y != oldValue { |
| 27 | + updateTopConstraint() |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + internal var width = 0 { |
| 33 | + didSet { |
| 34 | + if width != oldValue { |
| 35 | + updateWidthConstraint() |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + internal var height = 0 { |
| 41 | + didSet { |
| 42 | + if height != oldValue { |
| 43 | + updateHeightConstraint() |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + internal init() { |
| 49 | + super.init(frame: .zero) |
| 50 | + |
| 51 | + self.translatesAutoresizingMaskIntoConstraints = false |
| 52 | + } |
| 53 | + |
| 54 | + @available(*, unavailable) |
| 55 | + public required init?(coder: NSCoder) { |
| 56 | + fatalError("init(coder:) is not used for this view") |
| 57 | + } |
| 58 | + |
| 59 | + private func updateLeftConstraint() { |
| 60 | + leftConstraint?.isActive = false |
| 61 | + guard let superview else { return } |
| 62 | + leftConstraint = self.leftAnchor.constraint( |
| 63 | + equalTo: superview.safeAreaLayoutGuide.leftAnchor, constant: CGFloat(x)) |
| 64 | + leftConstraint!.isActive = true |
| 65 | + } |
| 66 | + |
| 67 | + private func updateTopConstraint() { |
| 68 | + topConstraint?.isActive = false |
| 69 | + guard let superview else { return } |
| 70 | + topConstraint = self.topAnchor.constraint( |
| 71 | + equalTo: superview.safeAreaLayoutGuide.topAnchor, constant: CGFloat(y)) |
| 72 | + topConstraint!.isActive = true |
| 73 | + } |
| 74 | + |
| 75 | + private func updateWidthConstraint() { |
| 76 | + widthConstraint?.isActive = false |
| 77 | + widthConstraint = self.widthAnchor.constraint(equalToConstant: CGFloat(width)) |
| 78 | + widthConstraint!.isActive = true |
| 79 | + } |
| 80 | + |
| 81 | + private func updateHeightConstraint() { |
| 82 | + heightConstraint?.isActive = false |
| 83 | + heightConstraint = self.heightAnchor.constraint(equalToConstant: CGFloat(height)) |
| 84 | + heightConstraint!.isActive = true |
| 85 | + } |
| 86 | + |
| 87 | + public override func didMoveToSuperview() { |
| 88 | + super.didMoveToSuperview() |
| 89 | + |
| 90 | + updateLeftConstraint() |
| 91 | + updateTopConstraint() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +extension UIKitBackend { |
| 96 | + public typealias Widget = BaseWidget |
| 97 | +} |
| 98 | + |
| 99 | +internal class WrapperWidget<View: UIView>: BaseWidget { |
| 100 | + init(child: View) { |
| 101 | + super.init() |
| 102 | + |
| 103 | + self.addSubview(child) |
| 104 | + child.translatesAutoresizingMaskIntoConstraints = false |
| 105 | + NSLayoutConstraint.activate([ |
| 106 | + child.topAnchor.constraint(equalTo: self.topAnchor), |
| 107 | + child.leadingAnchor.constraint(equalTo: self.leadingAnchor), |
| 108 | + child.bottomAnchor.constraint(equalTo: self.bottomAnchor), |
| 109 | + child.trailingAnchor.constraint(equalTo: self.trailingAnchor), |
| 110 | + ]) |
| 111 | + } |
| 112 | + |
| 113 | + override convenience init() { |
| 114 | + self.init(child: View(frame: .zero)) |
| 115 | + } |
| 116 | + |
| 117 | + var child: View { |
| 118 | + subviews[0] as! View |
| 119 | + } |
| 120 | + |
| 121 | + override var intrinsicContentSize: CGSize { |
| 122 | + child.intrinsicContentSize |
| 123 | + } |
| 124 | +} |
0 commit comments