mycode/myApp/HaruDanim/Widgets/ActionRunWidget.swift
songyc macbook bc6cd010b7 fix(widgets): 리퀴드 글라스 테마 시인성 개선 및 틴트/클리어 렌더링 모드 대응
- 글라스 테마: 틴트 셀 불투명도 0.5→0.72, 테두리 강화로 흰 텍스트 대비 확보
- 글라스 테마 전용 텍스트 그림자(glassTextShadow) 추가 — 홈 위젯 5종 셀 콘텐츠에 적용
- widgetRenderingMode != .fullColor(홈 화면 틴트/클리어)일 때 커스텀 배경·강제
  컬러 스킴을 끄고 시스템 바이브런트 렌더링에 맡기도록 분기
- 잠금화면 위젯: AccessoryWidgetBackground + widgetAccentable + .primary 전경색으로
  비브런트/악센트 렌더링에서 시인성 확보
- 미리보기 하네스: 글라스 테마가 시스템 컬러 스킴을 따르도록 수정

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013yKDMhuF39GVYy3FMcGHh7
2026-07-10 21:18:43 +09:00

219 lines
8.2 KiB
Swift

//
// ActionRunWidget.swift
// Haru_DanimWidgets
//
// (CLAUDE.md §8.2 A / A / A)
// Interactive Widgets: ( / +1)
//
import SwiftUI
import WidgetKit
import AppIntents
// MARK: -
struct ActionRunConfigIntent: WidgetConfigurationIntent {
static let title: LocalizedStringResource = "행동 실행 위젯"
static let description = IntentDescription("표시할 행동과 누적 기간을 선택하세요.")
@Parameter(title: "표시 기간", default: .day)
var period: SpanOption
@Parameter(title: "위젯 테마", default: .matchApp)
var theme: WidgetThemeOption
@Parameter(title: "행동 1")
var action1: ActionEntity?
@Parameter(title: "행동 2")
var action2: ActionEntity?
@Parameter(title: "행동 3")
var action3: ActionEntity?
@Parameter(title: "행동 4")
var action4: ActionEntity?
}
// MARK: -
struct ActionRunEntry: TimelineEntry {
let date: Date
let locked: Bool
let theme: WidgetThemeOption
let periodLabel: String
let cells: [ActionCellSnapshot]
}
struct ActionRunProvider: AppIntentTimelineProvider {
@MainActor
static func makeEntry(_ configuration: ActionRunConfigIntent) -> ActionRunEntry {
guard WidgetStore.isUnlocked else {
return ActionRunEntry(date: .now, locked: true, theme: configuration.theme, periodLabel: "", cells: [])
}
let span = configuration.period.statSpan
let chosen = [configuration.action1, configuration.action2, configuration.action3, configuration.action4]
.compactMap { $0 }
.compactMap { WidgetStore.action($0.id) }
let actions = chosen.isEmpty ? WidgetStore.defaultActions(4) : chosen
return ActionRunEntry(
date: .now,
locked: false,
theme: configuration.theme,
periodLabel: configuration.period.label,
cells: actions.map { ActionCellSnapshot.make(action: $0, span: span) }
)
}
func placeholder(in context: Context) -> ActionRunEntry {
ActionRunEntry(date: .now, locked: false, theme: .matchApp, periodLabel: "오늘", cells: [])
}
@MainActor
func snapshot(for configuration: ActionRunConfigIntent, in context: Context) async -> ActionRunEntry {
Self.makeEntry(configuration)
}
@MainActor
func timeline(for configuration: ActionRunConfigIntent, in context: Context) async -> Timeline<ActionRunEntry> {
// / reloadAllTimelines ,
// 15
Timeline(entries: [Self.makeEntry(configuration)], policy: .after(.now.addingTimeInterval(15 * 60)))
}
}
// MARK: -
struct ActionRunCellView: View {
let cell: ActionCellSnapshot
let periodLabel: String
var compact = false
/// ()
var fullBleed = false
var body: some View {
Button(intent: RunActionIntent(action: cell.entity)) {
VStack(alignment: .leading, spacing: compact ? 2 : 4) {
HStack {
Image(systemName: cell.symbolName)
.font(.system(size: compact ? 14 : 18, weight: .semibold))
Spacer()
if cell.isRunning {
Image(systemName: "record.circle")
.font(.system(size: compact ? 10 : 12, weight: .bold))
.foregroundStyle(AppTheme.yellow)
}
}
Spacer(minLength: 0)
Text(cell.name)
.font(compact ? .caption2.weight(.semibold) : .caption.weight(.semibold))
.lineLimit(1)
Group {
if cell.isCount {
Text("\(periodLabel) \(Int(cell.value))")
} else if let base = cell.tickingBase {
// :
Text(base, style: .timer)
} else {
Text("\(periodLabel) \(Format.durationShort(cell.value))")
}
}
.font(compact ? .caption2.monospacedDigit() : .caption.monospacedDigit())
.opacity(0.85)
.lineLimit(1)
}
.foregroundStyle(.white)
.glassTextShadow()
.padding(fullBleed ? 14 : (compact ? 8 : 12))
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.background(WidgetTintedCellBackground(color: cell.color, fullBleed: fullBleed))
.overlay {
if cell.isRunning {
if fullBleed {
ContainerRelativeShape()
.strokeBorder(AppTheme.yellow, lineWidth: 2)
} else {
RoundedRectangle(cornerRadius: 14, style: .continuous)
.strokeBorder(AppTheme.yellow, lineWidth: 2)
}
}
}
}
.buttonStyle(.plain)
}
}
struct ActionRunWidgetView: View {
@Environment(\.widgetFamily) private var envFamily
/// DEBUG
var previewFamily: WidgetFamily? = nil
private var family: WidgetFamily { previewFamily ?? envFamily }
let entry: ActionRunEntry
var body: some View {
Group {
if entry.locked {
WidgetLockedView()
.padding(12)
} else if entry.cells.isEmpty {
Text("앱에서 행동을 만들면 여기서 실행할 수 있어요.")
.font(.caption2)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
.padding(12)
} else {
switch family {
case .systemMedium:
HStack(spacing: 8) {
ForEach(entry.cells.prefix(3)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel, compact: true)
}
}
.padding(10)
case .systemLarge:
let cells = Array(entry.cells.prefix(4))
VStack(spacing: 8) {
HStack(spacing: 8) {
ForEach(cells.prefix(2)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel)
}
}
if cells.count > 2 {
HStack(spacing: 8) {
ForEach(cells.dropFirst(2)) { cell in
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel)
}
}
}
}
.padding(10)
default:
// : ()
if let cell = entry.cells.first {
ActionRunCellView(cell: cell, periodLabel: entry.periodLabel, fullBleed: true)
}
}
}
}
.widgetTheme(entry.theme)
}
}
// MARK: -
struct ActionRunWidget: Widget {
var body: some WidgetConfiguration {
AppIntentConfiguration(
kind: "HaruActionRunWidget",
intent: ActionRunConfigIntent.self,
provider: ActionRunProvider()
) { entry in
ActionRunWidgetView(entry: entry)
}
.configurationDisplayName("행동 실행")
.description("행동을 위젯에서 바로 실행하고 누적값을 확인해요. (프리미엄)")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
.contentMarginsDisabled()
}
}