home_stack.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import 'package:appflowy/core/frameless_window.dart';
  2. import 'package:appflowy/plugins/blank/blank.dart';
  3. import 'package:appflowy/startup/plugin/plugin.dart';
  4. import 'package:appflowy/startup/startup.dart';
  5. import 'package:appflowy/workspace/presentation/home/home_sizes.dart';
  6. import 'package:appflowy/workspace/presentation/home/navigation.dart';
  7. import 'package:appflowy/workspace/presentation/home/toast.dart';
  8. import 'package:appflowy_backend/dispatch/dispatch.dart';
  9. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  10. import 'package:flowy_infra_ui/style_widget/extension.dart';
  11. import 'package:flowy_infra_ui/widget/spacing.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:provider/provider.dart';
  14. import 'package:time/time.dart';
  15. import 'home_layout.dart';
  16. typedef NavigationCallback = void Function(String id);
  17. abstract class HomeStackDelegate {
  18. void didDeleteStackWidget(ViewPB view, int? index);
  19. }
  20. class HomeStack extends StatelessWidget {
  21. final HomeStackDelegate delegate;
  22. final HomeLayout layout;
  23. const HomeStack({
  24. required this.delegate,
  25. required this.layout,
  26. Key? key,
  27. }) : super(key: key);
  28. @override
  29. Widget build(BuildContext context) {
  30. return Column(
  31. mainAxisAlignment: MainAxisAlignment.start,
  32. children: [
  33. getIt<HomeStackManager>().stackTopBar(layout: layout),
  34. Expanded(
  35. child: Container(
  36. color: Theme.of(context).colorScheme.surface,
  37. child: FocusTraversalGroup(
  38. child: getIt<HomeStackManager>().stackWidget(
  39. onDeleted: (view, index) {
  40. delegate.didDeleteStackWidget(view, index);
  41. },
  42. ),
  43. ),
  44. ),
  45. ),
  46. ],
  47. );
  48. }
  49. }
  50. class FadingIndexedStack extends StatefulWidget {
  51. final int index;
  52. final List<Widget> children;
  53. final Duration duration;
  54. const FadingIndexedStack({
  55. Key? key,
  56. required this.index,
  57. required this.children,
  58. this.duration = const Duration(
  59. milliseconds: 250,
  60. ),
  61. }) : super(key: key);
  62. @override
  63. FadingIndexedStackState createState() => FadingIndexedStackState();
  64. }
  65. class FadingIndexedStackState extends State<FadingIndexedStack> {
  66. double _targetOpacity = 1;
  67. @override
  68. void initState() {
  69. super.initState();
  70. initToastWithContext(context);
  71. }
  72. @override
  73. void didUpdateWidget(FadingIndexedStack oldWidget) {
  74. if (oldWidget.index == widget.index) return;
  75. setState(() => _targetOpacity = 0);
  76. Future.delayed(1.milliseconds, () => setState(() => _targetOpacity = 1));
  77. super.didUpdateWidget(oldWidget);
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. return TweenAnimationBuilder<double>(
  82. duration: _targetOpacity > 0 ? widget.duration : 0.milliseconds,
  83. tween: Tween(begin: 0, end: _targetOpacity),
  84. builder: (_, value, child) {
  85. return Opacity(opacity: value, child: child);
  86. },
  87. child: IndexedStack(index: widget.index, children: widget.children),
  88. );
  89. }
  90. }
  91. abstract mixin class NavigationItem {
  92. Widget get leftBarItem;
  93. Widget? get rightBarItem => null;
  94. NavigationCallback get action => (id) {
  95. getIt<HomeStackManager>().setStackWithId(id);
  96. };
  97. }
  98. class HomeStackNotifier extends ChangeNotifier {
  99. Plugin _plugin;
  100. Widget get titleWidget => _plugin.widgetBuilder.leftBarItem;
  101. HomeStackNotifier({Plugin? plugin})
  102. : _plugin = plugin ?? makePlugin(pluginType: PluginType.blank);
  103. /// This is the only place where the plugin is set.
  104. /// No need compare the old plugin with the new plugin. Just set it.
  105. set plugin(Plugin newPlugin) {
  106. _plugin.dispose();
  107. /// Set the plugin view as the latest view.
  108. FolderEventSetLatestView(ViewIdPB(value: newPlugin.id)).send();
  109. _plugin = newPlugin;
  110. notifyListeners();
  111. }
  112. Plugin get plugin => _plugin;
  113. }
  114. // HomeStack is initialized as singleton to control the page stack.
  115. class HomeStackManager {
  116. final HomeStackNotifier _notifier = HomeStackNotifier();
  117. HomeStackManager();
  118. Widget title() {
  119. return _notifier.plugin.widgetBuilder.leftBarItem;
  120. }
  121. Plugin get plugin => _notifier.plugin;
  122. void setPlugin(Plugin newPlugin) {
  123. _notifier.plugin = newPlugin;
  124. }
  125. void setStackWithId(String id) {
  126. // Navigate to the page with id
  127. }
  128. Widget stackTopBar({required HomeLayout layout}) {
  129. return MultiProvider(
  130. providers: [
  131. ChangeNotifierProvider.value(value: _notifier),
  132. ],
  133. child: Selector<HomeStackNotifier, Widget>(
  134. selector: (context, notifier) => notifier.titleWidget,
  135. builder: (context, widget, child) {
  136. return MoveWindowDetector(child: HomeTopBar(layout: layout));
  137. },
  138. ),
  139. );
  140. }
  141. Widget stackWidget({required Function(ViewPB, int?) onDeleted}) {
  142. return MultiProvider(
  143. providers: [ChangeNotifierProvider.value(value: _notifier)],
  144. child: Consumer(
  145. builder: (_, HomeStackNotifier notifier, __) {
  146. return FadingIndexedStack(
  147. index: getIt<PluginSandbox>().indexOf(notifier.plugin.pluginType),
  148. children: getIt<PluginSandbox>().supportPluginTypes.map(
  149. (pluginType) {
  150. if (pluginType == notifier.plugin.pluginType) {
  151. final builder = notifier.plugin.widgetBuilder;
  152. final pluginWidget = builder.buildWidget(
  153. context: PluginContext(onDeleted: onDeleted),
  154. );
  155. return Padding(
  156. padding: builder.contentPadding,
  157. child: pluginWidget,
  158. );
  159. } else {
  160. return const BlankPage();
  161. }
  162. },
  163. ).toList(),
  164. );
  165. },
  166. ),
  167. );
  168. }
  169. }
  170. class HomeTopBar extends StatelessWidget {
  171. const HomeTopBar({Key? key, required this.layout}) : super(key: key);
  172. final HomeLayout layout;
  173. @override
  174. Widget build(BuildContext context) {
  175. return Container(
  176. color: Theme.of(context).colorScheme.onSecondaryContainer,
  177. height: HomeSizes.topBarHeight,
  178. child: Padding(
  179. padding: const EdgeInsets.symmetric(
  180. horizontal: HomeInsets.topBarTitlePadding,
  181. ),
  182. child: Row(
  183. crossAxisAlignment: CrossAxisAlignment.center,
  184. children: [
  185. HSpace(layout.menuSpacing),
  186. const FlowyNavigation(),
  187. const HSpace(16),
  188. ChangeNotifierProvider.value(
  189. value: Provider.of<HomeStackNotifier>(context, listen: false),
  190. child: Consumer(
  191. builder: (_, HomeStackNotifier notifier, __) =>
  192. notifier.plugin.widgetBuilder.rightBarItem ??
  193. const SizedBox.shrink(),
  194. ),
  195. ),
  196. ],
  197. ),
  198. ).bottomBorder(color: Theme.of(context).dividerColor),
  199. );
  200. }
  201. }