mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Tweaks discovered when using Compound overrides. (#4456)
- Fixes a bug where gradients wouldn't use the latest tokens after first use. - Makes the bloom aware of colour overrides (and discovers a bug with dark mode). - Decouples HomeScreenCell from the HomeScreenViewModel.Context (so it can be previewed with colour overrides).
This commit is contained in:
@@ -17,10 +17,12 @@ extension View {
|
||||
}
|
||||
|
||||
private struct BloomModifier: ViewModifier {
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
@State private var standardAppearance = UINavigationBarAppearance()
|
||||
@State private var scrollEdgeAppearance = UINavigationBarAppearance()
|
||||
|
||||
@State private var bloomGradientImage: UIImage?
|
||||
@State private var bloom = Bloom()
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
content
|
||||
@@ -28,33 +30,39 @@ private struct BloomModifier: ViewModifier {
|
||||
}
|
||||
|
||||
private func configureBloom(controller: UIViewController) {
|
||||
guard controller.navigationItem.standardAppearance != standardAppearance,
|
||||
controller.navigationItem.scrollEdgeAppearance != scrollEdgeAppearance else {
|
||||
if controller.navigationItem.standardAppearance == standardAppearance,
|
||||
controller.navigationItem.scrollEdgeAppearance == scrollEdgeAppearance,
|
||||
canUse(bloom) {
|
||||
return
|
||||
}
|
||||
|
||||
let image = makeBloomImage()
|
||||
let bloom = makeBloom()
|
||||
|
||||
standardAppearance.configureWithDefaultBackground()
|
||||
standardAppearance.backgroundImage = image
|
||||
standardAppearance.backgroundImage = bloom.image
|
||||
standardAppearance.backgroundImageContentMode = .scaleToFill
|
||||
controller.navigationItem.standardAppearance = standardAppearance
|
||||
|
||||
scrollEdgeAppearance.configureWithTransparentBackground()
|
||||
scrollEdgeAppearance.backgroundImage = image
|
||||
scrollEdgeAppearance.backgroundImage = bloom.image
|
||||
scrollEdgeAppearance.backgroundImageContentMode = .scaleToFill
|
||||
scrollEdgeAppearance.backgroundColor = .compound.bgCanvasDefault
|
||||
controller.navigationItem.scrollEdgeAppearance = scrollEdgeAppearance
|
||||
}
|
||||
|
||||
private func makeBloomImage() -> UIImage? {
|
||||
if let bloomGradientImage {
|
||||
return bloomGradientImage
|
||||
private func makeBloom() -> Bloom {
|
||||
if bloom.image != nil, canUse(bloom) {
|
||||
return bloom
|
||||
}
|
||||
|
||||
let newImage = ImageRenderer(content: bloomGradient).uiImage
|
||||
Task { bloomGradientImage = newImage }
|
||||
return newImage
|
||||
// There's a bug somewhere when rendering in dark mode (which we've mistakenly not been doing)
|
||||
// which results in the first 5 stops not having any alpha, only the last one…
|
||||
let newImage = ImageRenderer(content: bloomGradient /* .colorScheme(colorScheme) */ ).uiImage
|
||||
|
||||
bloom.image = newImage
|
||||
bloom.colorScheme = colorScheme
|
||||
bloom.baseColor = .compound.gradientSubtleStop1
|
||||
return bloom
|
||||
}
|
||||
|
||||
private var bloomGradient: some View {
|
||||
@@ -64,4 +72,18 @@ private struct BloomModifier: ViewModifier {
|
||||
.ignoresSafeArea(edges: .all)
|
||||
.frame(width: 256, height: 256)
|
||||
}
|
||||
|
||||
private func canUse(_ bloom: Bloom) -> Bool {
|
||||
// Don't check for a nil image in here, there's no point re-rendering over and over if the render fails.
|
||||
bloom.colorScheme == colorScheme && bloom.baseColor == .compound.gradientSubtleStop1
|
||||
}
|
||||
|
||||
// This is a class to avoid a "Modifying state during view update" warning when storing
|
||||
// the result on the same run-loop - we want to avoid dispatching that to the next loop as
|
||||
// that can result in further (unnecessary) renders being made.
|
||||
class Bloom {
|
||||
var image: UIImage?
|
||||
var colorScheme: ColorScheme?
|
||||
var baseColor: Color?
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user