navigation.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import 'dart:io';
  2. import 'package:app_flowy/generated/locale_keys.g.dart';
  3. import 'package:app_flowy/workspace/application/home/home_bloc.dart';
  4. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  5. import 'package:flowy_infra/color_extension.dart';
  6. import 'package:flowy_infra/image.dart';
  7. import 'package:flowy_infra/notifier.dart';
  8. import 'package:flowy_infra/size.dart';
  9. import 'package:flowy_infra_ui/style_widget/icon_button.dart';
  10. import 'package:flowy_infra_ui/style_widget/text.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:provider/provider.dart';
  13. import 'package:styled_widget/styled_widget.dart';
  14. import 'package:easy_localization/easy_localization.dart';
  15. import 'package:textstyle_extensions/textstyle_extensions.dart';
  16. typedef NaviAction = void Function();
  17. class NavigationNotifier with ChangeNotifier {
  18. List<NavigationItem> navigationItems;
  19. PublishNotifier<bool> collapasedNotifier;
  20. NavigationNotifier(
  21. {required this.navigationItems, required this.collapasedNotifier});
  22. void update(HomeStackNotifier notifier) {
  23. bool shouldNotify = false;
  24. if (navigationItems != notifier.plugin.display.navigationItems) {
  25. navigationItems = notifier.plugin.display.navigationItems;
  26. shouldNotify = true;
  27. }
  28. if (shouldNotify) {
  29. notifyListeners();
  30. }
  31. }
  32. }
  33. // [[diagram: HomeStack navigation flow]]
  34. // ┌───────────────────────┐
  35. // 2.notify listeners ┌──────│DefaultHomeStackContext│
  36. // ┌────────────────┐ ┌───────────┐ ┌────────────────┐ │ └───────────────────────┘
  37. // │HomeStackNotifie│◀──────────│ HomeStack │◀──│HomeStackContext│◀─ impl
  38. // └────────────────┘ └───────────┘ └────────────────┘ │ ┌───────────────────┐
  39. // │ ▲ └───────│ DocStackContext │
  40. // │ │ └───────────────────┘
  41. // 3.notify change 1.set context
  42. // │ │
  43. // ▼ │
  44. // ┌───────────────────┐ ┌──────────────────┐
  45. // │NavigationNotifier │ │ ViewSectionItem │
  46. // └───────────────────┘ └──────────────────┘
  47. // │
  48. // │
  49. // ▼
  50. // ┌─────────────────┐
  51. // │ FlowyNavigation │ 4.render navigation items
  52. // └─────────────────┘
  53. class FlowyNavigation extends StatelessWidget {
  54. const FlowyNavigation({Key? key}) : super(key: key);
  55. @override
  56. Widget build(BuildContext context) {
  57. return ChangeNotifierProxyProvider<HomeStackNotifier, NavigationNotifier>(
  58. create: (_) {
  59. final notifier = Provider.of<HomeStackNotifier>(context, listen: false);
  60. return NavigationNotifier(
  61. navigationItems: notifier.plugin.display.navigationItems,
  62. collapasedNotifier: notifier.collapsedNotifier,
  63. );
  64. },
  65. update: (_, notifier, controller) => controller!..update(notifier),
  66. child: Expanded(
  67. child: Row(children: [
  68. Selector<NavigationNotifier, PublishNotifier<bool>>(
  69. selector: (context, notifier) => notifier.collapasedNotifier,
  70. builder: (ctx, collapsedNotifier, child) =>
  71. _renderCollapse(ctx, collapsedNotifier)),
  72. Selector<NavigationNotifier, List<NavigationItem>>(
  73. selector: (context, notifier) => notifier.navigationItems,
  74. builder: (ctx, items, child) => Expanded(
  75. child: Row(
  76. children: _renderNavigationItems(items),
  77. // crossAxisAlignment: WrapCrossAlignment.start,
  78. ),
  79. ),
  80. ),
  81. ]),
  82. ),
  83. );
  84. }
  85. Widget _renderCollapse(
  86. BuildContext context, PublishNotifier<bool> collapsedNotifier) {
  87. return ChangeNotifierProvider.value(
  88. value: collapsedNotifier,
  89. child: Consumer(
  90. builder: (ctx, PublishNotifier<bool> notifier, child) {
  91. if (notifier.currentValue ?? false) {
  92. return RotationTransition(
  93. turns: const AlwaysStoppedAnimation(180 / 360),
  94. child: Tooltip(
  95. richMessage: sidebarTooltipTextSpan(
  96. context,
  97. LocaleKeys.sideBar_openSidebar.tr(),
  98. ),
  99. child: FlowyIconButton(
  100. width: 24,
  101. hoverColor: Colors.transparent,
  102. onPressed: () {
  103. notifier.value = false;
  104. ctx.read<HomeBloc>().add(const HomeEvent.collapseMenu());
  105. },
  106. iconPadding: const EdgeInsets.fromLTRB(2, 2, 2, 2),
  107. icon: svgWidget(
  108. "home/hide_menu",
  109. color: Theme.of(context).colorScheme.onSurface,
  110. ),
  111. )),
  112. );
  113. } else {
  114. return Container();
  115. }
  116. },
  117. ),
  118. );
  119. }
  120. List<Widget> _renderNavigationItems(List<NavigationItem> items) {
  121. if (items.isEmpty) {
  122. return [];
  123. }
  124. List<NavigationItem> newItems = _filter(items);
  125. Widget last = NaviItemWidget(newItems.removeLast());
  126. List<Widget> widgets = List.empty(growable: true);
  127. // widgets.addAll(newItems.map((item) => NaviItemDivider(child: NaviItemWidget(item))).toList());
  128. for (final item in newItems) {
  129. widgets.add(NaviItemWidget(item));
  130. widgets.add(const Text('/'));
  131. }
  132. widgets.add(last);
  133. return widgets;
  134. }
  135. List<NavigationItem> _filter(List<NavigationItem> items) {
  136. final length = items.length;
  137. if (length > 4) {
  138. final first = items[0];
  139. final ellipsisItems = items.getRange(1, length - 2).toList();
  140. final last = items.getRange(length - 2, length).toList();
  141. return [
  142. first,
  143. EllipsisNaviItem(items: ellipsisItems),
  144. ...last,
  145. ];
  146. } else {
  147. return items;
  148. }
  149. }
  150. }
  151. class NaviItemWidget extends StatelessWidget {
  152. final NavigationItem item;
  153. const NaviItemWidget(this.item, {Key? key}) : super(key: key);
  154. @override
  155. Widget build(BuildContext context) {
  156. return Expanded(
  157. child: item.leftBarItem.padding(horizontal: 2, vertical: 2));
  158. }
  159. }
  160. class NaviItemDivider extends StatelessWidget {
  161. final Widget child;
  162. const NaviItemDivider({Key? key, required this.child}) : super(key: key);
  163. @override
  164. Widget build(BuildContext context) {
  165. return Row(
  166. children: [child, const Text('/')],
  167. );
  168. }
  169. }
  170. class EllipsisNaviItem extends NavigationItem {
  171. final List<NavigationItem> items;
  172. EllipsisNaviItem({
  173. required this.items,
  174. });
  175. @override
  176. Widget get leftBarItem => FlowyText.medium(
  177. '...',
  178. fontSize: FontSizes.s16,
  179. );
  180. @override
  181. NavigationCallback get action => (id) {};
  182. }
  183. TextSpan sidebarTooltipTextSpan(BuildContext context, String hintText) =>
  184. TextSpan(
  185. children: [
  186. TextSpan(
  187. text: "$hintText\n",
  188. style: AFThemeExtension.of(context).callout.textColor(Colors.white),
  189. ),
  190. TextSpan(
  191. text: Platform.isMacOS ? "⌘+\\" : "Ctrl+\\",
  192. style: AFThemeExtension.of(context).caption.textColor(Colors.white60),
  193. ),
  194. ],
  195. );