Files
pipelines/app/lib/shell/windows/window_frame.dart
T
2026-12-04 00:46:54 +00:00

268 lines
8.7 KiB
Dart

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import '../../ui/theme/app_design_tokens.dart';
import 'window_kind.dart';
const double windowCornerRadius = 12;
/// A floating window's corners under the design at [context]:
/// [windowCornerRadius], a design's own window radius (High contrast's), or
/// a dense design's own corners, or a glass design's grown ones.
BorderRadius windowCorners(BuildContext context) {
final tokens = AppDesignTokens.of(context);
final radius = tokens.windowRadius;
if (radius != null) return BorderRadius.circular(radius);
const designed = BorderRadius.all(Radius.circular(windowCornerRadius));
return tokens.glass == null
? tokens.denseCorners(designed)
: tokens.corners(designed);
}
/// The height of a window's title bar: a compact row of icon buttons, taller
/// with larger text (a dense design's slimmer bar grows the same way).
double windowTitleBarHeight(BuildContext context) {
final height = AppDesignTokens.of(context).density.titleBarHeight;
return MediaQuery.textScalerOf(
context,
).scale(height).clamp(height, height + 24);
}
/// A floating window's card and title bar: the title (drag to move, double
/// click to maximise), the kind's actions, then Maximise and the X. The X
/// minimises: it hides the window and keeps its state, like the chord does
/// in reverse. The content is drawn over the card's body by the window's
/// navigator.
class WindowFrame extends StatelessWidget {
const WindowFrame({
super.key,
required this.title,
required this.icon,
required this.actions,
required this.maximized,
required this.onMove,
required this.onMoveEnd,
required this.onMinimize,
required this.onToggleMaximized,
});
final String title;
final IconData icon;
final List<WindowAction> actions;
final bool maximized;
/// The drag's delta and the pointer's global position.
final void Function(Offset delta, Offset globalPosition) onMove;
final VoidCallback onMoveEnd;
/// The X: hides the window, keeping it listed and its state.
final VoidCallback onMinimize;
final VoidCallback onToggleMaximized;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colors = theme.colorScheme;
// A glass design's window is an opaque glass card (its content is not
// blurred, so nothing behind may show through).
final glass = AppDesignTokens.of(context).glass;
final dense = AppDesignTokens.of(context).density.dense;
return Material(
elevation: 8,
clipBehavior: Clip.antiAlias,
borderRadius: windowCorners(context),
color: glass?.solid ?? colors.surfaceContainer,
child: Align(
alignment: Alignment.topCenter,
child: SizedBox(
height: windowTitleBarHeight(context),
child: IconButtonTheme(
data: IconButtonThemeData(
style: IconButton.styleFrom(
visualDensity: VisualDensity.compact,
foregroundColor: colors.onSurfaceVariant,
),
),
child: Row(
children: [
Expanded(child: _title(theme, dense)),
for (final action in actions)
IconButton(
tooltip: action.label,
onPressed: action.onPressed,
icon: Icon(action.icon),
),
IconButton(
tooltip: maximized ? 'Restore size' : 'Maximise',
onPressed: onToggleMaximized,
icon: Icon(
maximized ? Icons.fullscreen_exit : Icons.fullscreen,
),
),
IconButton(
tooltip: 'Minimise',
onPressed: onMinimize,
icon: const Icon(Icons.close),
),
const SizedBox(width: 4),
],
),
),
),
),
);
}
Widget _title(ThemeData theme, bool dense) => MouseRegion(
cursor: maximized ? SystemMouseCursors.basic : SystemMouseCursors.move,
child: GestureDetector(
key: const ValueKey('window-title'),
behavior: HitTestBehavior.opaque,
// The window follows the pointer from where it went down.
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: maximized
? null
: (details) => onMove(details.delta, details.globalPosition),
onPanEnd: maximized ? null : (_) => onMoveEnd(),
onDoubleTap: onToggleMaximized,
child: Padding(
padding: EdgeInsetsDirectional.only(start: dense ? 8 : 12, end: 8),
child: Row(
children: [
Icon(
icon,
size: dense ? 16 : 18,
color: theme.colorScheme.onSurfaceVariant,
),
SizedBox(width: dense ? 6 : 8),
Expanded(
child: Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.titleSmall,
),
),
],
),
),
),
);
}
/// The foreground edge of a floating window. This is separate from
/// [WindowFrame] because the window body is painted by a later navigator
/// child and may otherwise cover the card's decoration.
class WindowOutline extends StatelessWidget {
const WindowOutline({super.key});
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
// A design with heavier frames draws its own (High contrast's 2px of
// the ink); the others keep the hairline.
final tokens = AppDesignTokens.of(context);
final side = tokens.stroke > 1
? tokens.frameSide ?? BorderSide(color: colors.outlineVariant)
: BorderSide(color: colors.outlineVariant);
final corners = windowCorners(context);
final glass = tokens.glass;
return IgnorePointer(
child: DecoratedBox(
// A glass window's edge catches the light along its title bar.
decoration: glass == null
? BoxDecoration(
border: Border.fromBorderSide(side),
borderRadius: corners,
)
: ShapeDecoration(
shape: GlassBorder(
borderRadius: corners,
highlight: glass.highlight,
edge: colors.outlineVariant,
),
),
child: const SizedBox.expand(),
),
);
}
}
/// The lower right corner of a window: drag to resize.
class WindowResizeCorner extends StatelessWidget {
const WindowResizeCorner({
super.key,
required this.onResize,
required this.onResizeEnd,
this.showGrip = true,
});
final ValueChanged<Offset> onResize;
final VoidCallback onResizeEnd;
/// Whether this target draws the grip itself. A window's page draws it
/// instead ([WindowResizeGrip]), under the tooltips its overlay shows.
final bool showGrip;
@override
Widget build(BuildContext context) => Semantics(
label: 'Resize window',
child: MouseRegion(
cursor: SystemMouseCursors.resizeDownRight,
child: GestureDetector(
key: const ValueKey('window-resize'),
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => onResize(details.delta),
onPanEnd: (_) => onResizeEnd(),
child: showGrip ? const WindowResizeGrip() : const SizedBox.expand(),
),
),
);
}
/// The resize corner's arrow, drawn only (the drag target is
/// [WindowResizeCorner]).
class WindowResizeGrip extends StatelessWidget {
const WindowResizeGrip({super.key});
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.all(2),
child: Icon(
Icons.south_east,
size: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
);
}
/// A transparent resize hit region. The visible lower-right affordance
/// remains [WindowResizeCorner]; the other regions deliberately add no chrome.
class WindowResizeHandle extends StatelessWidget {
const WindowResizeHandle({
super.key,
required this.cursor,
required this.onResize,
required this.onResizeEnd,
this.child,
});
final MouseCursor cursor;
final ValueChanged<Offset> onResize;
final VoidCallback onResizeEnd;
final Widget? child;
@override
Widget build(BuildContext context) => MouseRegion(
cursor: cursor,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
dragStartBehavior: DragStartBehavior.down,
onPanUpdate: (details) => onResize(details.delta),
onPanEnd: (_) => onResizeEnd(),
child: child ?? const SizedBox.expand(),
),
);
}