appearance.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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(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. if (state.locale != newLocale) {
  50. context.setLocale(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. UserSettingsBackendService().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: _getTextTheme(fontFamily: fontFamily, fontColor: theme.text),
  187. textSelectionTheme: TextSelectionThemeData(
  188. cursorColor: theme.main2,
  189. selectionHandleColor: theme.main2,
  190. ),
  191. iconTheme: IconThemeData(color: theme.icon),
  192. tooltipTheme: TooltipThemeData(
  193. textStyle: _getFontStyle(
  194. fontFamily: fontFamily,
  195. fontSize: FontSizes.s11,
  196. fontWeight: FontWeight.w400,
  197. fontColor: theme.surface,
  198. ),
  199. ),
  200. scaffoldBackgroundColor: theme.surface,
  201. scrollbarTheme: ScrollbarThemeData(
  202. thumbColor: MaterialStateProperty.all(theme.shader3),
  203. thickness: MaterialStateProperty.resolveWith((states) {
  204. const Set<MaterialState> interactiveStates = <MaterialState>{
  205. MaterialState.pressed,
  206. MaterialState.hovered,
  207. MaterialState.dragged,
  208. };
  209. if (states.any(interactiveStates.contains)) {
  210. return 5.0;
  211. }
  212. return 3.0;
  213. }),
  214. crossAxisMargin: 0.0,
  215. mainAxisMargin: 0.0,
  216. radius: Corners.s10Radius,
  217. ),
  218. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  219. canvasColor: theme.shader6,
  220. dividerColor: theme.divider,
  221. hintColor: theme.hint,
  222. //action item hover color
  223. hoverColor: theme.hoverBG2,
  224. disabledColor: theme.shader4,
  225. highlightColor: theme.main1,
  226. indicatorColor: theme.main1,
  227. cardColor: theme.input,
  228. colorScheme: ColorScheme(
  229. brightness: brightness,
  230. primary: theme.primary,
  231. onPrimary: theme.onPrimary,
  232. primaryContainer: theme.main2,
  233. onPrimaryContainer: _white,
  234. // page title hover color
  235. secondary: theme.hoverBG1,
  236. onSecondary: theme.shader1,
  237. // setting value hover color
  238. secondaryContainer: theme.selector,
  239. onSecondaryContainer: theme.topbarBg,
  240. tertiary: theme.shader7,
  241. tertiaryContainer: theme.questionBubbleBG,
  242. background: theme.surface,
  243. onBackground: theme.text,
  244. surface: theme.surface,
  245. // text&icon color when it is hovered
  246. onSurface: theme.hoverFG,
  247. onError: theme.shader7,
  248. error: theme.red,
  249. outline: theme.shader4,
  250. surfaceVariant: theme.sidebarBg,
  251. shadow: theme.shadow,
  252. ),
  253. extensions: [
  254. AFThemeExtension(
  255. warning: theme.yellow,
  256. success: theme.green,
  257. tint1: theme.tint1,
  258. tint2: theme.tint2,
  259. tint3: theme.tint3,
  260. tint4: theme.tint4,
  261. tint5: theme.tint5,
  262. tint6: theme.tint6,
  263. tint7: theme.tint7,
  264. tint8: theme.tint8,
  265. tint9: theme.tint9,
  266. textColor: theme.text,
  267. greyHover: theme.hoverBG1,
  268. greySelect: theme.bg3,
  269. lightGreyHover: theme.hoverBG3,
  270. toggleOffFill: theme.shader5,
  271. progressBarBGcolor: theme.progressBarBGcolor,
  272. code: _getFontStyle(
  273. fontFamily: monospaceFontFamily,
  274. fontColor: theme.shader3,
  275. ),
  276. callout: _getFontStyle(
  277. fontFamily: fontFamily,
  278. fontSize: FontSizes.s11,
  279. fontColor: theme.shader3,
  280. ),
  281. caption: _getFontStyle(
  282. fontFamily: fontFamily,
  283. fontSize: FontSizes.s11,
  284. fontWeight: FontWeight.w400,
  285. fontColor: theme.hint,
  286. ),
  287. )
  288. ],
  289. );
  290. }
  291. TextStyle _getFontStyle({
  292. String? fontFamily,
  293. double? fontSize,
  294. FontWeight? fontWeight,
  295. Color? fontColor,
  296. double? letterSpacing,
  297. double? lineHeight,
  298. }) =>
  299. TextStyle(
  300. fontFamily: fontFamily,
  301. fontSize: fontSize ?? FontSizes.s12,
  302. color: fontColor,
  303. fontWeight: fontWeight ?? FontWeight.w500,
  304. fontFamilyFallback: const ["Noto Color Emoji"],
  305. letterSpacing: (fontSize ?? FontSizes.s12) * (letterSpacing ?? 0.005),
  306. height: lineHeight,
  307. );
  308. TextTheme _getTextTheme(
  309. {required String fontFamily, required Color fontColor}) {
  310. return TextTheme(
  311. displayLarge: _getFontStyle(
  312. fontFamily: fontFamily,
  313. fontSize: FontSizes.s32,
  314. fontColor: fontColor,
  315. fontWeight: FontWeight.w600,
  316. lineHeight: 42.0,
  317. ), // h2
  318. displayMedium: _getFontStyle(
  319. fontFamily: fontFamily,
  320. fontSize: FontSizes.s24,
  321. fontColor: fontColor,
  322. fontWeight: FontWeight.w600,
  323. lineHeight: 34.0,
  324. ), // h3
  325. displaySmall: _getFontStyle(
  326. fontFamily: fontFamily,
  327. fontSize: FontSizes.s20,
  328. fontColor: fontColor,
  329. fontWeight: FontWeight.w600,
  330. lineHeight: 28.0,
  331. ), // h4
  332. titleLarge: _getFontStyle(
  333. fontFamily: fontFamily,
  334. fontSize: FontSizes.s18,
  335. fontColor: fontColor,
  336. fontWeight: FontWeight.w600,
  337. ), // title
  338. titleMedium: _getFontStyle(
  339. fontFamily: fontFamily,
  340. fontSize: FontSizes.s16,
  341. fontColor: fontColor,
  342. fontWeight: FontWeight.w600,
  343. ), // heading
  344. titleSmall: _getFontStyle(
  345. fontFamily: fontFamily,
  346. fontSize: FontSizes.s14,
  347. fontColor: fontColor,
  348. fontWeight: FontWeight.w600,
  349. ), // subheading
  350. bodyMedium: _getFontStyle(
  351. fontFamily: fontFamily,
  352. fontColor: fontColor,
  353. ), // body-regular
  354. bodySmall: _getFontStyle(
  355. fontFamily: fontFamily,
  356. fontColor: fontColor,
  357. fontWeight: FontWeight.w400,
  358. ), // body-thin
  359. );
  360. }
  361. }