tangled
alpha
login
or
join now
jonmsterling.com
/
AquaKit
4
fork
atom
A collection of user interface components and drawing routines for building tasteful apps using AppKit.
appkit
swift
aqua
ui
mac
4
fork
atom
overview
issues
1
pulls
pipelines
Orientation => Axis
jonmsterling.com
3 weeks ago
130d4999
ea138dc8
+184
-51
5 changed files
expand all
collapse all
unified
split
Sources
AquaKit
General Purpose
Geometry
Orientation.swift
ViewGeometry.swift
Scroll Views
AquaScrollState.swift
AquaScroller.swift
Web Views
AquaWebViewController.swift
-40
Sources/AquaKit/General Purpose/Geometry/Orientation.swift
···
1
1
-
import AppKit
2
2
-
3
3
-
public enum Orientation {
4
4
-
case vertical
5
5
-
case horizontal
6
6
-
}
7
7
-
8
8
-
extension NSSize {
9
9
-
public subscript(orientation: Orientation) -> CGFloat {
10
10
-
get {
11
11
-
switch orientation {
12
12
-
case .vertical: height
13
13
-
case .horizontal: width
14
14
-
}
15
15
-
}
16
16
-
set {
17
17
-
switch orientation {
18
18
-
case .vertical: height = newValue
19
19
-
case .horizontal: width = newValue
20
20
-
}
21
21
-
}
22
22
-
}
23
23
-
}
24
24
-
25
25
-
extension NSPoint {
26
26
-
public subscript(orientation: Orientation) -> CGFloat {
27
27
-
get {
28
28
-
switch orientation {
29
29
-
case .vertical: y
30
30
-
case .horizontal: x
31
31
-
}
32
32
-
}
33
33
-
set {
34
34
-
switch orientation {
35
35
-
case .vertical: y = newValue
36
36
-
case .horizontal: x = newValue
37
37
-
}
38
38
-
}
39
39
-
}
40
40
-
}
+173
Sources/AquaKit/General Purpose/Geometry/ViewGeometry.swift
···
1
1
+
import Cocoa
2
2
+
3
3
+
public enum Axis {
4
4
+
case horizontal
5
5
+
case vertical
6
6
+
}
7
7
+
8
8
+
public enum Extreme {
9
9
+
case min
10
10
+
case max
11
11
+
}
12
12
+
13
13
+
extension Axis {
14
14
+
public var opposite: Axis {
15
15
+
switch self {
16
16
+
case .horizontal: .vertical
17
17
+
case .vertical: .horizontal
18
18
+
}
19
19
+
}
20
20
+
}
21
21
+
22
22
+
extension NSRectEdge {
23
23
+
public var opposite: Self? {
24
24
+
switch self {
25
25
+
case .minX: .maxX
26
26
+
case .maxX: .minX
27
27
+
case .minY: .maxY
28
28
+
case .maxY: .minY
29
29
+
@unknown default: nil
30
30
+
}
31
31
+
}
32
32
+
33
33
+
public var axis: Axis? {
34
34
+
switch self {
35
35
+
case .minX, .maxX: .horizontal
36
36
+
case .minY, .maxY: .vertical
37
37
+
@unknown default: nil
38
38
+
}
39
39
+
}
40
40
+
41
41
+
public var extreme: Extreme? {
42
42
+
switch self {
43
43
+
case .minX, .minY: .min
44
44
+
case .maxX, .maxY: .max
45
45
+
@unknown default: nil
46
46
+
}
47
47
+
}
48
48
+
49
49
+
public var split: (axis: Axis, extreme: Extreme)? {
50
50
+
switch self {
51
51
+
case .minX: (.horizontal, .min)
52
52
+
case .minY: (.vertical, .min)
53
53
+
case .maxX: (.horizontal, .max)
54
54
+
case .maxY: (.vertical, .max)
55
55
+
@unknown default: nil
56
56
+
}
57
57
+
}
58
58
+
59
59
+
public var counterclockwiseNextEdge: Self? {
60
60
+
switch self {
61
61
+
case .minX: .minY
62
62
+
case .minY: .maxX
63
63
+
case .maxX: .maxY
64
64
+
case .maxY: .minX
65
65
+
@unknown default: nil
66
66
+
}
67
67
+
}
68
68
+
69
69
+
public var clockwiseNextEdge: Self? {
70
70
+
switch self {
71
71
+
case .minX: .maxY
72
72
+
case .maxY: .maxX
73
73
+
case .maxX: .minY
74
74
+
case .minY: .minX
75
75
+
@unknown default: nil
76
76
+
}
77
77
+
}
78
78
+
}
79
79
+
80
80
+
extension NSPoint {
81
81
+
public subscript(_ axis: Axis) -> CGFloat {
82
82
+
get {
83
83
+
switch axis {
84
84
+
case .horizontal: x
85
85
+
case .vertical: y
86
86
+
}
87
87
+
}
88
88
+
set {
89
89
+
switch axis {
90
90
+
case .horizontal: x = newValue
91
91
+
case .vertical: y = newValue
92
92
+
}
93
93
+
}
94
94
+
}
95
95
+
}
96
96
+
97
97
+
extension NSSize {
98
98
+
public init(extent: CGFloat, coextent: CGFloat, axis: Axis) {
99
99
+
self =
100
100
+
switch axis {
101
101
+
case .horizontal: NSSize(width: extent, height: coextent)
102
102
+
case .vertical: NSSize(width: coextent, height: extent)
103
103
+
}
104
104
+
}
105
105
+
106
106
+
public subscript(_ axis: Axis) -> CGFloat {
107
107
+
get {
108
108
+
switch axis {
109
109
+
case .horizontal: width
110
110
+
case .vertical: height
111
111
+
}
112
112
+
}
113
113
+
set {
114
114
+
switch axis {
115
115
+
case .horizontal: width = newValue
116
116
+
case .vertical: height = newValue
117
117
+
}
118
118
+
}
119
119
+
}
120
120
+
}
121
121
+
122
122
+
extension NSRectEdge {
123
123
+
public func spaceAvailable(from parentRect: CGRect, in screenRect: CGRect) -> CGFloat {
124
124
+
switch self {
125
125
+
case .minX: parentRect.minX - screenRect.minX
126
126
+
case .maxX: screenRect.maxX - parentRect.maxX
127
127
+
case .minY: parentRect.minY - screenRect.minY
128
128
+
case .maxY: screenRect.maxY - parentRect.maxY
129
129
+
@unknown default: 0
130
130
+
}
131
131
+
}
132
132
+
}
133
133
+
134
134
+
extension CGFloat {
135
135
+
public mutating func clamp(lowerBound: Self?, upperBound: Self?) {
136
136
+
if let lowerBound {
137
137
+
self = CGFloat.maximum(self, lowerBound)
138
138
+
}
139
139
+
if let upperBound {
140
140
+
self = CGFloat.minimum(self, upperBound)
141
141
+
}
142
142
+
}
143
143
+
144
144
+
public func clamped(lowerBound: Self?, upperBound: Self?) -> Self {
145
145
+
var copy = self
146
146
+
copy.clamp(lowerBound: lowerBound, upperBound: upperBound)
147
147
+
return copy
148
148
+
}
149
149
+
}
150
150
+
151
151
+
extension NSEdgeInsets {
152
152
+
public subscript(_ edge: NSRectEdge) -> CGFloat? {
153
153
+
get {
154
154
+
switch edge {
155
155
+
case .minX: left
156
156
+
case .maxX: right
157
157
+
case .minY: bottom
158
158
+
case .maxY: top
159
159
+
@unknown default: nil
160
160
+
}
161
161
+
}
162
162
+
set {
163
163
+
guard let newValue else { return }
164
164
+
switch edge {
165
165
+
case .minX: left = newValue
166
166
+
case .maxX: right = newValue
167
167
+
case .minY: bottom = newValue
168
168
+
case .maxY: top = newValue
169
169
+
@unknown default: break
170
170
+
}
171
171
+
}
172
172
+
}
173
173
+
}
+3
-3
Sources/AquaKit/Scroll Views/AquaScrollState.swift
···
1
1
-
import Foundation
2
1
import AppKit
2
2
+
import Foundation
3
3
4
4
public struct AquaScrollState {
5
5
public var scrollPosition: NSPoint
6
6
public var contentSize: NSSize
7
7
public var visibleSize: NSSize
8
8
-
8
8
+
9
9
public init(scrollPosition: NSPoint, contentSize: NSSize, visibleSize: NSSize) {
10
10
self.scrollPosition = scrollPosition
11
11
self.contentSize = contentSize
···
47
47
48
48
49
49
extension NSScroller {
50
50
-
public func adopt(state: AquaScrollState, orientation: Orientation) {
50
50
+
public func adopt(state: AquaScrollState, orientation: Axis) {
51
51
let maxOffset = state.maxOffset
52
52
let effectiveContentSize = state.effectiveContentSize
53
53
knobProportion = min(max(state.visibleSize[orientation] / effectiveContentSize[orientation], 0.0), 1.0)
+7
-7
Sources/AquaKit/Scroll Views/AquaScroller.swift
···
2
2
import AquaKit
3
3
4
4
public class AquaScroller: NSScroller {
5
5
-
public let orientation: Orientation
5
5
+
public let orientation: Axis
6
6
7
7
public override var floatValue: Float {
8
8
didSet { setNeedsDisplay(bounds) }
···
12
12
didSet { setNeedsDisplay(bounds) }
13
13
}
14
14
15
15
-
public required init(orientation: Orientation) {
15
15
+
public required init(orientation: Axis) {
16
16
self.orientation = orientation
17
17
super.init(frame: .zero)
18
18
addSubview(WindowStateSentinelView())
···
221
221
222
222
let shineGradient = NSGradient(colors: [
223
223
.white,
224
224
-
.white.withAlphaComponent(isInKeyWindow ? 0.4 : 0.8),
224
224
+
.white.withAlphaComponent(isInKeyWindow ? 0.4 : 0.8)
225
225
])!
226
226
227
227
let shineRect = axis.rect(
···
243
243
let glowGradient = NSGradient(colors: [
244
244
.clear,
245
245
.clear,
246
246
-
baseColor.highlight(withLevel: 0.6)!,
246
246
+
baseColor.highlight(withLevel: 0.6)!
247
247
])!
248
248
249
249
let glowPath = NSBezierPath(
···
263
263
let glowGradient = NSGradient(colors: [
264
264
baseColor.highlight(withLevel: 0.6)!,
265
265
.clear,
266
266
-
.clear,
266
266
+
.clear
267
267
])!
268
268
269
269
let glowPath = NSBezierPath(
···
309
309
}
310
310
}
311
311
312
312
-
extension Orientation {
312
312
+
extension Axis {
313
313
/// The affine transform that maps between screen space and canonical (vertical)
314
314
/// coordinate space. For vertical this is the identity; for horizontal it swaps
315
315
/// the x and y axes. This transform is its own inverse.
···
355
355
let b = bounds.applying(transform)
356
356
return [
357
357
(NSPoint(x: b.minX, y: b.minY).applying(transform), NSPoint(x: b.minX, y: b.maxY).applying(transform)),
358
358
-
(NSPoint(x: b.maxX, y: b.minY).applying(transform), NSPoint(x: b.maxX, y: b.maxY).applying(transform)),
358
358
+
(NSPoint(x: b.maxX, y: b.minY).applying(transform), NSPoint(x: b.maxX, y: b.maxY).applying(transform))
359
359
]
360
360
}
361
361
}
+1
-1
Sources/AquaKit/Web Views/AquaWebViewController.swift
···
90
90
extension WKWebView {
91
91
fileprivate func scroll(
92
92
toFraction fraction: Double,
93
93
-
orientation: Orientation,
93
93
+
orientation: Axis,
94
94
completionHandler: (@MainActor (Any?, (any Error)?) -> Void)? = nil
95
95
) {
96
96
let script =