Files
Attractor/SBrowser/Script Policy UI/ScriptPolicyControl.swift
2020-07-31 14:08:10 -07:00

73 lines
2.3 KiB
Swift

//
// ScriptPolicyControl.swift
// SBrowser
//
// Created by James Magahern on 7/24/20.
//
import UIKit
class ScriptPolicyControl: UIControl
{
enum PolicyStatus {
case allowed
case blocked
}
var policyStatus: PolicyStatus = .blocked {
didSet { setNeedsLayout() }
}
private class PolicyButton: UIButton {
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
contentRect.insetBy(dx: 8.0, dy: 8.0)
}
}
private let allowButton = PolicyButton(frame: .zero)
private let denyButton = PolicyButton(frame: .zero)
convenience init() {
self.init(frame: .zero)
allowButton.addAction(UIAction(handler: { [unowned self] _ in
self.policyStatus = .allowed
self.sendActions(for: .valueChanged)
}), for: .touchUpInside)
allowButton.imageView?.contentMode = .scaleAspectFit
addSubview(allowButton)
denyButton.addAction(UIAction(handler: { [unowned self] _ in
self.policyStatus = .blocked
self.sendActions(for: .valueChanged)
}), for: .touchUpInside)
denyButton.imageView?.contentMode = .scaleAspectFit
addSubview(denyButton)
}
override var intrinsicContentSize: CGSize {
CGSize(width: 100.0, height: UIView.noIntrinsicMetric)
}
override func layoutSubviews() {
super.layoutSubviews()
allowButton.frame = CGRect(origin: .zero, size: CGSize(width: bounds.width / 2, height: bounds.height))
denyButton.frame = CGRect(origin: CGPoint(x: allowButton.frame.maxX, y: 0), size: allowButton.frame.size)
if policyStatus == .allowed {
allowButton.tintColor = .blue
allowButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal)
denyButton.tintColor = .darkGray
denyButton.setImage(UIImage(systemName: "stop.circle"), for: .normal)
} else {
allowButton.tintColor = .darkGray
allowButton.setImage(UIImage(systemName: "play.circle"), for: .normal)
denyButton.tintColor = .red
denyButton.setImage(UIImage(systemName: "stop.circle.fill"), for: .normal)
}
}
}