-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ConditionLayer.swift
305 lines (232 loc) · 11.1 KB
/
ConditionLayer.swift
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// ConditionLayer.swift
// Aerial
//
// Created by Guillaume Louel on 17/04/2020.
// Copyright © 2020 Guillaume Louel. All rights reserved.
//
import Foundation
import AVKit
// Vertically centered CATextLayer
class CAVCTextLayer: CATextLayer {
// REF: http://lists.apple.com/archives/quartz-dev/2008/Aug/msg00016.html
// CREDIT: David Hoerl - https://github.com/dhoerl
// USAGE: To fix the vertical alignment issue that currently exists within the CATextLayer class. Change made to the yDiff calculation.
override func draw(in context: CGContext) {
let height = self.bounds.size.height
let fontSize = self.fontSize
let yDiff = (height-fontSize)/2 - fontSize/10
context.saveGState()
context.translateBy(x: 0, y: -yDiff)
super.draw(in: context)
context.restoreGState()
}
}
class ConditionLayer: CALayer {
var condition: OWeather?
init(condition: OWeather, scale: CGFloat) {
self.condition = condition
super.init()
// backgroundColor = .init(gray: 0.2, alpha: 0.2)
contentsScale = scale
// First we make the temperatures block (accurate and feels like)
let tempBlock = makeTemperatureBlock()
let feelsBlock = makeFeelsLikeBlock()
var cityNameBlock: CALayer
if PrefsInfo.weather.showCity {
cityNameBlock = makeCityNameBlock()
} else {
cityNameBlock = CALayer()
}
// We make the symbol a square of the combined height of both blocks
let combinedHeight = tempBlock.frame.height + feelsBlock.frame.height
// Create a symbol that fits the size
let imglayer = ConditionSymbolLayer(weather: condition.weather![0],
dt: condition.dt!,
sunrise: condition.sys!.sunrise,
sunset: condition.sys!.sunset,
size: Int(combinedHeight))
// Add the Wind layer
var windHeight: CGFloat = 0
if PrefsInfo.weather.showWind || PrefsInfo.weather.showHumidity {
windHeight = addWindAndHumidity(x: (imglayer.frame.width + combinedHeight/10 + tempBlock.frame.width) / 2, y: cityNameBlock.frame.height)
}
imglayer.anchorPoint = CGPoint(x: 0, y: 0)
imglayer.position = CGPoint(x: 0, y: windHeight + cityNameBlock.frame.height)
self.addSublayer(imglayer)
frame.size = CGSize(width: imglayer.frame.width + combinedHeight/10 + tempBlock.frame.width,
height: tempBlock.frame.height + feelsBlock.frame.height + windHeight + cityNameBlock.frame.height)
addSublayer(cityNameBlock)
cityNameBlock.anchorPoint = CGPoint(x: 0.5, y: 0)
cityNameBlock.position = CGPoint(x: frame.size.width/2, y: 0)
addSublayer(tempBlock)
tempBlock.anchorPoint = CGPoint(x: 1, y: 1)
tempBlock.position = CGPoint(x: frame.size.width,
y: tempBlock.frame.height + feelsBlock.frame.height + windHeight + cityNameBlock.frame.height)
addSublayer(feelsBlock)
feelsBlock.anchorPoint = CGPoint(x: 0.5, y: 0)
feelsBlock.position = CGPoint(x: imglayer.frame.width + combinedHeight/10 + tempBlock.frame.width/2,
y: windHeight + cityNameBlock.frame.height)
}
override init(layer: Any) {
super.init(layer: layer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func makeCityNameBlock() -> CATextLayer {
let temp = CATextLayer()
temp.isWrapped = true
temp.string = condition!.name
(temp.font, temp.fontSize) = temp.makeFont(name: PrefsInfo.weather.fontName, size: PrefsInfo.weather.fontSize/1.5)
temp.alignmentMode = .center
// ReRect the temperature
let rect = temp.calculateRect(string: temp.string as! String, font: temp.font as! NSFont, maxWidth: 220)
temp.frame = rect
temp.contentsScale = self.contentsScale
return temp
}
func makeTemperatureBlock() -> CATextLayer {
let temp = CAVCTextLayer()
// First we start with the real temperature
// We keep the decimal for now on celcius, this may become optional
if PrefsInfo.weather.degree == .celsius {
temp.string = "\(condition!.main!.temp)°"
} else {
temp.string = "\(Int(condition!.main!.temp))°"
}
(temp.font, temp.fontSize) = temp.makeFont(name: PrefsInfo.weather.fontName, size: PrefsInfo.weather.fontSize)
// ReRect the temperature
let rect = temp.calculateRect(string: temp.string as! String, font: temp.font as! NSFont)
temp.frame = rect
temp.contentsScale = self.contentsScale
return temp
}
func makeFeelsLikeBlock() -> CATextLayer {
// Make a vertically centered layer for t°
let feel = CAVCTextLayer()
if PrefsInfo.weather.degree == .celsius {
feel.string = "(\(condition!.main!.feelsLike)°)"
} else {
feel.string = "(\(Int(condition!.main!.feelsLike))°)"
}
feel.contentsScale = self.contentsScale
(feel.font, feel.fontSize) = feel.makeFont(name: PrefsInfo.weather.fontName, size: PrefsInfo.weather.fontSize/2.2)
// ReRect the temperature
let rect2 = feel.calculateRect(string: feel.string as! String, font: feel.font as! NSFont)
feel.frame = rect2
return feel
}
// swiftlint:disable:next identifier_name
func addWindAndHumidity(x: CGFloat, y: CGFloat) -> CGFloat {
// We need to make sure we have the data, and the options are selected
var addWind = false, addHumidity = false
let wind = condition?.wind
let humidity = condition?.main?.humidity
if PrefsInfo.weather.showWind && wind != nil {
addWind = true
}
if PrefsInfo.weather.showHumidity && humidity != nil {
addHumidity = true
}
// If we shouldn't display/should and don't have data
if !addWind && !addHumidity {
return 0
}
// Ughhhhh, this code is so ugly
var windBlock: CALayer?
var humidityBlock: CALayer?
if addWind {
windBlock = makeWindBlock(wind: wind!)
// windBlock!.anchorPoint = CGPoint(x: 0, y: 0)
}
if addHumidity {
humidityBlock = makeHumidityBlock(humidity: humidity!)
// humidityBlock!.anchorPoint = CGPoint(x: 0, y: 0)
}
// Haaaaaaaa I hate this
if addWind && addHumidity {
let halfTotalWidth = (windBlock!.frame.size.width
+ humidityBlock!.frame.size.width)/2
windBlock!.position = CGPoint(x: x - halfTotalWidth + windBlock!.frame.size.width/2, y: y)
humidityBlock!.position = CGPoint(x: x + halfTotalWidth - humidityBlock!.frame.size.width/2, y: y)
self.addSublayer(windBlock!)
self.addSublayer(humidityBlock!)
return windBlock!.frame.height
} else if addWind {
windBlock!.position = CGPoint(x: x, y: y)
self.addSublayer(windBlock!)
return windBlock!.frame.height
} else if addHumidity {
humidityBlock!.position = CGPoint(x: x, y: y)
self.addSublayer(humidityBlock!)
return humidityBlock!.frame.height
}
// tmp
return 0
}
func makeHumidityBlock(humidity: Double) -> CALayer {
let humidityBlock = CALayer()
// Make a vertically centered layer for t°
let textHumidity = CAVCTextLayer()
textHumidity.string = " \(Int(humidity))%"
// Get something large first
(textHumidity.font, textHumidity.fontSize) = textHumidity.makeFont(name: PrefsInfo.weather.fontName, size: PrefsInfo.weather.fontSize/2.2)
textHumidity.contentsScale = self.contentsScale
// ReRect the temperature
let rect2 = textHumidity.calculateRect(string: textHumidity.string as! String, font: textHumidity.font as! NSFont)
textHumidity.frame = rect2
textHumidity.contentsScale = self.contentsScale
humidityBlock.addSublayer(textHumidity)
let imglayer = Aerial.helper.getSymbolLayer("humidity", size: CGFloat(PrefsInfo.weather.fontSize/2.8))
// We put the temperature at the right of the wind icon
textHumidity.anchorPoint = CGPoint(x: 0, y: 0)
textHumidity.position = CGPoint(x: imglayer.frame.height, y: 0)
imglayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
imglayer.position = CGPoint(x: imglayer.frame.height/2,
y: textHumidity.frame.height/2)
imglayer.contentsScale = self.contentsScale
humidityBlock.frame.size = CGSize(width: textHumidity.frame.width+imglayer.frame.width,
height: max(textHumidity.frame.height, imglayer.frame.height))
humidityBlock.anchorPoint = CGPoint(x: 0.5, y: 0)
humidityBlock.addSublayer(imglayer)
return humidityBlock
}
func makeWindBlock(wind: OWWind) -> CALayer {
let windBlock = CALayer()
// Make a vertically centered layer for t°
let textWind = CAVCTextLayer()
if PrefsInfo.weather.degree == .celsius {
if PrefsInfo.weatherWindMode == .kph {
textWind.string = "\(Int(wind.speed * 3.6)) km/h"
} else {
textWind.string = "\(Int(wind.speed)) m/s"
}
} else {
textWind.string = "\(Int(wind.speed)) mph"
}
// Get something large first
(textWind.font, textWind.fontSize) = textWind.makeFont(name: PrefsInfo.weather.fontName, size: PrefsInfo.weather.fontSize/2.2)
textWind.contentsScale = self.contentsScale
// ReRect the temperature
let rect2 = textWind.calculateRect(string: textWind.string as! String, font: textWind.font as! NSFont)
textWind.frame = rect2
textWind.contentsScale = self.contentsScale
windBlock.addSublayer(textWind)
// Create the wind indicator
let imglayer = WindDirectionLayer(direction: 225, size: CGFloat(PrefsInfo.weather.fontSize/2.8))
textWind.anchorPoint = CGPoint(x: 0, y: 0)
textWind.position = CGPoint(x: imglayer.frame.height, y: 0)
// Rotation is relative to anchorpoint, so it has to be middle
imglayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
imglayer.position = CGPoint(x: imglayer.frame.height/2, y: textWind.frame.height/2)
// Rotation is done here
imglayer.transform = CATransform3DMakeRotation(CGFloat((180 + wind.deg)) / 180.0 * .pi, 0.0, 0.0, -1.0)
imglayer.contentsScale = self.contentsScale
windBlock.frame.size = CGSize(width: textWind.frame.width+imglayer.frame.width,
height: max(textWind.frame.height, imglayer.frame.height))
windBlock.addSublayer(imglayer)
windBlock.anchorPoint = CGPoint(x: 0.5, y: 0)
return windBlock
}
}