Split view: drag the divider to resize the list pane, remembered per destination
A 12 px grab area over the divider line resizes the list live (mouse, touch, arrow keys; double tap resets to the default policy). The shell bar over the list follows. Widths persist per destination in SharedPreferences and clamp to the window without overwriting the saved value. Capture kit gains hover= and drag= steps.
This commit is contained in:
@@ -9,6 +9,7 @@ import '../ui/adaptive/floating_destination_bar.dart';
|
||||
import 'attention_badge.dart';
|
||||
import 'destination.dart';
|
||||
import 'list_detail.dart';
|
||||
import 'list_pane_widths.dart';
|
||||
import 'shell_bar.dart';
|
||||
|
||||
class AdaptiveShell extends StatefulWidget {
|
||||
@@ -20,8 +21,12 @@ class AdaptiveShell extends StatefulWidget {
|
||||
this.onDestinationSelected,
|
||||
this.onSearch,
|
||||
this.attentionCount = 0,
|
||||
this.listPaneWidthStore = const SharedPreferencesListPaneWidthStore(),
|
||||
});
|
||||
|
||||
/// Where each destination's dragged list-pane width is kept on this device.
|
||||
final ListPaneWidthStore listPaneWidthStore;
|
||||
|
||||
final Map<AppDestination, WidgetBuilder> destinationBodies;
|
||||
final bool cupertino;
|
||||
|
||||
@@ -46,6 +51,9 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
late AppDestination _selected;
|
||||
final Set<AppDestination> _visited = {};
|
||||
final ShellBarRegistry _bar = ShellBarRegistry();
|
||||
late final ListPaneWidths _paneWidths = ListPaneWidths(
|
||||
store: widget.listPaneWidthStore,
|
||||
);
|
||||
// Keeps every destination's state if the stack's parent ever changes.
|
||||
final GlobalKey _destinationStackKey = GlobalKey();
|
||||
|
||||
@@ -53,6 +61,7 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
void dispose() {
|
||||
HardwareKeyboard.instance.removeHandler(_handleDestinationKeyEvent);
|
||||
_bar.dispose();
|
||||
_paneWidths.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -168,9 +177,10 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
SegmentedBar currentBar() =>
|
||||
_segmentedBar(_bar.specFor(_selected), details.label, page: page);
|
||||
final topBar = _ListPaneBar(
|
||||
listenable: _bar.bar,
|
||||
listenable: Listenable.merge([_bar.bar, _paneWidths]),
|
||||
initial: currentBar(),
|
||||
split: () => _bar.split(_selected),
|
||||
preferredWidth: () => _paneWidths.widthFor(_selected.name),
|
||||
bar: currentBar,
|
||||
);
|
||||
// The scaffolds below swap the system insets for their bars'
|
||||
@@ -313,10 +323,13 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _destinationStack(List<Widget> contents) => FadeThroughStack(
|
||||
key: _destinationStackKey,
|
||||
index: AppDestination.values.indexOf(_selected),
|
||||
children: contents,
|
||||
Widget _destinationStack(List<Widget> contents) => ListPaneWidthsScope(
|
||||
widths: _paneWidths,
|
||||
child: FadeThroughStack(
|
||||
key: _destinationStackKey,
|
||||
index: AppDestination.values.indexOf(_selected),
|
||||
children: contents,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -334,12 +347,14 @@ class _ListPaneBar extends StatelessWidget
|
||||
required this.listenable,
|
||||
required this.initial,
|
||||
required this.split,
|
||||
required this.preferredWidth,
|
||||
required this.bar,
|
||||
});
|
||||
|
||||
final Listenable listenable;
|
||||
final SegmentedBar initial;
|
||||
final bool Function() split;
|
||||
final double? Function() preferredWidth;
|
||||
final SegmentedBar Function() bar;
|
||||
|
||||
@override
|
||||
@@ -354,6 +369,7 @@ class _ListPaneBar extends StatelessWidget
|
||||
listenable: listenable,
|
||||
builder: (context, _) => ListPaneWidth(
|
||||
split: split(),
|
||||
preferredWidth: preferredWidth(),
|
||||
alignment: AlignmentDirectional.topStart,
|
||||
child: bar(),
|
||||
),
|
||||
|
||||
+120
-109
@@ -1,47 +1,15 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../ui/adaptive/floating_bar_layout.dart';
|
||||
import '../ui/adaptive/host.dart';
|
||||
import 'detail_pane_navigator.dart';
|
||||
import 'list_detail_policy.dart';
|
||||
import 'list_pane_divider.dart';
|
||||
import 'list_pane_widths.dart';
|
||||
import 'shell_bar.dart';
|
||||
|
||||
/// At least this wide, a destination keeps its list beside the open item
|
||||
/// (with the destination rail) instead of pushing the item as its own page.
|
||||
const double kListDetailMinWidth = 600;
|
||||
const double _bodyWindowTarget = 0.6;
|
||||
const double _bodyWindowMinimum = 0.5;
|
||||
const double _bodyAvailableMaximum = 0.75;
|
||||
|
||||
/// The widest the list pane grows beside an open item: on a very wide window
|
||||
/// (a 4K monitor) the extra width goes to the open item, not the list.
|
||||
const double kListPaneMaxWidth = 440;
|
||||
|
||||
/// Whether the window at [context] shows list and detail side by side.
|
||||
bool showsListBesideDetail(BuildContext context) =>
|
||||
MediaQuery.sizeOf(context).width >= kListDetailMinWidth;
|
||||
|
||||
/// The open item's width beside a list, for a [window] this wide and
|
||||
/// [available] width for both panes and their divider.
|
||||
double listDetailBodyWidth(double window, double available) {
|
||||
// Target 60% of the full window. Keep at least half for the body while
|
||||
// leaving a flexible share for native list controls on tighter windows.
|
||||
final requestedMinimum = window * _bodyWindowMinimum;
|
||||
final minimum = available > requestedMinimum
|
||||
? requestedMinimum
|
||||
: available * _bodyWindowMinimum;
|
||||
final maximum = math.max(minimum, available * _bodyAvailableMaximum);
|
||||
final body = (window * _bodyWindowTarget).clamp(minimum, maximum);
|
||||
// Past the list's cap, the open item takes the rest.
|
||||
return math.max(body, available - 1 - kListPaneMaxWidth);
|
||||
}
|
||||
|
||||
/// The list pane's width beside an open item (see [listDetailBodyWidth]),
|
||||
/// one divider excluded.
|
||||
double listDetailListWidth(double window, double available) =>
|
||||
math.max(0, available - 1 - listDetailBodyWidth(window, available));
|
||||
export 'list_detail_policy.dart';
|
||||
|
||||
/// The system's own insets (status bar, navigation bar) as the shell saw
|
||||
/// them, before its scaffold replaced them with its bars' heights.
|
||||
@@ -79,10 +47,15 @@ class ListPaneWidth extends StatelessWidget {
|
||||
super.key,
|
||||
required this.split,
|
||||
required this.alignment,
|
||||
this.preferredWidth,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final bool split;
|
||||
|
||||
/// The list width the operator dragged the divider to, null for the
|
||||
/// default policy; see [listDetailListWidth].
|
||||
final double? preferredWidth;
|
||||
final AlignmentGeometry alignment;
|
||||
final Widget child;
|
||||
|
||||
@@ -93,6 +66,7 @@ class ListPaneWidth extends StatelessWidget {
|
||||
? listDetailListWidth(
|
||||
MediaQuery.sizeOf(context).width,
|
||||
constraints.maxWidth,
|
||||
preferredWidth,
|
||||
)
|
||||
: constraints.maxWidth;
|
||||
return Align(
|
||||
@@ -115,7 +89,7 @@ class ListPaneWidth extends StatelessWidget {
|
||||
/// [kStackedBarGap] as its bottom padding: its own tabs (or the chat
|
||||
/// composer) float above the shell's bar, never on it, and its content clears
|
||||
/// both. Pages the item opens stay in its pane ([DetailPaneNavigator]).
|
||||
class ListDetailPanes extends StatelessWidget {
|
||||
class ListDetailPanes extends StatefulWidget {
|
||||
const ListDetailPanes({
|
||||
super.key,
|
||||
required this.list,
|
||||
@@ -130,88 +104,125 @@ class ListDetailPanes extends StatelessWidget {
|
||||
/// previous one opened in its pane.
|
||||
final Object? detailIdentity;
|
||||
|
||||
@override
|
||||
State<ListDetailPanes> createState() => _ListDetailPanesState();
|
||||
}
|
||||
|
||||
class _ListDetailPanesState extends State<ListDetailPanes> {
|
||||
// Outside a shell (no [ListPaneWidthsScope]) the divider still drags; the
|
||||
// width lasts as long as the panes.
|
||||
ListPaneWidths? _ownWidths;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ownWidths?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final divider = usesCupertinoHost(context)
|
||||
? Container(
|
||||
width: 1,
|
||||
color: CupertinoColors.separator.resolveFrom(context),
|
||||
)
|
||||
: const VerticalDivider(width: 1);
|
||||
final widths =
|
||||
ListPaneWidthsScope.maybeOf(context) ??
|
||||
(_ownWidths ??= ListPaneWidths(store: MemoryListPaneWidthStore()));
|
||||
final key = ShellBarScope.maybeOf(context)?.destination.name ?? 'panes';
|
||||
final media = MediaQuery.of(context);
|
||||
final split = ShellBar.available(context);
|
||||
return ShellPaneSplit(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final bodyWidth = listDetailBodyWidth(
|
||||
media.size.width,
|
||||
constraints.maxWidth,
|
||||
);
|
||||
Widget body = detail;
|
||||
Widget listPane = list;
|
||||
if (split) {
|
||||
// Beside the list the item is not under the shell's top bar (it
|
||||
// stays over the list pane): it keeps the system's own top inset,
|
||||
// so its bar sits just below the status bar at the top edge. The
|
||||
// shell's tab bar floats at the bottom right, over this pane; its
|
||||
// height (the shell's bottom padding here) plus a gap lifts the
|
||||
// item's tabs or composer above it.
|
||||
final system = ShellSystemInsets.of(context);
|
||||
body = MediaQuery(
|
||||
data: media.copyWith(
|
||||
padding: media.padding.copyWith(
|
||||
top: system.top,
|
||||
bottom: media.padding.bottom + kStackedBarGap,
|
||||
),
|
||||
),
|
||||
child: PageTabsEdge(
|
||||
edge: FloatingBarEdge.bottom,
|
||||
child: DetailPaneNavigator(
|
||||
identity: detailIdentity,
|
||||
child: body,
|
||||
),
|
||||
),
|
||||
);
|
||||
// The tab bar sits over the detail pane, never the list (the
|
||||
// detail is at least half the window, wider than the bar), so
|
||||
// the list clears only the system's bottom inset.
|
||||
listPane = MediaQuery(
|
||||
data: media.copyWith(
|
||||
padding: media.padding.copyWith(bottom: system.bottom),
|
||||
),
|
||||
child: list,
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Expanded(
|
||||
child: SizedBox.expand(
|
||||
key: const ValueKey('list-detail-list'),
|
||||
child: Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
identifier: 'list-detail-list',
|
||||
child: listPane,
|
||||
child: ListenableBuilder(
|
||||
listenable: widths,
|
||||
builder: (context, _) => LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final window = media.size.width;
|
||||
final available = constraints.maxWidth;
|
||||
final preferred = widths.widthFor(key);
|
||||
final listWidth = listDetailListWidth(window, available, preferred);
|
||||
final bodyWidth = math.max(0.0, available - 1 - listWidth);
|
||||
final bounds = listPaneWidthBounds(window, available);
|
||||
Widget body = widget.detail;
|
||||
Widget listPane = widget.list;
|
||||
if (split) {
|
||||
// Beside the list the item is not under the shell's top bar (it
|
||||
// stays over the list pane): it keeps the system's own top
|
||||
// inset, so its bar sits just below the status bar at the top
|
||||
// edge. The shell's tab bar floats at the bottom right, over
|
||||
// this pane; its height (the shell's bottom padding here) plus
|
||||
// a gap lifts the item's tabs or composer above it.
|
||||
final system = ShellSystemInsets.of(context);
|
||||
body = MediaQuery(
|
||||
data: media.copyWith(
|
||||
padding: media.padding.copyWith(
|
||||
top: system.top,
|
||||
bottom: media.padding.bottom + kStackedBarGap,
|
||||
),
|
||||
),
|
||||
),
|
||||
divider,
|
||||
SizedBox(
|
||||
width: bodyWidth,
|
||||
child: SizedBox.expand(
|
||||
key: const ValueKey('list-detail-body'),
|
||||
child: Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
identifier: 'list-detail-body',
|
||||
child: PageTabsEdge(
|
||||
edge: FloatingBarEdge.bottom,
|
||||
child: DetailPaneNavigator(
|
||||
identity: widget.detailIdentity,
|
||||
child: body,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
// The tab bar sits over the detail pane, never the list (the
|
||||
// detail is at least half the window, wider than the bar), so
|
||||
// the list clears only the system's bottom inset.
|
||||
listPane = MediaQuery(
|
||||
data: media.copyWith(
|
||||
padding: media.padding.copyWith(bottom: system.bottom),
|
||||
),
|
||||
child: widget.list,
|
||||
);
|
||||
}
|
||||
return Stack(
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Expanded(
|
||||
child: SizedBox.expand(
|
||||
key: const ValueKey('list-detail-list'),
|
||||
child: Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
identifier: 'list-detail-list',
|
||||
child: listPane,
|
||||
),
|
||||
),
|
||||
),
|
||||
const ListPaneDividerLine(),
|
||||
SizedBox(
|
||||
width: bodyWidth,
|
||||
child: SizedBox.expand(
|
||||
key: const ValueKey('list-detail-body'),
|
||||
child: Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
identifier: 'list-detail-body',
|
||||
child: body,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (bounds != null)
|
||||
PositionedDirectional(
|
||||
start: listWidth + 0.5 - kListPaneDividerGrabWidth / 2,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
width: kListPaneDividerGrabWidth,
|
||||
child: ListPaneDivider(
|
||||
width: listWidth,
|
||||
min: bounds.min,
|
||||
max: bounds.max,
|
||||
onChanged: (value) => widths.preview(key, value),
|
||||
onChangeEnd: () => widths.commit(key),
|
||||
onReset: () => widths.reset(key),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// At least this wide, a destination keeps its list beside the open item
|
||||
/// (with the destination rail) instead of pushing the item as its own page.
|
||||
const double kListDetailMinWidth = 600;
|
||||
const double _bodyWindowTarget = 0.6;
|
||||
const double _bodyWindowMinimum = 0.5;
|
||||
const double _bodyAvailableMaximum = 0.75;
|
||||
|
||||
/// The widest the list pane grows beside an open item: on a very wide window
|
||||
/// (a 4K monitor) the extra width goes to the open item, not the list.
|
||||
const double kListPaneMaxWidth = 440;
|
||||
|
||||
/// Whether the window at [context] shows list and detail side by side.
|
||||
bool showsListBesideDetail(BuildContext context) =>
|
||||
MediaQuery.sizeOf(context).width >= kListDetailMinWidth;
|
||||
|
||||
/// The narrowest list pane a drag leaves.
|
||||
const double kListPaneMinWidth = 240;
|
||||
|
||||
/// The narrowest open item a drag leaves, beside the share of the window it
|
||||
/// always keeps (so the shell's tab bar, floating over it, still fits).
|
||||
const double kDetailPaneMinWidth = 320;
|
||||
const double _listWindowMaximum = 0.6;
|
||||
|
||||
/// How far a drag may resize the list pane for a [window] this wide and
|
||||
/// [available] width for both panes and their divider; null when the window
|
||||
/// is too small for any width but the default policy's.
|
||||
({double min, double max})? listPaneWidthBounds(
|
||||
double window,
|
||||
double available,
|
||||
) {
|
||||
final bodyMinimum = math.max(
|
||||
window * _bodyWindowMinimum,
|
||||
kDetailPaneMinWidth,
|
||||
);
|
||||
final maximum = math.min(
|
||||
available - 1 - bodyMinimum,
|
||||
window * _listWindowMaximum,
|
||||
);
|
||||
if (maximum < kListPaneMinWidth) return null;
|
||||
return (min: kListPaneMinWidth, max: maximum);
|
||||
}
|
||||
|
||||
/// The open item's width beside a list, for a [window] this wide and
|
||||
/// [available] width for both panes and their divider. A [preferred] list
|
||||
/// width (the operator's drag) replaces the default policy, clamped to
|
||||
/// [listPaneWidthBounds].
|
||||
double listDetailBodyWidth(
|
||||
double window,
|
||||
double available, [
|
||||
double? preferred,
|
||||
]) {
|
||||
if (preferred != null) {
|
||||
final bounds = listPaneWidthBounds(window, available);
|
||||
if (bounds != null) {
|
||||
return available - 1 - preferred.clamp(bounds.min, bounds.max);
|
||||
}
|
||||
}
|
||||
// Target 60% of the full window. Keep at least half for the body while
|
||||
// leaving a flexible share for native list controls on tighter windows.
|
||||
final requestedMinimum = window * _bodyWindowMinimum;
|
||||
final minimum = available > requestedMinimum
|
||||
? requestedMinimum
|
||||
: available * _bodyWindowMinimum;
|
||||
final maximum = math.max(minimum, available * _bodyAvailableMaximum);
|
||||
final body = (window * _bodyWindowTarget).clamp(minimum, maximum);
|
||||
// Past the list's cap, the open item takes the rest.
|
||||
return math.max(body, available - 1 - kListPaneMaxWidth);
|
||||
}
|
||||
|
||||
/// The list pane's width beside an open item (see [listDetailBodyWidth]),
|
||||
/// one divider excluded.
|
||||
double listDetailListWidth(
|
||||
double window,
|
||||
double available, [
|
||||
double? preferred,
|
||||
]) => math.max(
|
||||
0,
|
||||
available - 1 - listDetailBodyWidth(window, available, preferred),
|
||||
);
|
||||
@@ -0,0 +1,187 @@
|
||||
import 'package:flutter/gestures.dart' show DragStartBehavior;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../ui/adaptive/host.dart';
|
||||
|
||||
/// How wide the divider's invisible grab area is, centred on its line.
|
||||
const double kListPaneDividerGrabWidth = 12;
|
||||
|
||||
/// How far one arrow key press moves the divider.
|
||||
const double kListPaneDividerKeyStep = 16;
|
||||
|
||||
/// The one-pixel line between a list and its open item.
|
||||
class ListPaneDividerLine extends StatelessWidget {
|
||||
const ListPaneDividerLine({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => usesCupertinoHost(context)
|
||||
? Container(
|
||||
width: 1,
|
||||
color: CupertinoColors.separator.resolveFrom(context),
|
||||
)
|
||||
: const VerticalDivider(width: 1);
|
||||
}
|
||||
|
||||
/// The grab area over [ListPaneDividerLine]: dragging it (mouse or touch)
|
||||
/// resizes the list pane between [min] and [max], the arrow keys move it by
|
||||
/// [kListPaneDividerKeyStep], and a double tap or click returns it to the
|
||||
/// default policy. It shows a handle while hovered, focused or dragged.
|
||||
class ListPaneDivider extends StatefulWidget {
|
||||
const ListPaneDivider({
|
||||
super.key,
|
||||
required this.width,
|
||||
required this.min,
|
||||
required this.max,
|
||||
required this.onChanged,
|
||||
required this.onChangeEnd,
|
||||
required this.onReset,
|
||||
});
|
||||
|
||||
/// The list pane's current width.
|
||||
final double width;
|
||||
final double min;
|
||||
final double max;
|
||||
|
||||
/// A new list width, live while dragging.
|
||||
final ValueChanged<double> onChanged;
|
||||
|
||||
/// The drag or key press ended: the width is final.
|
||||
final VoidCallback onChangeEnd;
|
||||
final VoidCallback onReset;
|
||||
|
||||
@override
|
||||
State<ListPaneDivider> createState() => _ListPaneDividerState();
|
||||
}
|
||||
|
||||
class _ListPaneDividerState extends State<ListPaneDivider> {
|
||||
bool _hovered = false;
|
||||
bool _focused = false;
|
||||
// The unclamped width the pointer asks for since the drag began, so moving
|
||||
// back past a bound picks up where the pointer is, not where it stopped.
|
||||
double? _dragWidth;
|
||||
|
||||
double _clamp(double value) => value.clamp(widget.min, widget.max);
|
||||
|
||||
// The list is at the start: in a right-to-left layout a drag to the left
|
||||
// widens it.
|
||||
double _forward(double dx) =>
|
||||
Directionality.of(context) == TextDirection.rtl ? -dx : dx;
|
||||
|
||||
void _dragStart(DragStartDetails _) =>
|
||||
setState(() => _dragWidth = widget.width);
|
||||
|
||||
void _dragUpdate(DragUpdateDetails details) {
|
||||
final width = (_dragWidth ?? widget.width) + _forward(details.delta.dx);
|
||||
_dragWidth = width;
|
||||
widget.onChanged(_clamp(width));
|
||||
}
|
||||
|
||||
void _dragEnd() {
|
||||
if (_dragWidth == null) return;
|
||||
setState(() => _dragWidth = null);
|
||||
widget.onChangeEnd();
|
||||
}
|
||||
|
||||
// A width a key press sent that the panes have not rebuilt with yet, so
|
||||
// presses quicker than frames add up.
|
||||
double? _stepped;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ListPaneDivider oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_stepped = null;
|
||||
}
|
||||
|
||||
void _step(double delta) {
|
||||
final current = _stepped ?? widget.width;
|
||||
final width = _clamp(current + delta);
|
||||
if (width == current) return;
|
||||
_stepped = width;
|
||||
widget.onChanged(width);
|
||||
widget.onChangeEnd();
|
||||
}
|
||||
|
||||
KeyEventResult _key(FocusNode _, KeyEvent event) {
|
||||
if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final key = event.logicalKey;
|
||||
if (key == LogicalKeyboardKey.arrowRight ||
|
||||
key == LogicalKeyboardKey.arrowLeft) {
|
||||
final right = key == LogicalKeyboardKey.arrowRight ? 1.0 : -1.0;
|
||||
_step(_forward(right * kListPaneDividerKeyStep));
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
|
||||
String _describe(double width) => '${width.round()} pixels';
|
||||
|
||||
Color _handleColor(BuildContext context) => usesCupertinoHost(context)
|
||||
? CupertinoTheme.of(context).primaryColor
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dragging = _dragWidth != null;
|
||||
final active = _hovered || _focused || dragging;
|
||||
final increased = _clamp(widget.width + kListPaneDividerKeyStep);
|
||||
final decreased = _clamp(widget.width - kListPaneDividerKeyStep);
|
||||
return Semantics(
|
||||
identifier: 'list-detail-divider',
|
||||
label: 'List width',
|
||||
hint: 'Drag, or use the arrow keys, to resize the list',
|
||||
slider: true,
|
||||
value: _describe(widget.width),
|
||||
increasedValue: _describe(increased),
|
||||
decreasedValue: _describe(decreased),
|
||||
onIncrease: increased == widget.width
|
||||
? null
|
||||
: () => _step(kListPaneDividerKeyStep),
|
||||
onDecrease: decreased == widget.width
|
||||
? null
|
||||
: () => _step(-kListPaneDividerKeyStep),
|
||||
child: Focus(
|
||||
onKeyEvent: _key,
|
||||
onFocusChange: (value) => setState(() => _focused = value),
|
||||
child: MouseRegion(
|
||||
cursor: SystemMouseCursors.resizeColumn,
|
||||
onEnter: (_) => setState(() => _hovered = true),
|
||||
onExit: (_) => setState(() => _hovered = false),
|
||||
child: GestureDetector(
|
||||
key: const ValueKey('list-detail-divider'),
|
||||
behavior: HitTestBehavior.opaque,
|
||||
// The slider semantics above stand for the drag.
|
||||
excludeFromSemantics: true,
|
||||
// The handle follows the pointer from where it went down.
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onHorizontalDragStart: _dragStart,
|
||||
onHorizontalDragUpdate: _dragUpdate,
|
||||
onHorizontalDragEnd: (_) => _dragEnd(),
|
||||
onHorizontalDragCancel: _dragEnd,
|
||||
onDoubleTap: widget.onReset,
|
||||
child: Center(
|
||||
child: AnimatedOpacity(
|
||||
opacity: active ? 1 : 0,
|
||||
duration: const Duration(milliseconds: 150),
|
||||
child: Container(
|
||||
width: 4,
|
||||
decoration: BoxDecoration(
|
||||
// A hovered or focused handle is a hint; it firms up
|
||||
// while dragged.
|
||||
color: _handleColor(
|
||||
context,
|
||||
).withValues(alpha: dragging ? 1 : 0.5),
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Prefix of the persisted list-pane widths, one key per destination.
|
||||
const listPaneWidthStoragePrefix = 'pipelines.next.listPaneWidth.v1.';
|
||||
|
||||
/// The persistence seam for list-pane widths the operator dragged, in logical
|
||||
/// pixels per destination. Both directions may fail: a null read means
|
||||
/// "nothing saved", a false write means "this session only".
|
||||
abstract interface class ListPaneWidthStore {
|
||||
Future<double?> read(String destination);
|
||||
|
||||
/// Saves [width], or forgets the destination's width when it is null.
|
||||
Future<bool> write(String destination, double? width);
|
||||
}
|
||||
|
||||
/// `shared_preferences` storage under [listPaneWidthStoragePrefix].
|
||||
class SharedPreferencesListPaneWidthStore implements ListPaneWidthStore {
|
||||
const SharedPreferencesListPaneWidthStore();
|
||||
|
||||
@override
|
||||
Future<double?> read(String destination) async {
|
||||
try {
|
||||
return (await SharedPreferences.getInstance()).getDouble(
|
||||
'$listPaneWidthStoragePrefix$destination',
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> write(String destination, double? width) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final key = '$listPaneWidthStoragePrefix$destination';
|
||||
return width == null
|
||||
? await prefs.remove(key)
|
||||
: await prefs.setDouble(key, width);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An in-memory store for tests, fixtures and panes outside a shell.
|
||||
class MemoryListPaneWidthStore implements ListPaneWidthStore {
|
||||
MemoryListPaneWidthStore([Map<String, double>? values])
|
||||
: values = values ?? {};
|
||||
|
||||
final Map<String, double> values;
|
||||
|
||||
@override
|
||||
Future<double?> read(String destination) async => values[destination];
|
||||
|
||||
@override
|
||||
Future<bool> write(String destination, double? width) async {
|
||||
if (width == null) {
|
||||
values.remove(destination);
|
||||
} else {
|
||||
values[destination] = width;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// The list-pane widths the operator chose, per destination; a destination
|
||||
/// without one uses the default policy (`listDetailListWidth`).
|
||||
///
|
||||
/// A drag [preview]s widths live and [commit]s once; a window too small for
|
||||
/// a saved width clamps it when laying out, never here, so the saved value
|
||||
/// survives a small window.
|
||||
class ListPaneWidths extends ChangeNotifier {
|
||||
ListPaneWidths({ListPaneWidthStore? store})
|
||||
: _store = store ?? const SharedPreferencesListPaneWidthStore();
|
||||
|
||||
final ListPaneWidthStore _store;
|
||||
final Map<String, double> _widths = {};
|
||||
final Set<String> _loaded = {};
|
||||
bool _disposed = false;
|
||||
|
||||
/// The chosen width for [destination], null for the default policy. The
|
||||
/// first ask loads the saved width and notifies when it arrives.
|
||||
double? widthFor(String destination) {
|
||||
if (_loaded.add(destination)) _load(destination);
|
||||
return _widths[destination];
|
||||
}
|
||||
|
||||
Future<void> _load(String destination) async {
|
||||
final saved = await _store.read(destination);
|
||||
// A drag before the read finished wins over the saved value.
|
||||
if (_disposed || saved == null || _widths.containsKey(destination)) {
|
||||
return;
|
||||
}
|
||||
_widths[destination] = saved;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Shows [width] for [destination] without saving it.
|
||||
void preview(String destination, double width) {
|
||||
_loaded.add(destination);
|
||||
if (_widths[destination] == width) return;
|
||||
_widths[destination] = width;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Saves the destination's current width.
|
||||
Future<void> commit(String destination) async {
|
||||
await _store.write(destination, _widths[destination]);
|
||||
}
|
||||
|
||||
/// Shows and saves [width] for [destination].
|
||||
Future<void> set(String destination, double width) {
|
||||
preview(destination, width);
|
||||
return commit(destination);
|
||||
}
|
||||
|
||||
/// Returns [destination] to the default policy and forgets its width.
|
||||
Future<void> reset(String destination) async {
|
||||
_loaded.add(destination);
|
||||
if (_widths.remove(destination) != null) notifyListeners();
|
||||
await _store.write(destination, null);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the shell's [ListPaneWidths] to the list-detail panes below it.
|
||||
class ListPaneWidthsScope extends InheritedNotifier<ListPaneWidths> {
|
||||
const ListPaneWidthsScope({
|
||||
super.key,
|
||||
required ListPaneWidths widths,
|
||||
required super.child,
|
||||
}) : super(notifier: widths);
|
||||
|
||||
static ListPaneWidths? maybeOf(BuildContext context) => context
|
||||
.dependOnInheritedWidgetOfExactType<ListPaneWidthsScope>()
|
||||
?.notifier;
|
||||
}
|
||||
Reference in New Issue
Block a user