1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import SwiftUI
struct PadColumnFlick {
let releaseOffset: CGFloat
let projectedOffset: CGFloat
let direction: CGFloat
var recentFlick = false
}
enum PadColumnLayout {
static let actionSpacing: CGFloat = 12
static let edgePadding: CGFloat = 12
static func opensDetailsInColumns(windowSize: CGSize) -> Bool {
windowSize.width > windowSize.height
}
static func usesColumns(windowSize: CGSize, screenSize: CGSize) -> Bool {
let isMiniPortrait = isMini(screenSize: screenSize) && windowSize.height >= windowSize.width
return !isMiniPortrait && windowSize.width >= 24 + 320 * 2 + 12
}
static func usesFloatingComposeButton(windowSize: CGSize, screenSize: CGSize) -> Bool {
let screenWidth = min(screenSize.width, screenSize.height)
return screenWidth > 0 && windowSize.width > 0
&& !usesColumns(windowSize: windowSize, screenSize: screenSize)
&& windowSize.height >= windowSize.width
&& windowSize.width >= screenWidth - 1
}
static func isMini(screenSize: CGSize) -> Bool {
let shortEdge = min(screenSize.width, screenSize.height)
return shortEdge > 0 && shortEdge < 768
}
static func loadingColumnCount(visibleColumnCount: Int, existingColumnCount: Int) -> Int {
let count = (2...4).contains(visibleColumnCount) ? visibleColumnCount : 2
return max(0, count - existingColumnCount)
}
static func columnWidth(windowWidth: CGFloat, visibleColumnCount: Int) -> CGFloat {
let count = CGFloat((2...4).contains(visibleColumnCount) ? visibleColumnCount : 2)
return max(320, (windowWidth - 24 - 12 * (count - 1)) / count)
}
static func columnsFit(columnCount: Int, columnWidth: CGFloat, windowWidth: CGFloat) -> Bool {
let contentWidth = 24 + CGFloat(columnCount) * columnWidth + CGFloat(max(0, columnCount - 1)) * 12
return contentWidth <= windowWidth + 1
}
static func flickTarget(
release: PadColumnFlick,
columnWidth: CGFloat, columnCount: Int, contentWidth: CGFloat, windowWidth: CGFloat
) -> CGFloat? {
let releaseOffset = release.releaseOffset
let projectedOffset = release.projectedOffset
let direction = release.direction
let recentFlick = release.recentFlick
let projectedMovement = projectedOffset - releaseOffset
let movement = abs(projectedMovement) >= 12 ? projectedMovement : (recentFlick ? direction : 0)
guard movement * direction > 0,
columnWidth > 0, windowWidth > 0, columnCount > 0
else { return nil }
let maximum = max(0, contentWidth - windowWidth)
guard releaseOffset >= 0, releaseOffset <= maximum else { return nil }
let left = releaseOffset
let right = left + windowWidth
let stride = columnWidth + 12
let indices = 0..<columnCount
let first = indices.first(where: { 12 + CGFloat($0) * stride >= left - 1 }) ?? columnCount
let last = indices.last(where: { 12 + CGFloat($0) * stride + columnWidth <= right + 1 }) ?? -1
let targetIndex = movement > 0 ? last + 1 : first - 1
guard indices.contains(targetIndex) else { return nil }
let targetLeft = 12 + CGFloat(targetIndex) * stride
let target = movement > 0 ? targetLeft + columnWidth - windowWidth + edgePadding : targetLeft - edgePadding
let clamped = min(maximum, max(0, target))
guard (movement > 0 && clamped > left) || (movement < 0 && clamped < left) else { return nil }
return clamped
}
}