navigation.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/image.dart';
  6. import 'package:flowy_infra/notifier.dart';
  7. import 'package:flowy_infra/size.dart';
  8. import 'package:flowy_infra/text_style.dart';
  9. import 'package:flowy_infra/theme.dart';
  10. import 'package:flowy_infra_ui/style_widget/icon_button.dart';
  11. import 'package:flowy_infra_ui/style_widget/text.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:provider/provider.dart';
  14. import 'package:styled_widget/styled_widget.dart';
  15. import 'package:easy_localization/easy_localization.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. final theme = context.watch<AppTheme>();
  58. return ChangeNotifierProxyProvider<HomeStackNotifier, NavigationNotifier>(
  59. create: (_) {
  60. final notifier = Provider.of<HomeStackNotifier>(context, listen: false);
  61. return NavigationNotifier(
  62. navigationItems: notifier.plugin.display.navigationItems,
  63. collapasedNotifier: notifier.collapsedNotifier,
  64. );
  65. },
  66. update: (_, notifier, controller) => controller!..update(notifier),
  67. child: Expanded(
  68. child: Row(children: [
  69. Selector<NavigationNotifier, PublishNotifier<bool>>(
  70. selector: (context, notifier) => notifier.collapasedNotifier,
  71. builder: (ctx, collapsedNotifier, child) =>
  72. _renderCollapse(ctx, collapsedNotifier, theme)),
  73. Selector<NavigationNotifier, List<NavigationItem>>(
  74. selector: (context, notifier) => notifier.navigationItems,
  75. builder: (ctx, items, child) => Expanded(
  76. child: Row(
  77. children: _renderNavigationItems(items),
  78. // crossAxisAlignment: WrapCrossAlignment.start,
  79. ),
  80. ),
  81. ),
  82. ]),
  83. ),
  84. );
  85. }
  86. Widget _renderCollapse(BuildContext context,
  87. PublishNotifier<bool> collapsedNotifier, AppTheme theme) {
  88. return ChangeNotifierProvider.value(
  89. value: collapsedNotifier,
  90. child: Consumer(
  91. builder: (ctx, PublishNotifier<bool> notifier, child) {
  92. if (notifier.currentValue ?? false) {
  93. return RotationTransition(
  94. turns: const AlwaysStoppedAnimation(180 / 360),
  95. child: Tooltip(
  96. richMessage: sidebarTooltipTextSpan(),
  97. child: FlowyIconButton(
  98. width: 24,
  99. onPressed: () {
  100. notifier.value = false;
  101. ctx.read<HomeBloc>().add(const HomeEvent.collapseMenu());
  102. },
  103. iconPadding: const EdgeInsets.fromLTRB(2, 2, 2, 2),
  104. icon: svgWidget("home/hide_menu", color: theme.iconColor),
  105. )),
  106. );
  107. } else {
  108. return Container();
  109. }
  110. },
  111. ),
  112. );
  113. }
  114. List<Widget> _renderNavigationItems(List<NavigationItem> items) {
  115. if (items.isEmpty) {
  116. return [];
  117. }
  118. List<NavigationItem> newItems = _filter(items);
  119. Widget last = NaviItemWidget(newItems.removeLast());
  120. List<Widget> widgets = List.empty(growable: true);
  121. // widgets.addAll(newItems.map((item) => NaviItemDivider(child: NaviItemWidget(item))).toList());
  122. for (final item in newItems) {
  123. widgets.add(NaviItemWidget(item));
  124. widgets.add(const Text('/'));
  125. }
  126. widgets.add(last);
  127. return widgets;
  128. }
  129. List<NavigationItem> _filter(List<NavigationItem> items) {
  130. final length = items.length;
  131. if (length > 4) {
  132. final first = items[0];
  133. final ellipsisItems = items.getRange(1, length - 2).toList();
  134. final last = items.getRange(length - 2, length).toList();
  135. return [
  136. first,
  137. EllipsisNaviItem(items: ellipsisItems),
  138. ...last,
  139. ];
  140. } else {
  141. return items;
  142. }
  143. }
  144. }
  145. class NaviItemWidget extends StatelessWidget {
  146. final NavigationItem item;
  147. const NaviItemWidget(this.item, {Key? key}) : super(key: key);
  148. @override
  149. Widget build(BuildContext context) {
  150. return Expanded(
  151. child: item.leftBarItem.padding(horizontal: 2, vertical: 2));
  152. }
  153. }
  154. class NaviItemDivider extends StatelessWidget {
  155. final Widget child;
  156. const NaviItemDivider({Key? key, required this.child}) : super(key: key);
  157. @override
  158. Widget build(BuildContext context) {
  159. return Row(
  160. children: [child, const Text('/')],
  161. );
  162. }
  163. }
  164. class EllipsisNaviItem extends NavigationItem {
  165. final List<NavigationItem> items;
  166. EllipsisNaviItem({
  167. required this.items,
  168. });
  169. @override
  170. Widget get leftBarItem => const FlowyText.medium('...');
  171. @override
  172. NavigationCallback get action => (id) {};
  173. }
  174. TextSpan sidebarTooltipTextSpan() => TextSpan(
  175. children: [
  176. TextSpan(
  177. text: "${LocaleKeys.sideBar_openSidebar.tr()}\n",
  178. style: TextStyles.caption,
  179. ),
  180. TextSpan(
  181. text: Platform.isMacOS ? "⌘+\\" : "Ctrl+\\",
  182. style: TextStyles.general(
  183. fontSize: FontSizes.s11,
  184. color: Colors.white60,
  185. ),
  186. ),
  187. ],
  188. );