appearance.dart 12 KB

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