Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/text_book/view/text_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class _TextBookViewerBlocState extends State<TextBookViewerBloc>
final GlobalKey _searchTabKey = GlobalKey();
Object? _searchViewInputs;
TextBookSearchView? _searchView;
TocViewer? _tocView;

// בקשה לפתיחת דיאלוג הגדרות צורת הדף מתוך PageShapeScreen (עדכון חי)
final ValueNotifier<int> _pageShapeOpenSettingsNotifier = ValueNotifier<int>(
Expand Down Expand Up @@ -2527,7 +2528,7 @@ class _TextBookViewerBlocState extends State<TextBookViewerBloc>
children: [
NavPanelSearchSlot(
index: 0,
child: _buildTocViewer(context, state),
child: _buildTocViewer(),
),
if (_hasAltTitles)
NavPanelSearchSlot(
Expand Down Expand Up @@ -2619,9 +2620,13 @@ class _TextBookViewerBlocState extends State<TextBookViewerBloc>
);
}

Widget _buildTocViewer(BuildContext context, TextBookLoaded state) {
return TocViewer(
lines: widget.tab.lines,
TocViewer _buildTocViewer() {
final lines = widget.tab.lines;
// בחירת שורה בונה את הקורא; עץ הכותרות מאזין לבחירה בעצמו.
final view = _tocView;
if (view != null && identical(view.lines, lines)) return view;
return _tocView = TocViewer(
lines: lines,
focusNode: navigationSearchFocusNode,
closeLeftPaneCallback: _closeLeftPaneIfCovering,
);
Expand Down
91 changes: 53 additions & 38 deletions lib/text_book/view/toc_navigator_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:otzaria_icons/otzaria_icons.dart';
Expand Down Expand Up @@ -93,6 +94,10 @@ class _TocViewerState extends State<TocViewer>
// בשדה החיפוש, כמו ב"איתור". מתאפס בכל שינוי שאילתה.
int? _highlightedEntryIndex;

// השורה המסומנת בעץ. מעבר הכותרת הפעילה בזמן הקריאה מעדכן רק אותה, כדי
// שלא ייבנו מחדש כל השורות הגלויות אלא רק שתי השורות שסימונן השתנה.
final ValueNotifier<int?> _selectedEntry = ValueNotifier(null);

final ItemScrollController _virtualScrollController = ItemScrollController();
final ItemPositionsListener _virtualPositionsListener =
ItemPositionsListener.create();
Expand All @@ -115,6 +120,7 @@ class _TocViewerState extends State<TocViewer>
TocExpansionSettings.autoExpand.removeListener(_onAutoExpandChanged);
_searchDebounce?.cancel();
searchController.dispose();
_selectedEntry.dispose();
super.dispose();
}

Expand Down Expand Up @@ -165,7 +171,8 @@ class _TocViewerState extends State<TocViewer>
if (next == current) return;

final targetIndex = flat[next].entry.index;
setState(() => _highlightedEntryIndex = targetIndex);
_highlightedEntryIndex = targetIndex;
_selectedEntry.value = targetIndex;
SchedulerBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
final current = _displayDataFor(state);
Expand All @@ -174,6 +181,8 @@ class _TocViewerState extends State<TocViewer>
display: current,
);
});
// כשהתוצאה כבר מסומנת ממיקום הקריאה, ה-notifier לא מבקש פריים.
SchedulerBinding.instance.ensureVisualUpdate();
}

/// גולל את הספר אל השורה של ערך ה-TOC (לחיצה על שורה או אנטר בחיפוש).
Expand Down Expand Up @@ -264,6 +273,15 @@ class _TocViewerState extends State<TocViewer>
return activeIndex;
}

// ה-state שנשלח ל-BlocBuilder לא מתעדכן כשמשתנה רק מיקום הקריאה.
void _syncSelectedEntry() {
final state = context.read<TextBookBloc>().state;
if (state is! TextBookLoaded) return;
_selectedEntry.value =
_highlightedEntryIndex ??
_visibleActiveIndex(_activeTocIndex(state), _displayDataFor(state));
}

void _scrollToActiveItem(TextBookLoaded state) {
if (_isManuallyScrolling) return;
// כשהפאנל סגור הגלילה נכשלת (רוחב 0) אך משבשת את _lastScrolledTocIndex
Expand Down Expand Up @@ -402,7 +420,6 @@ class _TocViewerState extends State<TocViewer>
Widget _buildTocRow(
TocEntry entry, {
bool showFullText = false,
required int? activeIndex,
required bool isExpanded,
bool isGroupStart = false,
bool isGroupEnd = false,
Expand All @@ -414,10 +431,6 @@ class _TocViewerState extends State<TocViewer>
_navigateToLine(entry.index);
}

// בזמן דפדוף בחיצים הסימון הוא של תוצאת הדפדוף, לא של מיקום הקריאה.
final bool selected = _highlightedEntryIndex != null
? _highlightedEntryIndex == entry.index
: activeIndex == entry.index;
final text = showFullText ? entry.fullText : entry.text;
final title = holyNameStyle == null
? text
Expand All @@ -436,28 +449,38 @@ class _TocViewerState extends State<TocViewer>
_expandedRevision++;
});

// הלחיצה תמיד מנווטת; שורה סגורה גם נפתחת, ושורה פתוחה ופעילה גם מכווצת.
final toggleOnTap =
hasChildren && !showFullText && (!isExpanded || selected);
final tile = NavTreeTile.heading(
title: title,
// בחיפוש הפוקוס נשאר בשדה, והחיצים מדפדפים משם.
focusOnTap: !showFullText,
level: level,
isSelected: selected,
isExpanded: isExpanded,
hasChildren: hasChildren,
onTap: () {
if (toggleOnTap) setExpanded(!isExpanded);
navigateToEntry();
},
onToggleExpand: hasChildren ? () => setExpanded(!isExpanded) : null,
);

