import WidgetKit import SwiftUI import AppIntents struct NowEntry: TimelineEntry { let date: Date let snap: NowSnapshot let art: UIImage? } struct NowProvider: TimelineProvider { func placeholder(in context: Context) -> NowEntry { var s = NowSnapshot() s.title = "Drive"; s.artist = "Incubus"; s.album = "Make Yourself"; s.duration = 232; s.position = 60 s.palette = Palette.fromSeed(0xE07A5F, dark: true).widgetColors return NowEntry(date: Date(), snap: s, art: nil) } func getSnapshot(in context: Context, completion: @escaping (NowEntry) -> Void) { let s = NowSnapshot.load() completion(s.title.isEmpty ? placeholder(in: context) : NowEntry(date: Date(), snap: s, art: loadArt())) } func getTimeline(in context: Context, completion: @escaping (Timeline) -> Void) { let s = NowSnapshot.load() let art = loadArt() let now = Date() var entries = [NowEntry(date: now, snap: s, art: art)] if s.playing && s.duration > 0 { // One entry per upcoming synced lyric line (up to 60), so the lyrics card follows the song. let pos = s.position(at: now) let end = now.addingTimeInterval(max(0, s.duration - pos)) for t in s.lyrics where t > pos && entries.count < 60 { let d = now.addingTimeInterval(t - pos) if d < end { entries.append(NowEntry(date: d, snap: s, art: art)) } } var done = s done.playing = false done.position = s.duration entries.append(NowEntry(date: end.addingTimeInterval(1), snap: done, art: art)) completion(Timeline(entries: entries, policy: .after(end.addingTimeInterval(2)))) } else { completion(Timeline(entries: entries, policy: .never)) } } private func loadArt() -> UIImage? { guard let d = try? Data(contentsOf: NowSnapshot.artFile) else { return nil } return UIImage(data: d) } } extension Palette { var widgetColors: [UInt32] { [primary, onPrimary, primaryContainer, onPrimaryContainer, secondaryContainer, onSecondaryContainer, surface, surfaceHigh, onSurface, onSurfaceVariant] } } /// Colour lookup from the snapshot (falls back to the default palette). struct WColors { let c: [Color] init(_ s: NowSnapshot) { let raw = s.palette.count == 10 ? s.palette : Palette.fromSeed(Palette.defaultSeed, dark: s.dark).widgetColors c = raw.map { Color(rgb: $0) } } var primary: Color { c[0] } var onPrimary: Color { c[1] } var primaryC: Color { c[2] } var onPrimaryC: Color { c[3] } var secondaryC: Color { c[4] } var onSecondaryC: Color { c[5] } var surface: Color { c[6] } var surfaceHigh: Color { c[7] } var onSurface: Color { c[8] } var onSurfaceVar: Color { c[9] } } struct CookieShape: Shape { var depth: CGFloat = 0.045 func path(in rect: CGRect) -> Path { Shapes.cookie(center: CGPoint(x: rect.midX, y: rect.midY), radius: min(rect.width, rect.height) / 2, depth: depth, rotation: 0) } } struct CoverCookie: View { let art: UIImage? let colors: WColors var body: some View { ZStack { if let art { Image(uiImage: art).resizable().scaledToFill() } else { colors.primaryC.overlay(Image(systemName: "music.note").font(.title2).foregroundColor(colors.onPrimaryC)) } } .clipShape(CookieShape()) } } struct CtlButton: View { let cmd: MulberryCommand let icon: String let size: CGFloat let fg: Color let bg: Color var wide = false var body: some View { Button(intent: MulberryControlIntent(cmd)) { Image(systemName: icon) .font(.system(size: size * 0.4, weight: .bold)) .foregroundColor(fg) .frame(width: wide ? size * 1.5 : size, height: size) .background(Capsule().fill(bg)) } .buttonStyle(.plain) } } struct Controls: View { let s: NowSnapshot let col: WColors var size: CGFloat = 34 var body: some View { HStack(spacing: 8) { CtlButton(cmd: .shuffle, icon: "shuffle", size: size, fg: s.shuffle ? col.onPrimaryC : col.onSurfaceVar, bg: s.shuffle ? col.primaryC : col.surfaceHigh) CtlButton(cmd: .previous, icon: "backward.end.fill", size: size, fg: col.onSecondaryC, bg: col.secondaryC) CtlButton(cmd: .toggle, icon: s.playing ? "pause.fill" : "play.fill", size: size, fg: col.onPrimary, bg: col.primary, wide: true) CtlButton(cmd: .next, icon: "forward.end.fill", size: size, fg: col.onSecondaryC, bg: col.secondaryC) CtlButton(cmd: .repeatMode, icon: s.repeatMode == 2 ? "repeat.1" : "repeat", size: size, fg: s.repeatMode > 0 ? col.onPrimaryC : col.onSurfaceVar, bg: s.repeatMode > 0 ? col.primaryC : col.surfaceHigh) } } } /// Progress that advances by itself while playing. struct LiveProgress: View { let s: NowSnapshot let col: WColors let date: Date var showTimes = true var body: some View { let pos = s.position(at: date) let start = date.addingTimeInterval(-pos) let end = start.addingTimeInterval(max(1, s.duration)) VStack(spacing: 2) { if s.playing && s.duration > 0 { ProgressView(timerInterval: start...end, countsDown: false) { EmptyView() } currentValueLabel: { EmptyView() } .tint(col.primary) } else { ProgressView(value: s.duration > 0 ? min(1, pos / s.duration) : 0).tint(col.primary) } if showTimes { HStack { if s.playing { Text(start, style: .timer).monospacedDigit() } else { Text(TimeFmt.string(pos)) } Spacer() Text(TimeFmt.string(s.duration)) } .font(.system(size: 10).monospacedDigit()) .foregroundColor(col.onSurfaceVar) } } } } struct PlayerWidgetView: View { let e: NowEntry @Environment(\.widgetFamily) var family var body: some View { let s = e.snap, col = WColors(s) Group { if s.title.isEmpty { VStack(spacing: 8) { Image(systemName: "music.note").font(.title).foregroundColor(col.primary) Text("Nothing playing").font(.headline).foregroundColor(col.onSurface) Text("Tap to open Mulberry").font(.caption).foregroundColor(col.onSurfaceVar) } } else { switch family { case .systemSmall: small(s, col) case .systemLarge: large(s, col) default: medium(s, col) } } } .widgetURL(URL(string: "mulberry://nowplaying")) .containerBackground(for: .widget) { LinearGradient(colors: [Color(rgb: Palette.mix(s.palette.count == 10 ? s.palette[2] : 0x553355, s.palette.count == 10 ? s.palette[7] : 0x222222, 0.35)), col.surfaceHigh], startPoint: .topLeading, endPoint: .bottomTrailing) } } private func titles(_ s: NowSnapshot, _ col: WColors, big: Bool = false) -> some View { VStack(alignment: .leading, spacing: 1) { Text(s.title).font(.system(size: big ? 19 : 16, weight: .bold)).foregroundColor(col.onSurface).lineLimit(1) Text(s.artist).font(.system(size: big ? 15 : 13)).foregroundColor(col.onSurface.opacity(0.85)).lineLimit(1) if !s.album.isEmpty { Text(s.album).font(.system(size: big ? 14 : 12, weight: .semibold)).foregroundColor(col.primary).lineLimit(1) } } } private func small(_ s: NowSnapshot, _ col: WColors) -> some View { VStack(alignment: .leading, spacing: 6) { HStack(alignment: .top) { CoverCookie(art: e.art, colors: col).frame(width: 64, height: 64) Spacer() CtlButton(cmd: .toggle, icon: s.playing ? "pause.fill" : "play.fill", size: 40, fg: col.onPrimary, bg: col.primary) } Spacer(minLength: 0) titles(s, col) LiveProgress(s: s, col: col, date: e.date, showTimes: false) } } private func medium(_ s: NowSnapshot, _ col: WColors) -> some View { VStack(spacing: 6) { HStack(spacing: 12) { CoverCookie(art: e.art, colors: col).frame(width: 62, height: 62) titles(s, col) Spacer(minLength: 0) } LiveProgress(s: s, col: col, date: e.date) Controls(s: s, col: col, size: 34) } } private func large(_ s: NowSnapshot, _ col: WColors) -> some View { VStack(spacing: 10) { HStack(spacing: 14) { CoverCookie(art: e.art, colors: col).frame(width: 84, height: 84) titles(s, col, big: true) Spacer(minLength: 0) } LiveProgress(s: s, col: col, date: e.date) Controls(s: s, col: col, size: 40) LyricsCard(e: e, col: col, lines: 3) } } } /// The lyrics card (Android widget's lower panel): the current synced line, with the next ones dimmed. struct LyricsCard: View { let e: NowEntry let col: WColors var lines = 2 var body: some View { let s = e.snap let i = s.lyricIndex(at: e.date) VStack(alignment: .leading, spacing: 6) { Image(systemName: "music.note").font(.caption).foregroundColor(col.primary) if !s.lyrics.isEmpty { let cur = i >= 0 ? s.lyricText[i] : (s.lyricText.first ?? "") Text(cur.isEmpty ? "♪" : cur).font(.system(size: 17, weight: .bold)).foregroundColor(col.onSurface).lineLimit(2) ForEach(Array(s.lyricText.dropFirst(i + 1).prefix(lines - 1).enumerated()), id: \.offset) { _, l in Text(l.isEmpty ? "♪" : l).font(.system(size: 13)).foregroundColor(col.onSurfaceVar).lineLimit(1) } } else if !s.plainLyric.isEmpty { Text(s.plainLyric).font(.system(size: 13)).foregroundColor(col.onSurfaceVar).lineLimit(lines + 1) } else { Text(s.title.isEmpty ? "Nothing playing" : s.title).font(.system(size: 16, weight: .bold)).foregroundColor(col.onSurface) Text("No lyrics").font(.system(size: 13)).foregroundColor(col.onSurfaceVar) } Spacer(minLength: 0) } .frame(maxWidth: .infinity, alignment: .leading) .padding(12) .background(RoundedRectangle(cornerRadius: 18, style: .continuous).fill(col.surface.opacity(0.55))) } } struct LyricsWidgetView: View { let e: NowEntry var body: some View { let col = WColors(e.snap) LyricsCard(e: e, col: col, lines: 3) .padding(-12) .widgetURL(URL(string: "mulberry://nowplaying")) .containerBackground(for: .widget) { col.surfaceHigh } } } struct LockWidgetView: View { let e: NowEntry @Environment(\.widgetFamily) var family var body: some View { let s = e.snap Group { switch family { case .accessoryCircular: ZStack { AccessoryWidgetBackground() if s.playing && s.duration > 0 { let start = e.date.addingTimeInterval(-s.position(at: e.date)) ProgressView(timerInterval: start...start.addingTimeInterval(s.duration), countsDown: false) { EmptyView() } currentValueLabel: { Image(systemName: "pause.fill") } .progressViewStyle(.circular) } else { Image(systemName: "play.fill") } } .widgetLabel(s.title) case .accessoryInline: Text(s.title.isEmpty ? "Mulberry" : "♪ \(s.title) · \(s.artist)") default: VStack(alignment: .leading, spacing: 1) { if !s.lyrics.isEmpty, s.playing, case let i = s.lyricIndex(at: e.date), i >= 0 { Text(s.lyricText[i].isEmpty ? "♪" : s.lyricText[i]).font(.headline).lineLimit(2) Text("\(s.title) · \(s.artist)").font(.caption2).lineLimit(1).opacity(0.8) } else { Text(s.title.isEmpty ? "Nothing playing" : s.title).font(.headline).lineLimit(1) Text(s.artist).font(.caption).lineLimit(1) LiveProgress(s: s, col: WColors(s), date: e.date, showTimes: false) } } } } .widgetURL(URL(string: "mulberry://nowplaying")) .containerBackground(for: .widget) { Color.clear } } } struct PlayerWidget: Widget { var body: some WidgetConfiguration { StaticConfiguration(kind: NowSnapshot.kind, provider: NowProvider()) { PlayerWidgetView(e: $0) } .configurationDisplayName("Mulberry") .description("What's playing, with controls.") .supportedFamilies([.systemSmall, .systemMedium, .systemLarge]) } } struct LyricsWidget: Widget { var body: some WidgetConfiguration { StaticConfiguration(kind: NowSnapshot.lyricsKind, provider: NowProvider()) { LyricsWidgetView(e: $0) } .configurationDisplayName("Mulberry lyrics") .description("The current line of synced lyrics.") .supportedFamilies([.systemMedium, .systemSmall]) } } struct LockWidget: Widget { var body: some WidgetConfiguration { StaticConfiguration(kind: NowSnapshot.shapeKind, provider: NowProvider()) { LockWidgetView(e: $0) } .configurationDisplayName("Mulberry") .description("Now playing on the Lock Screen.") .supportedFamilies([.accessoryRectangular, .accessoryCircular, .accessoryInline]) } } @main struct MulberryWidgets: WidgetBundle { var body: some Widget { PlayerWidget() LyricsWidget() LockWidget() } }