Forked from ABashkirova/BackdeployedWidgetAccentedRenderingMode.swift
Created
October 15, 2024 08:03
-
-
Save cute/ac7d03ae5a9d0429de6dcb6a3faa9f58 to your computer and use it in GitHub Desktop.
iOS 18 Widgets: Notes about updating the widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import WidgetKit | |
@available(iOS, deprecated: 18.0, message: "use WidgetAccentedRenderingMode instead") | |
enum BackdeployedWidgetAccentedRenderingMode { | |
case accented | |
case accentedDesaturated | |
case desaturated | |
case fullColor | |
@available(iOS 18.0, *) | |
func toRenderingMode() -> WidgetAccentedRenderingMode { | |
switch self { | |
case .accented: .accented | |
case .accentedDesaturated: .accentedDesaturated | |
case .desaturated: .desaturated | |
case .fullColor: .fullColor | |
} | |
} | |
} | |
extension Image { | |
@available(iOS, deprecated: 18.0, message: "use widgetAccentedRenderingMode instead") | |
@ViewBuilder | |
func backdeployedWidgetAccentedRenderingMode( | |
_ mode: BackdeployedWidgetAccentedRenderingMode | |
) -> some View { | |
if #available(iOS 18.0, *) { | |
self.widgetAccentedRenderingMode(mode.toRenderingMode()) | |
} | |
else { | |
self | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you want to get black text on a white background, | |
// there is only one way out - to cut it out of the background | |
Circle() | |
.fill(.white) | |
.frame(width: 20, height: 20, alignment: .center) | |
.mask( | |
ZStack { | |
Circle() | |
.fill(Color.white) | |
Text(model.formattedDayNumber) | |
.font(.system(size: 9, weight: .bold)) | |
.foregroundColor(.black) | |
} | |
.compositingGroup() | |
.luminanceToAlpha() | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Circle() | |
.fill(model.isCurrent ? .white : .clear) | |
// No need for widgetAccentable(false) as it's not accentable by default | |
Text("21") | |
.font(.system(size: 15)) | |
.foregroundColor(model.isCurrent ? .black : .white) | |
.widgetAccentable(model.isCurrent) // Accent only the current day |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension EnvironmentValues { | |
@available(iOS, deprecated: 16.0, message: "use `widgetRenderingMode` instead") | |
var isFullColorWidget: Bool { | |
get { | |
if #available(iOS 16.0, *) { | |
widgetRenderingMode == .fullColor | |
} | |
else { | |
self[FullColorWidgetKey.self] | |
} | |
} | |
} | |
} | |
private enum FullColorWidgetKey: EnvironmentKey { | |
static let defaultValue = true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image("userAvatar") | |
.resizable() | |
.widgetAccentedRenderingMode(.fullColor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PeridoDayView: View { | |
@Environment(\.widgetRenderingMode) | |
private var widgetRenderingMode | |
var body: some View { | |
DayRangeView() | |
.foregroundStyle( | |
widgetRenderingMode == .fullColor ? .periodColor : .white60Color | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
@available(iOS, deprecated: 16.0, message: "use widgetAccentable(_:)` instead") | |
@ViewBuilder | |
func backdeployedWidgetAccentable( | |
_ accentable: Bool = true | |
) -> some View { | |
if #available(iOS 16.0, *) { | |
self | |
.widgetAccentable(accentable) | |
} | |
else { | |
self | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment