Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ jobs:
python3 -m unittest discover -s Tests/ReleaseTests
- name: Host tests
run: swift test -j 2
# Every Storage access page and dialog at the size of every iPhone, iPod touch
# and iPad (App/LayoutCheck.swift): fails on a width or height left to iOS
# (iOS 12 drew such a page two letters wide in the companion), broken or cut
# off text and anything off the screen.
layout:
runs-on: macos-15-intel # arm64 Mac runners are short; queued jobs are dropped after 15 min
timeout-minutes: 20
env:
DEVELOPER_DIR: /Applications/Xcode_16.4.app/Contents/Developer
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- name: Layout check
run: scripts/check-layout.sh
15 changes: 15 additions & 0 deletions App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var frame = UIScreen.main.bounds
#if targetEnvironment(simulator)
if ProcessInfo.processInfo.environment["ORCHARD_LAYOUT_CHECK"] != nil {
let pages: [(String, StorageAccess.Phase)] = [("access", .start), ("access-working", .getting), ("access-failed", .failed)]
let dialogs = [("dialog-stop", DialogController(title: "Stop resizing?", message: "The current step finishes first. APFS keeps the size it has then and the partition stays 64 GB. You can continue or grow APFS back later.", action: "Stop", cancel: "Keep resizing") {}),
("dialog-snapshot", DialogController(title: "Remove system snapshot?", message: "iOS then starts without it, as after checkra1n’s Install Cydia, and can shrink further. It can’t be brought back.", action: "Remove") {})]
LayoutCheck.start { pages.flatMap { name, phase in
LayoutCheck.problems(name) { _ in
let model = AppModel()
model.showAccessPage(phase)
let controller = RootViewController(model: model)
controller.loadViewIfNeeded()
return controller
}
} + dialogs.flatMap { name, dialog in LayoutCheck.problems(name) { _ in dialog } }}
return true
}
// UI tests check the iPod touch's 320 × 568 pt layout, which current simulators lack.
if ProcessInfo.processInfo.arguments.contains("--ipod-size") { frame = CGRect(x: 0, y: 0, width: 320, height: 568) }
#endif
Expand Down
4 changes: 4 additions & 0 deletions App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ final class AppModel {
/// `.getting` and `.failed` have their own pages, before any storage is shown.
private(set) var accessPhase: StorageAccess.Phase
private var accessBegun = false
#if targetEnvironment(simulator)
/// The layout check (LayoutCheck.swift) shows each Storage access page.
func showAccessPage(_ phase: StorageAccess.Phase) { accessPhase = phase }
#endif

init(access: StorageAccess? = nil) {
self.access = access
Expand Down
5 changes: 4 additions & 1 deletion App/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,11 @@ final class ActionButton: UIControl {
row.isUserInteractionEnabled = false
row.translatesAutoresizingMaskIntoConstraints = false
addSubview(row)
// 50 high: a height left open is one iOS versions pick differently (LayoutCheck.swift).
let height = heightAnchor.constraint(equalToConstant: 50)
height.priority = UILayoutPriority(999)
NSLayoutConstraint.activate([row.centerXAnchor.constraint(equalTo: centerXAnchor), row.centerYAnchor.constraint(equalTo: centerYAnchor),
heightAnchor.constraint(greaterThanOrEqualToConstant: 50)])
heightAnchor.constraint(greaterThanOrEqualToConstant: 50), height])
isEnabled = enabled
addTarget(self, action: #selector(tapped), for: .touchUpInside)
isAccessibilityElement = true
Expand Down
3 changes: 2 additions & 1 deletion App/Dialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ final class DialogController: UIViewController {
box.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(box)
let width = box.widthAnchor.constraint(equalToConstant: 300)
width.priority = .defaultHigh
// Above the text's own wishes (750): its width is never left to iOS (LayoutCheck.swift).
width.priority = UILayoutPriority(999)
NSLayoutConstraint.activate([
column.topAnchor.constraint(equalTo: box.topAnchor), column.bottomAnchor.constraint(equalTo: box.bottomAnchor),
column.leadingAnchor.constraint(equalTo: box.leadingAnchor), column.trailingAnchor.constraint(equalTo: box.trailingAnchor),
Expand Down
157 changes: 157 additions & 0 deletions App/LayoutCheck.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// The layout check (scripts/check-layout.sh): every page at the size of every
// iPhone, iPod touch and iPad, upright and on its side. It fails on what went
// wrong on devices before: a width Auto Layout has to pick by itself (iOS 12
// picked about two letters for the companion's Storage access page), text broken
// inside a word or cut off, anything outside the screen. Simulator only. The same
// check as OrchardLinux/tools' companion/App/LayoutCheck.swift.
#if targetEnvironment(simulator)
import UIKit

enum LayoutCheck {
/// Points: iPhone SE (1st) and iPod touch (6th), iPhone 8, iPhone XR, iPad mini
/// and iPad 9.7", iPad 10.2", iPad Pro 12.9"; upright and on their side.
static let sizes: [CGSize] = {
let upright = [CGSize(width: 320, height: 568), CGSize(width: 375, height: 667),
CGSize(width: 414, height: 896), CGSize(width: 768, height: 1024),
CGSize(width: 810, height: 1080), CGSize(width: 1024, height: 1366)]
return upright + upright.map { CGSize(width: $0.height, height: $0.width) }
}()

/// The problems of one screen: `make` builds its controller for a size (it may
/// word the screen for the device of that size), laid out first in a tiny window,
/// as a window can be before it gets its size, then at each size in turn.
static func problems(_ screen: String, sizes: [CGSize] = sizes, _ make: (CGSize) -> UIViewController) -> [String] {
var problems: [String] = []
for size in sizes {
let controller = make(size)
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
// The size classes of the device of that size (an iPad is regular both ways;
// a phone is compact across, and compact high on its side).
let host = UIViewController()
host.addChild(controller)
controller.view.frame = host.view.bounds
controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
host.view.addSubview(controller.view)
controller.didMove(toParent: host)
let pad = min(size.width, size.height) >= 600
host.setOverrideTraitCollection(UITraitCollection(traitsFrom: [
UITraitCollection(horizontalSizeClass: pad ? .regular : .compact),
UITraitCollection(verticalSizeClass: pad || size.height > size.width ? .regular : .compact),
UITraitCollection(userInterfaceIdiom: pad ? .pad : .phone)]), forChild: controller)
window.rootViewController = host
window.isHidden = false
window.layoutIfNeeded()
window.frame = CGRect(origin: .zero, size: size)
window.layoutIfNeeded()
problems += check(controller.view, in: window).map { "\(screen) at \(Int(size.width))x\(Int(size.height)): \($0)" }
// ORCHARD_LAYOUT_SHOTS=DIR: a picture of the screen, upright, at 2x, for review.
if let dir = ProcessInfo.processInfo.environment["ORCHARD_LAYOUT_SHOTS"], size.height > size.width {
let format = UIGraphicsImageRendererFormat()
format.scale = 2
let image = UIGraphicsImageRenderer(bounds: window.bounds, format: format).image { window.layer.render(in: $0.cgContext) }
let name = screen.replacingOccurrences(of: " ", with: "_").replacingOccurrences(of: "(", with: "").replacingOccurrences(of: ")", with: "")
try? image.pngData()?.write(to: URL(fileURLWithPath: "\(dir)/\(name)@\(Int(size.width))x\(Int(size.height)).png"))
}
window.isHidden = true
}
return problems
}

/// Runs `check` once the app has launched (iOS ends an app whose launch takes as long
/// as the check does on a slow Mac), then prints its problems and ends the app.
static func start(_ check: @escaping () -> [String]) {
DispatchQueue.main.async { finish(check()) }
}

/// Prints the problems and ends the app: status 0 without any.
static func finish(_ problems: [String]) -> Never {
setvbuf(stdout, nil, _IOLBF, 0)
problems.forEach { print("LAYOUT: \($0)") }
print(problems.isEmpty ? "LAYOUT OK" : "LAYOUT FAILED: \(problems.count) problems")
exit(problems.isEmpty ? 0 : 1)
}

private static func describe(_ view: UIView) -> String {
if let label = view as? UILabel { return "label \"\(label.text ?? "")\"" }
return String(describing: type(of: view))
}

private static func labels(_ view: UIView) -> [UILabel] {
(view as? UILabel).map { [$0] } ?? view.subviews.flatMap(labels)
}

/// Text that wraps must get its width from the layout, never from its own wish
/// to be narrow or wide: where that wish decides, iOS versions decide differently
/// (iOS 12 made the Storage access page's text about two letters wide). So each
/// such label is laid out wanting no width, then all it can get, and must not move.
private static func undecided(_ root: UIView, in window: UIWindow) -> [String] {
let wrapping = labels(root).filter { $0.numberOfLines != 1 && !($0.text ?? "").isEmpty }
let saved = wrapping.map { ($0.contentHuggingPriority(for: .horizontal), $0.contentCompressionResistancePriority(for: .horizontal)) }
func widths(hug: UILayoutPriority, resist: UILayoutPriority) -> [CGFloat] {
for label in wrapping {
label.setContentHuggingPriority(hug, for: .horizontal)
label.setContentCompressionResistancePriority(resist, for: .horizontal)
label.invalidateIntrinsicContentSize()
}
root.setNeedsLayout()
window.layoutIfNeeded()
return wrapping.map { $0.bounds.width }
}
// 990: far above the system's own 750 and 250, below the 999 a screen
// decides a width with (StatusController's column, keepNote).
let narrow = widths(hug: UILayoutPriority(990), resist: UILayoutPriority(1))
let wide = widths(hug: UILayoutPriority(1), resist: UILayoutPriority(990))
for (label, (hug, resist)) in zip(wrapping, saved) {
label.setContentHuggingPriority(hug, for: .horizontal)
label.setContentCompressionResistancePriority(resist, for: .horizontal)
}
root.setNeedsLayout()
window.layoutIfNeeded()
return zip(wrapping, zip(narrow, wide)).compactMap { label, w in
abs(w.0 - w.1) > 0.5 ? "\(describe(label)) has no width of its own: \(Int(w.0)) to \(Int(w.1)), as the system picks" : nil
}
}

private static func check(_ root: UIView, in window: UIWindow) -> [String] {
var problems = undecided(root, in: window)
func visit(_ view: UIView, clip: CGRect) {
guard !view.isHidden, view.alpha > 0 else { return }
let frame = view.convert(view.bounds, to: window)
if view.hasAmbiguousLayout { problems.append("\(describe(view)) has no definite size or place") }
if frame.minX < clip.minX - 0.5 || frame.maxX > clip.maxX + 0.5 || frame.minY < clip.minY - 0.5 || frame.maxY > clip.maxY + 0.5 {
problems.append("\(describe(view)) at \(frame) is outside \(clip)")
}
if let label = view as? UILabel, let text = label.attributedText, text.length > 0 {
// Each word as the label draws it: its own attributes, on one line.
let string = text.string as NSString
var at = 0
while at < string.length {
let space = string.rangeOfCharacter(from: .whitespacesAndNewlines, options: [], range: NSRange(location: at, length: string.length - at))
let end = space.location == NSNotFound ? string.length : space.location
if end > at {
let word = text.attributedSubstring(from: NSRange(location: at, length: end - at))
let width = word.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: .greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin], context: nil).width
if width > label.bounds.width + 0.5 {
problems.append("\(describe(view)) is \(Int(label.bounds.width)) wide, which breaks \"\(word.string)\" (\(Int(ceil(width))) wide)")
break
}
}
at = end + 1
}
let needed = label.sizeThatFits(CGSize(width: label.bounds.width, height: .greatestFiniteMagnitude)).height
if needed > label.bounds.height + 1 { problems.append("\(describe(view)) is cut off (\(Int(label.bounds.height)) of \(Int(needed)) high)") }
}
// A scroll view's content may go past its frame, vertically only.
var inner = clip
if let scroll = view as? UIScrollView {
if scroll.contentSize.width > scroll.bounds.width + 0.5 { problems.append("scrolls sideways (\(scroll.contentSize.width) in \(scroll.bounds.width))") }
inner = CGRect(x: frame.minX, y: -.greatestFiniteMagnitude / 2, width: frame.width, height: .greatestFiniteMagnitude)
}
view.subviews.forEach { visit($0, clip: inner) }
}
visit(root, clip: window.bounds)
return problems
}
}
#endif
8 changes: 7 additions & 1 deletion App/Pages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ struct Pages {
let lines = stack(.vertical, 3, device.enumerated().map { label($1, Theme.font(m.caption, $0 == 0 ? .medium : .regular), $0 == 0 ? Theme.ink : Theme.muted, lines: 1, align: .right) }, align: .trailing)
return stack(.horizontal, 10, [tile, name, spacer(), lines] + trailing, align: .center)
}
// The names take the rest of the row: no width of theirs is left to iOS.
let titles = stack(.vertical, 4, [name, label(device.joined(separator: " · "), Theme.font(m.caption), Theme.muted, lines: 2)])
return stack(.horizontal, 12, [tile, titles, spacer()] + trailing, align: .center)
titles.setContentHuggingPriority(UILayoutPriority(1), for: .horizontal)
trailing.forEach { $0.setContentHuggingPriority(.required, for: .horizontal) }
return stack(.horizontal, 12, [tile, titles] + trailing, align: .center)
}

private func title(_ text: String, _ subtitle: String?, identifier: String? = nil, centered: Bool = false) -> UIView {
Expand All @@ -190,6 +193,9 @@ struct Pages {
heading.textAlignment = .center
let views: [UIView] = [badge, heading] + lines.map { label($0, Theme.font(m.description), Theme.muted, align: .center) }
let column = stack(.vertical, 10, views, align: .center)
// The text as wide as the page, centred in it: a width left to iOS is one iOS 12
// can make a few letters (the companion's Storage access page did).
for view in views where view is UILabel { view.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true }
column.setCustomSpacing(16, after: badge)
column.isLayoutMarginsRelativeArrangement = true
column.layoutMargins = UIEdgeInsets(top: m.regular ? 24 : 12, left: 0, bottom: 0, right: 0)
Expand Down
5 changes: 5 additions & 0 deletions App/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
page.topAnchor.constraint(equalTo: scroll.contentLayoutGuide.topAnchor, constant: 8),
page.bottomAnchor.constraint(equalTo: scroll.contentLayoutGuide.bottomAnchor, constant: -16),
page.centerXAnchor.constraint(equalTo: scroll.centerXAnchor),
// Scrolls up and down only: the content is as wide as the screen.
scroll.contentLayoutGuide.widthAnchor.constraint(equalTo: scroll.frameLayoutGuide.widthAnchor),
page.widthAnchor.constraint(lessThanOrEqualToConstant: metrics.contentWidth), width, widthCap,
footerBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
footerBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
Expand All @@ -108,6 +110,8 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
}
private var sideConstraints: [NSLayoutConstraint] = []
private var headerTop: NSLayoutConstraint?
/// An empty footer is no height, not one iOS picks (LayoutCheck.swift).
private lazy var footerEmpty = footer.heightAnchor.constraint(equalToConstant: 0)

/// A hairline under the header once the page scrolls beneath it.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
Expand Down Expand Up @@ -199,6 +203,7 @@ final class RootViewController: UIViewController, UIScrollViewDelegate {
footer.arrangedSubviews.forEach { $0.removeFromSuperview() }
for view in Pages(self, metrics).footer() { footer.addArrangedSubview(view) }
footerBar.isHidden = footer.arrangedSubviews.isEmpty
footerEmpty.isActive = footer.arrangedSubviews.isEmpty
footerKey = key
}

Expand Down
Loading
Loading