Skip to content

Commit

Permalink
selected hierarchy entries are now auto scrolled into view
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHeitmann committed Oct 3, 2023
1 parent 734f660 commit c4df04f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/widgets/FileHierarchyExplorer/HierarchyEntryWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -84,7 +85,7 @@ class _HierarchyEntryState extends ChangeNotifierState<HierarchyEntryWidget> {
child: optionallySetupSelectable(context,
Container(
padding: const EdgeInsets.symmetric(vertical: 3),
height: 25,
height: HierarchyEntryWidget.height,
child: Row(
children: [
SizedBox(width: 15.0 * widget.depth,),
Expand Down
29 changes: 16 additions & 13 deletions lib/widgets/FileHierarchyExplorer/HierarchyFlatList.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -177,23 +177,26 @@ class _HierarchyFlatListState extends ChangeNotifierState<HierarchyFlatList> {
}

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,
);
}
}
}
Expand Down

0 comments on commit c4df04f

Please sign in to comment.