refactor(desktop): reorganize workspace panels and navigation
This commit is contained in:
1 parent
826745b407
commit
1f73765745
92 files changed
+5811
-2650
No files matched your search
@@ -1,4 +1,7 @@
|
||||
abstract final class WorkspaceLayoutMetrics {
|
||||
static const desktopSpacing = 1.0;
|
||||
static const desktopResizeSlop = 2.0;
|
||||
static const desktopResizeHitExtent = desktopSpacing + 2 * desktopResizeSlop;
|
||||
static const dualPaneMinimumWidth = 840.0;
|
||||
|
||||
static const sidebarDefaultWidth = 260.0;
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../errors/app_failure.dart';
|
||||
import '../../l10n/l10n.dart';
|
||||
|
||||
class ErrorNotification {
|
||||
const ErrorNotification({required this.content, this.details});
|
||||
final Widget content;
|
||||
final String? details;
|
||||
|
||||
String get copyText {
|
||||
String textOf(Widget widget) {
|
||||
if (widget is Text) {
|
||||
return widget.data ?? widget.textSpan?.toPlainText() ?? '';
|
||||
}
|
||||
if (widget is SingleChildScrollView && widget.child != null) {
|
||||
return textOf(widget.child!);
|
||||
}
|
||||
if (widget is SingleChildRenderObjectWidget && widget.child != null) {
|
||||
return textOf(widget.child!);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
final message = textOf(content);
|
||||
return details == null || details!.isEmpty || details == message
|
||||
? message
|
||||
: '$message\n\n$details';
|
||||
}
|
||||
}
|
||||
|
||||
/// One error queue above navigation and dialogs, independent of local scaffolds.
|
||||
class ErrorNotificationHost extends StatefulWidget {
|
||||
const ErrorNotificationHost({super.key, required this.child});
|
||||
final Widget child;
|
||||
|
||||
static ErrorNotificationHostState? maybeOf(BuildContext context) =>
|
||||
context.findAncestorStateOfType<ErrorNotificationHostState>();
|
||||
static ErrorNotificationHostState of(BuildContext context) =>
|
||||
maybeOf(context)!;
|
||||
|
||||
@override
|
||||
State<ErrorNotificationHost> createState() => ErrorNotificationHostState();
|
||||
}
|
||||
|
||||
class ErrorNotificationHostState extends State<ErrorNotificationHost> {
|
||||
late final OverlayEntry _entry = OverlayEntry(builder: _buildContent);
|
||||
final _pending = Queue<ErrorNotification>();
|
||||
ErrorNotification? _current;
|
||||
Timer? _timer;
|
||||
bool _copied = false;
|
||||
bool _hovered = false;
|
||||
|
||||
void show(ErrorNotification notification) {
|
||||
if (!mounted) return;
|
||||
_pending.add(notification);
|
||||
if (_current == null) _next();
|
||||
}
|
||||
|
||||
void showFailure(AppFailure failure, String message) => show(
|
||||
ErrorNotification(
|
||||
content: Text(message),
|
||||
details: [
|
||||
'code: ${failure.code.name}',
|
||||
if (failure.httpStatusCode != null) 'HTTP: ${failure.httpStatusCode}',
|
||||
if (failure.diagnostic != null) failure.diagnostic!,
|
||||
].join('\n'),
|
||||
),
|
||||
);
|
||||
|
||||
void _startTimer() {
|
||||
_timer?.cancel();
|
||||
if (_current != null && !_hovered) {
|
||||
_timer = Timer(const Duration(seconds: 8), dismiss);
|
||||
}
|
||||
}
|
||||
|
||||
void _next() {
|
||||
setState(() {
|
||||
_current = _pending.isEmpty ? null : _pending.removeFirst();
|
||||
_copied = false;
|
||||
});
|
||||
_startTimer();
|
||||
}
|
||||
|
||||
void dismiss() {
|
||||
_timer?.cancel();
|
||||
_next();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_timer?.cancel();
|
||||
_pending.clear();
|
||||
setState(() => _current = null);
|
||||
}
|
||||
|
||||
Future<void> _copy() async {
|
||||
final current = _current;
|
||||
if (current == null) return;
|
||||
try {
|
||||
await Clipboard.setData(ClipboardData(text: current.copyText));
|
||||
if (mounted && identical(current, _current)) {
|
||||
setState(() => _copied = true);
|
||||
_startTimer();
|
||||
}
|
||||
} catch (_) {
|
||||
// Keep the error visible so the user can retry copying.
|
||||
_timer?.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_entry.markNeedsBuild();
|
||||
return Overlay(initialEntries: [_entry]);
|
||||
}
|
||||
|
||||
Widget _buildContent(BuildContext context) => Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
widget.child,
|
||||
if (_current case final current?)
|
||||
Positioned(
|
||||
top: MediaQuery.paddingOf(context).top + 48,
|
||||
left: 12,
|
||||
right: 12,
|
||||
child: Align(
|
||||
alignment: Alignment.topCenter,
|
||||
child: MouseRegion(
|
||||
onEnter: (_) {
|
||||
_hovered = true;
|
||||
_timer?.cancel();
|
||||
},
|
||||
onExit: (_) {
|
||||
_hovered = false;
|
||||
_startTimer();
|
||||
},
|
||||
child: Material(
|
||||
key: const Key('global-error-notification'),
|
||||
elevation: 8,
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 520),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 8, 4, 8),
|
||||
child: Semantics(
|
||||
liveRegion: true,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
size: 20,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 180),
|
||||
child: SingleChildScrollView(
|
||||
child: current.content,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('copy-error-button'),
|
||||
tooltip: MaterialLocalizations.of(context)
|
||||
.copyButtonLabel,
|
||||
onPressed: _copy,
|
||||
icon: Icon(
|
||||
_copied ? Icons.check : Icons.copy,
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('close-error-button'),
|
||||
tooltip: MaterialLocalizations.of(context)
|
||||
.closeButtonTooltip,
|
||||
onPressed: dismiss,
|
||||
icon: const Icon(Icons.close, size: 18),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// Publishes a changed structured failure after the current build completes.
|
||||
class ErrorFailureListener extends StatefulWidget {
|
||||
const ErrorFailureListener({super.key, required this.message});
|
||||
final AppFailure? message;
|
||||
@override
|
||||
State<ErrorFailureListener> createState() => _ErrorFailureListenerState();
|
||||
}
|
||||
|
||||
class _ErrorFailureListenerState extends State<ErrorFailureListener> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_schedule();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ErrorFailureListener oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.message != oldWidget.message) _schedule();
|
||||
}
|
||||
|
||||
void _schedule() {
|
||||
final message = widget.message;
|
||||
if (message == null) return;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
ErrorNotificationHost.maybeOf(context)
|
||||
?.showFailure(message, context.l10n.failureMessage(message));
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => const SizedBox.shrink();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// Rebuilds a content region only when its selected presentation data changes.
|
||||
class ListenableSelector<T> extends StatefulWidget {
|
||||
const ListenableSelector({
|
||||
super.key,
|
||||
required this.listenable,
|
||||
required this.select,
|
||||
required this.builder,
|
||||
});
|
||||
|
||||
final Listenable? listenable;
|
||||
final T Function() select;
|
||||
final Widget Function(BuildContext context, T value) builder;
|
||||
|
||||
@override
|
||||
State<ListenableSelector<T>> createState() => _ListenableSelectorState<T>();
|
||||
}
|
||||
|
||||
class _ListenableSelectorState<T> extends State<ListenableSelector<T>> {
|
||||
late T _value;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_value = widget.select();
|
||||
widget.listenable?.addListener(_changed);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ListenableSelector<T> oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.listenable != widget.listenable) {
|
||||
oldWidget.listenable?.removeListener(_changed);
|
||||
widget.listenable?.addListener(_changed);
|
||||
}
|
||||
_value = widget.select();
|
||||
}
|
||||
|
||||
void _changed() {
|
||||
final value = widget.select();
|
||||
if (value == _value) return;
|
||||
setState(() => _value = value);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => widget.builder(context, _value);
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.listenable?.removeListener(_changed);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import 'package:cutdex_ui/theme.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Paints the border outside the content clip, keeping both curves concentric.
|
||||
class WorkspaceCard extends StatefulWidget {
|
||||
const WorkspaceCard({super.key, required this.child});
|
||||
|
||||
static const radius = 12.0;
|
||||
static const borderWidth = 1.0;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<WorkspaceCard> createState() => _WorkspaceCardState();
|
||||
}
|
||||
|
||||
class _WorkspaceCardState extends State<WorkspaceCard> {
|
||||
late final ValueNotifier<Widget> _content;
|
||||
late final Widget _frame;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_content = ValueNotifier(widget.child);
|
||||
_frame = _WorkspaceCardFrame(
|
||||
child: ValueListenableBuilder<Widget>(
|
||||
valueListenable: _content,
|
||||
builder: (context, child, _) => child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant WorkspaceCard oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_content.value = widget.child;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => _frame;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_content.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// The frame subscribes only to theme changes; content has its own update slot.
|
||||
class _WorkspaceCardFrame extends StatelessWidget {
|
||||
const _WorkspaceCardFrame({required this.child});
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final surface = Theme.of(context).colorScheme.surface;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: surface,
|
||||
borderRadius: BorderRadius.circular(WorkspaceCard.radius),
|
||||
border: Border.all(
|
||||
color: AppTheme.sidebarDividerColor(context),
|
||||
width: WorkspaceCard.borderWidth,
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(WorkspaceCard.borderWidth),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(
|
||||
WorkspaceCard.radius - WorkspaceCard.borderWidth,
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Material(color: surface, child: child),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Keeps the resize cursor active across the window while a split is dragged.
|
||||
class WorkspaceResizeHandle extends StatefulWidget {
|
||||
const WorkspaceResizeHandle({
|
||||
super.key,
|
||||
required this.axis,
|
||||
required this.initialValue,
|
||||
required this.onResize,
|
||||
this.direction = 1,
|
||||
this.onEnd,
|
||||
});
|
||||
|
||||
final Axis axis;
|
||||
final double initialValue;
|
||||
final double direction;
|
||||
final ValueChanged<double> onResize;
|
||||
final VoidCallback? onEnd;
|
||||
|
||||
@override
|
||||
State<WorkspaceResizeHandle> createState() => _WorkspaceResizeHandleState();
|
||||
}
|
||||
|
||||
class _WorkspaceResizeHandleState extends State<WorkspaceResizeHandle> {
|
||||
OverlayEntry? _cursorOverlay;
|
||||
double? _startPosition;
|
||||
double _startValue = 0;
|
||||
|
||||
MouseCursor get _cursor => widget.axis == Axis.horizontal
|
||||
? SystemMouseCursors.resizeColumn
|
||||
: SystemMouseCursors.resizeRow;
|
||||
|
||||
double _coordinate(Offset position) =>
|
||||
widget.axis == Axis.horizontal ? position.dx : position.dy;
|
||||
|
||||
void _start(DragStartDetails details) {
|
||||
_startPosition = _coordinate(details.globalPosition);
|
||||
_startValue = widget.initialValue;
|
||||
_releaseCursor();
|
||||
final cursor = _cursor;
|
||||
_cursorOverlay = OverlayEntry(
|
||||
builder: (_) => Positioned.fill(
|
||||
child: MouseRegion(
|
||||
cursor: cursor,
|
||||
// Register the cursor while letting pointer hits reach widgets below.
|
||||
hitTestBehavior: HitTestBehavior.translucent,
|
||||
child: const SizedBox.expand(),
|
||||
),
|
||||
),
|
||||
);
|
||||
Overlay.of(context, rootOverlay: true).insert(_cursorOverlay!);
|
||||
}
|
||||
|
||||
void _update(DragUpdateDetails details) {
|
||||
final start = _startPosition;
|
||||
if (start == null) return;
|
||||
widget.onResize(
|
||||
_startValue +
|
||||
widget.direction * (_coordinate(details.globalPosition) - start),
|
||||
);
|
||||
}
|
||||
|
||||
void _releaseCursor() {
|
||||
_cursorOverlay?.remove();
|
||||
_cursorOverlay?.dispose();
|
||||
_cursorOverlay = null;
|
||||
}
|
||||
|
||||
void _finish() {
|
||||
_startPosition = null;
|
||||
_releaseCursor();
|
||||
widget.onEnd?.call();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_releaseCursor();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => MouseRegion(
|
||||
cursor: _cursor,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onHorizontalDragStart: widget.axis == Axis.horizontal ? _start : null,
|
||||
onHorizontalDragUpdate: widget.axis == Axis.horizontal ? _update : null,
|
||||
onHorizontalDragEnd: widget.axis == Axis.horizontal
|
||||
? (_) => _finish()
|
||||
: null,
|
||||
onVerticalDragStart: widget.axis == Axis.vertical ? _start : null,
|
||||
onVerticalDragUpdate: widget.axis == Axis.vertical ? _update : null,
|
||||
onVerticalDragEnd: widget.axis == Axis.vertical ? (_) => _finish() : null,
|
||||
onHorizontalDragCancel: widget.axis == Axis.horizontal ? _finish : null,
|
||||
onVerticalDragCancel: widget.axis == Axis.vertical ? _finish : null,
|
||||
child: const SizedBox.expand(),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user