Script policy UI adjustments

This commit is contained in:
James Magahern
2021-06-14 15:46:01 -07:00
parent 52b3f7fe0a
commit ecee3bd9bb
4 changed files with 64 additions and 187 deletions

View File

@@ -1,73 +0,0 @@
//
// 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 func sizeThatFits(_ size: CGSize) -> CGSize {
return segmentContainer.sizeThatFits(size)
}
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)
}
}
}

View File

@@ -12,75 +12,6 @@ protocol ScriptPolicyViewControllerDelegate: AnyObject {
func setScriptsEnabledForTab(_ enabled: Bool)
}
class ScriptPolicyControlListCell: UICollectionViewListCell
{
var enabled: Bool = true {
didSet {
if enabled != oldValue {
setNeedsLayout()
}
}
}
let policyControl = ScriptPolicyControl()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(policyControl)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
let policyControlWidth = policyControl.sizeThatFits(bounds.size).width
policyControl.frame = CGRect(
x: bounds.maxX - policyControlWidth - layoutMargins.right,
y: 0,
width: policyControlWidth,
height: bounds.height * 0.75
)
policyControl.frame = policyControl.frame.centeredY(inRect: bounds)
bringSubviewToFront(policyControl)
super.layoutSubviews()
if enabled {
contentView.alpha = 1.0
policyControl.alpha = 1.0
policyControl.isUserInteractionEnabled = true
} else {
contentView.alpha = 0.5
policyControl.alpha = 0.5
policyControl.isUserInteractionEnabled = false
}
}
}
class SwitchListCell: UICollectionViewListCell
{
let switchView = UISwitch(frame: .zero)
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(switchView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
let switchWidth: CGFloat = switchView.sizeThatFits(bounds.size).width
switchView.frame = CGRect(x: bounds.maxX - switchWidth - layoutMargins.right, y: 0,
width: switchWidth, height: bounds.height * 0.85)
switchView.frame = switchView.frame.centeredY(inRect: bounds)
contentView.frame = CGRect(origin: contentView.frame.origin, size: CGSize(width: switchView.frame.minX, height: contentView.frame.height))
}
}
class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
{
var collectionView: UICollectionView?
@@ -95,17 +26,13 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
case origins
}
override var traitCollection: UITraitCollection {
get { return super.traitCollection.alwaysPadLike() }
}
private static let enableScriptsForTabItem: String = "enableScriptsForTab"
convenience init(policyManager: ResourcePolicyManager, hostOrigin: String, loadedScripts: Set<String>, scriptsAllowedForTab: Bool) {
self.init(nibName: nil, bundle: nil)
allowScriptsForTab = scriptsAllowedForTab
let listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
let listConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
@@ -115,51 +42,44 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
let originItems = [ hostOrigin ] + allowedScripts + otherOriginScripts.subtracting(allowedScripts)
let switchCellRegistry = UICollectionView.CellRegistration<SwitchListCell, String> { [unowned self] (listCell, indexPath, item) in
let switchCellRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, String> { [unowned self] (listCell, indexPath, item) in
var config = listCell.defaultContentConfiguration()
if item == Self.enableScriptsForTabItem {
config.text = "Allow for Tab"
listCell.switchView.isOn = self.allowScriptsForTab
listCell.switchView.addAction(.init(handler: { _ in
let enabled = listCell.switchView.isOn
self.allowScriptsForTab = enabled
self.didChangeScriptPolicy = true
if var snapshot = self.dataSource?.snapshot() {
if enabled {
// Hide script origins
snapshot.deleteSections([ .origins ])
} else {
if !snapshot.sectionIdentifiers.contains(.origins) {
snapshot.appendSections([ .origins ])
}
snapshot.appendItems(originItems, toSection: .origins)
}
self.dataSource?.apply(snapshot, animatingDifferences: true)
}
}), for: .valueChanged)
if allowScriptsForTab {
config.text = "Shields Up"
config.image = UIImage(systemName: "shield.fill")
} else {
config.text = "Allow for Tab"
config.image = UIImage(systemName: "shield.slash")
}
config.textProperties.color = UIColor.systemBlue
}
listCell.contentConfiguration = config
}
let scriptPolicyRegistry = UICollectionView.CellRegistration<ScriptPolicyControlListCell, String> { [unowned self] (listCell, indexPath, item) in
let scriptPolicyRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, String> { [unowned self] (listCell, indexPath, item) in
var config = listCell.defaultContentConfiguration()
config.text = item
listCell.contentConfiguration = config
if policyManager.allowedOriginsForScriptResources().contains(item) {
listCell.policyControl.policyStatus = .allowed
} else {
listCell.policyControl.policyStatus = .blocked
}
let segmentedControl = UISegmentedControl(items: [
UIImage(systemName: "xmark.seal")!, // Disabled
UIImage(systemName: "checkmark.seal.fill")! // Enabled
])
listCell.policyControl.removeTarget(nil, action: nil, for: .valueChanged)
listCell.policyControl.addAction(UIAction(handler: { _ in
let allowed: Bool = listCell.policyControl.policyStatus == .allowed
segmentedControl.setTitleTextAttributes([ .foregroundColor: UIColor.init(dynamicProvider: { traits in
if traits.userInterfaceStyle == .dark {
return UIColor.systemTeal
} else {
return UIColor.systemBlue
}
}) ], for: .selected)
segmentedControl.addAction(UIAction(handler: { [unowned segmentedControl] _ in
let allowed: Bool = (segmentedControl.selectedSegmentIndex == 1)
if allowed {
policyManager.allowOriginToLoadScriptResources(item)
@@ -175,11 +95,27 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
}
self.didChangeScriptPolicy = true
}), for: .valueChanged)
if item != hostOrigin {
listCell.enabled = policyManager.allowedOriginsForScriptResources().contains(hostOrigin)
if policyManager.allowedOriginsForScriptResources().contains(item) {
segmentedControl.selectedSegmentIndex = 1
} else {
segmentedControl.selectedSegmentIndex = 0
}
let customViewConfiguration = UICellAccessory.CustomViewConfiguration(
customView: segmentedControl,
placement: .trailing(displayed: .always, at: { _ in return 0 }),
isHidden: false,
reservedLayoutWidth: .actual,
tintColor: nil,
maintainsFixedSize: true
)
listCell.accessories = [
.customView(configuration: customViewConfiguration)
]
}
let dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
@@ -226,10 +162,26 @@ class ScriptPolicyViewController: UIViewController, UICollectionViewDelegate
// MARK: UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
false
indexPath.section != Section.origins.rawValue
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
false
indexPath.section != Section.origins.rawValue
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let dataSource = dataSource else { return }
if indexPath.section == Section.tabOptions.rawValue {
let identifier = dataSource.itemIdentifier(for: indexPath)
if identifier == Self.enableScriptsForTabItem {
self.allowScriptsForTab = !self.allowScriptsForTab
// Immediately notify and dismiss
self.delegate?.didChangeScriptPolicy()
self.delegate?.setScriptsEnabledForTab(self.allowScriptsForTab)
self.dismiss(animated: true, completion: nil)
}
}
}
}