appearance.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import 'dart:async';
  2. import 'package:app_flowy/user/application/user_settings_service.dart';
  3. import 'package:appflowy_backend/log.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-user/user_setting.pb.dart';
  5. import 'package:easy_localization/easy_localization.dart';
  6. import 'package:flowy_infra/size.dart';
  7. import 'package:flowy_infra/theme.dart';
  8. import 'package:flowy_infra/theme_extension.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. import 'package:freezed_annotation/freezed_annotation.dart';
  12. part 'appearance.freezed.dart';
  13. const _white = Color(0xFFFFFFFF);
  14. /// [AppearanceSettingsCubit] is used to modify the appearance of AppFlowy.
  15. /// It includes the [AppTheme], [ThemeMode], [TextStyles] and [Locale].
  16. class AppearanceSettingsCubit extends Cubit<AppearanceSettingsState> {
  17. final AppearanceSettingsPB _setting;
  18. AppearanceSettingsCubit(AppearanceSettingsPB setting)
  19. : _setting = setting,
  20. super(AppearanceSettingsState.initial(
  21. setting.theme,
  22. setting.themeMode,
  23. setting.font,
  24. setting.monospaceFont,
  25. setting.locale,
  26. setting.isMenuCollapsed,
  27. setting.menuOffset,
  28. ));
  29. /// Update selected theme in the user's settings and emit an updated state
  30. /// with the AppTheme named [themeName].
  31. void setTheme(String themeName) {
  32. _setting.theme = themeName;
  33. _saveAppearanceSettings();
  34. emit(state.copyWith(appTheme: AppTheme.fromName(themeName)));
  35. }
  36. /// Update the theme mode in the user's settings and emit an updated state.
  37. void setThemeMode(ThemeMode themeMode) {
  38. _setting.themeMode = _themeModeToPB(themeMode);
  39. _saveAppearanceSettings();
  40. emit(state.copyWith(themeMode: themeMode));
  41. }
  42. /// Updates the current locale and notify the listeners the locale was
  43. /// changed. Fallback to [en] locale if [newLocale] is not supported.
  44. void setLocale(BuildContext context, Locale newLocale) {
  45. if (!context.supportedLocales.contains(newLocale)) {
  46. Log.warn("Unsupported locale: $newLocale, Fallback to locale: en");
  47. newLocale = const Locale('en');
  48. }
  49. context.setLocale(newLocale);
  50. if (state.locale != newLocale) {
  51. _setting.locale.languageCode = newLocale.languageCode;
  52. _setting.locale.countryCode = newLocale.countryCode ?? "";
  53. _saveAppearanceSettings();
  54. emit(state.copyWith(locale: newLocale));
  55. }
  56. }
  57. // Saves the menus current visibility
  58. void saveIsMenuCollapsed(bool collapsed) {
  59. _setting.isMenuCollapsed = collapsed;
  60. _saveAppearanceSettings();
  61. }
  62. // Saves the current resize offset of the menu
  63. void saveMenuOffset(double offset) {
  64. _setting.menuOffset = offset;
  65. _saveAppearanceSettings();
  66. }
  67. /// Saves key/value setting to disk.
  68. /// Removes the key if the passed in value is null
  69. void setKeyValue(String key, String? value) {
  70. if (key.isEmpty) {
  71. Log.warn("The key should not be empty");
  72. return;
  73. }
  74. if (value == null) {
  75. _setting.settingKeyValue.remove(key);
  76. }
  77. if (_setting.settingKeyValue[key] != value) {
  78. if (value == null) {
  79. _setting.settingKeyValue.remove(key);
  80. } else {
  81. _setting.settingKeyValue[key] = value;
  82. }
  83. }
  84. _saveAppearanceSettings();
  85. }
  86. String? getValue(String key) {
  87. if (key.isEmpty) {
  88. Log.warn("The key should not be empty");
  89. return null;
  90. }
  91. return _setting.settingKeyValue[key];
  92. }
  93. /// Called when the application launches.
  94. /// Uses the device locale when the application is opened for the first time.
  95. void readLocaleWhenAppLaunch(BuildContext context) {
  96. if (_setting.resetToDefault) {
  97. _setting.resetToDefault = false;
  98. _saveAppearanceSettings();
  99. setLocale(context, context.deviceLocale);
  100. return;
  101. }
  102. setLocale(context, state.locale);
  103. }
  104. Future<void> _saveAppearanceSettings() async {
  105. SettingsFFIService().setAppearanceSetting(_setting).then((result) {
  106. result.fold(
  107. (l) => null,
  108. (error) => Log.error(error),
  109. );
  110. });
  111. }
  112. }
  113. ThemeMode _themeModeFromPB(ThemeModePB themeModePB) {
  114. switch (themeModePB) {
  115. case ThemeModePB.Light:
  116. return ThemeMode.light;
  117. case ThemeModePB.Dark:
  118. return ThemeMode.dark;
  119. case ThemeModePB.System:
  120. default:
  121. return ThemeMode.system;
  122. }
  123. }
  124. ThemeModePB _themeModeToPB(ThemeMode themeMode) {
  125. switch (themeMode) {
  126. case ThemeMode.light:
  127. return ThemeModePB.Light;
  128. case ThemeMode.dark:
  129. return ThemeModePB.Dark;
  130. case ThemeMode.system:
  131. default:
  132. return ThemeModePB.System;
  133. }
  134. }
  135. @freezed
  136. class AppearanceSettingsState with _$AppearanceSettingsState {
  137. const AppearanceSettingsState._();
  138. const factory AppearanceSettingsState({
  139. required AppTheme appTheme,
  140. required ThemeMode themeMode,
  141. required String font,
  142. required String monospaceFont,
  143. required Locale locale,
  144. required bool isMenuCollapsed,
  145. required double menuOffset,
  146. }) = _AppearanceSettingsState;
  147. factory AppearanceSettingsState.initial(
  148. String themeName,
  149. ThemeModePB themeModePB,
  150. String font,
  151. String monospaceFont,
  152. LocaleSettingsPB localePB,
  153. bool isMenuCollapsed,
  154. double menuOffset,
  155. ) {
  156. return AppearanceSettingsState(
  157. appTheme: AppTheme.fromName(themeName),
  158. font: font,
  159. monospaceFont: monospaceFont,
  160. themeMode: _themeModeFromPB(themeModePB),
  161. locale: Locale(localePB.languageCode, localePB.countryCode),
  162. isMenuCollapsed: isMenuCollapsed,
  163. menuOffset: menuOffset,
  164. );
  165. }
  166. ThemeData get lightTheme => _getThemeData(Brightness.light);
  167. ThemeData get darkTheme => _getThemeData(Brightness.dark);
  168. ThemeData _getThemeData(Brightness brightness) {
  169. // Poppins and SF Mono are not well supported in some languages, so use the
  170. // built-in font for the following languages.
  171. final useBuiltInFontLanguages = [
  172. const Locale('zh', 'CN'),
  173. const Locale('zh', 'TW'),
  174. ];
  175. String fontFamily = font;
  176. String monospaceFontFamily = monospaceFont;
  177. if (useBuiltInFontLanguages.contains(locale)) {
  178. fontFamily = '';
  179. monospaceFontFamily = '';
  180. }
  181. final theme = brightness == Brightness.light
  182. ? appTheme.lightTheme
  183. : appTheme.darkTheme;
  184. return ThemeData(
  185. brightness: brightness,
  186. textTheme:
  187. _getTextTheme(fontFamily: fontFamily, fontColor: theme.shader1),
  188. textSelectionTheme: TextSelectionThemeData(
  189. cursorColor: theme.main2,
  190. selectionHandleColor: theme.main2,
  191. ),
  192. primaryIconTheme: IconThemeData(color: theme.hover),
  193. iconTheme: IconThemeData(color: theme.shader1),
  194. scrollbarTheme: ScrollbarThemeData(
  195. thumbColor: MaterialStateProperty.all(theme.shader3),
  196. thickness: MaterialStateProperty.resolveWith((states) {
  197. const Set<MaterialState> interactiveStates = <MaterialState>{
  198. MaterialState.pressed,
  199. MaterialState.hovered,
  200. MaterialState.dragged,
  201. };
  202. if (states.any(interactiveStates.contains)) {
  203. return 5.0;
  204. }
  205. return 3.0;
  206. }),
  207. crossAxisMargin: 0.0,
  208. mainAxisMargin: 0.0,
  209. radius: Corners.s10Radius,
  210. ),
  211. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  212. canvasColor: theme.shader6,
  213. dividerColor: theme.shader6,
  214. hintColor: theme.shader3,
  215. disabledColor: theme.shader4,
  216. highlightColor: theme.main1,
  217. indicatorColor: theme.main1,
  218. toggleableActiveColor: theme.main1,
  219. colorScheme: ColorScheme(
  220. brightness: brightness,
  221. primary: theme.main1,
  222. onPrimary: _white,
  223. primaryContainer: theme.main2,
  224. onPrimaryContainer: _white,
  225. secondary: theme.hover,
  226. onSecondary: theme.shader1,
  227. secondaryContainer: theme.selector,
  228. onSecondaryContainer: theme.shader1,
  229. background: theme.surface,
  230. onBackground: theme.shader1,
  231. surface: theme.surface,
  232. onSurface: theme.shader1,
  233. onError: theme.shader7,
  234. error: theme.red,
  235. outline: theme.shader4,
  236. surfaceVariant: theme.bg1,
  237. shadow: theme.shadow,
  238. ),
  239. extensions: [
  240. AFThemeExtension(
  241. warning: theme.yellow,
  242. success: theme.green,
  243. tint1: theme.tint1,
  244. tint2: theme.tint2,
  245. tint3: theme.tint3,
  246. tint4: theme.tint4,
  247. tint5: theme.tint5,
  248. tint6: theme.tint6,
  249. tint7: theme.tint7,
  250. tint8: theme.tint8,
  251. tint9: theme.tint9,
  252. greyHover: theme.bg2,
  253. greySelect: theme.bg3,
  254. lightGreyHover: theme.shader6,
  255. toggleOffFill: theme.shader5,
  256. code: _getFontStyle(
  257. fontFamily: monospaceFontFamily,
  258. fontColor: theme.shader3,
  259. ),
  260. callout: _getFontStyle(
  261. fontFamily: fontFamily,
  262. fontSize: FontSizes.s11,
  263. fontColor: theme.shader3,
  264. ),
  265. caption: _getFontStyle(
  266. fontFamily: fontFamily,
  267. fontSize: FontSizes.s11,
  268. fontWeight: FontWeight.w400,
  269. fontColor: theme.shader3,
  270. ),
  271. )
  272. ],
  273. );
  274. }
  275. TextStyle _getFontStyle({
  276. String? fontFamily,
  277. double? fontSize,
  278. FontWeight? fontWeight,
  279. Color? fontColor,
  280. double? letterSpacing,
  281. double? lineHeight,
  282. }) =>
  283. TextStyle(
  284. fontFamily: fontFamily,
  285. fontSize: fontSize ?? FontSizes.s12,
  286. color: fontColor,
  287. fontWeight: fontWeight ?? FontWeight.w500,
  288. fontFamilyFallback: const ["Noto Color Emoji"],
  289. letterSpacing: (fontSize ?? FontSizes.s12) * (letterSpacing ?? 0.005),
  290. height: lineHeight,
  291. );
  292. TextTheme _getTextTheme(
  293. {required String fontFamily, required Color fontColor}) {
  294. return TextTheme(
  295. displayLarge: _getFontStyle(
  296. fontFamily: fontFamily,
  297. fontSize: FontSizes.s32,
  298. fontColor: fontColor,
  299. fontWeight: FontWeight.w600,
  300. lineHeight: 42.0,
  301. ), // h2
  302. displayMedium: _getFontStyle(
  303. fontFamily: fontFamily,
  304. fontSize: FontSizes.s24,
  305. fontColor: fontColor,
  306. fontWeight: FontWeight.w600,
  307. lineHeight: 34.0,
  308. ), // h3
  309. displaySmall: _getFontStyle(
  310. fontFamily: fontFamily,
  311. fontSize: FontSizes.s20,
  312. fontColor: fontColor,
  313. fontWeight: FontWeight.w600,
  314. lineHeight: 28.0,
  315. ), // h4
  316. titleLarge: _getFontStyle(
  317. fontFamily: fontFamily,
  318. fontSize: FontSizes.s18,
  319. fontColor: fontColor,
  320. fontWeight: FontWeight.w600,
  321. ), // title
  322. titleMedium: _getFontStyle(
  323. fontFamily: fontFamily,
  324. fontSize: FontSizes.s16,
  325. fontColor: fontColor,
  326. fontWeight: FontWeight.w600,
  327. ), // heading
  328. titleSmall: _getFontStyle(
  329. fontFamily: fontFamily,
  330. fontSize: FontSizes.s14,
  331. fontColor: fontColor,
  332. fontWeight: FontWeight.w600,
  333. ), // subheading
  334. bodyMedium: _getFontStyle(
  335. fontFamily: fontFamily,
  336. fontColor: fontColor,
  337. ), // body-regular
  338. bodySmall: _getFontStyle(
  339. fontFamily: fontFamily,
  340. fontColor: fontColor,
  341. fontWeight: FontWeight.w400,
  342. ), // body-thin
  343. );
  344. }
  345. }