import "package:flutter/widgets.dart"; import "package:google_fonts/google_fonts.dart"; import "package:garage_ui/theme/garage_theme.dart"; // The apps type scale. shadcn exposed a big Typography object; the GarageUI widgets // only reach for the small set below. `small` is a size style, `medium`/`normal` // are weight styles, and `mono` swaps the font family while inheriting the // ambient size/weight unless a caller overrides them. class Typography { const Typography({ this.normal = const TextStyle(fontWeight: FontWeight.w400), this.medium = const TextStyle(fontWeight: FontWeight.w500), this.semiBold = const TextStyle(fontWeight: FontWeight.w600), // `small` is the shared control font used by buttons, fields and selects. // Prefer [Typography.forDensity] over setting this by hand - the size // and line height belong to the density, and the whole control geometry // chain hangs off them. this.small = const TextStyle( fontSize: 10, height: 1.1, fontWeight: FontWeight.w400, ), }); /// Builds the control type from the density, so `small` can never drift from /// the line box the control heights are derived from. factory Typography.forDensity(Density density) => Typography( small: TextStyle( fontSize: density.fontSize, height: density.lineHeight, fontWeight: FontWeight.w400, ), ); final TextStyle normal; final TextStyle medium; /// Emphasis above [medium] - section labels, a dialog's title. Was a /// FontWeight.w600 literal in menu.dart, properties.dart and toast.dart. final TextStyle semiBold; final TextStyle small; TextStyle sansStyle(TextStyle style) => GoogleFonts.geist(textStyle: style, fontWeight: style.fontWeight); TextStyle get mono => GoogleFonts.geistMono(); TextStyle monoStyle(TextStyle style) => GoogleFonts.geistMono(textStyle: style, fontWeight: style.fontWeight); } // shadcn hung these little text helpers off every widget (usually a Text). they // merge a style change over whatever DefaultTextStyle is in scope. sizes are // multiplied by the theme scaling so they track the rest of the ui. extension TextStyleExtension on Widget { // sizes come off the density's text ladder, NOT a literal times scaling - // see Density.textXs for why. scaling is left out on purpose: Density isn't // scaled, and these have to stay in step with the control font. Widget xSmall() => _StyledText( child: this, style: (t) => TextStyle(fontSize: t.density.textXs), ); Widget small() => _StyledText( child: this, style: (t) => TextStyle(fontSize: t.density.textSm), ); Widget large() => _StyledText( child: this, style: (t) => TextStyle(fontSize: t.density.textLg), ); // these DONT go through _StyledText. Merging `fontWeight: w800` over an // already resolved google_fonts family changes nothing about which font // FILE gets used - the file is the weight - so skia synthesises instead, // and skia has exactly one fake bold. w600, w700 and w800 all came out the // same stroke, which is how extraBold ended up looking identical to bold. Widget medium() => _WeightText(child: this, weight: FontWeight.w500); Widget semiBold() => _WeightText(child: this, weight: FontWeight.w600); Widget bold() => _WeightText(child: this, weight: FontWeight.w700); /// Top of the ladder. A display weight - for a figure or a name somebody is /// meant to read across a desk, not for body copy. Widget extraBold() => _WeightText(child: this, weight: FontWeight.w800); Widget muted() => _StyledText( child: this, style: (t) => TextStyle(color: t.colorScheme.mutedForeground), ); /// Geist Mono, keeping whatever size, weight and colour are already in /// scope. /// /// Chains in either order - `.mono().bold()` and `.bold().mono()` both land /// on the real mono bold, because the weight helpers re-resolve whichever /// family they find in scope rather than assuming sans. /// /// Only w300 and w700 are in main.dart's `pendingFonts`. Any other weight /// resolves fine and lands a frame or two late. Widget mono() => _MonoText(child: this); } // mono cant ride on _StyledText the way the weights do. those merge a style // naming ONE property and dont care what else is in scope, so they compose in // any order. A family out of google_fonts isnt one property - the family name // encodes the weight - so this one has to read the inherited style first and // hand the whole thing over to be re-resolved. // re-resolves the family for the weight it's setting, keeping whichever face // is already in scope. sans unless somebody put mono there first. class _WeightText extends StatelessWidget { const _WeightText({required this.child, required this.weight}); final Widget child; final FontWeight weight; @override Widget build(BuildContext context) { final theme = GarageTheme.of(context); final inherited = DefaultTextStyle.of(context).style; final wanted = inherited.copyWith(fontWeight: weight); // google_fonts names a family "_", so the bit before the // underscore is the face. taken off typography.mono rather than spelled // out, so renaming the font doesnt quietly turn every .bold() on a mono // run back into sans. String? face(String? family) => family?.split("_").first; final isMono = face(inherited.fontFamily) == face(theme.typography.mono.fontFamily); return DefaultTextStyle.merge( style: isMono ? theme.typography.monoStyle(wanted) : theme.typography.sansStyle(wanted), child: child, ); } } class _MonoText extends StatelessWidget { const _MonoText({required this.child}); final Widget child; @override Widget build(BuildContext context) { final theme = GarageTheme.of(context); // .of(context), not the theme: this is the point of the class. everything // a caller stacked ABOVE this widget is already merged into here. final inherited = DefaultTextStyle.of(context).style; return DefaultTextStyle.merge( style: theme.typography.monoStyle(inherited), child: child, ); } } typedef _StyleFromTheme = TextStyle Function(ThemeData theme); class _StyledText extends StatelessWidget { const _StyledText({required this.child, required this.style}); final Widget child; final _StyleFromTheme style; @override Widget build(BuildContext context) { final theme = GarageTheme.of(context); return DefaultTextStyle.merge(style: style(theme), child: child); } }