appearance.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. const Set<MaterialState> scrollbarInteractiveStates = <MaterialState>{
  216. MaterialState.pressed,
  217. MaterialState.hovered,
  218. MaterialState.dragged,
  219. };
  220. return ThemeData(
  221. brightness: brightness,
  222. dialogBackgroundColor: theme.surface,
  223. textTheme: _getTextTheme(fontFamily: fontFamily, fontColor: theme.text),
  224. textSelectionTheme: TextSelectionThemeData(
  225. cursorColor: theme.main2,
  226. selectionHandleColor: theme.main2,
  227. ),
  228. iconTheme: IconThemeData(color: theme.icon),
  229. tooltipTheme: TooltipThemeData(
  230. textStyle: _getFontStyle(
  231. fontFamily: fontFamily,
  232. fontSize: FontSizes.s11,
  233. fontWeight: FontWeight.w400,
  234. fontColor: theme.surface,
  235. ),
  236. ),
  237. scaffoldBackgroundColor: theme.surface,
  238. snackBarTheme: SnackBarThemeData(
  239. backgroundColor: colorScheme.primary,
  240. contentTextStyle: TextStyle(color: colorScheme.onSurface),
  241. ),
  242. scrollbarTheme: ScrollbarThemeData(
  243. thumbColor: MaterialStateProperty.resolveWith((states) {
  244. if (states.any(scrollbarInteractiveStates.contains)) {
  245. return theme.shader7;
  246. }
  247. return theme.shader5;
  248. }),
  249. thickness: MaterialStateProperty.resolveWith((states) {
  250. if (states.any(scrollbarInteractiveStates.contains)) {
  251. return 4;
  252. }
  253. return 3.0;
  254. }),
  255. crossAxisMargin: 0.0,
  256. mainAxisMargin: 6.0,
  257. radius: Corners.s10Radius,
  258. ),
  259. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  260. //dropdown menu color
  261. canvasColor: theme.surface,
  262. dividerColor: theme.divider,
  263. hintColor: theme.hint,
  264. //action item hover color
  265. hoverColor: theme.hoverBG2,
  266. disabledColor: theme.shader4,
  267. highlightColor: theme.main1,
  268. indicatorColor: theme.main1,
  269. cardColor: theme.input,
  270. colorScheme: colorScheme,
  271. extensions: [
  272. AFThemeExtension(
  273. warning: theme.yellow,
  274. success: theme.green,
  275. tint1: theme.tint1,
  276. tint2: theme.tint2,
  277. tint3: theme.tint3,
  278. tint4: theme.tint4,
  279. tint5: theme.tint5,
  280. tint6: theme.tint6,
  281. tint7: theme.tint7,
  282. tint8: theme.tint8,
  283. tint9: theme.tint9,
  284. textColor: theme.text,
  285. greyHover: theme.hoverBG1,
  286. greySelect: theme.bg3,
  287. lightGreyHover: theme.hoverBG3,
  288. toggleOffFill: theme.shader5,
  289. progressBarBGColor: theme.progressBarBGColor,
  290. toggleButtonBGColor: theme.toggleButtonBGColor,
  291. code: _getFontStyle(
  292. fontFamily: monospaceFontFamily,
  293. fontColor: theme.shader3,
  294. ),
  295. callout: _getFontStyle(
  296. fontFamily: fontFamily,
  297. fontSize: FontSizes.s11,
  298. fontColor: theme.shader3,
  299. ),
  300. caption: _getFontStyle(
  301. fontFamily: fontFamily,
  302. fontSize: FontSizes.s11,
  303. fontWeight: FontWeight.w400,
  304. fontColor: theme.hint,
  305. ),
  306. )
  307. ],
  308. );
  309. }
  310. TextStyle _getFontStyle({
  311. String? fontFamily,
  312. double? fontSize,
  313. FontWeight? fontWeight,
  314. Color? fontColor,
  315. double? letterSpacing,
  316. double? lineHeight,
  317. }) =>
  318. TextStyle(
  319. fontFamily: fontFamily,
  320. fontSize: fontSize ?? FontSizes.s12,
  321. color: fontColor,
  322. fontWeight: fontWeight ?? FontWeight.w500,
  323. fontFamilyFallback: const ["Noto Color Emoji"],
  324. letterSpacing: (fontSize ?? FontSizes.s12) * (letterSpacing ?? 0.005),
  325. height: lineHeight,
  326. );
  327. TextTheme _getTextTheme({
  328. required String fontFamily,
  329. required Color fontColor,
  330. }) {
  331. return TextTheme(
  332. displayLarge: _getFontStyle(
  333. fontFamily: fontFamily,
  334. fontSize: FontSizes.s32,
  335. fontColor: fontColor,
  336. fontWeight: FontWeight.w600,
  337. lineHeight: 42.0,
  338. ), // h2
  339. displayMedium: _getFontStyle(
  340. fontFamily: fontFamily,
  341. fontSize: FontSizes.s24,
  342. fontColor: fontColor,
  343. fontWeight: FontWeight.w600,
  344. lineHeight: 34.0,
  345. ), // h3
  346. displaySmall: _getFontStyle(
  347. fontFamily: fontFamily,
  348. fontSize: FontSizes.s20,
  349. fontColor: fontColor,
  350. fontWeight: FontWeight.w600,
  351. lineHeight: 28.0,
  352. ), // h4
  353. titleLarge: _getFontStyle(
  354. fontFamily: fontFamily,
  355. fontSize: FontSizes.s18,
  356. fontColor: fontColor,
  357. fontWeight: FontWeight.w600,
  358. ), // title
  359. titleMedium: _getFontStyle(
  360. fontFamily: fontFamily,
  361. fontSize: FontSizes.s16,
  362. fontColor: fontColor,
  363. fontWeight: FontWeight.w600,
  364. ), // heading
  365. titleSmall: _getFontStyle(
  366. fontFamily: fontFamily,
  367. fontSize: FontSizes.s14,
  368. fontColor: fontColor,
  369. fontWeight: FontWeight.w600,
  370. ), // subheading
  371. bodyMedium: _getFontStyle(
  372. fontFamily: fontFamily,
  373. fontColor: fontColor,
  374. ), // body-regular
  375. bodySmall: _getFontStyle(
  376. fontFamily: fontFamily,
  377. fontColor: fontColor,
  378. fontWeight: FontWeight.w400,
  379. ), // body-thin
  380. );
  381. }
  382. }