Files
nvrl 0d53d1cf72 feat(core,app,android,ios): solid accents, a copper scale and one scrim curve
- The accent is a flat fill. With gradients on (now off by default) it
  shades into a darker tone of itself, never into orchid.
- Copper gets a 100-700 scale; as an accent on dark it uses the lit 400.
  iOS shows the copper rating star and new-episodes badge like Android.
- Every scrim over artwork follows one eased curve (SCRIM_STOPS), layered
  by job instead of one many-stop ramp. The desktop banner is taller and
  loses its full-picture wash.
- One solid focus ring on every client; the TV gradient border and the
  desktop's inner copper ring are gone.
- Missing art is a plain card with initials, not a hashed colour ramp.
- A portrait poster in a landscape card fills the frame with a dimmed
  crop of itself.
2026-11-24 19:05:31 +01:00

217 lines
7.8 KiB
Swift

import Observation
import SwiftUI
/// The palette in force, shared across every view.
///
/// The choice is stored by Rust and resolved by Rust — the same flavours, the
/// same accent pairings and the same contrast rule the desktop client uses — so
/// what lives here is only the resolved answer and a way to replace it when the
/// viewer picks something else.
@MainActor
@Observable
final class AppearanceState {
static let shared = AppearanceState()
private(set) var palette: PaletteRecord?
/// Before the first read, and if it fails, the palette this app shipped
/// with — a theme file that cannot be read must never be a reason the
/// library does not open.
var scheme: Scheme { Scheme(palette ?? protonFallback) }
/// The palette read before the first frame, from
/// `NativeRuntime.initialPalette()`.
func seed(_ palette: PaletteRecord?) {
if let palette { self.palette = palette }
}
/// Read the stored choice through the engine. A no-op once `seed` has run.
func load() async {
if palette != nil { return }
if let read = try? await Task.detached(operation: { try NativeRuntime.blockingEngine().palette() }).value {
palette = read
}
}
/// Read the stored choice again, after sync rewrote it.
func reload() async {
if let read = try? await Task.detached(operation: { try NativeRuntime.blockingEngine().palette() }).value {
palette = read
}
}
/// Repaint immediately, before the write that stores the choice lands.
func apply(_ palette: PaletteRecord) {
self.palette = palette
}
}
/// One resolved palette as the roles the views draw with — Material's names,
/// so a view ported from the Android client asks for the same role.
///
/// Every role is answered from the palette, and the mapping is Android's
/// `schemeOf` line for line; `SchemeRolesTests` pins it.
struct Scheme: Equatable {
let light: Bool
/// `background` as the bridge hands it over, for the shared colour work
/// that takes it back — `Scheme.veil`.
let backgroundArgb: UInt32
let primary: Color
let onPrimary: Color
let primaryContainer: Color
let onPrimaryContainer: Color
let inversePrimary: Color
let secondary: Color
let onSecondary: Color
let secondaryContainer: Color
let onSecondaryContainer: Color
let tertiary: Color
let onTertiary: Color
let tertiaryContainer: Color
let onTertiaryContainer: Color
let background: Color
let onBackground: Color
let surface: Color
let onSurface: Color
let surfaceVariant: Color
let onSurfaceVariant: Color
let inverseSurface: Color
let inverseOnSurface: Color
let error: Color
let onError: Color
let errorContainer: Color
let onErrorContainer: Color
let outline: Color
let outlineVariant: Color
let scrim: Color
let surfaceBright: Color
let surfaceDim: Color
let surfaceContainerLowest: Color
let surfaceContainerLow: Color
let surfaceContainer: Color
let surfaceContainerHigh: Color
let surfaceContainerHighest: Color
/// The suite's copper — a new-episodes badge, a rating star — which no
/// Material role carries. Android's `LocalBrand`.
let highlight: Color
let onHighlight: Color
init(_ palette: PaletteRecord) {
light = palette.light
backgroundArgb = palette.background
primary = argb(palette.accent)
onPrimary = argb(palette.onAccent)
primaryContainer = argb(palette.accentDim)
onPrimaryContainer = argb(palette.text)
// Readable *on* `inverseSurface`, which is the ink colour: `accentDim`
// is the accent taken towards the page, so it inverts with the rest.
inversePrimary = argb(palette.accentDim)
secondary = argb(palette.accentAlt)
onSecondary = argb(palette.onAccent)
// The accent taken back towards the page, not the card's hover shade:
// it fills every tonal button.
secondaryContainer = argb(palette.accentDim)
onSecondaryContainer = argb(palette.text)
// The palette has two hues, not three. Tertiary takes the gradient
// partner rather than inventing a third that no flavour chose.
tertiary = argb(palette.accentAlt)
onTertiary = argb(palette.onAccent)
tertiaryContainer = argb(palette.accentDim)
onTertiaryContainer = argb(palette.text)
background = argb(palette.background)
onBackground = argb(palette.text)
surface = argb(palette.surface)
onSurface = argb(palette.text)
surfaceVariant = argb(palette.card)
onSurfaceVariant = argb(palette.muted)
// Genuinely inverted, both ways round: on a dark flavour this is a pale
// snackbar with dark ink, on Latte a dark one with pale ink.
inverseSurface = argb(palette.text)
inverseOnSurface = argb(palette.background)
error = argb(palette.danger)
onError = argb(palette.background)
errorContainer = argb(palette.dangerDim)
onErrorContainer = argb(palette.text)
// `outline` is a border meant to be seen, `outlineVariant` a divider
// meant to be barely there.
outline = argb(palette.muted)
outlineVariant = argb(palette.border)
scrim = .black
surfaceBright = argb(palette.elevated)
surfaceDim = argb(palette.background)
// The containment ladder, in the order the palette already runs.
surfaceContainerLowest = argb(palette.sunken)
surfaceContainerLow = argb(palette.surface)
surfaceContainer = argb(palette.card)
surfaceContainerHigh = argb(palette.cardHover)
surfaceContainerHighest = argb(palette.elevated)
highlight = argb(palette.highlight)
onHighlight = palette.light ? .white : argb(palette.background)
}
var colorScheme: ColorScheme { light ? .light : .dark }
/// The accent, as a gradient when there is one to draw: top to bottom,
/// into a shade of itself (`Palette::resolve`), never into a second hue.
/// With gradients off `accent_alt` equals `accent`, so this collapses to
/// a flat fill on its own.
var accentGradient: LinearGradient {
LinearGradient(colors: [primary, secondary], startPoint: .top, endPoint: .bottom)
}
}
/// A packed `0xAARRGGBB` from the bridge, as SwiftUI wants it.
func argb(_ value: UInt32) -> Color {
Color(
.sRGB,
red: Double((value >> 16) & 0xFF) / 255,
green: Double((value >> 8) & 0xFF) / 255,
blue: Double(value & 0xFF) / 255,
opacity: Double((value >> 24) & 0xFF) / 255
)
}
/// The shipped default, spelled out for the case where Rust cannot be asked.
/// The values are `pstr_core::appearance::Palette::PROTON`.
let protonFallback = PaletteRecord(
background: 0xFF10_0E17,
surface: 0xFF17_141F,
sunken: 0xFF0B_0910,
card: 0xFF1E_1A29,
cardHover: 0xFF2E_283D,
border: 0xFF2E_283D,
text: 0xFFF4_F2FA,
muted: 0xFFAA_A4BD,
accent: 0xFF6D_4AFF,
accentAlt: 0xFF6D_4AFF,
accentDim: 0xFF53_38C4,
onAccent: 0xFFF4_F2FA,
danger: 0xFFE0_5561,
elevated: 0xFF3A_324B,
dangerDim: 0xFF7F_2F38,
highlight: 0xFFD6_9B77,
light: false
)
private struct SchemeKey: EnvironmentKey {
static let defaultValue = Scheme(protonFallback)
}
extension EnvironmentValues {
var scheme: Scheme {
get { self[SchemeKey.self] }
set { self[SchemeKey.self] = newValue }
}
}
/// The suite's corner radii, which are the desktop client's and Android's
/// `Shape.kt`: 12 for cards and tiles, 8 for a small control, 6 for a badge,
/// 16 for a sheet, 24 for something floating. Buttons are capsules.
enum Corner {
static let standard: CGFloat = 12
static let small: CGFloat = 8
static let tight: CGFloat = 6
static let sheet: CGFloat = 16
static let float: CGFloat = 24
}