Files
pipelines/app/lib/shell/navigation_rail.dart
T

219 lines
7.7 KiB
Dart

import 'package:flutter/material.dart';
import '../ui/theme/app_design_tokens.dart';
import 'destination.dart';
import 'list_detail_policy.dart';
/// The width of the destination rail on a wide window.
const double kNavigationRailWidth = 96;
/// The rail with its divider: what the rail takes from a wide window's width.
const double kNavigationRailSpace = kNavigationRailWidth + 1;
/// The rail's width under the design at [context]: [kNavigationRailWidth],
/// or a dense design's narrower, icon-only rail.
double navigationRailWidthOf(BuildContext context) =>
AppDesignTokens.of(context).density.railWidth;
/// The rail with its divider under the design at [context].
double navigationRailSpaceOf(BuildContext context) =>
navigationRailWidthOf(context) + 1;
/// The narrowest window that shows the destination rail: the list-detail
/// minimum for the body beside it, plus the rail and its divider (the space the
/// shell takes from the window). Narrower windows keep the floating destination
/// bar, unchanged.
const double kNavigationRailMinWindowWidth =
kListDetailMinWidth + kNavigationRailSpace;
/// The rail's height below which Settings scrolls with the tabs instead of
/// staying pinned to the rail's foot: room for Settings and about two tabs.
const double kNavigationRailPinnedSettingsMinHeight = 240;
/// Whether the window at [context] shows the destination rail. Read from the
/// window's own [MediaQuery], so the shell and the floating windows layer
/// (which sits above the shell) agree.
bool showsNavigationRail(BuildContext context) =>
MediaQuery.sizeOf(context).width >= kNavigationRailMinWindowWidth;
/// Whether the window shows the floating tab bar alone: wide enough for a list
/// beside an open item, too narrow for the rail. That bar lists the tabs only,
/// so there Settings is reached from the Pipelines bar's gear.
bool showsFloatingTabBarOnly(BuildContext context) =>
showsListBesideDetail(context) && !showsNavigationRail(context);
/// The wide layout's left-edge navigation: a Material [NavigationRail] with
/// an icon and a label per tab, the selected tab marked by the rail's
/// indicator, and Settings pinned to its bottom.
///
/// The tabs scroll when the rail is too short for them (a tall terminal
/// panel shrinks the app above it), and on a very short rail Settings
/// scrolls with them, so items never overlap at any height.
///
/// [icon] builds each tab's icon (its running or unread badge included), so
/// the rail shows the same counts the floating bar did. [selected] is the
/// shell's destination: Settings (a page, not a tab) selects no tab but its
/// own item.
class ShellNavigationRail extends StatelessWidget {
const ShellNavigationRail({
super.key,
required this.selected,
required this.icon,
required this.onSelected,
this.padding = EdgeInsets.zero,
});
final AppDestination selected;
final Widget Function(AppDestination destination) icon;
final ValueChanged<AppDestination> onSelected;
/// The system insets the rail keeps clear of: the status bar above and the
/// navigation bar below.
final EdgeInsets padding;
@override
Widget build(BuildContext context) {
final index = shellTabDestinations.indexOf(selected);
final surface = Theme.of(context).colorScheme.surface;
final density = AppDesignTokens.of(context).density;
// The surface runs under the top inset too: the padding only moves the
// rail's content below the system's top, so no unpainted strip shows above
// the rail's background.
return ColoredBox(
key: const ValueKey('shell.navigation-rail.surface'),
color: surface,
child: Padding(
padding: padding,
child: SizedBox(
width: density.railWidth,
child: LayoutBuilder(
builder: (context, constraints) => _rail(
index,
surface,
density,
pinSettings:
constraints.maxHeight >=
kNavigationRailPinnedSettingsMinHeight,
),
),
),
),
);
}
Widget _rail(
int index,
Color surface,
DesignDensity density, {
required bool pinSettings,
}) => NavigationRail(
key: const ValueKey('shell.navigation-rail'),
backgroundColor: surface,
minWidth: density.railWidth,
// An icon-only rail names each tab in its tooltip alone.
labelType: density.railLabels
? NavigationRailLabelType.all
: NavigationRailLabelType.none,
selectedIndex: index < 0 ? null : index,
onDestinationSelected: (value) => onSelected(shellTabDestinations[value]),
destinations: [
for (final destination in shellTabDestinations)
NavigationRailDestination(
icon: KeyedSubtree(
key: ValueKey('shell.destination.${destination.name}'),
// The bar it replaces named each tab in a tooltip too.
// The destination's label already names it for assistive
// technology; the tooltip must not repeat it.
child: Tooltip(
message: destinationDetails(destination).label,
excludeFromSemantics: true,
child: icon(destination),
),
),
label: Text(destinationDetails(destination).label),
),
],
scrollable: true,
trailingAtBottom: pinSettings,
trailing: _SettingsRailItem(
density: density,
selected: selected == AppDestination.settings,
onTap: () => onSelected(AppDestination.settings),
),
);
}
/// Settings at the rail's foot, drawn as a [NavigationRailDestination] is: an
/// icon on a stadium indicator with its label under it, and the same colours.
/// It is not a [NavigationRailDestination] itself because the rail's
/// destination list is the tabs, and `trailing` is what the rail pins.
class _SettingsRailItem extends StatelessWidget {
const _SettingsRailItem({
required this.density,
required this.selected,
required this.onTap,
});
final DesignDensity density;
final bool selected;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final item = destinationDetails(AppDestination.settings);
final foreground = selected
? scheme.onSecondaryContainer
: scheme.onSurfaceVariant;
final textStyle = Theme.of(context).textTheme.labelMedium?.copyWith(
color: foreground,
fontWeight: selected ? FontWeight.w600 : FontWeight.w500,
);
final indicator = Container(
width: 56,
height: 32,
alignment: Alignment.center,
decoration: ShapeDecoration(
shape: AppDesignTokens.of(context).shape(const StadiumBorder()),
color: selected ? scheme.secondaryContainer : null,
),
child: Icon(item.icon, color: foreground),
);
final Widget mark = density.railLabels
? Column(
mainAxisSize: MainAxisSize.min,
children: [
indicator,
const SizedBox(height: 4),
Text(item.label, style: textStyle),
],
)
// An icon-only rail names Settings in a tooltip, as its tabs.
: Center(
child: Tooltip(
message: item.label,
excludeFromSemantics: true,
child: indicator,
),
);
return Semantics(
button: true,
selected: selected,
label: item.label,
child: ExcludeSemantics(
child: InkWell(
key: const ValueKey('shell.destination.settings'),
onTap: onTap,
child: SizedBox(
width: density.railWidth,
child: Padding(
padding: EdgeInsets.symmetric(vertical: density.space(12)),
child: mark,
),
),
),
),
);
}
}