navigation.dart 7.8 KB

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