import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'window_kind.dart'; const double windowCornerRadius = 12; /// The height of a window's title bar: a compact row of icon buttons, taller /// with larger text. double windowTitleBarHeight(BuildContext context) => MediaQuery.textScalerOf(context).scale(40).clamp(40.0, 64.0); /// A floating window's card and title bar: the title (drag to move, double /// click to maximise), the kind's actions, then Minimise, Maximise and /// Close. 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, required this.onClose, }); final String title; final IconData icon; final List 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; final VoidCallback onMinimize; final VoidCallback onToggleMaximized; final VoidCallback onClose; @override Widget build(BuildContext context) { final theme = Theme.of(context); final colors = theme.colorScheme; return Material( elevation: 8, clipBehavior: Clip.antiAlias, borderRadius: BorderRadius.circular(windowCornerRadius), color: 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)), for (final action in actions) IconButton( tooltip: action.label, onPressed: action.onPressed, icon: Icon(action.icon), ), IconButton( tooltip: 'Minimise', onPressed: onMinimize, icon: const Icon(Icons.minimize), ), IconButton( tooltip: maximized ? 'Restore size' : 'Maximise', onPressed: onToggleMaximized, icon: Icon( maximized ? Icons.fullscreen_exit : Icons.fullscreen, ), ), IconButton( tooltip: 'Close', onPressed: onClose, icon: const Icon(Icons.close), ), const SizedBox(width: 4), ], ), ), ), ), ); } Widget _title(ThemeData theme) => 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: const EdgeInsetsDirectional.only(start: 12, end: 8), child: Row( children: [ Icon(icon, size: 18, color: theme.colorScheme.onSurfaceVariant), const SizedBox(width: 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; return IgnorePointer( child: DecoratedBox( decoration: BoxDecoration( border: Border.all(color: colors.outlineVariant), borderRadius: BorderRadius.circular(windowCornerRadius), ), 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, }); final ValueChanged onResize; final VoidCallback onResizeEnd; @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: Padding( padding: const EdgeInsets.all(2), child: Icon( Icons.south_east, size: 16, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ), ), ); }