Font size adjustment

This commit is contained in:
James Magahern
2020-09-22 15:37:13 -07:00
parent 679e59c20b
commit 399e779371
7 changed files with 262 additions and 1 deletions

View File

@@ -173,6 +173,33 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
autocompleteViewController.delegate = self
autocompleteViewController.view.isHidden = true
// Font size adjust
toolbarController.urlBar.documentButton.addAction(UIAction(handler: { [unowned self] _ in
let documentControls = DocumentControlViewController()
documentControls.modalPresentationStyle = .popover
documentControls.popoverPresentationController?.permittedArrowDirections = [ .down, .up ]
documentControls.popoverPresentationController?.sourceView = toolbarController.urlBar.documentButton
documentControls.popoverPresentationController?.delegate = self
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .percent
let label = documentControls.fontSizeAdjustView.labelView
label.text = numberFormatter.string(for: tab.webView._viewScale)
documentControls.fontSizeAdjustView.decreaseSizeButton.addAction(UIAction(handler: { [unowned self] _ in
tab.webView._viewScale -= 0.10
label.text = numberFormatter.string(for: tab.webView._viewScale)
}), for: .touchUpInside)
documentControls.fontSizeAdjustView.increaseSizeButton.addAction(UIAction(handler: { [unowned self] _ in
tab.webView._viewScale += 0.10
label.text = numberFormatter.string(for: tab.webView._viewScale)
}), for: .touchUpInside)
present(documentControls, animated: true, completion: nil)
}), for: .touchUpInside)
self.view = browserView
}

View File

@@ -0,0 +1,32 @@
//
// DocumentControlViewController.swift
// App
//
// Created by James Magahern on 9/22/20.
//
import UIKit
class DocumentControlViewController: UIViewController
{
let documentControlView = StackView(dimension: .vertical)
let fontSizeAdjustView = FontSizeAdjustView()
static public let preferredWidth = CGFloat(200.0)
static public let controlHeight = CGFloat(48.0)
convenience init() {
self.init(nibName: nil, bundle: nil)
documentControlView.addArrangedSubview(fontSizeAdjustView)
}
override func loadView() {
self.view = documentControlView
}
override var preferredContentSize: CGSize {
get { documentControlView.sizeThatFits(CGSize(width: Self.preferredWidth, height: -1)) }
set {}
}
}

View File

@@ -0,0 +1,58 @@
//
// FontSizeAdjustView.swift
// App
//
// Created by James Magahern on 9/22/20.
//
import UIKit
class FontSizeAdjustView: UIView
{
let decreaseSizeButton = UIButton(frame: .zero)
let increaseSizeButton = UIButton(frame: .zero)
let labelView = UILabel(frame: .zero)
convenience init() {
self.init(frame: .zero)
labelView.textColor = .secondaryLabel
labelView.textAlignment = .center
labelView.text = "100%"
tintColor = .black
decreaseSizeButton.setImage(UIImage(systemName: "minus"), for: .normal)
increaseSizeButton.setImage(UIImage(systemName: "plus"), for: .normal)
addSubview(increaseSizeButton)
addSubview(decreaseSizeButton)
addSubview(labelView)
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
CGSize(width: size.width, height: DocumentControlViewController.controlHeight)
}
override func layoutSubviews() {
super.layoutSubviews()
decreaseSizeButton.frame = CGRect(
x: 0.0, y: 0.0,
width: bounds.height,
height: bounds.height
)
increaseSizeButton.frame = CGRect(
x: bounds.width - bounds.height, y: 0.0,
width: bounds.height,
height: bounds.height
)
labelView.frame = CGRect(
x: decreaseSizeButton.frame.maxX, y: 0.0,
width: bounds.width - decreaseSizeButton.frame.width - increaseSizeButton.frame.width,
height: bounds.height
)
}
}

View File

@@ -6,3 +6,4 @@
// SPI
#import <UIKit/UITextField_Private.h>
#import <WebKit/WKWebViewPrivate.h>

View File