// אותו מופע נשמר כל עוד סימון השורה לא השתנה, ו-Flutter מדלג עליו.
Widget? tile;
bool? tileSelected;
return NavTreeGroupCard(
isGroupStart: isGroupStart,
isGroupEnd: isGroupEnd,
child: tile,
child: ValueListenableBuilder<int?>(
valueListenable: _selectedEntry,
builder: (context, selectedIndex, _) {
final selected = selectedIndex == entry.index;
if (tile != null && tileSelected == selected) return tile!;
tileSelected = selected;
// הלחיצה תמיד מנווטת; שורה סגורה גם נפתחת, ושורה פתוחה ופעילה גם
// מכווצת.
final toggleOnTap =
hasChildren && !showFullText && (!isExpanded || selected);
return tile = NavTreeTile.heading(
title: title,
// בחיפוש הפוקוס נשאר בשדה, והחיצים מדפדפים משם.
focusOnTap: !showFullText,
level: level,
isSelected: selected,
isExpanded: isExpanded,
hasChildren: hasChildren,
onTap: () {
if (toggleOnTap) setExpanded(!isExpanded);
navigateToEntry();
},
onToggleExpand: hasChildren ? () => setExpanded(!isExpanded) : null,
);
},
),
);
}

Expand Down Expand Up @@ -554,8 +577,7 @@ class _TocViewerState extends State<TocViewer>

/// בונה רשימה וירטואלית מהעץ השטוח והממוטמן.
Widget _buildVirtualizedTocList(
List<TocFlatItem> flat,
int? activeIndex, {
List<TocFlatItem> flat, {
required bool isSearching,
required String title,
required _TocDisplayData display,
Expand Down Expand Up @@ -589,7 +611,6 @@ class _TocViewerState extends State<TocViewer>
child: _buildTocRow(
item.entry,
showFullText: isSearching,
activeIndex: activeIndex,
isExpanded: item.isExpanded,
isGroupStart: index == 1,
isGroupEnd: index == flat.length,
Expand Down Expand Up @@ -624,31 +645,26 @@ class _TocViewerState extends State<TocViewer>
listener: (context, state) {
if (state is TextBookLoaded) {
_scrollToActiveItem(state);
_syncSelectedEntry();
}
},
child: BlocBuilder<TextBookBloc, TextBookState>(
bloc: context.read<TextBookBloc>(),
// build רק כשהכותרת הפעילה או העץ משתנים: בניית העץ בכל שורה של
// גלילה יוצרת frames ארוכים בספרים עם אלפי כותרות.
// build רק כשהעץ משתנה: בניית העץ בכל שורה של גלילה יוצרת frames
// ארוכים בספרים עם אלפי כותרות. הכותרת הפעילה מתעדכנת ב-listener.
buildWhen: (previous, current) {
if (current is! TextBookLoaded) return true;
if (previous is! TextBookLoaded) return true;
return !identical(
previous.tableOfContents,
current.tableOfContents,
) ||
_activeTocIndex(previous) != _activeTocIndex(current) ||
previous.bodyDisplayProfile.holyName !=
current.bodyDisplayProfile.holyName;
},
builder: (context, state) {
if (state is! TextBookLoaded) return const Center();
final display = _displayDataFor(state);
// מחושב פעם אחת ומועבר לכל הפריטים, כדי שהבנייה לא תהיה O(n²).
final int? activeIndex = _visibleActiveIndex(
_activeTocIndex(state),
display,
);
_syncSelectedEntry();
final expandMenu = _buildExpandMenu(display);

final delegate = NavPanelSearchDelegate(
Expand Down Expand Up @@ -687,7 +703,6 @@ class _TocViewerState extends State<TocViewer>
child: NavTreeFocusGroup(
child: _buildVirtualizedTocList(
_flatItemsFor(display),
activeIndex,
isSearching: display.isSearching,
title: state.book.title,
display: display,
Expand Down
36 changes: 36 additions & 0 deletions test/text_book/view/text_book_nav_panel_preserved_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,42 @@ void main() {
);
});

testWidgets('בחירת שורה בספר מעדכנת רק את סימון הכותרות שהשתנו', (
tester,
) async {
await pumpScreen(tester);
final toc = find.byType(TocViewer);
final bloc = tester.element(toc).read<TextBookBloc>() as _TestTextBookBloc;
bloc.update(
(bloc.state as TextBookLoaded).copyWith(
tableOfContents: [
TocEntry(text: 'כותרת א', index: 0, level: 1),
TocEntry(text: 'כותרת ב', index: 1, level: 1),
TocEntry(text: 'כותרת ג', index: 2, level: 1),
],
),
);
await tester.pumpAndSettle();

NavTreeTile tile(String title) => tester.widget<NavTreeTile>(
find.descendant(
of: toc,
matching: find.byWidgetPredicate(
(widget) => widget is NavTreeTile && widget.title == title,
),
),
);
final untouched = tile('כותרת ג');
expect(tile('כותרת א').isSelected, isTrue);

bloc.add(UpdateSelectedIndex(1));
await tester.pumpAndSettle();

expect(tile('כותרת א').isSelected, isFalse);
expect(tile('כותרת ב').isSelected, isTrue);
expect(tile('כותרת ג'), same(untouched));
});

testWidgets('הקלדה בחיפוש-בספר שורדת את סיום בדיקת הכותרות החלופיות', (
tester,
) async {
Expand Down
Loading
Loading