Mulberry/App/UI/Components.swift
astar76 8e6f8a86a4 Mulberry for iOS: initial SwiftUI port of the Android app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 19:30:49 -05:00

235 lines
7.9 KiB
Swift

import SwiftUI
/// Rounded cover thumbnail that loads off the main thread (Android Lists.coverView/bindCover).
struct CoverThumb: View {
let song: Song?
var size: CGFloat = 48
var radius: CGFloat = 8
var circle = false
@EnvironmentObject var theme: Theme
@State private var img: UIImage?
@State private var loadedFor: String?
var body: some View {
let shape = RoundedRectangle(cornerRadius: circle ? size / 2 : radius, style: .continuous)
ZStack {
if let img {
Image(uiImage: img).resizable().scaledToFill()
} else {
theme.palette.cSurfaceHighest
Image(systemName: "music.note")
.font(.system(size: size * 0.36, weight: .medium))
.foregroundColor(theme.palette.cOnSurfaceVar.opacity(0.7))
}
}
.frame(width: size, height: size)
.clipShape(shape)
.task(id: song?.albumKey ?? song?.path) { await load() }
}
private func load() async {
guard let s = song else { img = nil; return }
let px = Int(size * UIScreen.main.scale) <= 160 ? 160 : 300
if let c = Covers.cached(s, px) { img = c; return }
img = nil
let r = await Task.detached(priority: .utility) { Covers.thumb(s, px: px) }.value
if !Task.isCancelled { img = r }
}
}
struct SongRow: View {
let song: Song
var current = false
var showCover = true
var trailing: String?
var onMenu: (() -> Void)?
@EnvironmentObject var theme: Theme
var body: some View {
let p = theme.palette
HStack(spacing: 14) {
if showCover { CoverThumb(song: song, size: 48) }
VStack(alignment: .leading, spacing: 2) {
Text(song.displayTitle)
.font(.system(size: 16, weight: current ? .semibold : .regular))
.foregroundColor(current ? p.cPrimary : p.cOnSurface)
.lineLimit(1)
Text(song.subtitle)
.font(.system(size: 14))
.foregroundColor(p.cOnSurfaceVar)
.lineLimit(1)
}
Spacer(minLength: 6)
Text(trailing ?? (song.duration > 0 ? TimeFmt.string(song.duration) : ""))
.font(.system(size: 13).monospacedDigit())
.foregroundColor(p.cOnSurfaceVar)
if let onMenu {
Button(action: onMenu) {
Image(systemName: "ellipsis")
.rotationEffect(.degrees(90))
.font(.system(size: 17, weight: .semibold))
.foregroundColor(p.cOnSurfaceVar)
.frame(width: 32, height: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
}
.padding(.leading, 16)
.padding(.trailing, onMenu == nil ? 16 : 6)
.padding(.vertical, 6)
.contentShape(Rectangle())
}
}
struct GroupRow: View {
let group: Library.Group
var circle = false
var folder = false
@EnvironmentObject var theme: Theme
var body: some View {
let p = theme.palette
HStack(spacing: 14) {
if folder {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(p.cSurfaceHighest)
.frame(width: 48, height: 48)
.overlay(Image(systemName: "folder").font(.system(size: 20)).foregroundColor(p.cOnSurfaceVar))
} else {
CoverThumb(song: group.cover, size: 48, circle: circle)
}
VStack(alignment: .leading, spacing: 2) {
Text(group.title).font(.system(size: 16)).foregroundColor(p.cOnSurface).lineLimit(1)
Text(group.subtitle).font(.system(size: 14)).foregroundColor(p.cOnSurfaceVar).lineLimit(1)
}
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 6)
.contentShape(Rectangle())
}
}
/// Filled or tonal pill button (Android Ui.pill).
struct Pill: View {
let icon: String?
let title: String
var filled = false
let action: () -> Void
@EnvironmentObject var theme: Theme
var body: some View {
let p = theme.palette
Button(action: action) {
HStack(spacing: 8) {
if let icon { Image(systemName: icon).font(.system(size: 15, weight: .semibold)) }
Text(title).font(.system(size: 14, weight: .medium)).lineLimit(1)
}
.padding(.leading, 16)
.padding(.trailing, 20)
.padding(.vertical, 10)
.foregroundColor(filled ? p.cOnPrimary : p.cOnSecondaryC)
.background(Capsule().fill(filled ? p.cPrimary : p.cSecondaryC))
}
.buttonStyle(PressScale())
}
}
/// Filter chip (Songs / Albums / Artists / Folders).
struct Chip: View {
let title: String
let selected: Bool
let action: () -> Void
@EnvironmentObject var theme: Theme
var body: some View {
let p = theme.palette
Button(action: action) {
Text(title)
.font(.system(size: 15))
.padding(.horizontal, 18)
.padding(.vertical, 9)
.foregroundColor(selected ? p.cOnSecondaryC : p.cOnSurface)
.background(Capsule().fill(selected ? p.cSecondaryC : Color.clear))
.overlay(Capsule().stroke(selected ? Color.clear : p.cOutlineVar, lineWidth: 1))
}
.buttonStyle(PressScale())
}
}
struct IconButton: View {
let icon: String
var size: CGFloat = 44
var tint: Color?
var filled: Color?
let action: () -> Void
@EnvironmentObject var theme: Theme
var body: some View {
Button(action: action) {
Image(systemName: icon)
.font(.system(size: size * 0.42, weight: .semibold))
.foregroundColor(tint ?? theme.palette.cOnSurfaceVar)
.frame(width: size, height: size)
.background(Circle().fill(filled ?? .clear))
.contentShape(Circle())
}
.buttonStyle(PressScale())
}
}
struct PressScale: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? 0.94 : 1)
.opacity(configuration.isPressed ? 0.85 : 1)
.animation(.easeOut(duration: 0.12), value: configuration.isPressed)
}
}
/// Section header like "Smart playlists" / "Appearance".
struct SectionLabel: View {
let text: String
@EnvironmentObject var theme: Theme
var body: some View {
Text(text)
.font(.system(size: 14, weight: .medium))
.foregroundColor(theme.palette.cPrimary)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 20)
.padding(.top, 18)
.padding(.bottom, 8)
}
}
/// Rounded container card used in Settings.
struct Card<Content: View>: View {
@ViewBuilder let content: Content
@EnvironmentObject var theme: Theme
var body: some View {
VStack(spacing: 0) { content }
.background(RoundedRectangle(cornerRadius: 24, style: .continuous).fill(theme.palette.cSurfaceC))
.padding(.horizontal, 12)
}
}
/// Toast banner shown at the bottom.
struct Toast: View {
let text: String
@EnvironmentObject var theme: Theme
var body: some View {
Text(text)
.font(.system(size: 14))
.foregroundColor(theme.palette.cSurface)
.padding(.horizontal, 18)
.padding(.vertical, 12)
.background(Capsule().fill(theme.palette.cOnSurface))
.shadow(radius: 6, y: 2)
}
}
extension View {
/// Applies the palette's surface background.
func surface(_ p: Palette) -> some View { background(p.cSurface.ignoresSafeArea()) }
}