Rename to rossler\\attix

This commit is contained in:
James Magahern
2020-07-31 17:35:03 -07:00
parent 030c1db45c
commit c723b3de88
30 changed files with 25 additions and 207 deletions

View File

@@ -0,0 +1,70 @@
//
// ScriptControllerIconView.swift
// SBrowser
//
// Created by James Magahern on 7/24/20.
//
import UIKit
class ScriptControllerIconView: UIButton
{
public var shieldsDown: Bool = false {
didSet { setNeedsLayout() }
}
public var someScriptsAllowed: Bool = false {
didSet { setNeedsLayout() }
}
private let labelView = UILabel(frame: .zero)
private let shieldsDownImage = UIImage(systemName: "shield.slash")
private let shieldsUpImage = UIImage(systemName: "shield")
private let shieldsPartiallyUpImage = UIImage(systemName: "shield.lefthalf.fill")
convenience init() {
self.init(frame: .zero)
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
setBlockedScriptsNumber(0)
}
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)
labelView.bounds = labelView.bounds.insetBy(dx: -3.0, dy: -2.0)
if shieldsDown {
setImage(shieldsDownImage, for: .normal)
} else {
if someScriptsAllowed {
setImage(shieldsPartiallyUpImage, for: .normal)
} else {
setImage(shieldsUpImage, for: .normal)
}
}
}
}

View File

@@ -0,0 +1,72 @@
//
// 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)
}
}
}

View File

@@ -0,0 +1,220 @@
//
// ScriptPolicyViewController.swift
// SBrowser
//
// Created by James Magahern on 7/24/20.
//
import UIKit
protocol ScriptPolicyViewControllerDelegate: class {
func didChangeScriptPolicy()
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 = CGFloat(80.0)
policyControl.frame = CGRect(x: bounds.maxX - policyControlWidth - layoutMargins.right, y: 0, width: policyControlWidth, height: bounds.height)
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)
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?
var allowScriptsForTab = false
weak var delegate: ScriptPolicyViewControllerDelegate? = nil
private var dataSource: UICollectionViewDiffableDataSource<Section, String>?
private var didChangeScriptPolicy = false
private enum Section: Int {
case tabOptions
case origins
}
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 listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
// Make sure host origin goes first in the list.
let otherOriginScripts = loadedScripts.subtracting([ hostOrigin ])
let originItems = [ hostOrigin ] + otherOriginScripts
let switchCellRegistry = UICollectionView.CellRegistration<SwitchListCell, 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)
}
listCell.contentConfiguration = config
}
let scriptPolicyRegistry = UICollectionView.CellRegistration<ScriptPolicyControlListCell, 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
}
listCell.policyControl.addAction(UIAction(handler: { _ in
let allowed: Bool = listCell.policyControl.policyStatus == .allowed
if allowed {
policyManager.allowOriginToLoadScriptResources(item)
} else {
policyManager.disallowOriginToLoadScriptResources(item)
}
if item == hostOrigin {
if var snapshot = self.dataSource?.snapshot() {
snapshot.reloadItems(Array(otherOriginScripts))
self.dataSource?.apply(snapshot, animatingDifferences: true)
}
}
self.didChangeScriptPolicy = true
}), for: .valueChanged)
if item != hostOrigin {
listCell.enabled = policyManager.allowedOriginsForScriptResources().contains(hostOrigin)
}
}
let dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
if item == Self.enableScriptsForTabItem {
return collectionView.dequeueConfiguredReusableCell(using: switchCellRegistry, for: indexPath, item: item)
}
return collectionView.dequeueConfiguredReusableCell(using: scriptPolicyRegistry, for: indexPath, item: item)
}
collectionView.dataSource = dataSource
collectionView.delegate = self
var snapshot = dataSource.snapshot()
snapshot.appendSections([ .tabOptions ])
snapshot.appendItems([ Self.enableScriptsForTabItem ], toSection: .tabOptions)
if !allowScriptsForTab {
snapshot.appendSections([ .origins ])
snapshot.appendItems(originItems, toSection: .origins)
}
dataSource.apply(snapshot)
self.dataSource = dataSource
self.collectionView = collectionView
title = "Script Origin Policy"
navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction(handler: { [unowned self] action in
if self.didChangeScriptPolicy {
self.delegate?.didChangeScriptPolicy()
self.delegate?.setScriptsEnabledForTab(self.allowScriptsForTab)
}
self.dismiss(animated: true, completion: nil)
}), menu: nil)
}
override func loadView() {
self.view = collectionView
}
// MARK: UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
false
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
false
}
}