178 lines
6.2 KiB
Swift
178 lines
6.2 KiB
Swift
//
|
|
// ButtonIcon.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 5/12/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
@objc(VDSButtonIcon)
|
|
open class ButtonIcon: Control {
|
|
//--------------------------------------------------
|
|
// MARK: - Models
|
|
//--------------------------------------------------
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
public enum Kind: String, CaseIterable {
|
|
case ghost, lowContrast, highContrast
|
|
}
|
|
|
|
public enum SurfaceType: String, CaseIterable {
|
|
case colorFill, media
|
|
}
|
|
|
|
public enum Size: String, EnumSubset {
|
|
case large
|
|
case small
|
|
|
|
public var defaultValue: Icon.Size { .large }
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Properties
|
|
//--------------------------------------------------
|
|
private var iconCenterXConstraint: NSLayoutConstraint?
|
|
private var iconCenterYConstraint: NSLayoutConstraint?
|
|
private var containerViewWidthConstraint: NSLayoutConstraint?
|
|
private var containerViewHeightConstraint: NSLayoutConstraint?
|
|
private var containerView = UIView().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
$0.backgroundColor = .clear
|
|
}
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
open var icon = Icon()
|
|
|
|
open var kind: Kind = .ghost { didSet { setNeedsUpdate() } }
|
|
open var surfaceType: SurfaceType = .colorFill { didSet { setNeedsUpdate() } }
|
|
open var iconName: Icon.Name? { didSet { setNeedsUpdate() } }
|
|
open var size: Size = .large { didSet { setNeedsUpdate() } }
|
|
open var customSize: Int? { didSet { setNeedsUpdate() }}
|
|
open var floating: Bool = false { didSet { setNeedsUpdate() } }
|
|
open var hideBorder: Bool = true { didSet { setNeedsUpdate() } }
|
|
open var iconOffset: CGPoint? { didSet { setNeedsUpdate() } }
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Configuration
|
|
//--------------------------------------------------
|
|
private var padding: CGFloat = 10.0
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Lifecycle
|
|
//--------------------------------------------------
|
|
|
|
open override func setup() {
|
|
super.setup()
|
|
addSubview(containerView)
|
|
containerView.addSubview(icon)
|
|
|
|
containerView.pinToSuperView(.init(top: padding, left: padding, bottom: padding, right: padding))
|
|
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: size.value.dimensions.width + padding)
|
|
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: size.value.dimensions.height + padding)
|
|
|
|
iconCenterXConstraint = icon.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0)
|
|
iconCenterYConstraint = icon.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0)
|
|
|
|
NSLayoutConstraint.activate([containerViewWidthConstraint!, containerViewHeightConstraint!, iconCenterXConstraint!, iconCenterYConstraint!])
|
|
}
|
|
|
|
open override func reset() {
|
|
super.reset()
|
|
shouldUpdateView = false
|
|
kind = .ghost
|
|
surfaceType = .colorFill
|
|
size = .large
|
|
floating = false
|
|
hideBorder = true
|
|
iconOffset = nil
|
|
iconName = nil
|
|
shouldUpdateView = true
|
|
setNeedsUpdate()
|
|
}
|
|
|
|
open override func updateView() {
|
|
super.updateView()
|
|
|
|
if let iconName {
|
|
icon.name = iconName
|
|
icon.size = size.value
|
|
icon.surface = surface
|
|
icon.disabled = disabled
|
|
icon.customSize = customSize
|
|
} else {
|
|
icon.reset()
|
|
}
|
|
|
|
setNeedsLayout()
|
|
}
|
|
|
|
open override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
let bgColor = UIColor.red //backgroundColorConfiguration.getColor(self)
|
|
let borderColor = UIColor.green// borderColorConfiguration.getColor(self)
|
|
let borderWidth = 2.0
|
|
let cornerRadius = min(frame.width, frame.height) / 2.0
|
|
|
|
if let iconOffset {
|
|
// calculate center point for child view with offset
|
|
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
|
|
iconCenterXConstraint?.constant = childCenter.x - containerView.center.x
|
|
iconCenterYConstraint?.constant = childCenter.y - containerView.center.y
|
|
} else {
|
|
iconCenterXConstraint?.constant = 0
|
|
iconCenterYConstraint?.constant = 0
|
|
}
|
|
|
|
if let customSize {
|
|
containerViewWidthConstraint?.constant = CGFloat(customSize) + padding
|
|
containerViewHeightConstraint?.constant = CGFloat(customSize) + padding
|
|
} else {
|
|
containerViewWidthConstraint?.constant = size.value.dimensions.width + padding
|
|
containerViewHeightConstraint?.constant = size.value.dimensions.height + padding
|
|
}
|
|
|
|
print("containerViewWidthConstraint :\(containerViewWidthConstraint!.constant)")
|
|
print("containerViewHeightConstraint :\(containerViewHeightConstraint!.constant)")
|
|
|
|
//container
|
|
backgroundColor = bgColor
|
|
layer.borderColor = borderColor.cgColor
|
|
layer.cornerRadius = cornerRadius
|
|
layer.borderWidth = borderWidth
|
|
|
|
//icon
|
|
icon.layer.borderColor = UIColor.purple.cgColor
|
|
icon.layer.borderWidth = 2
|
|
|
|
}
|
|
}
|
|
|
|
// MARK: AppleGuidlinesTouchable
|
|
extension ButtonIcon: AppleGuidlinesTouchable {
|
|
|
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
Self.acceptablyOutsideBounds(point: point, bounds: bounds)
|
|
}
|
|
|
|
}
|