Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

35.8 KB
Raw Blame
import 'dart:async';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

import '../settings/experimental_features.dart';
import '../shared/theme/app_icons.dart';
import '../shared/theme/app_theme.dart' as grid;
import '../shared/widgets/app_menu.dart';
import '../shared/widgets/skeleton.dart';
import '../shared/widgets/app_icon_button.dart';
import '../core/test_run.dart';
import '../state/app_state.dart';
import '../super_tabs/visible_tick.dart';
import '../widgets/desktop_chrome.dart';
import 'device_artwork.dart';
import 'device_settings.dart';
import 'device_setup_dialog.dart';
import 'devices_controller.dart';
import 'pet_settings.dart';
import 'pet_source.dart';
import 'wifi_robots.dart';

final harnessDeviceShopUrl = Uri.parse(
  'https://www.autonomous.ai/harness-device',
);

class DevicesTab extends StatefulWidget {
  const DevicesTab({super.key, required this.notifier, this.refreshEvery});
  final AppNotifier notifier;

  /// How often the machines' devices are read again while the tab is seen. Null: every 10
  /// seconds, and never under test (where a test passes its own).
  @visibleForTesting
  final Duration? refreshEvery;
  @override
  State<DevicesTab> createState() => _DevicesTabState();
}

class _DevicesTabState extends State<DevicesTab>
    with WidgetsBindingObserver, VisibleTick<DevicesTab> {
  late final controller = DevicesController(
    hosts: widget.notifier.deviceHosts,
    accountId: widget.notifier.currentUser?.id ?? 'guest',
    storage: widget.notifier.deviceLibraryStorage,
    sendHostSettings: widget.notifier.setHostDeviceSettings,
    petRequest: widget.notifier.petRequest,
  );

  /// The robots paired over the network, through this computer's CLI.
  late final wifi = WifiRobotsController(
    onLocalNetworkBlocked: widget.notifier.localNetworkBlocked,
  );
  late final _every =
      widget.refreshEvery ?? (kUnderTest ? null : const Duration(seconds: 10));
  @override
  void initState() {
    super.initState();
    unawaited(widget.notifier.refreshDevices());
    // Again on the interval while the tab can be seen, and once as it is seen again.
    if (_every case final every?) visibleTick(every);
  }

  @override
  void onTick() => unawaited(widget.notifier.refreshDevices());

  @override
  void onScreenChanged(bool onScreen) {
    if (onScreen && _every != null) onTick();
  }

  @override
  void dispose() {
    controller.dispose();
    wifi.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => ListenableBuilder(
    listenable: widget.notifier.experimentalFeatures,
    builder: (context, _) => DevicesScreen(
      controller: controller,
      wifi: wifi,
      showCompanion: widget.notifier.experimentalFeatures.enabled(
        ExperimentalFeature.focusBarCreature,
      ),
      // Under a Swarm's Manage, whose own title and tabs already say where this
      // is.
      underManage: widget.notifier.activeSwarm.isSuperTab,
      onRefresh: () async {
        await Future.wait([widget.notifier.refreshDevices(), wifi.refresh()]);
      },
    ),
  );
}

class DevicesScreen extends StatefulWidget {
  const DevicesScreen({
    super.key,
    required this.controller,
    this.wifi,
    this.showCompanion = false,
    this.underManage = false,
    this.openShop,
    this.onRefresh,
    this.pickPetFile,
    this.resolvePetSource,
    this.loadPetSheet,
  });
  final DevicesController controller;

  /// The robots paired over the network. Null leaves Add device to the USB cable alone.
  final WifiRobotsController? wifi;

  /// Whether the desktop companion is on (Experimental), for the rows that follow it.
  final bool showCompanion;

  /// Drawn under Manage's title and tabs: no second title, and on the page's
  /// content edge.
  final bool underManage;
  final Future<bool> Function(Uri)? openShop;
  final Future<void> Function()? onRefresh;

  /// Replace the pet file chooser, [resolvePetSource] and [decodePetSheet]
  /// (tests).
  final Future<String?> Function()? pickPetFile;
  final Future<PetSource> Function(String path)? resolvePetSource;
  final Future<PetSheet?> Function(String path)? loadPetSheet;
  @override
  State<DevicesScreen> createState() => _DevicesScreenState();
}

class _DevicesScreenState extends State<DevicesScreen>
    with WidgetsBindingObserver, VisibleTick<DevicesScreen> {
  String? _selectedKey;
  String? _shopError;
  final _cards = ScrollController();
  final _page = ScrollController();

  /// The selected dial's pet edit, shared by the Pet section and the row
  /// viewer in the left column. One per dial: another selection drops it.
  PetEditor? _pet;

  /// While a pet is edited on a wide screen, its row viewer sits in the left
  /// column beside the Pet section: [_besideKey] marks the two columns,
  /// [_petKey] the Pet section, [_showcaseKey] what the left column holds
  /// above the viewer, [_viewerKey] the viewer.
  final _besideKey = GlobalKey();
  final _petKey = GlobalKey();
  final _showcaseKey = GlobalKey();
  final _viewerKey = GlobalKey();

  /// Where those were last laid out; null until measured.
  _Beside? _beside;

  /// The viewer's top in the columns, kept beside the part of the Pet
  /// section in view; null until measured (the viewer is hidden till then).
  final _viewerTop = ValueNotifier<double?>(null);

  /// Room under the columns for a viewer longer than both.
  double _room = 0;

  /// Room above the Pet section, so it starts level with the viewer when
  /// the left column holds more above it than the right.
  double _petGap = 0;
  bool _measuring = false;

  @override
  void initState() {
    super.initState();
    _page.addListener(_place);
    unawaited(widget.controller.load());
    widget.wifi?.start();
  }

  // The robots are read again only while the screen can be seen.
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widget.wifi?.seen = onScreen;
  }

  @override
  void onScreenChanged(bool onScreen) => widget.wifi?.seen = onScreen;

  @override
  void didUpdateWidget(DevicesScreen oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.controller != widget.controller) _dropPet();
  }

  @override
  void dispose() {
    _cards.dispose();
    _page.dispose();
    _viewerTop.dispose();
    _pet?.removeListener(_petChanged);
    _pet?.dispose();
    super.dispose();
  }

  void _petChanged() {
    if (mounted) setState(() {});
  }

  /// Ends the current pet edit. Disposed after the frame, once no widget
  /// listens to it any more.
  void _dropPet() {
    final old = _pet;
    if (old == null) return;
    _pet = null;
    old.removeListener(_petChanged);
    WidgetsBinding.instance.addPostFrameCallback((_) => old.dispose());
  }

  /// The pet edit for [device], made on first use; null where pets are not
  /// offered.
  PetEditor? _petFor(HarnessDevice? device) {
    final key =
        device != null && device.local && widget.controller.petRequest != null
        ? device.key
        : null;
    if (_pet?.deviceKey == key) return _pet;
    _dropPet();
    if (key == null) return null;
    return _pet = PetEditor(
      controller: widget.controller,
      deviceKey: key,
      resolveSource: widget.resolvePetSource,
      loadSheet: widget.loadPetSheet,
    )..addListener(_petChanged);
  }

  /// Measures the columns after this frame, once.
  void _measureSoon() {
    if (_measuring) return;
    _measuring = true;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _measuring = false;
      if (mounted) _measure();
    });
  }

  RenderBox? _box(GlobalKey key) {
    final box = key.currentContext?.findRenderObject();
    return box is RenderBox && box.attached && box.hasSize ? box : null;
  }

  void _measure() {
    final columns = _box(_besideKey);
    final pet = _box(_petKey);
    final showcase = _box(_showcaseKey);
    final viewer = _box(_viewerKey);
    final page = _page.hasClients
        ? _page.position.context.storageContext.findRenderObject()
        : null;
    if (columns == null ||
        pet == null ||
        showcase == null ||
        viewer == null ||
        page is! RenderBox) {
      _beside = null;
      _viewerTop.value = null;
      return;
    }
    final petTop = pet.localToGlobal(Offset.zero, ancestor: columns).dy;
    final beside = _Beside(
      top:
          columns.localToGlobal(Offset.zero).dy -
          page.localToGlobal(Offset.zero).dy +
          _page.offset,
      height: columns.size.height,
      petTop: petTop,
      petBottom: petTop + pet.size.height,
      showcaseBottom:
          showcase.localToGlobal(Offset.zero, ancestor: columns).dy +
          showcase.size.height,
      viewer: viewer.size.height,
    );
    _beside = beside;
    final gap = math.max(0.0, beside.showcaseBottom + 16 - (petTop - _petGap));
    final room = math.max(0.0, beside.highest + beside.viewer - beside.height);
    if ((gap - _petGap).abs() > .5 || (room - _room).abs() > .5) {
      // Measured again once laid out with them.
      setState(() {
        _petGap = gap;
        _room = room;
      });
      return;
    }
    _place();
  }

  /// Keeps the viewer beside the part of the Pet section in view.
  void _place() {
    final beside = _beside;
    if (beside == null || !_page.hasClients) return;
    final low = beside.highest;
    final high = math.max(low, beside.petBottom - beside.viewer);
    _viewerTop.value = (_page.offset - beside.top + _stickyGap).clamp(
      low,
      high,
    );
  }

  /// The viewer's distance from the top of the window while it follows the
  /// Pet section.
  static const _stickyGap = 16.0;

  Future<void> _shop() async {
    var opened = false;
    try {
      opened =
          await (widget.openShop?.call(harnessDeviceShopUrl) ??
              launchUrl(
                harnessDeviceShopUrl,
                mode: LaunchMode.externalApplication,
              ));
    } catch (_) {
      /* The inline retry remains beside the action. */
    }
    if (mounted) {
      setState(
        () => _shopError = opened ? null : 'Couldn’t open the shop. Try again.',
      );
    }
  }

  /// The left column's width on a wide screen.
  static const _leftColumn = 340.0;

  /// After this frame, measures the viewer's place when it is [beside] the
  /// Pet section, else forgets it.
  void _settle(bool beside) {
    if (beside) return _measureSoon();
    if (_beside == null &&
        _room == 0 &&
        _petGap == 0 &&
        _viewerTop.value == null) {
      return;
    }
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (!mounted) return;
      _beside = null;
      _viewerTop.value = null;
      if (_room != 0 || _petGap != 0) {
        setState(() => _room = _petGap = 0);
      }
    });
  }

  final _addMenu = MenuController();

  Future<void> _pairWifi() async {
    final wifi = widget.wifi;
    if (wifi == null) return;
    await showWifiPairDialog(context, wifi);
  }

  /// Add device: on a cable, or — with the network's robots on offer — by the code a robot shows.
  Widget _addButton() {
    final button = FilledButton(
      key: const Key('devices-add'),
      onPressed: widget.wifi == null
          ? () => _setup()
          : () => _addMenu.isOpen ? _addMenu.close() : _addMenu.open(),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Icon(AppIcons.plus, size: 16),
          const SizedBox(width: 7),
          const Text('Add device'),
          if (widget.wifi != null) ...[
            const SizedBox(width: 5),
            const Icon(AppIcons.chevronDown, size: 14),
          ],
        ],
      ),
    );
    if (widget.wifi == null) return button;
    return AppDropdown(
      controller: _addMenu,
      children: [
        AppMenuItem(
          key: const Key('devices-add-usb'),
          label: 'USB cable',
          leadingSlot: false,
          onPressed: () => unawaited(_setup()),
        ),
        AppMenuItem(
          key: const Key('devices-add-wifi'),
          label: 'Wi-Fi code',
          leadingSlot: false,
          onPressed: () => unawaited(_pairWifi()),
        ),
      ],
      child: button,
    );
  }

  Future<void> _setup({HarnessDevice? editing}) async {
    final key = await showDeviceSetup(
      context,
      widget.controller,
      editing: editing,
    );
    if (mounted && key != null) setState(() => _selectedKey = key);
  }

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return ColoredBox(
      color: grid.AppPalette.swarmWelcome,
      child: ListenableBuilder(
        listenable: widget.controller,
        builder: (context, _) => LayoutBuilder(
          builder: (context, bounds) {
            // Keep saved identities for reconnection, but only present devices
            // that are plugged in now, on a host that has answered. A device
            // seen once and unplugged since comes back when it is plugged in.
            final devices = widget.controller.devices
                .where(
                  (device) =>
                      device.hostOnline &&
                      device.hostAvailable &&
                      device.status.attached,
                )
                .toList();
            final selected =
                devices.where((d) => d.key == _selectedKey).firstOrNull ??
                devices.where((d) => d.status.attached).firstOrNull ??
                devices.firstOrNull;
            _selectedKey = selected?.key;
            final pet = _petFor(selected);
            final pad = widget.underManage
                ? grid.AppDesktop.panelPadding
                : bounds.maxWidth < 640
                ? 20.0
                : 40.0;
            return SingleChildScrollView(
              key: const PageStorageKey('devices-page'),
              controller: _page,
              padding: EdgeInsets.fromLTRB(
                pad,
                widget.underManage ? grid.AppDesktop.groupGap : 36,
                pad,
                40,
              ),
              child: Center(
                child: ConstrainedBox(
                  constraints: const BoxConstraints(maxWidth: 1120),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Wrap(
                        alignment: WrapAlignment.spaceBetween,
                        crossAxisAlignment: WrapCrossAlignment.center,
                        spacing: 24,
                        runSpacing: 18,
                        children: [
                          if (widget.underManage)
                            Text(
                              'All your devices. Across your computers.',
                              style: DesktopChrome.text(
                                color: DesktopChrome.muted,
                              ),
                            )
                          else
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text('Devices', style: grid.AppType.display()),
                                const SizedBox(height: 7),
                                Text(
                                  'All your devices. Across your computers.',
                                  style: DesktopChrome.text(
                                    color: DesktopChrome.muted,
                                  ),
                                ),
                              ],
                            ),
                          Wrap(
                            spacing: 10,
                            runSpacing: 10,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              if (widget.onRefresh != null)
                                AppIconButton(
                                  tooltip: 'Refresh devices',
                                  onPressed: widget.onRefresh,
                                  icon: AppIcons.refreshCw,
                                ),
                              TextButton(
                                onPressed: _shop,
                                child: const Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Text('Shop'),
                                    SizedBox(width: 7),
                                    Icon(AppIcons.arrowUpRight, size: 16),
                                  ],
                                ),
                              ),
                              _addButton(),
                            ],
                          ),
                        ],
                      ),
                      if (_shopError != null) ...[
                        const SizedBox(height: 12),
                        Semantics(
                          liveRegion: true,
                          child: Text(
                            _shopError!,
                            style: DesktopChrome.metadata(
                              color: Theme.of(context).colorScheme.error,
                            ),
                          ),
                        ),
                      ],
                      if (widget.controller.error case final error?) ...[
                        const SizedBox(height: 16),
                        Wrap(
                          crossAxisAlignment: WrapCrossAlignment.center,
                          spacing: 12,
                          children: [
                            Text(error, style: DesktopChrome.metadata()),
                            TextButton(
                              onPressed: widget.controller.retrySave,
                              child: const Text('Retry'),
                            ),
                          ],
                        ),
                      ],
                      if (widget.wifi case final wifi?)
                        WifiRobotsList(controller: wifi, topGap: 32),
                      const SizedBox(height: 32),
                      if (!widget.controller.loaded)
                        const SkeletonBlock(
                          child: Skeleton(
                            width: double.infinity,
                            height: 320,
                            radius: 18,
                          ),
                        )
                      else if (selected == null)
                        _EmptyDevices(
                          onAdd: () => _setup(),
                          onShop: _shop,
                          returning: widget.controller.devices.isNotEmpty,
                        )
                      else ...[
                        Text(
                          '${devices.length} ${devices.length == 1 ? 'device' : 'devices'}',
                          style: DesktopChrome.metadata(),
                        ),
                        const SizedBox(height: 12),
                        LayoutBuilder(
                          builder: (context, constraints) {
                            final scale = MediaQuery.textScalerOf(context)
                                .scale(1);
                            final cardWidth = math.max(
                              194.0,
                              math.min(220.0, (constraints.maxWidth - 64) / 5),
                            );
                            return Scrollbar(
                              controller: _cards,
                              child: SingleChildScrollView(
                                controller: _cards,
                                scrollDirection: Axis.horizontal,
                                padding: const EdgeInsets.only(bottom: 14),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    for (
                                      var i = 0;
                                      i < devices.length;
                                      i++
                                    ) ...[
                                      if (i > 0) const SizedBox(width: 16),
                                      SizedBox(
                                        width: cardWidth * math.min(scale, 1.4),
                                        child: _DeviceCard(
                                          device: devices[i],
                                          selected:
                                              devices[i].key == selected.key,
                                          onPressed: () => setState(
                                            () => _selectedKey = devices[i].key,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ],
                                ),
                              ),
                            );
                          },
                        ),
                        const SizedBox(height: 22),
                        Divider(color: DesktopChrome.rim, height: 1),
                        const SizedBox(height: 26),
                        Wrap(
                          alignment: WrapAlignment.spaceBetween,
                          crossAxisAlignment: WrapCrossAlignment.center,
                          spacing: 20,
                          runSpacing: 8,
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  selected.name,
                                  style: grid.AppType.title(),
                                ),
                                const SizedBox(height: 5),
                                Text(
                                  '${selected.model.label} · ${selected.machineName} · ${selected.connectionLabel}',
                                  style: DesktopChrome.metadata(),
                                ),
                              ],
                            ),
                            TextButton(
                              onPressed: () => _setup(editing: selected),
                              child: const Text('Edit details'),
                            ),
                          ],
                        ),
                        const SizedBox(height: 24),
                        LayoutBuilder(
                          builder: (context, constraints) {
                            final wide =
                                constraints.maxWidth >= 850 &&
                                MediaQuery.textScalerOf(context).scale(1) <=
                                    1.5;
                            final beside = wide && pet != null && pet.editing;
                            final showcase = _DeviceShowcase(device: selected);
                            final settings = DeviceSettingsPanel(
                              key: ValueKey(selected.key),
                              device: selected,
                              controller: widget.controller,
                              petEditor: pet,
                              petViewerBeside: beside,
                              petKey: _petKey,
                              petGap: beside ? _petGap : 0,
                              pickPetFile: widget.pickPetFile,
                              showCompanion: widget.showCompanion,
                            );
                            if (!wide) {
                              _settle(false);
                              return Column(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  showcase,
                                  const SizedBox(height: 24),
                                  settings,
                                ],
                              );
                            }
                            final columns = Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  width: _leftColumn,
                                  child: KeyedSubtree(
                                    key: _showcaseKey,
                                    child: showcase,
                                  ),
                                ),
                                const SizedBox(width: 36),
                                Expanded(child: settings),
                              ],
                            );
                            _settle(beside);
                            if (!beside) return columns;
                            return Padding(
                              padding: EdgeInsets.only(bottom: _room),
                              child: Stack(
                                key: _besideKey,
                                clipBehavior: Clip.none,
                                children: [
                                  columns,
                                  ValueListenableBuilder<double?>(
                                    valueListenable: _viewerTop,
                                    builder: (context, top, child) =>
                                        Positioned(
                                          left: 0,
                                          width: _leftColumn,
                                          top: top ?? 0,
                                          child: Opacity(
                                            opacity: top == null ? 0 : 1,
                                            child: child,
                                          ),
                                        ),
                                    child: KeyedSubtree(
                                      key: _viewerKey,
                                      child: PetRowViewer(
                                        key: const ValueKey(
                                          'devices-pet-viewer',
                                        ),
                                        editor: pet,
                                        maxViewer: (bounds.maxHeight - 330)
                                            .clamp(160.0, 300.0),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            );
                          },
                        ),
                      ],
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

class _DeviceCard extends StatelessWidget {
  const _DeviceCard({
    required this.device,
    required this.selected,
    required this.onPressed,
  });
  final HarnessDevice device;
  final bool selected;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) => Semantics(
    selected: selected,
    button: true,
    label:
        '${device.name}, ${device.model.label}, ${device.machineName}, ${device.connectionLabel}',
    child: Material(
      color: selected ? DesktopChrome.selection : DesktopChrome.surface,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(18),
        side: BorderSide(
          color: selected ? DesktopChrome.accent : DesktopChrome.rim,
          width: selected ? 1.5 : 1,
        ),
      ),
      clipBehavior: Clip.antiAlias,
      child: InkWell(
        key: ValueKey('device-card-${device.key}'),
        onTap: onPressed,
        child: ExcludeSemantics(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              SizedBox(
                height: 116,
                child: DeviceArtwork(
                  closeUp: true,
                  square: device.hasSquareDisplay,
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(15, 13, 15, 14),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: Tooltip(
                            message: device.name,
                            child: Text(
                              device.name,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: DesktopChrome.text(medium: true),
                            ),
                          ),
                        ),
                        if (selected) ...[
                          const SizedBox(width: 5),
                          Icon(
                            AppIcons.circleCheck,
                            size: 16,
                            color: DesktopChrome.accent,
                          ),
                        ],
                      ],
                    ),
                    const SizedBox(height: 3),
                    Text(device.model.label, style: DesktopChrome.metadata()),
                    const SizedBox(height: 3),
                    Text(
                      device.machineName,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: DesktopChrome.metadata(),
                    ),
                    const SizedBox(height: 9),
                    Text(
                      device.connectionLabel,
                      style: DesktopChrome.metadata(
                        color: device.status.attached
                            ? DesktopChrome.foreground
                            : DesktopChrome.muted,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

class _DeviceShowcase extends StatelessWidget {
  const _DeviceShowcase({required this.device});
  final HarnessDevice device;
  @override
  Widget build(BuildContext context) => Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      ClipRRect(
        borderRadius: BorderRadius.circular(18),
        child: SizedBox(
          height: 205,
          child: DeviceArtwork(
            side: true,
            closeUp: true,
            square: device.hasSquareDisplay,
          ),
        ),
      ),
      const SizedBox(height: 16),
      if (!device.status.isPro) ...[
        Material(
          color: DesktopChrome.surface,
          borderRadius: BorderRadius.circular(14),
          child: InkWell(
            key: const Key('devices-faces'),
            borderRadius: BorderRadius.circular(14),
            onTap: () => showDeviceFaces(context),
            child: Padding(
              padding: const EdgeInsets.all(14),
              child: Row(
                children: [
                  const FocusFacePreview(size: 52),
                  const SizedBox(width: 14),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Faces',
                          style: DesktopChrome.control(medium: true),
                        ),
                        const SizedBox(height: 3),
                        Text('Focus', style: DesktopChrome.metadata()),
                      ],
                    ),
                  ),
                  Icon(
                    AppIcons.chevronRight,
                    size: 16,
                    color: DesktopChrome.muted,
                  ),
                ],
              ),
            ),
          ),
        ),
        const SizedBox(height: 16),
      ],
      _DeviceFacts(device: device),
    ],
  );
}

