navigation.dart 7.3 KB

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