|
1 | 1 | import 'dart:io';
|
2 | 2 | import 'package:flutter/foundation.dart';
|
3 |
| -import 'package:flutter/widgets.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +/// An app-wide [Typography] for Zulip, customized from the Material default. |
| 6 | +/// |
| 7 | +/// Include this in the app-wide [MaterialApp.theme]. |
| 8 | +/// |
| 9 | +/// We expect these text styles to be the basis of all the styles chosen by the |
| 10 | +/// Material library's widgets, such as the default styling of |
| 11 | +/// an [AppBar]'s title, of an [ElevatedButton]'s label, and so on. |
| 12 | +/// |
| 13 | +/// In all the comprised [TextStyle]s, this sets: |
| 14 | +/// |
| 15 | +/// - [TextStyle.fontFamily] to [kDefaultFontFamily], and |
| 16 | +/// - [TextStyle.fontFamilyFallback] to [kDefaultFontFamilyFallback]. |
| 17 | +/// |
| 18 | +/// Since [kDefaultFontFamily] is a variable-weight font, |
| 19 | +/// each [TextStyle] needs some processing, which this provides, |
| 20 | +/// in order to correctly specify the font weight. |
| 21 | +/// |
| 22 | +/// When building on top of these [TextStyles], callers that wish to specify |
| 23 | +/// a different font weight are still responsible for reprocessing the style |
| 24 | +/// with [weightVariableTextStyle] before passing it to a [Text]. |
| 25 | +/// (Widgets in the Material library won't do this; they aren't yet equipped |
| 26 | +/// to set font weights on variable-weight fonts. If this causes visible bugs, |
| 27 | +/// we should investigate and fix, but they should become less likely as |
| 28 | +/// we transition from Material's widgets to our own bespoke ones.) |
| 29 | +Typography zulipTypography(BuildContext context) { |
| 30 | + final typography = Theme.of(context).typography; |
| 31 | + return _weightVariableTypography(context, typography) |
| 32 | + .copyWith( |
| 33 | + black: typography.black.apply( |
| 34 | + fontFamily: kDefaultFontFamily, |
| 35 | + fontFamilyFallback: defaultFontFamilyFallback), |
| 36 | + white: typography.white.apply( |
| 37 | + fontFamily: kDefaultFontFamily, |
| 38 | + fontFamilyFallback: defaultFontFamilyFallback), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +/// Convert an input [Typography] to one that works with "wght"-variable fonts. |
| 43 | +/// |
| 44 | +/// Processes the "geometry" [TextTheme] fields |
| 45 | +/// ([Typography.dense], [Typography.englishLike], and [Typography.tall]) |
| 46 | +/// with [_weightVariableTextTheme], passing along the [BuildContext]. |
| 47 | +Typography _weightVariableTypography(BuildContext context, Typography input) => |
| 48 | + input.copyWith( |
| 49 | + dense: _weightVariableTextTheme(context, input.dense), |
| 50 | + englishLike: _weightVariableTextTheme(context, input.englishLike), |
| 51 | + tall: _weightVariableTextTheme(context, input.tall), |
| 52 | + ); |
| 53 | + |
| 54 | +/// Convert a geometry [TextTheme] to one that works with "wght"-variable fonts. |
| 55 | +/// |
| 56 | +/// A "geometry [TextTheme]" is a [TextTheme] that's meant to specify |
| 57 | +/// font weight and other parameters about shape, size, distance, etc. |
| 58 | +/// See [Typography]. |
| 59 | +/// |
| 60 | +/// This looks at each of the [TextStyle]s found on the input [TextTheme] |
| 61 | +/// (such as [TextTheme.bodyMedium]), |
| 62 | +/// and uses [weightVariableTextStyle] to adjust the [TextStyle]. |
| 63 | +/// Fields that are null in the input [TextTheme] remain null in the output. |
| 64 | +/// |
| 65 | +/// For each input [TextStyle], the `wght` value passed |
| 66 | +/// to [weightVariableTextStyle] is based on the input's [TextStyle.fontWeight]. |
| 67 | +/// A null [TextStyle.fontWeight] is interpreted as the normal font weight. |
| 68 | +TextTheme _weightVariableTextTheme(BuildContext context, TextTheme input) { |
| 69 | + TextStyle? convert(TextStyle? maybeInputStyle) { |
| 70 | + if (maybeInputStyle == null) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + final maybeInputFontWeight = maybeInputStyle.fontWeight; |
| 74 | + return maybeInputStyle.merge(weightVariableTextStyle(context, |
| 75 | + wght: maybeInputFontWeight != null |
| 76 | + ? wghtFromFontWeight(maybeInputFontWeight) |
| 77 | + : null)); |
| 78 | + } |
| 79 | + |
| 80 | + return TextTheme( |
| 81 | + displayLarge: convert(input.displayLarge), |
| 82 | + displayMedium: convert(input.displayMedium), |
| 83 | + displaySmall: convert(input.displaySmall), |
| 84 | + headlineLarge: convert(input.headlineLarge), |
| 85 | + headlineMedium: convert(input.headlineMedium), |
| 86 | + headlineSmall: convert(input.headlineSmall), |
| 87 | + titleLarge: convert(input.titleLarge), |
| 88 | + titleMedium: convert(input.titleMedium), |
| 89 | + titleSmall: convert(input.titleSmall), |
| 90 | + bodyLarge: convert(input.bodyLarge), |
| 91 | + bodyMedium: convert(input.bodyMedium), |
| 92 | + bodySmall: convert(input.bodySmall), |
| 93 | + labelLarge: convert(input.labelLarge), |
| 94 | + labelMedium: convert(input.labelMedium), |
| 95 | + labelSmall: convert(input.labelSmall), |
| 96 | + ); |
| 97 | +} |
4 | 98 |
|
5 | 99 | /// The [TextStyle.fontFamily] to use in most of the app.
|
6 | 100 | ///
|
@@ -133,3 +227,12 @@ FontWeight clampVariableFontWeight(double wght) {
|
133 | 227 | }
|
134 | 228 | }
|
135 | 229 | }
|
| 230 | + |
| 231 | +/// A good guess at a font's "wght" value to match a given [FontWeight]. |
| 232 | +/// |
| 233 | +/// Returns [FontWeight.value] as a double. |
| 234 | +/// |
| 235 | +/// This might not be exactly where the font designer would land on their |
| 236 | +/// font's own custom-defined "wght" axis. But it's a great guess, |
| 237 | +/// at least without knowledge of the particular font. |
| 238 | +double wghtFromFontWeight(FontWeight fontWeight) => fontWeight.value.toDouble(); |
0 commit comments