c0d68f7c47
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.
188 lines
6.2 KiB
Dart
188 lines
6.2 KiB
Dart
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),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|