tabs_state.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. part of 'tabs_bloc.dart';
  2. class TabsState {
  3. final int currentIndex;
  4. final List<PageManager> _pageManagers;
  5. int get pages => _pageManagers.length;
  6. PageManager get currentPageManager => _pageManagers[currentIndex];
  7. List<PageManager> get pageManagers => _pageManagers;
  8. TabsState({
  9. this.currentIndex = 0,
  10. List<PageManager>? pageManagers,
  11. }) : _pageManagers = pageManagers ?? [PageManager()];
  12. /// This opens a new tab given a [Plugin] and a [View].
  13. ///
  14. /// If the [Plugin.id] is already associated with an open tab,
  15. /// then it selects that tab.
  16. ///
  17. TabsState openView(Plugin plugin, ViewPB view) {
  18. final selectExistingPlugin = _selectPluginIfOpen(plugin.id);
  19. if (selectExistingPlugin == null) {
  20. _pageManagers.add(PageManager()..setPlugin(plugin));
  21. return copyWith(newIndex: pages - 1, pageManagers: [..._pageManagers]);
  22. }
  23. return selectExistingPlugin;
  24. }
  25. TabsState closeView(String pluginId) {
  26. _pageManagers.removeWhere((pm) => pm.plugin.id == pluginId);
  27. /// If currentIndex is greater than the amount of allowed indices
  28. /// And the current selected tab isn't the first (index 0)
  29. /// as currentIndex cannot be -1
  30. /// Then decrease currentIndex by 1
  31. final newIndex = currentIndex > pages - 1 && currentIndex > 0
  32. ? currentIndex - 1
  33. : currentIndex;
  34. return copyWith(
  35. newIndex: newIndex,
  36. pageManagers: [..._pageManagers],
  37. );
  38. }
  39. /// This opens a plugin in the current selected tab,
  40. /// due to how Document currently works, only one tab
  41. /// per plugin can currently be active.
  42. ///
  43. /// If the plugin is already open in a tab, then that tab
  44. /// will become selected.
  45. ///
  46. TabsState openPlugin({required Plugin plugin}) {
  47. final selectExistingPlugin = _selectPluginIfOpen(plugin.id);
  48. if (selectExistingPlugin == null) {
  49. final pageManagers = [..._pageManagers];
  50. pageManagers[currentIndex].setPlugin(plugin);
  51. return copyWith(pageManagers: pageManagers);
  52. }
  53. return selectExistingPlugin;
  54. }
  55. /// Checks if a [Plugin.id] is already associated with an open tab.
  56. /// Returns a [TabState] with new index if there is a match.
  57. ///
  58. /// If no match it returns null
  59. ///
  60. TabsState? _selectPluginIfOpen(String id) {
  61. final index = _pageManagers.indexWhere((pm) => pm.plugin.id == id);
  62. if (index == -1) {
  63. return null;
  64. }
  65. return copyWith(newIndex: index);
  66. }
  67. TabsState copyWith({
  68. int? newIndex,
  69. List<PageManager>? pageManagers,
  70. }) =>
  71. TabsState(
  72. currentIndex: newIndex ?? currentIndex,
  73. pageManagers: pageManagers ?? _pageManagers,
  74. );
  75. }