@@ -12,6 +12,7 @@ class URLBar: ReliefButton
let textField = UITextField(frame: .zero)
let refreshButton = UIButton(frame: .zero)
let errorButton = UIButton(frame: .zero)
let documentButton = UIButton(frame: .zero)
public enum LoadProgress {
case complete
@@ -28,11 +29,14 @@ class URLBar: ReliefButton
private let progressIndicatorView = ProgressIndicatorView()
private var progressIndicatorAnimating = false
private let documentImage = UIImage(systemName: "doc.plaintext")
private let refreshImage = UIImage(systemName: "arrow.clockwise")
private let stopImage = UIImage(systemName: "xmark")
private let backgroundCornerRadius: CGFloat = 0
private let documentSeparatorView = UIView(frame: .zero)
override init() {
super.init()
@@ -70,6 +74,13 @@ class URLBar: ReliefButton
errorButton.setTitle("ERR", for: .normal)
addSubview(errorButton)
documentButton.tintColor = .secondaryLabel
documentButton.setImage(documentImage, for: .normal)
addSubview(documentButton)
documentSeparatorView.backgroundColor = .secondarySystemFill
addSubview(documentSeparatorView)
setErrorButtonAnimating(false)
}
@@ -172,7 +183,27 @@ class URLBar: ReliefButton
backgroundView.frame = bounds
shadowView.frame = bounds
progressIndicatorView.frame = backgroundView.bounds
textField.frame = bounds.insetBy(dx: 6.0, dy: 0)
// Document button
documentButton.frame = CGRect(x: 0.0, y: 0.0, width: bounds.height, height: bounds.height)
// Document separator
documentSeparatorView.frame = CGRect(
x: documentButton.frame.maxX, y: 0.0,
width: 1.0, height: bounds.height
)
documentSeparatorView.frame = documentSeparatorView.frame.insetBy(dx: 0.0, dy: 3.0)
// Text field
let textFieldPadding: CGFloat = 5.0
let textFieldOrigin = CGPoint(x: documentButton.frame.maxX + textFieldPadding, y: 0.0)
textField.frame = CGRect(
origin: textFieldOrigin,
size: CGSize(
width: bounds.width - textFieldOrigin.x - textFieldPadding,
height: bounds.height
)
)
var fadeCutoffLocation: CGFloat = 0.8

View File

@@ -0,0 +1,92 @@
//
// StackView.swift
// App
//
// Created by James Magahern on 9/22/20.
//
import UIKit
class StackView: UIView
{
var arrangedSubviews: [UIView] = []
{ didSet { setNeedsLayout() } }
var layoutDimension: UIAxis = .vertical
convenience init(dimension: UIAxis) {
self.init(frame: .zero)
self.layoutDimension = dimension
}
// Convenience
public func addArrangedSubview(_ view: UIView) {
addSubview(view)
arrangedSubviews.append(view)
setNeedsLayout()
}
public func removeArrangedSubview(_ view: UIView) {
if view.superview == self {
view.removeFromSuperview()
}
arrangedSubviews.removeAll { $0 == view }
setNeedsLayout()
}
public func removeAllArrangedSubviews() {
arrangedSubviews.forEach { $0.removeFromSuperview() }
arrangedSubviews.removeAll()
setNeedsLayout()
}
override func sizeThatFits(_ containerSize: CGSize) -> CGSize {
var size: CGSize = .zero
if layoutDimension == .horizontal {
size.height = containerSize.height
} else {
size.width = containerSize.width
}
var spaceRemaining = containerSize
for view in arrangedSubviews {
let viewSize = view.sizeThatFits(spaceRemaining)
if layoutDimension == .horizontal {
size.width += viewSize.width
spaceRemaining.width -= viewSize.width
} else {
size.height += viewSize.height
spaceRemaining.height -= viewSize.height
}
}
return size
}
override func layoutSubviews() {
super.layoutSubviews()
var origin: CGPoint = CGPoint(x: safeAreaInsets.left, y: safeAreaInsets.top)
var spaceRemaining = bounds.size
for view in arrangedSubviews {
var viewSize = view.sizeThatFits(spaceRemaining)
var offset: CGPoint = .zero
if layoutDimension == .horizontal {
offset.x = viewSize.width
viewSize.height = bounds.height
spaceRemaining.width -= viewSize.width
} else {
offset.y = viewSize.height
viewSize.width = bounds.width
spaceRemaining.height -= viewSize.height
}
view.frame = CGRect(origin: origin, size: viewSize)
origin.x += offset.x
origin.y += offset.y
}
}
}

View File

@@ -42,6 +42,9 @@
CD853BCE24E7763900D2BDCC /* BrowserHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD853BCD24E7763900D2BDCC /* BrowserHistory.swift */; };
CD853BD124E778B800D2BDCC /* History.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = CD853BCF24E778B800D2BDCC /* History.xcdatamodeld */; };
CD853BD424E77BF900D2BDCC /* HistoryItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD853BD324E77BF900D2BDCC /* HistoryItem.swift */; };
CDCE2664251AA80F007FE92A /* DocumentControlViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2663251AA80F007FE92A /* DocumentControlViewController.swift */; };
CDCE2666251AA840007FE92A /* StackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2665251AA840007FE92A /* StackView.swift */; };
CDCE2668251AAA9A007FE92A /* FontSizeAdjustView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2667251AAA9A007FE92A /* FontSizeAdjustView.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -113,6 +116,9 @@
CD853BCD24E7763900D2BDCC /* BrowserHistory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserHistory.swift; sourceTree = "<group>"; };
CD853BD024E778B800D2BDCC /* History.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = History.xcdatamodel; sourceTree = "<group>"; };
CD853BD324E77BF900D2BDCC /* HistoryItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryItem.swift; sourceTree = "<group>"; };
CDCE2663251AA80F007FE92A /* DocumentControlViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentControlViewController.swift; sourceTree = "<group>"; };
CDCE2665251AA840007FE92A /* StackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackView.swift; sourceTree = "<group>"; };
CDCE2667251AAA9A007FE92A /* FontSizeAdjustView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontSizeAdjustView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -203,6 +209,7 @@
1ADFF47A24C7E176006DC7AE /* Backend */,
1ADFF47724C7DFE8006DC7AE /* Browser View */,
1A03810E24E71CCA00826501 /* Common UI */,
CDCE2662251AA7FC007FE92A /* Document Controls UI */,
1ADFF4CE24CBBCBD006DC7AE /* Script Policy UI */,
1AB88F0324D3E1EC0006F850 /* Tabs */,
1AB88F0424D3E1F90006F850 /* Titlebar and URL Bar */,
@@ -277,6 +284,7 @@
isa = PBXGroup;
children = (
1ADFF4C224CA6AF6006DC7AE /* Geometry.swift */,
CDCE2665251AA840007FE92A /* StackView.swift */,
CD7A8918251989C90075991E /* UIKeyCommand+ConvInit.swift */,
1ADFF4C624CA6DEB006DC7AE /* UIEdgeInsets+Layout.swift */,
1AB88F0524D4D3A90006F850 /* UIGestureRecognizer+Actions.swift */,
@@ -312,6 +320,15 @@
path = History;
sourceTree = "<group>";
};
CDCE2662251AA7FC007FE92A /* Document Controls UI */ = {
isa = PBXGroup;
children = (
CDCE2663251AA80F007FE92A /* DocumentControlViewController.swift */,
CDCE2667251AAA9A007FE92A /* FontSizeAdjustView.swift */,
);
path = "Document Controls UI";
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -418,11 +435,13 @@
1A03811024E71CF000826501 /* ReliefButton.swift in Sources */,
1A03811224E71EAA00826501 /* GradientView.swift in Sources */,
1ADFF4C024CA6964006DC7AE /* URLBar.swift in Sources */,
CDCE2666251AA840007FE92A /* StackView.swift in Sources */,
CD853BD124E778B800D2BDCC /* History.xcdatamodeld in Sources */,
CD7A8919251989C90075991E /* UIKeyCommand+ConvInit.swift in Sources */,
1ADFF4C724CA6DEB006DC7AE /* UIEdgeInsets+Layout.swift in Sources */,
1ADFF4AE24C8ED32006DC7AE /* ResourcePolicyManager.swift in Sources */,
1ADFF47424C7DE9C006DC7AE /* BrowserViewController.swift in Sources */,
CDCE2668251AAA9A007FE92A /* FontSizeAdjustView.swift in Sources */,
1ADFF4D024CBBCD1006DC7AE /* ScriptPolicyControl.swift in Sources */,
1A03810D24E71CA700826501 /* ToolbarView.swift in Sources */,
CD853BD424E77BF900D2BDCC /* HistoryItem.swift in Sources */,
@@ -440,6 +459,7 @@
1ADFF4CD24CBB0C8006DC7AE /* ScriptPolicyViewController.swift in Sources */,
1A14FC2824D26749009B3F83 /* Tab.swift in Sources */,
1ADFF47924C7DFF8006DC7AE /* BrowserView.swift in Sources */,
CDCE2664251AA80F007FE92A /* DocumentControlViewController.swift in Sources */,
1AB88EFF24D3BBA50006F850 /* TabPickerViewController.swift in Sources */,
1A14FC2324D203D9009B3F83 /* TitlebarView.swift in Sources */,
);