Rename to rossler\\attix
This commit is contained in:
111
App/Tabs/Tab.swift
Normal file
111
App/Tabs/Tab.swift
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// Tab.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/29/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import Combine
|
||||
|
||||
protocol TabDelegate: class
|
||||
{
|
||||
func didBlockScriptOrigin(_ origin: String, forTab: Tab)
|
||||
}
|
||||
|
||||
class Tab: NSObject, SBRProcessBundleBridgeDelegate
|
||||
{
|
||||
public weak var delegate: TabDelegate?
|
||||
|
||||
public let homeURL: URL?
|
||||
public let bridge = SBRProcessBundleBridge()
|
||||
public var webView: WKWebView {
|
||||
if self.loadedWebView == nil {
|
||||
self.loadedWebView = bridge.webView
|
||||
|
||||
if let homeURL = homeURL {
|
||||
beginLoadingURL(homeURL)
|
||||
}
|
||||
}
|
||||
|
||||
return bridge.webView
|
||||
}
|
||||
public var policyManager: ResourcePolicyManager
|
||||
|
||||
private var loadedWebView: WKWebView? = nil
|
||||
public var title: String? { loadedWebView?.title }
|
||||
public var url: URL? { loadedWebView?.url ?? self.homeURL }
|
||||
|
||||
public var javaScriptEnabled: Bool = false {
|
||||
didSet { bridge.allowAllScripts = javaScriptEnabled }
|
||||
}
|
||||
|
||||
public var identifier = UUID()
|
||||
|
||||
public var favicon: UIImage?
|
||||
private var faviconHost: String?
|
||||
private var faviconRequest: AnyCancellable?
|
||||
|
||||
public var allowedScriptOrigins = Set<String>()
|
||||
public var blockedScriptOrigins = Set<String>()
|
||||
|
||||
private var titleObservation: NSKeyValueObservation?
|
||||
private var urlObservation: NSKeyValueObservation?
|
||||
|
||||
convenience init(policyManager: ResourcePolicyManager) {
|
||||
self.init(url: nil, policyManager: policyManager)
|
||||
}
|
||||
|
||||
convenience init(urlString: String, policyManager: ResourcePolicyManager) {
|
||||
self.init(url: URL(string: urlString), policyManager: policyManager)
|
||||
}
|
||||
|
||||
init(url: URL?, policyManager: ResourcePolicyManager) {
|
||||
self.homeURL = url
|
||||
self.policyManager = policyManager
|
||||
bridge.policyDataSource = policyManager
|
||||
|
||||
super.init()
|
||||
|
||||
bridge.delegate = self
|
||||
}
|
||||
|
||||
deinit {
|
||||
bridge.tearDown()
|
||||
}
|
||||
|
||||
func beginLoadingURL(_ url: URL) {
|
||||
let request = URLRequest(url: url)
|
||||
webView.load(request)
|
||||
}
|
||||
|
||||
// MARK: SBRProcessBundleBridgeDelegate
|
||||
|
||||
func webProcess(_ bridge: SBRProcessBundleBridge, didAllowScriptResourceFromOrigin origin: String) {
|
||||
print("Allowed script resource from origin: \(origin)")
|
||||
allowedScriptOrigins.formUnion([ origin ])
|
||||
}
|
||||
|
||||
func webProcess(_ bridge: SBRProcessBundleBridge, didBlockScriptResourceFromOrigin origin: String) {
|
||||
print("Blocked script resource from origin: \(origin)")
|
||||
blockedScriptOrigins.formUnion([ origin ])
|
||||
delegate?.didBlockScriptOrigin(origin, forTab: self)
|
||||
}
|
||||
|
||||
func updateFaviconForURL(_ url: URL) {
|
||||
if let faviconHost = faviconHost, url.host == faviconHost {} else {
|
||||
guard var faviconURLComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return }
|
||||
faviconURLComponents.path = "/favicon.ico"
|
||||
|
||||
let defaultImage = UIImage(systemName: "globe")
|
||||
guard let faviconURL = faviconURLComponents.url else { return }
|
||||
faviconRequest = URLSession.shared.dataTaskPublisher(for: faviconURL)
|
||||
.map { (data: Data, response: URLResponse) -> UIImage? in
|
||||
UIImage(data: data)
|
||||
}
|
||||
.replaceError(with: defaultImage)
|
||||
.replaceNil(with: defaultImage)
|
||||
.assign(to: \.favicon, on: self)
|
||||
}
|
||||
}
|
||||
}
|
||||
40
App/Tabs/TabController.swift
Normal file
40
App/Tabs/TabController.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// TabController.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/30/20.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class TabController
|
||||
{
|
||||
var tabs: [Tab] = []
|
||||
var policyManager = ResourcePolicyManager()
|
||||
|
||||
init() {
|
||||
// TODO: load tabs from disk.
|
||||
_ = createNewTab(url: nil)
|
||||
}
|
||||
|
||||
func tab(forURL url: URL) -> Tab? {
|
||||
tabs.first { $0.url == url }
|
||||
}
|
||||
|
||||
func tab(forIdentifier identifier: UUID) -> Tab? {
|
||||
tabs.first { $0.identifier == identifier }
|
||||
}
|
||||
|
||||
func createNewTab(url: URL?) -> Tab {
|
||||
let tab = Tab(url: url, policyManager: policyManager)
|
||||
tabs.append(tab)
|
||||
|
||||
return tab
|
||||
}
|
||||
|
||||
func closeTab(_ tab: Tab) {
|
||||
if let index = tabs.firstIndex(of: tab) {
|
||||
tabs.remove(at: index)
|
||||
}
|
||||
}
|
||||
}
|
||||
129
App/Tabs/TabPickerViewController.swift
Normal file
129
App/Tabs/TabPickerViewController.swift
Normal file
@@ -0,0 +1,129 @@
|
||||
//
|
||||
// TabPickerViewController.swift
|
||||
// SBrowser
|
||||
//
|
||||
// Created by James Magahern on 7/30/20.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
protocol TabPickerViewControllerDelegate: class
|
||||
{
|
||||
func tabPicker(_ picker: TabPickerViewController, didSelectTab tab: Tab)
|
||||
func tabPicker(_ picker: TabPickerViewController, willCloseTab tab: Tab)
|
||||
}
|
||||
|
||||
class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
{
|
||||
let tabController: TabController!
|
||||
var selectedTab: Tab?
|
||||
|
||||
weak var delegate: TabPickerViewControllerDelegate?
|
||||
|
||||
typealias TabID = UUID
|
||||
|
||||
private var collectionView: UICollectionView?
|
||||
private var dataSource: UICollectionViewDiffableDataSource<Int, TabID>?
|
||||
|
||||
init(tabController: TabController) {
|
||||
self.tabController = tabController
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
||||
self.title = "Tabs"
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func loadView() {
|
||||
var listConfig = UICollectionLayoutListConfiguration(appearance: .grouped)
|
||||
listConfig.trailingSwipeActionsConfigurationProvider = { [unowned self] indexPath in
|
||||
if self.dataSource?.snapshot().numberOfItems ?? 0 <= 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return UISwipeActionsConfiguration(actions: [ UIContextualAction(style: .destructive, title: "Close", handler: { [unowned self] (action, view, completionHandler) in
|
||||
if let item = self.dataSource?.itemIdentifier(for: indexPath), var snapshot = self.dataSource?.snapshot() {
|
||||
if let tab = self.tabController.tab(forIdentifier: item) {
|
||||
self.delegate?.tabPicker(self, willCloseTab: tab)
|
||||
|
||||
self.tabController.closeTab(tab)
|
||||
snapshot.deleteItems([ item ])
|
||||
self.dataSource?.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
}
|
||||
})])
|
||||
}
|
||||
|
||||
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
|
||||
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
|
||||
|
||||
let registry = UICollectionView.CellRegistration<UICollectionViewListCell, TabID> { [unowned self] (listCell, indexPath, item) in
|
||||
var config = listCell.defaultContentConfiguration()
|
||||
|
||||
if let tab = self.tabController.tab(forIdentifier: item) {
|
||||
if let title = tab.title, title.count > 0 {
|
||||
config.text = title
|
||||
config.secondaryText = tab.url?.absoluteString
|
||||
} else if let url = tab.url {
|
||||
config.text = url.absoluteString
|
||||
config.secondaryText = url.absoluteString
|
||||
} else {
|
||||
config.text = "New Tab"
|
||||
}
|
||||
|
||||
config.textProperties.numberOfLines = 1
|
||||
config.secondaryTextProperties.numberOfLines = 1
|
||||
|
||||
if let image = tab.favicon {
|
||||
config.image = image
|
||||
} else {
|
||||
config.image = UIImage(systemName: "safari")
|
||||
}
|
||||
|
||||
config.imageProperties.maximumSize = CGSize(width: 21.0, height: 21.0)
|
||||
config.imageProperties.cornerRadius = 3.0
|
||||
|
||||
if tab == self.selectedTab {
|
||||
listCell.accessories = [ .checkmark() ]
|
||||
} else {
|
||||
listCell.accessories = []
|
||||
}
|
||||
}
|
||||
|
||||
listCell.contentConfiguration = config
|
||||
}
|
||||
|
||||
let dataSource = UICollectionViewDiffableDataSource<Int, TabID>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
|
||||
return collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: item)
|
||||
}
|
||||
|
||||
collectionView.dataSource = dataSource
|
||||
collectionView.delegate = self
|
||||
|
||||
var snapshot = dataSource.snapshot()
|
||||
snapshot.appendSections([ 0 ])
|
||||
tabController.tabs.forEach { tab in
|
||||
snapshot.appendItems([ tab.identifier ])
|
||||
}
|
||||
|
||||
dataSource.apply(snapshot)
|
||||
|
||||
self.dataSource = dataSource
|
||||
self.collectionView = collectionView
|
||||
self.view = self.collectionView
|
||||
|
||||
let newTabButton = UIBarButtonItem(systemItem: .add, primaryAction: UIAction(handler: { [unowned self] _ in
|
||||
let newTab = self.tabController.createNewTab(url: nil)
|
||||
self.delegate?.tabPicker(self, didSelectTab: newTab)
|
||||
}), menu: nil)
|
||||
|
||||
navigationItem.rightBarButtonItem = newTabButton
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
let tab = tabController.tabs[indexPath.row]
|
||||
delegate?.tabPicker(self, didSelectTab: tab)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user