blob: 11e5423d9d98eed8693aa118122d83dcf21042ea [file] [log] [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import Foundation
import SwiftUI
import WidgetKit
struct DinoGameWidget: Widget {
// Changing |kind| or deleting this widget will cause all installed instances of this widget to
// stop updating and show the placeholder state.
let kind: String = "DinoGameWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
DinoGameWidgetEntryView(entry: entry)
}
.configurationDisplayName(
Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_DISPLAY_NAME")
)
.description(Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_DESCRIPTION"))
.supportedFamilies([.systemSmall])
.crDisfavoredLocations()
.crContentMarginsDisabled()
.crContainerBackgroundRemovable(false)
}
}
#if IOS_ENABLE_WIDGETS_FOR_MIM
@available(iOS 17, *)
struct DinoGameWidgetConfigurable: Widget {
// Changing `kind` or deleting this widget will cause all installed instances of this widget to
// stop updating and show the placeholder state.
let kind: String = "DinoGameWidget"
var body: some WidgetConfiguration {
AppIntentConfiguration(
kind: kind, intent: SelectAccountIntent.self, provider: ConfigurableProvider()
) { entry in
DinoGameWidgetEntryView(entry: entry)
}
.configurationDisplayName(
Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_DISPLAY_NAME")
)
.description(Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_DESCRIPTION"))
.supportedFamilies([.systemSmall])
.crDisfavoredLocations()
.crContentMarginsDisabled()
.crContainerBackgroundRemovable(false)
}
}
#endif
struct DinoGameWidgetEntryView: View {
let background = "widget_dino_background"
let backgroundPlaceholder = "widget_dino_background_placeholder"
var entry: ConfigureWidgetEntry
@Environment(\.redactionReasons) var redactionReasons
var body: some View {
// The account to display was deleted (entry.deleted can only be true if
// IOS_ENABLE_WIDGETS_FOR_MIM is enabled).
if entry.deleted && !entry.isPreview {
SmallWidgetDeletedAccountView()
} else {
// We wrap this widget in a link on top of using `widgetUrl` so that the voice over will treat
// the widget as one tap target. Without the wrapping, voice over treats the content within
// the widget as multiple tap targets.
Link(destination: WidgetConstants.DinoGameWidget.url) {
ZStack {
Image(redactionReasons.isEmpty ? background : backgroundPlaceholder)
.resizable()
.unredacted()
VStack(alignment: .leading, spacing: 0) {
Spacer()
.frame(minWidth: 0, maxWidth: .infinity)
HStack {
Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_TITLE")
.foregroundColor(Color("widget_text_color"))
.fontWeight(.semibold)
.font(.subheadline)
Spacer()
#if IOS_ENABLE_WIDGETS_FOR_MIM
AvatarForDinoGame(entry: entry)
#endif
}
.padding([.leading, .bottom], 16)
}
}
}
.widgetURL(destinationURL(url: WidgetConstants.DinoGameWidget.url, gaia: entry.gaiaID))
.accessibility(
label: Text("IDS_IOS_WIDGET_KIT_EXTENSION_GAME_A11Y_LABEL")
)
// Background is not used as the image takes the whole widget.
.crContainerBackground(Color("widget_background_color").unredacted())
}
}
}
#if IOS_ENABLE_WIDGETS_FOR_MIM
struct AvatarForDinoGame: View {
var entry: ConfigureWidgetEntry
var body: some View {
if entry.isPreview {
Circle()
.foregroundColor(Color("widget_text_color"))
.opacity(0.2)
.frame(width: 25, height: 25)
.padding(.trailing, 16)
} else if let avatar = entry.avatar {
avatar
.resizable()
.clipShape(Circle())
.unredacted()
.scaledToFill()
.frame(width: 25, height: 25)
.padding(.trailing, 16)
}
}
}
#endif