home_stack.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:io' show Platform;
  2. import 'package:app_flowy/startup/startup.dart';
  3. import 'package:app_flowy/workspace/application/home/home_bloc.dart';
  4. import 'package:app_flowy/plugins/blank/blank.dart';
  5. import 'package:app_flowy/workspace/presentation/home/toast.dart';
  6. import 'package:flowy_infra/theme.dart';
  7. import 'package:flowy_sdk/log.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. import 'package:provider/provider.dart';
  11. import 'package:time/time.dart';
  12. import 'package:app_flowy/startup/plugin/plugin.dart';
  13. import 'package:app_flowy/workspace/presentation/home/home_sizes.dart';
  14. import 'package:app_flowy/workspace/presentation/home/navigation.dart';
  15. import 'package:app_flowy/core/frameless_window.dart';
  16. import 'package:flowy_infra_ui/widget/spacing.dart';
  17. import 'package:flowy_infra_ui/style_widget/extension.dart';
  18. import 'package:flowy_infra/notifier.dart';
  19. typedef NavigationCallback = void Function(String id);
  20. class HomeStack extends StatelessWidget {
  21. const HomeStack({Key? key}) : super(key: key);
  22. @override
  23. Widget build(BuildContext context) {
  24. Log.info('HomePage build');
  25. final theme = context.watch<AppTheme>();
  26. return Column(
  27. mainAxisAlignment: MainAxisAlignment.start,
  28. children: [
  29. getIt<HomeStackManager>().stackTopBar(),
  30. Expanded(
  31. child: Container(
  32. color: theme.surface,
  33. child: FocusTraversalGroup(
  34. child: getIt<HomeStackManager>().stackWidget(),
  35. ),
  36. ),
  37. ),
  38. ],
  39. );
  40. }
  41. }
  42. class FadingIndexedStack extends StatefulWidget {
  43. final int index;
  44. final List<Widget> children;
  45. final Duration duration;
  46. const FadingIndexedStack({
  47. Key? key,
  48. required this.index,
  49. required this.children,
  50. this.duration = const Duration(
  51. milliseconds: 250,
  52. ),
  53. }) : super(key: key);
  54. @override
  55. _FadingIndexedStackState createState() => _FadingIndexedStackState();
  56. }
  57. class _FadingIndexedStackState extends State<FadingIndexedStack> {
  58. double _targetOpacity = 1;
  59. @override
  60. void initState() {
  61. super.initState();
  62. initToastWithContext(context);
  63. }
  64. @override
  65. void didUpdateWidget(FadingIndexedStack oldWidget) {
  66. if (oldWidget.index == widget.index) return;
  67. setState(() => _targetOpacity = 0);
  68. Future.delayed(1.milliseconds, () => setState(() => _targetOpacity = 1));
  69. super.didUpdateWidget(oldWidget);
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return TweenAnimationBuilder<double>(
  74. duration: _targetOpacity > 0 ? widget.duration : 0.milliseconds,
  75. tween: Tween(begin: 0, end: _targetOpacity),
  76. builder: (_, value, child) {
  77. return Opacity(opacity: value, child: child);
  78. },
  79. child: IndexedStack(index: widget.index, children: widget.children),
  80. );
  81. }
  82. }
  83. abstract class NavigationItem {
  84. Widget get leftBarItem;
  85. Widget? get rightBarItem => null;
  86. NavigationCallback get action => (id) {
  87. getIt<HomeStackManager>().setStackWithId(id);
  88. };
  89. }
  90. class HomeStackNotifier extends ChangeNotifier {
  91. Plugin _plugin;
  92. PublishNotifier<bool> collapsedNotifier = PublishNotifier();
  93. Widget get titleWidget => _plugin.display.leftBarItem;
  94. HomeStackNotifier({Plugin? plugin})
  95. : _plugin = plugin ?? makePlugin(pluginType: DefaultPlugin.blank.type());
  96. set plugin(Plugin newPlugin) {
  97. if (newPlugin.id == _plugin.id) {
  98. return;
  99. }
  100. _plugin.display.notifier?.removeListener(notifyListeners);
  101. _plugin.dispose();
  102. _plugin = newPlugin;
  103. _plugin.display.notifier?.addListener(notifyListeners);
  104. notifyListeners();
  105. }
  106. Plugin get plugin => _plugin;
  107. }
  108. // HomeStack is initialized as singleton to controll the page stack.
  109. class HomeStackManager {
  110. final HomeStackNotifier _notifier = HomeStackNotifier();
  111. HomeStackManager();
  112. Widget title() {
  113. return _notifier.plugin.display.leftBarItem;
  114. }
  115. PublishNotifier<bool> get collapsedNotifier => _notifier.collapsedNotifier;
  116. void setPlugin(Plugin newPlugin) {
  117. _notifier.plugin = newPlugin;
  118. }
  119. void setStackWithId(String id) {}
  120. Widget stackTopBar() {
  121. return MultiProvider(
  122. providers: [
  123. ChangeNotifierProvider.value(value: _notifier),
  124. ],
  125. child: Selector<HomeStackNotifier, Widget>(
  126. selector: (context, notifier) => notifier.titleWidget,
  127. builder: (context, widget, child) {
  128. return const MoveWindowDetector(child: HomeTopBar());
  129. },
  130. ),
  131. );
  132. }
  133. Widget stackWidget() {
  134. return MultiProvider(
  135. providers: [
  136. ChangeNotifierProvider.value(value: _notifier),
  137. ],
  138. child: Consumer(builder: (ctx, HomeStackNotifier notifier, child) {
  139. return FadingIndexedStack(
  140. index: getIt<PluginSandbox>().indexOf(notifier.plugin.ty),
  141. children: getIt<PluginSandbox>().supportPluginTypes.map((pluginType) {
  142. if (pluginType == notifier.plugin.ty) {
  143. return notifier.plugin.display
  144. .buildWidget()
  145. .padding(horizontal: 40, vertical: 28);
  146. } else {
  147. return const BlankPage();
  148. }
  149. }).toList(),
  150. );
  151. }),
  152. );
  153. }
  154. }
  155. class HomeTopBar extends StatelessWidget {
  156. const HomeTopBar({Key? key}) : super(key: key);
  157. @override
  158. Widget build(BuildContext context) {
  159. final theme = context.watch<AppTheme>();
  160. return Container(
  161. color: theme.surface,
  162. height: HomeSizes.topBarHeight,
  163. child: Row(
  164. crossAxisAlignment: CrossAxisAlignment.center,
  165. children: [
  166. BlocBuilder<HomeBloc, HomeState>(
  167. buildWhen: ((previous, current) =>
  168. previous.isMenuCollapsed != current.isMenuCollapsed),
  169. builder: (context, state) {
  170. if (state.isMenuCollapsed && Platform.isMacOS) {
  171. return const HSpace(80);
  172. }
  173. return const HSpace(0);
  174. }),
  175. const FlowyNavigation(),
  176. const HSpace(16),
  177. ChangeNotifierProvider.value(
  178. value: Provider.of<HomeStackNotifier>(context, listen: false),
  179. child: Consumer(
  180. builder: (BuildContext context, HomeStackNotifier notifier,
  181. Widget? child) {
  182. return notifier.plugin.display.rightBarItem ?? const SizedBox();
  183. },
  184. ),
  185. ) // _renderMoreButton(),
  186. ],
  187. )
  188. .padding(
  189. horizontal: HomeInsets.topBarTitlePadding,
  190. )
  191. .bottomBorder(color: Colors.grey.shade300),
  192. );
  193. }
  194. }