45 lines
1.5 KiB
Dart
45 lines
1.5 KiB
Dart
import 'destination.dart';
|
|
|
|
/// The tabs the operator left, most recent first, for the system Back.
|
|
///
|
|
/// Every tab change (a tap, a search result or notification opening
|
|
/// another tab) goes through [record]: the tab being left moves to the top
|
|
/// and the tab being opened leaves the stack, so each tab appears at most once
|
|
/// and the current tab is never in it. A page that belongs to a tab
|
|
/// (Settings, of Pipelines) is that tab here.
|
|
///
|
|
/// [takePrevious] steps back: it removes the top tab and returns it, and the
|
|
/// next [record] that opens that tab does not push the tab being left, so Back
|
|
/// never builds a forward trail.
|
|
class TabHistory {
|
|
final List<AppDestination> _tabs = [];
|
|
AppDestination? _returningTo;
|
|
|
|
/// The left tabs, most recent first.
|
|
List<AppDestination> get tabs => List.unmodifiable(_tabs);
|
|
|
|
bool get isEmpty => _tabs.isEmpty;
|
|
|
|
/// Notes a change of the shown destination from [from] to [to].
|
|
void record(AppDestination from, AppDestination to) {
|
|
final left = shellTabFor(from);
|
|
final opened = shellTabFor(to);
|
|
if (left == opened) return;
|
|
final returning = _returningTo == opened;
|
|
_returningTo = null;
|
|
_tabs.remove(opened);
|
|
if (returning) return;
|
|
_tabs
|
|
..remove(left)
|
|
..insert(0, left);
|
|
}
|
|
|
|
/// The tab Back returns to, removed from the stack; null when empty.
|
|
AppDestination? takePrevious() {
|
|
if (_tabs.isEmpty) return null;
|
|
final previous = _tabs.removeAt(0);
|
|
_returningTo = previous;
|
|
return previous;
|
|
}
|
|
}
|