2020-07-24 19:26:35 -07:00
|
|
|
//
|
|
|
|
|
// 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 {
|
2020-07-29 17:46:53 -07:00
|
|
|
didSet { setNeedsLayout() }
|
2020-07-24 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 17:40:01 -07:00
|
|
|
private let allowButton = ReliefButton()
|
|
|
|
|
private let denyButton = ReliefButton()
|
|
|
|
|
private let segmentContainer = SegmentedReliefButton(children: [])
|
2020-07-24 19:26:35 -07:00
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
|
self.init(frame: .zero)
|
|
|
|
|
|
2020-08-14 17:40:01 -07:00
|
|
|
segmentContainer.children = [ allowButton, denyButton ]
|
|
|
|
|
addSubview(segmentContainer)
|
|
|
|
|
|
2020-08-14 18:42:40 -07:00
|
|
|
allowButton.showsTouchWhenHighlighted = false
|
2020-07-31 14:08:10 -07:00
|
|
|
allowButton.addAction(UIAction(handler: { [unowned self] _ in
|
|
|
|
|
self.policyStatus = .allowed
|
|
|
|
|
self.sendActions(for: .valueChanged)
|
2020-07-24 19:26:35 -07:00
|
|
|
}), for: .touchUpInside)
|
|
|
|
|
allowButton.imageView?.contentMode = .scaleAspectFit
|
|
|
|
|
|
2020-08-14 18:42:40 -07:00
|
|
|
denyButton.showsTouchWhenHighlighted = false
|
2020-07-31 14:08:10 -07:00
|
|
|
denyButton.addAction(UIAction(handler: { [unowned self] _ in
|
|
|
|
|
self.policyStatus = .blocked
|
|
|
|
|
self.sendActions(for: .valueChanged)
|
2020-07-24 19:26:35 -07:00
|
|
|
}), for: .touchUpInside)
|
|
|
|
|
denyButton.imageView?.contentMode = .scaleAspectFit
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 18:47:11 -07:00
|
|
|
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
|
|
|
|
return segmentContainer.sizeThatFits(size)
|
2020-07-24 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
2020-08-14 17:40:01 -07:00
|
|
|
segmentContainer.frame = bounds
|
2020-07-24 19:26:35 -07:00
|
|
|
|
|
|
|
|
if policyStatus == .allowed {
|
|
|
|
|
allowButton.tintColor = .blue
|
2020-08-14 18:42:40 -07:00
|
|
|
allowButton.remainsPressed = true
|
2020-07-24 19:26:35 -07:00
|
|
|
allowButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal)
|
|
|
|
|
|
2020-08-14 18:42:40 -07:00
|
|
|
denyButton.tintColor = nil
|
|
|
|
|
denyButton.remainsPressed = false
|
2020-07-24 19:26:35 -07:00
|
|
|
denyButton.setImage(UIImage(systemName: "stop.circle"), for: .normal)
|
|
|
|
|
} else {
|
2020-08-14 18:42:40 -07:00
|
|
|
allowButton.tintColor = nil
|
|
|
|
|
allowButton.remainsPressed = false
|
2020-07-24 19:26:35 -07:00
|
|
|
allowButton.setImage(UIImage(systemName: "play.circle"), for: .normal)
|
|
|
|
|
|
|
|
|
|
denyButton.tintColor = .red
|
2020-08-14 18:42:40 -07:00
|
|
|
denyButton.remainsPressed = true
|
2020-07-24 19:26:35 -07:00
|
|
|
denyButton.setImage(UIImage(systemName: "stop.circle.fill"), for: .normal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|