Files
Attractor/App/Titlebar and URL Bar/TitlebarView.swift

156 lines
5.6 KiB
Swift
Raw Normal View History

2020-07-29 14:16:25 -07:00
//
// TitlebarView.swift
// SBrowser
//
// Created by James Magahern on 7/29/20.
//
import UIKit
2021-06-14 16:32:51 -07:00
import QuartzCore_Private
2020-07-29 14:16:25 -07:00
2021-06-14 18:09:33 -07:00
class SecurityIndicatorView: UIView
{
let imageView = UIImageView(image: UIImage(systemName: "lock.fill"))
let label = UILabel(frame: .zero)
var labelVisible: Bool = true { didSet { setNeedsLayout() } }
private let imageViewPadding = CGFloat(3.0)
convenience init() {
self.init(frame: .zero)
addSubview(imageView)
addSubview(label)
label.text = "Secure Connection"
imageView.contentMode = .scaleAspectFit
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
var size = CGSize(width: imageView.intrinsicContentSize.width, height: size.height)
if labelVisible {
size.width += label.sizeThatFits(size).width + imageViewPadding
}
return size
}
override func layoutSubviews() {
super.layoutSubviews()
imageView.frame = CGRect(x: 0, y: 0, width: bounds.height, height: bounds.height)
if labelVisible {
label.isHidden = false
label.frame = CGRect(x: imageView.frame.maxX + imageViewPadding, y: 1.0, width: bounds.width - (imageView.frame.maxX + imageViewPadding), height: bounds.height)
} else {
label.isHidden = true
}
}
}
2020-07-29 14:16:25 -07:00
class TitlebarView: UIView
{
2021-06-14 18:09:33 -07:00
public var showsSecurityIndicator: Bool = false { didSet { setNeedsLayout() } }
2020-07-31 17:44:38 -07:00
private let titleLabelView = UILabel(frame: .zero)
2020-08-14 15:55:08 -07:00
private let backgroundView = GradientView(direction: .horizontal, colors: [
UIColor(red: 0.101, green: 0.176, blue: 0.415, alpha: 1.0),
UIColor(red: 0.153, green: 0.000, blue: 0.153, alpha: 1.0)
])
2020-07-29 14:16:25 -07:00
2021-06-14 16:32:51 -07:00
private let separatorView = UIView(frame: .zero)
2021-06-14 18:09:33 -07:00
private let securityIndicatorView = SecurityIndicatorView()
2024-05-10 17:01:14 -07:00
2020-07-29 14:16:25 -07:00
convenience init() {
self.init(frame: .zero)
2020-08-14 15:55:08 -07:00
addSubview(backgroundView)
2020-07-29 14:16:25 -07:00
addSubview(titleLabelView)
2021-06-14 18:09:33 -07:00
addSubview(securityIndicatorView)
2021-06-14 16:32:51 -07:00
addSubview(separatorView)
separatorView.backgroundColor = UIColor(white: 1.0, alpha: 0.20)
separatorView.layer.compositingFilter = kCAFilterPlusL
2020-07-29 14:16:25 -07:00
titleLabelView.textColor = .white
titleLabelView.layer.shadowColor = UIColor.black.cgColor
titleLabelView.layer.shadowRadius = 0.0
titleLabelView.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
titleLabelView.font = UIFont.boldSystemFont(ofSize: 12.0)
2021-06-14 18:09:33 -07:00
let securityIndicatorLabelColor = UIColor(white: 1.0, alpha: 0.7)
securityIndicatorView.imageView.tintColor = securityIndicatorLabelColor
securityIndicatorView.label.textColor = securityIndicatorLabelColor
securityIndicatorView.label.font = UIFont.systemFont(ofSize: 10.0)
2020-08-14 15:55:08 -07:00
backgroundView.alpha = 0.98
2024-05-10 17:01:14 -07:00
// Make draggable
if #available(iOS 17.0, *) {
addInteraction(UIWindowSceneDragInteraction())
}
2020-07-29 14:16:25 -07:00
}
2020-07-31 17:44:38 -07:00
func setTitle(_ title: String) {
let titleAttributes: [NSAttributedString.Key : Any] = [
.font : UIFont.boldSystemFont(ofSize: 12.0),
.foregroundColor : UIColor.white
]
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
let appNameAttributes: [NSAttributedString.Key : Any] = [
.font : UIFont.systemFont(ofSize: 12.0),
.foregroundColor : UIColor(white: 1.0, alpha: 0.75)
]
if title.count > 0 {
let attributedString = NSMutableAttributedString(string: title, attributes: titleAttributes)
let appAttributedString = NSAttributedString(string: " :: \(appName)", attributes: appNameAttributes)
attributedString.append(appAttributedString)
titleLabelView.attributedText = attributedString
} else {
titleLabelView.attributedText = NSAttributedString(string: appName, attributes: titleAttributes)
}
}
func setColorTheme(_ colorTheme: [UIColor]) {
backgroundView.colors = colorTheme
}
2020-07-29 14:16:25 -07:00
override func layoutSubviews() {
super.layoutSubviews()
2021-06-14 18:09:33 -07:00
let edgePadding: CGFloat = 8.0
2021-07-20 19:17:04 -07:00
securityIndicatorView.labelVisible = false
2021-06-14 18:09:33 -07:00
var securityIndicatorSize = showsSecurityIndicator ? securityIndicatorView.sizeThatFits(bounds.size) : .zero
securityIndicatorSize.height = 10.0
2020-08-14 15:55:08 -07:00
backgroundView.frame = bounds
2021-06-14 18:09:33 -07:00
titleLabelView.frame = bounds
.avoiding(verticalInsets: safeAreaInsets)
.insetBy(dx: edgePadding + layoutMargins.left, dy: 0.0)
.subtracting(width: securityIndicatorSize.width, height: 0)
if showsSecurityIndicator {
securityIndicatorView.isHidden = false
2021-07-20 19:17:04 -07:00
securityIndicatorView.frame = CGRect(
origin: CGPoint(x: edgePadding + layoutMargins.left, y: 0.0),
size: CGSize(width: securityIndicatorSize.height, height: securityIndicatorSize.height)
)
2021-06-14 18:09:33 -07:00
2021-07-20 19:17:04 -07:00
// Scooch the title over a bit
titleLabelView.frame.origin.x = securityIndicatorView.frame.maxX + 4.0
2021-06-14 18:09:33 -07:00
securityIndicatorView.frame = securityIndicatorView.frame.centeredY(inRect: titleLabelView.frame)
} else {
securityIndicatorView.isHidden = true
}
2021-06-14 16:32:51 -07:00
let separatorHeight = 1.0 / UIScreen.main.scale
separatorView.frame = CGRect(x: 0, y: bounds.height - separatorHeight, width: bounds.width, height: separatorHeight)
2020-07-29 14:16:25 -07:00
}
}