appearance.dart 13 KB

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