Files
RedLintu16andClaude Opus 5.5 64ec524ba0 CrossSound for Apple platforms
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 13:28:34 -07:00

233 lines
8.3 KiB
Swift
Executable File

// BareButtons — CrossSound's buttons: no background in any state, so colour
// alone carries hover, press and selection.
//
// The colour rule, for text and red glyphs alike:
// rest → primary (p.red)
// hover → the hover colour (p.redHover)
// active → the hover colour (selected, pressed, the playing track)
// active + hover → primary again
// i.e. the hover colour shows when exactly one of hover / active is true. A
// press under the pointer therefore flips back to primary, which is the click
// feedback; on iOS, with no hover, a press shows the hover colour.
//
// CrossSound only, on purpose: RedWhiteUI's RwButtonStyle keeps its washes for
// every other app. Library chrome CrossSound does not draw itself (RwMenu's
// rows, RwShellMenuButton, rwDialog's buttons, the sidebar) keeps the library
// look.
//
// How the colour reaches a label: the style re-plants `\.rw` with the lit
// palette for its label, so `.rw()` text and any child view reading `\.rw`
// follow on their own. A colour the CALLER captured from its own palette
// (`.foregroundStyle(p.red)` written in the page's body) was resolved before
// the style ran, so it stays put — leave labels uncoloured to get the rule.
import RedWhiteUI
import SwiftUI
extension RwPalette {
/// Every text token, and the pastel inactive-icon tone, at the primary
/// colour: all text and every passive glyph in CrossSound is primary.
/// textSubtle (placeholders, hints) keeps its own red tone so an empty
/// field still reads as empty. A disabled control dims with
/// `red.withAlpha(0.4)` instead (BareButtonStyle's disabled tone).
func allTextPrimary() -> RwPalette {
var q = self
q.text = red
q.textMuted = red
q.contentText = red
q.iconInactive = red
return q
}
/// Red and white only: the status colours (green success, amber warn, blue
/// info) become the primary, their soft washes the red one. Remapped here
/// rather than at each call site, so library pieces that pick a tone
/// themselves (RwAlert, RwPill) follow too.
func statusInRed() -> RwPalette {
var q = self
q.success = red
q.warn = red
q.info = red
q.successSoft = redSoft
q.warnSoft = redSoft
q.infoSoft = redSoft
return q
}
/// The palette for something lit (see the rule above): the primary and every
/// text token become the hover colour, so `.rw()` text, red glyphs and
/// anything else reading `\.rw` underneath follow along.
func lit(_ on: Bool = true) -> RwPalette {
guard on else { return self }
var q = self
q.red = redHover
q.text = redHover
q.textMuted = redHover
q.contentText = redHover
q.iconInactive = redHover
return q
}
}
/// The rule itself: the hover colour when exactly one of the two is true.
func isLit(active: Bool, hovering: Bool) -> Bool { active != hovering }
/// Hands its content the palette in effect where it SITS. Inside a bare
/// button's label that is the lit palette, so a label written against `p`
/// (`.foregroundStyle(p.red)`, `.rw(.bodyLarge, color: p.text.color)`)
/// follows the hover rule without being rewritten:
///
/// ```swift
/// Button(action: play) {
/// LitPalette { p in
/// Image(systemName: "play.fill").foregroundStyle(p.red)
/// }
/// }
/// .buttonStyle(.bareRow())
/// ```
///
/// Tokens the rule leaves alone (iconInactive, hair…) stay as they are, so an
/// off toggle keeps its pastel.
struct LitPalette<Content: View>: View {
@Environment(\.rw) private var p
@ViewBuilder let content: (RwPalette) -> Content
var body: some View { content(p) }
}
/// Applied once, inside `.rwTheme()` (see CrossSoundScene): all text primary,
/// red and white only (no status greens / ambers / blues), and BareButtonStyle as the default look of every Button that does not pick
/// a style. Inside the theme because the closest `.environment` /
/// `.buttonStyle` wins, and the theme sets its own on the view it wraps.
struct CrossSoundLook: ViewModifier {
@Environment(\.rw) private var p
func body(content: Content) -> some View {
let q = p.allTextPrimary().statusInRed()
content
.foregroundStyle(q.text.color)
.buttonStyle(.bare)
.textFieldStyle(.bare)
.environment(\.rw, q)
}
}
extension View {
/// The rule for a row that is not one Button (a tap gesture with buttons
/// inside, like the queue's rows): tracks the pointer over the WHOLE row and
/// plants the lit palette for everything in it. [active] is the row's own
/// "on" state, e.g. the playing track.
func litRow(active: Bool = false) -> some View {
modifier(LitRow(active: active))
}
}
private struct LitRow: ViewModifier {
let active: Bool
@Environment(\.rw) private var p
@State private var hovering = false
func body(content: Content) -> some View {
let q = p.lit(isLit(active: active, hovering: hovering))
content
// Plain Text / Image with no colour of its own takes the text colour.
.foregroundStyle(q.text.color)
.environment(\.rw, q)
.onHover { hovering = $0 }
.animation(.easeOut(duration: 0.12), value: hovering)
}
}
/// No background, ever. Sizes mirror RedWhiteUI's presets (`.rw`, `.rwText`,
/// `.rwIcon()`) so swapping a style never moves a layout.
struct BareButtonStyle: ButtonStyle {
var active = false
var padding = EdgeInsets()
var minWidth: CGFloat?
var minHeight: CGFloat?
/// Follow the density setting's height adjustment (the library's rule).
var densityAware = false
/// The library's button font (14 bold); off for rows and hand-laid-out
/// labels that set their own type.
var buttonFont = false
func makeBody(configuration: Configuration) -> some View {
Chrome(configuration: configuration, style: self)
}
private struct Chrome: View {
let configuration: Configuration
let style: BareButtonStyle
@Environment(\.rw) private var p
@Environment(\.rwMetrics) private var m
@Environment(\.rwFontFamily) private var family
@Environment(\.isEnabled) private var enabled
@State private var hover = false
var body: some View {
let active = style.active || (configuration.isPressed && enabled)
let lit = enabled && isLit(active: active, hovering: hover)
let q = p.lit(lit)
let delta = style.densityAware ? m.density.controlDelta : 0
label
.foregroundStyle((enabled ? q.red : p.red.withAlpha(0.4)).color)
.environment(\.rw, q)
.padding(style.padding)
.frame(
minWidth: style.minWidth,
minHeight: style.minHeight.map { max(0, $0 + delta) }
)
.contentShape(Rectangle())
.onHover { hover = $0 }
.animation(.easeOut(duration: 0.12), value: lit)
}
@ViewBuilder private var label: some View {
if style.buttonFont {
// titleSmall is the library's button type (14 bold) through public API.
configuration.label.font(RwTextRole.titleSmall.font(family: family))
} else {
configuration.label
}
}
}
}
extension ButtonStyle where Self == BareButtonStyle {
/// Stands in for `.rw`: the base button's size, no wash.
static var bare: BareButtonStyle { bare() }
/// Stands in for `.rw(selected:)`: selected shows the hover colour.
static func bare(selected: Bool = false) -> BareButtonStyle {
BareButtonStyle(
active: selected,
padding: EdgeInsets(top: 12, leading: 22, bottom: 12, trailing: 22),
minWidth: 64, minHeight: RwSizes.controlHeight,
densityAware: true, buttonFont: true)
}
/// Stands in for `.rwText`: compact.
static var bareText: BareButtonStyle {
BareButtonStyle(
padding: EdgeInsets(top: 8, leading: 12, bottom: 8, trailing: 12),
minWidth: 48, minHeight: 36,
densityAware: true, buttonFont: true)
}
/// Stands in for `.rwIcon()` (and `.rwDisc`): a 40pt square icon button.
static func bareIcon(selected: Bool = false) -> BareButtonStyle {
BareButtonStyle(
active: selected,
padding: EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8),
minWidth: 40, minHeight: 40, buttonFont: true)
}
/// Stands in for `.plain` on rows, cards and hand-laid-out labels: no
/// padding, size or font of its own, just the colour rule. [active] is the
/// item's own "on" state (a selected filter, an enabled toggle).
static func bareRow(active: Bool = false) -> BareButtonStyle {
BareButtonStyle(active: active)
}
}