74 lines
2.3 KiB
Swift
74 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 let allowButton = ReliefButton()
|
|
private let denyButton = ReliefButton()
|
|
private let segmentContainer = SegmentedReliefButton(children: [])
|
|
|
|
convenience init() {
|
|
self.init(frame: .zero)
|
|
|
|
segmentContainer.children = [ allowButton, denyButton ]
|
|
addSubview(segmentContainer)
|
|
|
|
allowButton.showsTouchWhenHighlighted = false
|
|
allowButton.addAction(UIAction(handler: { [unowned self] _ in
|
|
self.policyStatus = .allowed
|
|
self.sendActions(for: .valueChanged)
|
|
}), for: .touchUpInside)
|
|
allowButton.imageView?.contentMode = .scaleAspectFit
|
|
|
|
denyButton.showsTouchWhenHighlighted = false
|
|
denyButton.addAction(UIAction(handler: { [unowned self] _ in
|
|
self.policyStatus = .blocked
|
|
self.sendActions(for: .valueChanged)
|
|
}), for: .touchUpInside)
|
|
denyButton.imageView?.contentMode = .scaleAspectFit
|
|
}
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
CGSize(width: 100.0, height: UIView.noIntrinsicMetric)
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
segmentContainer.frame = bounds
|
|
|
|
if policyStatus == .allowed {
|
|
allowButton.tintColor = .blue
|
|
allowButton.remainsPressed = true
|
|
allowButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal)
|
|
|
|
denyButton.tintColor = nil
|
|
denyButton.remainsPressed = false
|
|
denyButton.setImage(UIImage(systemName: "stop.circle"), for: .normal)
|
|
} else {
|
|
allowButton.tintColor = nil
|
|
allowButton.remainsPressed = false
|
|
allowButton.setImage(UIImage(systemName: "play.circle"), for: .normal)
|
|
|
|
denyButton.tintColor = .red
|
|
denyButton.remainsPressed = true
|
|
denyButton.setImage(UIImage(systemName: "stop.circle.fill"), for: .normal)
|
|
}
|
|
}
|
|
}
|