2020-07-24 19:26:35 -07:00
|
|
|
//
|
|
|
|
|
// ScriptControllerIconView.swift
|
|
|
|
|
// SBrowser
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 7/24/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2020-08-14 15:55:08 -07:00
|
|
|
class ScriptControllerIconView: ReliefButton
|
2020-07-24 19:26:35 -07:00
|
|
|
{
|
2020-07-31 15:03:00 -07:00
|
|
|
public var shieldsDown: Bool = false {
|
|
|
|
|
didSet { setNeedsLayout() }
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 15:08:04 -07:00
|
|
|
@Invalidating(.layout)
|
|
|
|
|
public var currentPolicy: ScriptPolicy = ScriptPolicy(securityOrigin: "", policyType: .alpha)
|
2020-07-31 15:03:00 -07:00
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
private let labelView = UILabel(frame: .zero)
|
2021-10-21 15:08:04 -07:00
|
|
|
private let policyImageView = UIImageView(frame: .zero)
|
2020-07-24 19:26:35 -07:00
|
|
|
|
2020-07-31 15:03:00 -07:00
|
|
|
private let shieldsDownImage = UIImage(systemName: "shield.slash")
|
|
|
|
|
private let shieldsUpImage = UIImage(systemName: "shield")
|
|
|
|
|
private let shieldsPartiallyUpImage = UIImage(systemName: "shield.lefthalf.fill")
|
|
|
|
|
|
2020-08-14 15:55:08 -07:00
|
|
|
override init() {
|
|
|
|
|
super.init()
|
2020-07-24 19:26:35 -07:00
|
|
|
|
|
|
|
|
addSubview(labelView)
|
|
|
|
|
|
|
|
|
|
imageView?.contentMode = .scaleAspectFit
|
|
|
|
|
labelView.backgroundColor = .systemRed
|
|
|
|
|
labelView.textAlignment = .center
|
|
|
|
|
labelView.layer.cornerRadius = 4.0
|
|
|
|
|
labelView.layer.masksToBounds = true
|
|
|
|
|
labelView.font = .boldSystemFont(ofSize: 8)
|
|
|
|
|
labelView.textColor = .white
|
|
|
|
|
|
2021-10-21 15:08:04 -07:00
|
|
|
policyImageView.contentMode = .scaleAspectFit
|
|
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
setBlockedScriptsNumber(0)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 15:55:08 -07:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 19:26:35 -07:00
|
|
|
public func setBlockedScriptsNumber(_ num: Int) {
|
|
|
|
|
if num > 0 {
|
|
|
|
|
labelView.isHidden = false
|
|
|
|
|
labelView.text = "\(num)"
|
|
|
|
|
} else {
|
|
|
|
|
labelView.isHidden = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setNeedsLayout()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
|
|
labelView.sizeToFit()
|
|
|
|
|
labelView.center = CGPoint(x: bounds.center.x + 10, y: bounds.center.y + 10)
|
2020-07-31 14:39:18 -07:00
|
|
|
labelView.bounds = labelView.bounds.insetBy(dx: -3.0, dy: -2.0)
|
2020-07-31 15:03:00 -07:00
|
|
|
|
2021-10-21 15:08:04 -07:00
|
|
|
let policyImageWidth = round(bounds.width / 3)
|
|
|
|
|
let policyImageSize = CGSize(
|
|
|
|
|
// Make multiple of 2 to ensure proper centering
|
|
|
|
|
width: policyImageWidth - policyImageWidth.truncatingRemainder(dividingBy: 2),
|
|
|
|
|
height: policyImageWidth - policyImageWidth.truncatingRemainder(dividingBy: 2)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if let imageView = imageView {
|
|
|
|
|
imageView.addSubview(policyImageView)
|
|
|
|
|
policyImageView.frame = CGRect(origin: .zero, size: policyImageSize)
|
|
|
|
|
.centered(inRect: imageView.bounds)
|
|
|
|
|
.offsetBy(dx: 0.0, dy: -1.0)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 15:03:00 -07:00
|
|
|
if shieldsDown {
|
|
|
|
|
setImage(shieldsDownImage, for: .normal)
|
|
|
|
|
} else {
|
2021-10-21 15:08:04 -07:00
|
|
|
setImage(shieldsUpImage, for: .normal)
|
|
|
|
|
|
|
|
|
|
if isEnabled {
|
|
|
|
|
policyImageView.isHidden = false
|
|
|
|
|
policyImageView.image = ScriptPolicy.iconRepresentation(forPolicyType: currentPolicy.policyType, configuration: ScriptPolicy.IconConfiguration(
|
|
|
|
|
size: policyImageSize,
|
|
|
|
|
foregroundColor: .label,
|
|
|
|
|
backgroundColor: .clear
|
|
|
|
|
))
|
2020-07-31 15:03:00 -07:00
|
|
|
} else {
|
2021-10-21 15:08:04 -07:00
|
|
|
policyImageView.isHidden = true
|
2020-07-31 15:03:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
}
|