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.
144 lines
4.5 KiB
Dart
144 lines
4.5 KiB
Dart
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;
|
|
}
|