Skip to content

Commit de936a5

Browse files
BohdanBohdan
authored andcommitted
Fixed Slide and Scale animation appear
1 parent 01ef22d commit de936a5

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

LanguageFlag/Animations/Basic/ScaleAnimation.swift

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,77 @@ class ScaleAnimation: BaseWindowAnimation, WindowAnimation {
55

66
// MARK: - WindowAnimation
77
func animateIn(window: NSWindow, duration: TimeInterval, completion: (() -> Void)?) {
8+
guard let layer = prepareLayer(from: window) else {
9+
completion?()
10+
return
11+
}
812
setupWindow(window)
9-
10-
let originalFrame = window.frame
11-
let centerX = originalFrame.midX
12-
let centerY = originalFrame.midY
13-
14-
// Start with small scale
15-
let startScale: CGFloat = 0.3
16-
var startFrame = originalFrame
17-
startFrame.size.width *= startScale
18-
startFrame.size.height *= startScale
19-
startFrame.origin.x = centerX - startFrame.width / 2
20-
startFrame.origin.y = centerY - startFrame.height / 2
21-
22-
window.setFrame(startFrame, display: false, animate: false)
23-
24-
NSAnimationContext.runAnimationGroup({ context in
13+
14+
// Hide window initially so we can fade it in
15+
window.alphaValue = 0
16+
17+
let oldAnchor = layer.anchorPoint
18+
let oldPosition = layer.position
19+
20+
layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
21+
layer.position = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
22+
23+
let scaleAnim = createAnimation(keyPath: "transform.scale",
24+
from: 0.3,
25+
to: 1.0,
26+
duration: duration)
27+
scaleAnim.fillMode = .forwards
28+
scaleAnim.isRemovedOnCompletion = false
29+
30+
scaleAnim.delegate = AnimationCompletionDelegate { [weak layer] finished in
31+
layer?.transform = CATransform3DIdentity
32+
layer?.anchorPoint = oldAnchor
33+
layer?.position = oldPosition
34+
completion?()
35+
}
36+
37+
layer.add(scaleAnim, forKey: "scaleIn")
38+
39+
NSAnimationContext.runAnimationGroup { context in
2540
context.duration = duration
2641
context.timingFunction = AnimationTiming.easeOut
27-
window.animator().setFrame(originalFrame, display: true)
2842
window.animator().alphaValue = CGFloat(UserPreferences.shared.opacity)
29-
}, completionHandler: completion)
43+
}
3044
}
31-
45+
3246
func animateOut(window: NSWindow, duration: TimeInterval, completion: (() -> Void)?) {
33-
let currentFrame = window.frame
34-
let centerX = currentFrame.midX
35-
let centerY = currentFrame.midY
36-
37-
// End with small scale
38-
let endScale: CGFloat = 0.3
39-
var endFrame = currentFrame
40-
endFrame.size.width *= endScale
41-
endFrame.size.height *= endScale
42-
endFrame.origin.x = centerX - endFrame.width / 2
43-
endFrame.origin.y = centerY - endFrame.height / 2
44-
45-
NSAnimationContext.runAnimationGroup({ context in
47+
guard let layer = window.contentView?.layer else {
48+
completion?()
49+
return
50+
}
51+
52+
let oldAnchor = layer.anchorPoint
53+
let oldPosition = layer.position
54+
55+
layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
56+
layer.position = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
57+
58+
let scaleAnim = createAnimation(keyPath: "transform.scale",
59+
from: 1.0,
60+
to: 0.3,
61+
duration: duration,
62+
timing: CAMediaTimingFunction(name: .easeIn))
63+
scaleAnim.fillMode = .forwards
64+
scaleAnim.isRemovedOnCompletion = false
65+
66+
scaleAnim.delegate = AnimationCompletionDelegate { [weak layer] finished in
67+
layer?.transform = CATransform3DIdentity
68+
layer?.anchorPoint = oldAnchor
69+
layer?.position = oldPosition
70+
completion?()
71+
}
72+
73+
layer.add(scaleAnim, forKey: "scaleOut")
74+
75+
NSAnimationContext.runAnimationGroup { context in
4676
context.duration = duration
4777
context.timingFunction = AnimationTiming.easeIn
48-
window.animator().setFrame(endFrame, display: true)
4978
window.animator().alphaValue = 0
50-
}, completionHandler: completion)
79+
}
5180
}
5281
}

LanguageFlag/Animations/Core/BaseWindowAnimation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class BaseWindowAnimation {
3535
// Remove VHS overlays if any
3636
layer.sublayers?.filter { $0.name == "vhsScanline" || $0.name == "vhsNoise" }
3737
.forEach { $0.removeFromSuperlayer() }
38+
39+
// Fix: Reset contentView alpha & layer opacity that might be permanently stuck at 0 from FadeAnimation
40+
contentView.alphaValue = 1.0
41+
layer.opacity = 1.0
3842
}
3943

4044
window.orderFrontRegardless()

LanguageFlag/Models/UserPreferences.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ final class UserPreferences: ObservableObject {
167167
self.showShortcuts = false
168168
self.showInMenuBar = false
169169
self.resetAnimationOnChange = true
170-
self.showCapsLockIndicator = false
170+
self.showCapsLockIndicator = true
171171
self.bypassClick = true
172172
self.displayPosition = .bottomCenter
173173
self.windowSize = .medium
@@ -185,7 +185,7 @@ final class UserPreferences: ObservableObject {
185185
self.showShortcuts = false
186186
self.showInMenuBar = false
187187
self.resetAnimationOnChange = true
188-
self.showCapsLockIndicator = false
188+
self.showCapsLockIndicator = true
189189
self.bypassClick = true
190190
self.displayPosition = .bottomCenter
191191
self.windowSize = .medium
@@ -202,7 +202,7 @@ final class UserPreferences: ObservableObject {
202202
self.showShortcuts = defaults.object(forKey: Keys.showShortcuts) as? Bool ?? false
203203
self.showInMenuBar = defaults.object(forKey: Keys.showInMenuBar) as? Bool ?? false
204204
self.resetAnimationOnChange = defaults.object(forKey: Keys.resetAnimationOnChange) as? Bool ?? true
205-
self.showCapsLockIndicator = defaults.object(forKey: Keys.showCapsLockIndicator) as? Bool ?? false
205+
self.showCapsLockIndicator = defaults.object(forKey: Keys.showCapsLockIndicator) as? Bool ?? true
206206
self.bypassClick = defaults.object(forKey: Keys.bypassClick) as? Bool ?? true
207207

208208
// Decode complex types
@@ -244,7 +244,7 @@ final class UserPreferences: ObservableObject {
244244
showShortcuts = false
245245
showInMenuBar = false
246246
resetAnimationOnChange = true
247-
showCapsLockIndicator = false
247+
showCapsLockIndicator = true
248248
bypassClick = true
249249
}
250250
}

0 commit comments

Comments
 (0)