Goofing around with Box
This commit is contained in:
@@ -287,6 +287,11 @@ class BrowserViewController: UIViewController
|
||||
showSettingsWindow()
|
||||
}, for: .touchUpInside)
|
||||
|
||||
// Share
|
||||
documentControls.sharingView.addAction(UIAction { [unowned self] _ in
|
||||
showShareSheetForCurrentURL(fromViewController: documentControls)
|
||||
}, for: .touchUpInside)
|
||||
|
||||
present(documentControls, animated: true, completion: nil)
|
||||
}), for: .touchUpInside)
|
||||
|
||||
|
||||
@@ -14,6 +14,13 @@ class DocumentControlItemView: UIControl
|
||||
let imageView = UIImageView(frame: .zero)
|
||||
let label = UILabel(frame: .zero)
|
||||
|
||||
enum ViewType: String {
|
||||
case imageView
|
||||
case label
|
||||
case separator
|
||||
case highlightView
|
||||
}
|
||||
|
||||
var drawsBottomSeparator: Bool = false {
|
||||
didSet { setNeedsLayout() }
|
||||
}
|
||||
@@ -50,38 +57,68 @@ class DocumentControlItemView: UIControl
|
||||
CGSize(width: size.width, height: Self.controlHeight)
|
||||
}
|
||||
|
||||
private func box(_ bounds: CGRect) -> Box<ViewType> {
|
||||
Box {
|
||||
let padding: CGFloat = 18.0
|
||||
let imageSize: CGFloat = 24.0
|
||||
let ibounds = bounds.inset(by: layoutMargins)
|
||||
|
||||
let imageRect = CGRect(
|
||||
x: ibounds.minX + 6.0, y: 0.0,
|
||||
width: imageSize, height: imageSize
|
||||
).centeredY(inRect: bounds)
|
||||
|
||||
(ViewType.imageView, imageRect)
|
||||
|
||||
(ViewType.highlightView, bounds)
|
||||
|
||||
(ViewType.label, CGRect(
|
||||
x: imageRect.maxX + padding, y: ibounds.minY,
|
||||
width: ibounds.width - imageRect.maxX - padding, height: ibounds.height
|
||||
))
|
||||
|
||||
let separatorHeight: CGFloat = 1.0
|
||||
|
||||
if drawsBottomSeparator {
|
||||
(ViewType.separator, CGRect(
|
||||
x: bounds.minX, y: bounds.height - separatorHeight,
|
||||
width: bounds.width, height: separatorHeight
|
||||
))
|
||||
} else {
|
||||
(ViewType.separator, CGRect.zero)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
highlightView.frame = bounds
|
||||
box(bounds).fill([
|
||||
.imageView : imageView,
|
||||
.separator : separatorView,
|
||||
.label : label,
|
||||
.highlightView : highlightView
|
||||
])
|
||||
|
||||
let padding: CGFloat = 18.0
|
||||
let imageSize: CGFloat = 24.0
|
||||
let bounds = self.bounds.inset(by: layoutMargins)
|
||||
imageView.frame = CGRect(
|
||||
x: bounds.minX + 6.0, y: 0.0,
|
||||
width: imageSize, height: imageSize
|
||||
).centeredY(inRect: self.bounds)
|
||||
|
||||
label.frame = CGRect(
|
||||
x: imageView.frame.maxX + padding, y: bounds.minY,
|
||||
width: bounds.width - imageView.frame.maxX - padding, height: bounds.height
|
||||
)
|
||||
|
||||
let separatorHeight: CGFloat = 1.0
|
||||
if drawsBottomSeparator {
|
||||
separatorView.isHidden = false
|
||||
separatorView.frame = CGRect(
|
||||
x: self.bounds.minX, y: self.bounds.height - separatorHeight,
|
||||
width: self.bounds.width, height: separatorHeight
|
||||
)
|
||||
} else {
|
||||
separatorView.isHidden = true
|
||||
}
|
||||
separatorView.isHidden = !drawsBottomSeparator
|
||||
}
|
||||
|
||||
override func setTracking(_ tracking: Bool) {
|
||||
super.setTracking(tracking)
|
||||
highlightView.isHidden = !tracking
|
||||
}
|
||||
|
||||
public func title(_ title: String) -> Self {
|
||||
self.label.text = title
|
||||
return self
|
||||
}
|
||||
|
||||
public func image(_ image: UIImage?) -> Self {
|
||||
self.imageView.image = image
|
||||
return self
|
||||
}
|
||||
|
||||
public func symbol(_ name: String) -> Self {
|
||||
return self.image(UIImage(systemName: name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,37 +10,25 @@ import UIKit
|
||||
class DocumentControlViewController: UIViewController
|
||||
{
|
||||
let documentControlsView = DocumentControlsView()
|
||||
let fontSizeAdjustView = FontSizeAdjustView()
|
||||
let findOnPageControlView = DocumentControlItemView()
|
||||
let navigationControlView = NavigationControlsView()
|
||||
let settingsView = DocumentControlItemView()
|
||||
let readabilityView = DocumentControlItemView()
|
||||
let darkModeView = DocumentControlItemView()
|
||||
let archiveView = DocumentControlItemView()
|
||||
let emailView = DocumentControlItemView()
|
||||
|
||||
let fontSizeAdjustView = FontSizeAdjustView()
|
||||
let navigationControlView = NavigationControlsView()
|
||||
|
||||
let findOnPageControlView = DocumentControlItemView().title("Find On Page") .symbol("magnifyingglass")
|
||||
let settingsView = DocumentControlItemView().title("Settings") .symbol("gear")
|
||||
let readabilityView = DocumentControlItemView().title("Reader Mode") .symbol("doc.richtext")
|
||||
let archiveView = DocumentControlItemView().title("Archive.today") .symbol("shippingbox")
|
||||
let emailView = DocumentControlItemView().title("Email") .symbol("envelope")
|
||||
let sharingView = DocumentControlItemView().title("Share") .symbol("square.and.arrow.up")
|
||||
let darkModeView = DocumentControlItemView().title("Dark Mode")
|
||||
|
||||
var observations: [NSKeyValueObservation] = []
|
||||
|
||||
static public let preferredWidth = CGFloat(200.0)
|
||||
static public let preferredWidth = CGFloat(230.0)
|
||||
|
||||
init(darkModeEnabled: Bool) {
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
||||
findOnPageControlView.label.text = "Find On Page"
|
||||
findOnPageControlView.imageView.image = UIImage(systemName: "magnifyingglass")
|
||||
|
||||
settingsView.label.text = "Settings"
|
||||
settingsView.imageView.image = UIImage(systemName: "gear")
|
||||
|
||||
readabilityView.label.text = "Reader Mode"
|
||||
readabilityView.imageView.image = UIImage(systemName: "doc.richtext")
|
||||
|
||||
archiveView.label.text = "Archive.today"
|
||||
archiveView.imageView.image = UIImage(systemName: "shippingbox")
|
||||
|
||||
emailView.label.text = "Email"
|
||||
emailView.imageView.image = UIImage(systemName: "envelope")
|
||||
|
||||
if darkModeEnabled {
|
||||
darkModeView.label.text = "Disable Dark Mode"
|
||||
} else {
|
||||
@@ -53,6 +41,7 @@ class DocumentControlViewController: UIViewController
|
||||
documentControlsView.stackView.addArrangedSubview(fontSizeAdjustView)
|
||||
|
||||
documentControlsView.stackView.addArrangedSubview(emailView)
|
||||
documentControlsView.stackView.addArrangedSubview(sharingView)
|
||||
documentControlsView.stackView.addArrangedSubview(findOnPageControlView)
|
||||
documentControlsView.stackView.addArrangedSubview(darkModeView)
|
||||
documentControlsView.stackView.addArrangedSubview(readabilityView)
|
||||
|
||||
50
App/Utilities/Box.swift
Normal file
50
App/Utilities/Box.swift
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// Box.swift
|
||||
// Box
|
||||
//
|
||||
// Created by James Magahern on 7/13/21.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
struct Box<Identifier: Hashable> {
|
||||
typealias Compartment = (Identifier, CGRect)
|
||||
typealias Realized = (Identifier, UIView)
|
||||
|
||||
@resultBuilder
|
||||
struct BoxBuilder {
|
||||
static func buildEither(first component: [Compartment]) -> Compartment {
|
||||
component[0]
|
||||
}
|
||||
|
||||
static func buildEither(second component: [Compartment]) -> Compartment {
|
||||
component[0]
|
||||
}
|
||||
|
||||
static func buildBlock(_ compartments: Compartment...) -> [Compartment] {
|
||||
return compartments
|
||||
}
|
||||
}
|
||||
|
||||
public var boundingRect: CGRect {
|
||||
get {
|
||||
compartmentMap.reduce(into: CGRect.zero) { (result, compartment) in
|
||||
result = result.union(compartment.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var compartmentMap: [Identifier: CGRect] = [:]
|
||||
|
||||
init(@BoxBuilder _ compartments: () -> [Compartment]) {
|
||||
self.compartmentMap = compartments().reduce(into: [:]) { (result, compartment) in
|
||||
result[compartment.0] = compartment.1
|
||||
}
|
||||
}
|
||||
|
||||
public func fill(_ realized: [Identifier: UIView]) {
|
||||
realized.forEach { (key, value) in
|
||||
value.frame = compartmentMap[key] ?? .zero
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user