-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fire button and shield button animations #476
Changes from 4 commits
e0aea05
6afdb35
568ebe7
27a53df
320bd0f
b3af925
ffe0a36
e4a732c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// AnimationView.swift | ||
// | ||
// Copyright © 2022 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Lottie | ||
|
||
extension AnimationView { | ||
|
||
convenience init?(named animationName: String, imageProvider: AnimationImageProvider? = nil) { | ||
guard let animation = Animation.named(animationName, animationCache: LottieAnimationCache.shared) else { | ||
return nil | ||
} | ||
|
||
self.init(animation: animation, imageProvider: imageProvider) | ||
identifier = NSUserInterfaceItemIdentifier(rawValue: animationName) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// | ||
// MouseOverAnimationButton.swift | ||
// | ||
// Copyright © 2022 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Lottie | ||
import Combine | ||
|
||
final class MouseOverAnimationButton: AddressBarButton { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class encapsulates the mouse over animation. Developer only sets |
||
|
||
// MARK: - Events | ||
|
||
override func awakeFromNib() { | ||
super.awakeFromNib() | ||
|
||
subscribeToIsMouseOver() | ||
subscribeToEffectiveAppearance() | ||
} | ||
|
||
private var isMouseOverCancellable: AnyCancellable? | ||
|
||
private func subscribeToIsMouseOver() { | ||
isMouseOverCancellable = $isMouseOver | ||
.dropFirst() | ||
.sink { [weak self] isMouseOver in | ||
if isMouseOver { | ||
self?.animate() | ||
} else { | ||
self?.stopAnimation() | ||
} | ||
} | ||
} | ||
|
||
private var effectiveAppearanceCancellable: AnyCancellable? | ||
|
||
private func subscribeToEffectiveAppearance() { | ||
effectiveAppearanceCancellable = NSApp.publisher(for: \.effectiveAppearance) | ||
.dropFirst() | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] _ in | ||
self?.updateAnimationView() | ||
} | ||
} | ||
|
||
// MARK: - Loading & Updating of Animation Views | ||
|
||
struct AnimationNames: Equatable { | ||
let aqua: String | ||
let dark: String | ||
} | ||
|
||
var animationNames: AnimationNames? { | ||
didSet { | ||
if oldValue != animationNames { | ||
loadAnimationViews() | ||
updateAnimationView() | ||
} | ||
} | ||
} | ||
|
||
struct AnimationViews { | ||
let aqua: AnimationView | ||
let dark: AnimationView | ||
} | ||
|
||
private var animationViewCache: AnimationViews? | ||
|
||
private func loadAnimationViews() { | ||
guard let animationNames = animationNames, | ||
let aquaAnimationView = AnimationView(named: animationNames.aqua), | ||
let darkAnimationView = AnimationView(named: animationNames.dark) else { | ||
assertionFailure("Missing animation names or animation files in the bundle") | ||
return | ||
} | ||
|
||
animationViewCache = AnimationViews( | ||
aqua: aquaAnimationView, | ||
dark: darkAnimationView) | ||
} | ||
|
||
private var currentAnimationView: AnimationView? | ||
|
||
private func updateAnimationView() { | ||
guard let animationViewCache = animationViewCache else { | ||
assertionFailure("No animations loaded") | ||
return | ||
} | ||
|
||
let isAquaMode = NSApp.effectiveAppearance.name == NSAppearance.Name.aqua | ||
let newAnimationView: AnimationView | ||
// Animation view causes problems in tests | ||
if AppDelegate.isRunningTests { | ||
newAnimationView = AnimationView() | ||
} else { | ||
newAnimationView = isAquaMode ? animationViewCache.aqua : animationViewCache.dark | ||
} | ||
|
||
guard currentAnimationView?.identifier != newAnimationView.identifier else { | ||
// No need to update | ||
return | ||
} | ||
|
||
currentAnimationView?.removeFromSuperview() | ||
currentAnimationView = newAnimationView | ||
|
||
newAnimationView.isHidden = true | ||
addAndLayout(newAnimationView) | ||
} | ||
|
||
// MARK: - Animating | ||
|
||
@Published var isAnimationViewVisible = false | ||
|
||
override var image: NSImage? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case something sets the image while animation is in progress |
||
get { | ||
return super.image | ||
} | ||
|
||
set { | ||
if isAnimationViewVisible { | ||
imageCache = newValue | ||
} else { | ||
super.image = newValue | ||
} | ||
} | ||
} | ||
|
||
var imageCache: NSImage? | ||
|
||
private func hideImage() { | ||
imageCache = image | ||
super.image = nil | ||
} | ||
|
||
private func showImage() { | ||
if let imageCache = imageCache { | ||
NSAppearance.withAppAppearance { | ||
image = imageCache | ||
} | ||
} | ||
} | ||
|
||
private func hideAnimation() { | ||
currentAnimationView?.isHidden = true | ||
isAnimationViewVisible = false | ||
} | ||
|
||
private func showAnimation() { | ||
currentAnimationView?.isHidden = false | ||
isAnimationViewVisible = true | ||
} | ||
|
||
private func animate() { | ||
hideImage() | ||
showAnimation() | ||
currentAnimationView?.play() | ||
} | ||
|
||
private func stopAnimation() { | ||
hideAnimation() | ||
showImage() | ||
currentAnimationView?.stop() | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved animations to their own subfolder. My apology for the harder readability of this PR because of this