From c4df04f72b0fdb9d45f407a611d706f7f809742a Mon Sep 17 00:00:00 2001 From: ArthurHeitmann <37270165+ArthurHeitmann@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:13:54 +0200 Subject: [PATCH] selected hierarchy entries are now auto scrolled into view --- .../HierarchyEntryWidget.dart | 3 +- .../HierarchyFlatList.dart | 29 ++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/widgets/FileHierarchyExplorer/HierarchyEntryWidget.dart b/lib/widgets/FileHierarchyExplorer/HierarchyEntryWidget.dart index 0afeb11a..8168551b 100644 --- a/lib/widgets/FileHierarchyExplorer/HierarchyEntryWidget.dart +++ b/lib/widgets/FileHierarchyExplorer/HierarchyEntryWidget.dart @@ -13,6 +13,7 @@ import 'wemPreviewButton.dart'; class HierarchyEntryWidget extends ChangeNotifierWidget { final HierarchyEntry entry; final int depth; + static const height = 25.0; HierarchyEntryWidget({ required this.entry, Key? key, this.depth = 0 }) : super(key: key ?? Key(entry.uuid), notifiers: [entry, shouldAutoTranslate, openHierarchySearch]); @@ -84,7 +85,7 @@ class _HierarchyEntryState extends ChangeNotifierState { child: optionallySetupSelectable(context, Container( padding: const EdgeInsets.symmetric(vertical: 3), - height: 25, + height: HierarchyEntryWidget.height, child: Row( children: [ SizedBox(width: 15.0 * widget.depth,), diff --git a/lib/widgets/FileHierarchyExplorer/HierarchyFlatList.dart b/lib/widgets/FileHierarchyExplorer/HierarchyFlatList.dart index 755272fa..c5505f6c 100644 --- a/lib/widgets/FileHierarchyExplorer/HierarchyFlatList.dart +++ b/lib/widgets/FileHierarchyExplorer/HierarchyFlatList.dart @@ -1,6 +1,6 @@ import 'package:flutter/services.dart'; -import 'package:flutter/widgets.dart' show BuildContext, Focus, FocusNode, FocusScope, Key, ListView, ScrollController, State, Widget; +import 'package:flutter/widgets.dart' show BuildContext, Curves, FocusScope, Key, ListView, ScrollController, State, Widget; import '../../keyboardEvents/BetterShortcuts.dart'; import '../../stateManagement/ChangeNotifierWidget.dart'; @@ -177,23 +177,26 @@ class _HierarchyFlatListState extends ChangeNotifierState { } void scrollToSelectedEntry() { - return; if (openHierarchyManager.selectedEntry == null) return; var currentIndex = cachedFlatTree.indexWhere((e) => e.entry == openHierarchyManager.selectedEntry); if (currentIndex == -1) return; - var currentEntry = cachedFlatTree[currentIndex]; - var currentEntryTop = currentEntry.depth * 20.0; - var currentEntryBottom = currentEntryTop + 20.0; - var currentEntryCenter = (currentEntryTop + currentEntryBottom) / 2; - var currentEntryCenterInViewport = currentEntryCenter - scrollController.offset; - var viewportHeight = scrollController.position.viewportDimension; - if (currentEntryCenterInViewport < 0) { - scrollController.jumpTo(scrollController.offset + currentEntryCenterInViewport); - } - else if (currentEntryCenterInViewport > viewportHeight) { - scrollController.jumpTo(scrollController.offset + currentEntryCenterInViewport - viewportHeight); + var currentEntryTop = currentIndex * HierarchyEntryWidget.height; + var currentEntryBottom = currentEntryTop + HierarchyEntryWidget.height; + var scrollOffset = scrollController.offset; + var scrollBottom = scrollOffset + scrollController.position.viewportDimension; + double? scrollOffsetAfter; + if (currentEntryTop < scrollOffset) + scrollOffsetAfter = currentEntryTop; + else if (currentEntryBottom > scrollBottom) + scrollOffsetAfter = currentEntryBottom - scrollController.position.viewportDimension; + if (scrollOffsetAfter != null) { + scrollController.animateTo( + scrollOffsetAfter, + duration: const Duration(milliseconds: 150), + curve: Curves.easeOut, + ); } } }