navigation.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import 'dart:io';
  2. import 'package:app_flowy/generated/locale_keys.g.dart';
  3. import 'package:app_flowy/workspace/application/home/home_setting_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. class FlowyNavigation extends StatelessWidget {
  34. const FlowyNavigation({Key? key}) : super(key: key);
  35. @override
  36. Widget build(BuildContext context) {
  37. return ChangeNotifierProxyProvider<HomeStackNotifier, NavigationNotifier>(
  38. create: (_) {
  39. final notifier = Provider.of<HomeStackNotifier>(context, listen: false);
  40. return NavigationNotifier(
  41. navigationItems: notifier.plugin.display.navigationItems,
  42. collapasedNotifier: notifier.collapsedNotifier,
  43. );
  44. },
  45. update: (_, notifier, controller) => controller!..update(notifier),
  46. child: Expanded(
  47. child: Row(children: [
  48. Selector<NavigationNotifier, PublishNotifier<bool>>(
  49. selector: (context, notifier) => notifier.collapasedNotifier,
  50. builder: (ctx, collapsedNotifier, child) =>
  51. _renderCollapse(ctx, collapsedNotifier)),
  52. Selector<NavigationNotifier, List<NavigationItem>>(
  53. selector: (context, notifier) => notifier.navigationItems,
  54. builder: (ctx, items, child) => Expanded(
  55. child: Row(
  56. children: _renderNavigationItems(items),
  57. // crossAxisAlignment: WrapCrossAlignment.start,
  58. ),
  59. ),
  60. ),
  61. ]),
  62. ),
  63. );
  64. }
  65. Widget _renderCollapse(
  66. BuildContext context, PublishNotifier<bool> collapsedNotifier) {
  67. return ChangeNotifierProvider.value(
  68. value: collapsedNotifier,
  69. child: Consumer(
  70. builder: (ctx, PublishNotifier<bool> notifier, child) {
  71. if (notifier.currentValue ?? false) {
  72. return RotationTransition(
  73. turns: const AlwaysStoppedAnimation(180 / 360),
  74. child: Tooltip(
  75. richMessage: sidebarTooltipTextSpan(
  76. context,
  77. LocaleKeys.sideBar_openSidebar.tr(),
  78. ),
  79. child: FlowyIconButton(
  80. width: 24,
  81. hoverColor: Colors.transparent,
  82. onPressed: () {
  83. notifier.value = false;
  84. ctx
  85. .read<HomeSettingBloc>()
  86. .add(const HomeSettingEvent.collapseMenu());
  87. },
  88. iconPadding: const EdgeInsets.fromLTRB(2, 2, 2, 2),
  89. icon: svgWidget(
  90. "home/hide_menu",
  91. color: Theme.of(context).colorScheme.onSurface,
  92. ),
  93. )),
  94. );
  95. } else {
  96. return Container();
  97. }
  98. },
  99. ),
  100. );
  101. }
  102. List<Widget> _renderNavigationItems(List<NavigationItem> items) {
  103. if (items.isEmpty) {
  104. return [];
  105. }
  106. List<NavigationItem> newItems = _filter(items);
  107. Widget last = NaviItemWidget(newItems.removeLast());
  108. List<Widget> widgets = List.empty(growable: true);
  109. // widgets.addAll(newItems.map((item) => NaviItemDivider(child: NaviItemWidget(item))).toList());
  110. for (final item in newItems) {
  111. widgets.add(NaviItemWidget(item));
  112. widgets.add(const Text('/'));
  113. }
  114. widgets.add(last);
  115. return widgets;
  116. }
  117. List<NavigationItem> _filter(List<NavigationItem> items) {
  118. final length = items.length;
  119. if (length > 4) {
  120. final first = items[0];
  121. final ellipsisItems = items.getRange(1, length - 2).toList();
  122. final last = items.getRange(length - 2, length).toList();
  123. return [
  124. first,
  125. EllipsisNaviItem(items: ellipsisItems),
  126. ...last,
  127. ];
  128. } else {
  129. return items;
  130. }
  131. }
  132. }
  133. class NaviItemWidget extends StatelessWidget {
  134. final NavigationItem item;
  135. const NaviItemWidget(this.item, {Key? key}) : super(key: key);
  136. @override
  137. Widget build(BuildContext context) {
  138. return Expanded(
  139. child: item.leftBarItem.padding(horizontal: 2, vertical: 2));
  140. }
  141. }
  142. class NaviItemDivider extends StatelessWidget {
  143. final Widget child;
  144. const NaviItemDivider({Key? key, required this.child}) : super(key: key);
  145. @override
  146. Widget build(BuildContext context) {
  147. return Row(
  148. children: [child, const Text('/')],
  149. );
  150. }
  151. }
  152. class EllipsisNaviItem extends NavigationItem {
  153. final List<NavigationItem> items;
  154. EllipsisNaviItem({
  155. required this.items,
  156. });
  157. @override
  158. Widget get leftBarItem => FlowyText.medium(
  159. '...',
  160. fontSize: FontSizes.s16,
  161. );
  162. @override
  163. NavigationCallback get action => (id) {};
  164. }
  165. TextSpan sidebarTooltipTextSpan(BuildContext context, String hintText) =>
  166. TextSpan(
  167. children: [
  168. TextSpan(
  169. text: "$hintText\n",
  170. style: AFThemeExtension.of(context).callout.textColor(Colors.white),
  171. ),
  172. TextSpan(
  173. text: Platform.isMacOS ? "⌘+\\" : "Ctrl+\\",
  174. style: AFThemeExtension.of(context).caption.textColor(Colors.white60),
  175. ),
  176. ],
  177. );