mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.9 KiB
Swift
161 lines
4.9 KiB
Swift
3 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import UIKit
|
||
|
|
||
3 years ago
|
public final class SessionButton: UIButton {
|
||
3 years ago
|
public enum Style {
|
||
3 years ago
|
case bordered
|
||
3 years ago
|
case borderless
|
||
|
case destructive
|
||
3 years ago
|
case destructiveBorderless
|
||
3 years ago
|
case filled
|
||
|
}
|
||
|
|
||
|
public enum Size {
|
||
|
case small
|
||
|
case medium
|
||
|
case large
|
||
|
}
|
||
|
|
||
3 years ago
|
private let style: Style
|
||
|
|
||
3 years ago
|
public override var isEnabled: Bool {
|
||
|
didSet {
|
||
3 years ago
|
guard isEnabled else {
|
||
|
setThemeTitleColor(
|
||
|
{
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .borderless, .destructive,
|
||
3 years ago
|
.destructiveBorderless:
|
||
|
return .disabled
|
||
|
|
||
|
case .filled: return .white
|
||
|
}
|
||
|
}(),
|
||
|
for: .normal
|
||
|
)
|
||
|
setThemeBackgroundColor(
|
||
|
{
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .borderless, .destructive,
|
||
3 years ago
|
.destructiveBorderless:
|
||
|
return .clear
|
||
|
|
||
|
case .filled: return .disabled
|
||
|
}
|
||
|
}(),
|
||
|
for: .normal
|
||
|
)
|
||
|
setThemeBackgroundColor(nil, for: .highlighted)
|
||
|
|
||
|
themeBorderColor = {
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .destructive: return .disabled
|
||
3 years ago
|
case .filled, .borderless, .destructiveBorderless: return nil
|
||
|
}
|
||
|
}()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// If we enable the button they just re-apply the existing style
|
||
|
setup(style: style)
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: - Initialization
|
||
|
|
||
3 years ago
|
public init(style: Style, size: Size) {
|
||
3 years ago
|
self.style = style
|
||
|
|
||
3 years ago
|
super.init(frame: .zero)
|
||
|
|
||
3 years ago
|
setup(size: size)
|
||
|
setup(style: style)
|
||
3 years ago
|
}
|
||
|
|
||
|
override init(frame: CGRect) {
|
||
|
preconditionFailure("Use init(style:) instead.")
|
||
|
}
|
||
|
|
||
|
required init?(coder: NSCoder) {
|
||
|
preconditionFailure("Use init(style:) instead.")
|
||
|
}
|
||
3 years ago
|
|
||
|
private func setup(size: Size) {
|
||
3 years ago
|
clipsToBounds = true
|
||
3 years ago
|
contentEdgeInsets = UIEdgeInsets(
|
||
|
top: 0,
|
||
|
left: Values.smallSpacing,
|
||
|
bottom: 0,
|
||
|
right: Values.smallSpacing
|
||
|
)
|
||
3 years ago
|
titleLabel?.font = .boldSystemFont(ofSize: (size == .small ?
|
||
|
Values.smallFontSize :
|
||
|
Values.mediumFontSize
|
||
|
))
|
||
3 years ago
|
|
||
|
let height: CGFloat = {
|
||
|
switch size {
|
||
|
case .small: return Values.smallButtonHeight
|
||
|
case .medium: return Values.mediumButtonHeight
|
||
|
case .large: return Values.largeButtonHeight
|
||
|
}
|
||
|
}()
|
||
|
set(.height, to: height)
|
||
|
layer.cornerRadius = {
|
||
|
switch style {
|
||
|
case .borderless, .destructiveBorderless: return 5
|
||
|
default: return (height / 2)
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
private func setup(style: Style) {
|
||
3 years ago
|
setThemeTitleColor(
|
||
|
{
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .borderless: return .sessionButton_text
|
||
|
case .destructive, .destructiveBorderless: return .sessionButton_destructiveText
|
||
|
case .filled: return .sessionButton_filledText
|
||
3 years ago
|
}
|
||
|
}(),
|
||
|
for: .normal
|
||
|
)
|
||
|
|
||
|
setThemeBackgroundColor(
|
||
|
{
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .borderless: return .sessionButton_background
|
||
|
case .destructive, .destructiveBorderless: return .sessionButton_destructiveBackground
|
||
|
case .filled: return .sessionButton_filledBackground
|
||
3 years ago
|
}
|
||
|
}(),
|
||
|
for: .normal
|
||
|
)
|
||
|
setThemeBackgroundColor(
|
||
|
{
|
||
|
switch style {
|
||
3 years ago
|
case .bordered, .borderless: return .sessionButton_highlight
|
||
|
case .destructive, .destructiveBorderless: return .sessionButton_destructiveHighlight
|
||
|
case .filled: return .sessionButton_filledHighlight
|
||
3 years ago
|
}
|
||
|
}(),
|
||
|
for: .highlighted
|
||
|
)
|
||
|
|
||
3 years ago
|
layer.borderWidth = {
|
||
|
switch style {
|
||
|
case .borderless, .destructiveBorderless: return 0
|
||
|
default: return 1
|
||
|
}
|
||
|
}()
|
||
3 years ago
|
themeBorderColor = {
|
||
|
switch style {
|
||
3 years ago
|
case .bordered: return .sessionButton_border
|
||
|
case .destructive: return .sessionButton_destructiveBorder
|
||
3 years ago
|
case .filled, .borderless, .destructiveBorderless: return nil
|
||
3 years ago
|
}
|
||
|
}()
|
||
|
}
|
||
|
}
|