/// The dial's firmware, display and hardware address.
class _DeviceFacts extends StatelessWidget {
  const _DeviceFacts({required this.device});
  final HarnessDevice device;
  @override
  Widget build(BuildContext context) => Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        [
          if (device.status.fw != null) 'Firmware ${device.status.fw}',
          if (device.status.settings != null)
            '${device.status.settings!.face} px display',
        ].join(' · '),
        style: DesktopChrome.metadata(),
      ),
      if (device.status.mac ?? device.status.id case final id?) ...[
        const SizedBox(height: 4),
        SelectableText(id, style: DesktopChrome.metadata()),
      ],
    ],
  );
}

/// Where the two columns, the Pet section and the viewer were laid out:
/// [top] is the columns' place in the page, the rest is in the columns.
@immutable
class _Beside {
  const _Beside({
    required this.top,
    required this.height,
    required this.petTop,
    required this.petBottom,
    required this.showcaseBottom,
    required this.viewer,
  });
  final double top, height, petTop, petBottom, showcaseBottom, viewer;

  /// The highest the viewer sits: level with the Pet section, below what the
  /// left column holds above it.
  double get highest => math.max(petTop, showcaseBottom + 16);
}

class _EmptyDevices extends StatelessWidget {
  const _EmptyDevices({
    required this.onAdd,
    required this.onShop,
    this.returning = false,
  });
  final VoidCallback onAdd, onShop;
  final bool returning;
  @override
  Widget build(BuildContext context) => LayoutBuilder(
    builder: (context, constraints) {
      final copy = Padding(
        padding: const EdgeInsets.all(36),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              returning
                  ? 'No devices connected'
                  : 'A little closer\nto your work.',
              style: grid.AppType.display(),
            ),
            const SizedBox(height: 18),
            Text(
              returning
                  ? 'Connect a device by USB-C to a computer running Harness. It will appear here automatically.'
                  : 'Your agents, at a glance. A quiet chime when they need you. Make a home for every Harness on your desk.',
              style: DesktopChrome.text(color: DesktopChrome.muted),
            ),
            const SizedBox(height: 28),
            Wrap(
              spacing: 10,
              runSpacing: 10,
              children: [
                FilledButton(
                  onPressed: onAdd,
                  child: Text(
                    returning ? 'Add device' : 'Add your first device',
                  ),
                ),
                TextButton(
                  onPressed: onShop,
                  child: const Text('Shop Harness'),
                ),
              ],
            ),
            const SizedBox(height: 24),
            Text(
              'Have a device? Connect it with a USB-C cable to get started.',
              style: DesktopChrome.metadata(),
            ),
          ],
        ),
      );
      return Material(
        color: DesktopChrome.surface,
        borderRadius: BorderRadius.circular(24),
        clipBehavior: Clip.antiAlias,
        child:
            constraints.maxWidth < 760 ||
                MediaQuery.textScalerOf(context).scale(1) > 1.5
            ? Column(
                children: [
                  const SizedBox(height: 260, child: DeviceArtwork(desk: true)),
                  copy,
                ],
              )
            : Row(
                children: [
                  Expanded(
                    flex: 6,
                    child: SizedBox(
                      height: 440,
                      child: const DeviceArtwork(desk: true),
                    ),
                  ),
                  Expanded(flex: 5, child: copy),
                ],
              ),
      );
    },
  );
}