appearance.dart 12 KB

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