updated configurations
This commit is contained in:
parent
facafa308b
commit
94453b5c2e
@ -113,11 +113,7 @@ open class ButtonIcon: Control {
|
||||
}()
|
||||
|
||||
private var highContrastIconColorConfig: AnyColorable = {
|
||||
return ControlColorConfiguration().with {
|
||||
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .normal)
|
||||
$0.setSurfaceColors(VDSColor.interactiveActiveOndark, VDSColor.interactiveActiveOnlight, forState: .highlighted)
|
||||
$0.setSurfaceColors(VDSColor.interactiveDisabledOndark, VDSColor.interactiveDisabledOnlight, forState: .disabled)
|
||||
}.eraseToAnyColorable()
|
||||
return SurfaceColorConfiguration(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight).eraseToAnyColorable()
|
||||
}()
|
||||
|
||||
private var selectedIconColorConfig: AnyColorable = {
|
||||
@ -128,143 +124,6 @@ open class ButtonIcon: Control {
|
||||
}.eraseToAnyColorable()
|
||||
}()
|
||||
|
||||
//--------------------------------------------------
|
||||
// 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()
|
||||
|
||||
//create a layoutGuide for the icon to key off of
|
||||
let iconLayoutGuide = UILayoutGuide()
|
||||
addLayoutGuide(iconLayoutGuide)
|
||||
|
||||
//add the icon
|
||||
addSubview(icon)
|
||||
|
||||
//determines the height/width of the icon
|
||||
layoutGuideWidthConstraint = iconLayoutGuide.widthAnchor.constraint(equalToConstant: size.containerSize)
|
||||
layoutGuideHeightConstraint = iconLayoutGuide.heightAnchor.constraint(equalToConstant: size.containerSize)
|
||||
|
||||
//determines the center point of the icon
|
||||
centerXConstraint = icon.centerXAnchor.constraint(equalTo: iconLayoutGuide.centerXAnchor, constant: 0)
|
||||
centerYConstraint = icon.centerYAnchor.constraint(equalTo: iconLayoutGuide.centerYAnchor, constant: 0)
|
||||
|
||||
//activate the constraints
|
||||
NSLayoutConstraint.activate([layoutGuideWidthConstraint!,
|
||||
layoutGuideHeightConstraint!,
|
||||
centerXConstraint!,
|
||||
centerYConstraint!,
|
||||
iconLayoutGuide.topAnchor.constraint(equalTo: topAnchor),
|
||||
iconLayoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||
iconLayoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||
iconLayoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
|
||||
}
|
||||
|
||||
open override func reset() {
|
||||
super.reset()
|
||||
shouldUpdateView = false
|
||||
kind = .ghost
|
||||
surfaceType = .colorFill
|
||||
size = .large
|
||||
floating = false
|
||||
hideBorder = true
|
||||
iconOffset = .init(x: 0, y: 0)
|
||||
iconName = nil
|
||||
shouldUpdateView = true
|
||||
setNeedsUpdate()
|
||||
}
|
||||
|
||||
open override func updateView() {
|
||||
super.updateView()
|
||||
|
||||
//ensure there is an icon to set
|
||||
if let currentIconName {
|
||||
icon.name = currentIconName
|
||||
icon.color = iconColorConfig.getColor(self)
|
||||
icon.size = size.value
|
||||
icon.surface = surface
|
||||
icon.disabled = disabled
|
||||
icon.customSize = customSize
|
||||
} else {
|
||||
icon.reset()
|
||||
}
|
||||
|
||||
icon.backgroundColor = .green
|
||||
|
||||
setNeedsLayout()
|
||||
}
|
||||
|
||||
open override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
let currentConfig = currentConfiguration
|
||||
|
||||
backgroundColor = currentConfig.backgroundColorConfig.getColor(self)
|
||||
|
||||
// calculate center point for child view with offset
|
||||
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
|
||||
centerXConstraint?.constant = childCenter.x - center.x
|
||||
centerYConstraint?.constant = childCenter.y - center.y
|
||||
|
||||
//updating current container size
|
||||
var iconLayoutSize = size.containerSize
|
||||
if let customSize {
|
||||
iconLayoutSize = CGFloat(customSize)
|
||||
}
|
||||
// check to see if this is fitToIcon
|
||||
if fitToIcon && kind == .ghost {
|
||||
iconLayoutSize = size.value.dimensions.width
|
||||
layer.cornerRadius = 0
|
||||
} else {
|
||||
layer.cornerRadius = min(frame.width, frame.height) / 2.0
|
||||
}
|
||||
|
||||
layoutGuideWidthConstraint?.constant = iconLayoutSize
|
||||
layoutGuideHeightConstraint?.constant = iconLayoutSize
|
||||
|
||||
//border
|
||||
if let borderable = currentConfig as? Borderable {
|
||||
layer.borderColor = borderable.borderColorConfig.getColor(self).cgColor
|
||||
layer.borderWidth = borderable.borderWidth
|
||||
icon.layer.borderWidth = borderable.borderWidth
|
||||
} else {
|
||||
layer.borderColor = nil
|
||||
layer.borderWidth = 0
|
||||
icon.layer.borderWidth = 0
|
||||
}
|
||||
|
||||
if let dropshadowable = currentConfig as? Dropshadowable {
|
||||
layer.masksToBounds = false
|
||||
layer.shadowColor = dropshadowable.shadowColorConfig.getColor(self).cgColor
|
||||
layer.shadowOpacity = Float(dropshadowable.shadowOpacity)
|
||||
layer.shadowOffset = dropshadowable.shadowOffset
|
||||
layer.shadowRadius = dropshadowable.shadowRadius
|
||||
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
|
||||
layer.shouldRasterize = true
|
||||
layer.rasterizationScale = UIScreen.main.scale
|
||||
} else {
|
||||
layer.shadowOpacity = 0
|
||||
layer.shadowRadius = 0
|
||||
layer.shadowPath = nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct GhostConfiguration: Configuration {
|
||||
var kind: Kind = .ghost
|
||||
var surfaceType: SurfaceType = .colorFill
|
||||
@ -279,7 +138,7 @@ open class ButtonIcon: Control {
|
||||
var surfaceType: SurfaceType = .colorFill
|
||||
var floating: Bool = false
|
||||
var backgroundColorConfig: AnyColorable = {
|
||||
SurfaceColorConfiguration(VDSColor.paletteGray44.withAlphaComponent(0.94), .clear).eraseToAnyColorable()
|
||||
SurfaceColorConfiguration(VDSColor.paletteGray44.withAlphaComponent(0.06), VDSColor.paletteGray44.withAlphaComponent(0.26)).eraseToAnyColorable()
|
||||
}()
|
||||
}
|
||||
|
||||
@ -346,6 +205,140 @@ open class ButtonIcon: Control {
|
||||
}.eraseToAnyColorable()
|
||||
}()
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// 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()
|
||||
|
||||
//create a layoutGuide for the icon to key off of
|
||||
let iconLayoutGuide = UILayoutGuide()
|
||||
addLayoutGuide(iconLayoutGuide)
|
||||
|
||||
//add the icon
|
||||
addSubview(icon)
|
||||
|
||||
//determines the height/width of the icon
|
||||
layoutGuideWidthConstraint = iconLayoutGuide.widthAnchor.constraint(equalToConstant: size.containerSize)
|
||||
layoutGuideHeightConstraint = iconLayoutGuide.heightAnchor.constraint(equalToConstant: size.containerSize)
|
||||
|
||||
//determines the center point of the icon
|
||||
centerXConstraint = icon.centerXAnchor.constraint(equalTo: iconLayoutGuide.centerXAnchor, constant: 0)
|
||||
centerYConstraint = icon.centerYAnchor.constraint(equalTo: iconLayoutGuide.centerYAnchor, constant: 0)
|
||||
|
||||
//activate the constraints
|
||||
NSLayoutConstraint.activate([layoutGuideWidthConstraint!,
|
||||
layoutGuideHeightConstraint!,
|
||||
centerXConstraint!,
|
||||
centerYConstraint!,
|
||||
iconLayoutGuide.topAnchor.constraint(equalTo: topAnchor),
|
||||
iconLayoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||
iconLayoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||
iconLayoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
|
||||
}
|
||||
|
||||
open override func reset() {
|
||||
super.reset()
|
||||
shouldUpdateView = false
|
||||
kind = .ghost
|
||||
surfaceType = .colorFill
|
||||
size = .large
|
||||
floating = false
|
||||
hideBorder = true
|
||||
iconOffset = .init(x: 0, y: 0)
|
||||
iconName = nil
|
||||
shouldUpdateView = true
|
||||
setNeedsUpdate()
|
||||
}
|
||||
|
||||
open override func updateView() {
|
||||
super.updateView()
|
||||
|
||||
//ensure there is an icon to set
|
||||
if let currentIconName {
|
||||
icon.name = currentIconName
|
||||
let color = iconColorConfig.getColor(self)
|
||||
icon.color = color
|
||||
icon.size = size.value
|
||||
icon.customSize = customSize
|
||||
} else {
|
||||
icon.reset()
|
||||
}
|
||||
|
||||
setNeedsLayout()
|
||||
}
|
||||
|
||||
open override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
let currentConfig = currentConfiguration
|
||||
|
||||
backgroundColor = currentConfig.backgroundColorConfig.getColor(self)
|
||||
|
||||
// calculate center point for child view with offset
|
||||
let childCenter = CGPoint(x: center.x + iconOffset.x, y: center.y + iconOffset.y)
|
||||
centerXConstraint?.constant = childCenter.x - center.x
|
||||
centerYConstraint?.constant = childCenter.y - center.y
|
||||
|
||||
//updating current container size
|
||||
var iconLayoutSize = size.containerSize
|
||||
if let customSize {
|
||||
iconLayoutSize = CGFloat(customSize)
|
||||
}
|
||||
// check to see if this is fitToIcon
|
||||
if fitToIcon && kind == .ghost {
|
||||
iconLayoutSize = size.value.dimensions.width
|
||||
layer.cornerRadius = 0
|
||||
} else {
|
||||
layer.cornerRadius = min(frame.width, frame.height) / 2.0
|
||||
}
|
||||
|
||||
layoutGuideWidthConstraint?.constant = iconLayoutSize
|
||||
layoutGuideHeightConstraint?.constant = iconLayoutSize
|
||||
|
||||
//border
|
||||
if let borderable = currentConfig as? Borderable {
|
||||
layer.borderColor = borderable.borderColorConfig.getColor(self).cgColor
|
||||
layer.borderWidth = borderable.borderWidth
|
||||
icon.layer.borderWidth = borderable.borderWidth
|
||||
} else {
|
||||
layer.borderColor = nil
|
||||
layer.borderWidth = 0
|
||||
icon.layer.borderWidth = 0
|
||||
}
|
||||
|
||||
if let dropshadowable = currentConfig as? Dropshadowable {
|
||||
layer.masksToBounds = false
|
||||
layer.shadowColor = dropshadowable.shadowColorConfig.getColor(self).cgColor
|
||||
layer.shadowOpacity = Float(dropshadowable.shadowOpacity)
|
||||
layer.shadowOffset = dropshadowable.shadowOffset
|
||||
layer.shadowRadius = dropshadowable.shadowRadius
|
||||
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
|
||||
layer.shouldRasterize = true
|
||||
layer.rasterizationScale = UIScreen.main.scale
|
||||
} else {
|
||||
layer.shadowOpacity = 0
|
||||
layer.shadowRadius = 0
|
||||
layer.shadowPath = nil
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: AppleGuidlinesTouchable
|
||||
@ -364,7 +357,7 @@ extension ButtonIcon: AppleGuidlinesTouchable {
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private protocol Borderable {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user