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.

8.7 KB
Raw Blame
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/harness_file_store.dart';
import '../../core/local_key_value_store.dart';
import '../../core/phone_app_links.dart';
import '../../shared/theme/app_icons.dart';
import '../../shared/theme/app_theme.dart' as grid;
import '../../shared/theme/app_type.dart';
import '../../state/app_state.dart';
import '../../widgets/web_download_button.dart' show openInNewTab;
import 'web_hidden_under_keyboard.dart';

final _appStore = Uri.parse(kAppStoreUrl);
final _googlePlay = Uri.parse(kGooglePlayUrl);

/// The Harness app's store page for the phone or tablet this browser runs on;
/// null on a computer, which has no store app to send anyone to.
Uri? webStoreLink([TargetPlatform? platform]) =>
    switch (platform ?? defaultTargetPlatform) {
      TargetPlatform.iOS => _appStore,
      TargetPlatform.android => _googlePlay,
      _ => null,
    };

/// Everything a browser build draws, under a bar that sends a phone's browser
/// to the Harness app in its store. It stands over every screen — sign-in, a
/// shared harness, the workspace — until it is closed, and that is remembered.
/// A computer's browser gets [child] alone.
class WebStoreFrame extends StatefulWidget {
  const WebStoreFrame({
    super.key,
    required this.child,
    this.storage,
    this.open = openInNewTab,
    this.quiet = false,
  });

  final Widget child;

  /// True draws Get the app outlined rather than filled, so it does not
  /// compete with the page's own primary action (sign-in).
  final bool quiet;

  /// Where a closed bar is remembered; the browser's own store when absent.
  final LocalKeyValueStore? storage;

  /// How the store page is opened; tests pass a recorder.
  final Future<bool> Function(Uri uri) open;

  @override
  State<WebStoreFrame> createState() => _WebStoreFrameState();
}

class _WebStoreFrameState extends State<WebStoreFrame> {
  static const _dismissedKey = 'web_store_banner_dismissed';

  late final Uri? _link = webStoreLink();
  late final LocalKeyValueStore _storage =
      widget.storage ?? HarnessFileStore.shared;

  /// Unknown until the saved answer is read: the bar never flashes up only to
  /// leave for someone who closed it before.
  bool? _dismissed;

  @override
  void initState() {
    super.initState();
    if (_link != null) unawaited(_restore());
  }

  Future<void> _restore() async {
    // A browser that refuses storage still gets the bar, every visit.
    final saved = await _storage.read(_dismissedKey).catchError((_) => null);
    if (mounted) setState(() => _dismissed = saved != null);
  }

  void _dismiss() {
    setState(() => _dismissed = true);
    unawaited(_storage.write(_dismissedKey, '1').catchError((_) {}));
  }

  @override
  Widget build(BuildContext context) {
    final link = _link;
    final shown = link != null && _dismissed == false;
    final media = MediaQuery.of(context);
    // One shape whether or not the bar shows: the app below must never be
    // remounted by a bar coming or going.
    return Column(
      children: [
        if (shown)
          WebHiddenUnderKeyboard(
            child: _WebStoreBar(
              quiet: widget.quiet,
              onOpen: () => unawaited(widget.open(link)),
              onDismiss: _dismiss,
            ),
          ),
        Expanded(
          key: const ValueKey('web-store-frame-app'),
          child: MediaQuery(
            // The bar took the top inset; the app below starts under it.
            data: shown ? media.removePadding(removeTop: true) : media,
            child: widget.child,
          ),
        ),
      ],
    );
  }
}

/// Close, what the app is for, and the way to it.
class _WebStoreBar extends StatelessWidget {
  const _WebStoreBar({
    required this.quiet,
    required this.onOpen,
    required this.onDismiss,
  });

  final bool quiet;
  final VoidCallback onOpen;
  final VoidCallback onDismiss;

  static const _iconSize = 40.0;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return Material(
      key: const ValueKey('web-store-banner'),
      color: grid.AppPalette.swarmTabBar,
      child: SafeArea(
        bottom: false,
        child: DecoratedBox(
          decoration: BoxDecoration(
            border: Border(bottom: BorderSide(color: grid.AppPalette.divider)),
          ),
          child: Padding(
            padding: const EdgeInsets.fromLTRB(4, 8, 12, 8),
            child: Row(
              children: [
                // No tooltip: this bar sits above the app's overlay.
                Semantics(
                  button: true,
                  label: 'Close',
                  child: IconButton(
                    key: const ValueKey('web-store-banner-close'),
                    onPressed: onDismiss,
                    icon: const Icon(AppIcons.close, size: 16),
                    color: grid.AppPalette.textSecondary,
                    visualDensity: VisualDensity.compact,
                  ),
                ),
                // The app's own icon, as its store page shows it: what makes
                // this bar read as an app to get, not a line of chrome. The
                // asset carries its rounded corners, so it is drawn bare.
                Image.asset(
                  'assets/app_icon.png',
                  key: const ValueKey('web-store-banner-icon'),
                  width: _iconSize,
                  height: _iconSize,
                  filterQuality: FilterQuality.medium,
                ),
                const SizedBox(width: 10),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        'Harness',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: AppType.label(
                          color: grid.AppPalette.textPrimary,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      // Wraps on a narrow phone rather than losing its end.
                      Text(
                        'Run your fleet from your phone',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: AppType.caption(
                          color: grid.AppPalette.textSecondary,
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(width: 8),
                // Narrower than a form's button: the words beside it need
                // the width on a phone. Padded: a thumb's target.
                if (quiet)
                  OutlinedButton(
                    key: const ValueKey('web-store-banner-open'),
                    onPressed: onOpen,
                    style: OutlinedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      tapTargetSize: MaterialTapTargetSize.padded,
                    ),
                    child: const Text('Get the app'),
                  )
                else
                  FilledButton(
                    key: const ValueKey('web-store-banner-open'),
                    onPressed: onOpen,
                    style: FilledButton.styleFrom(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      tapTargetSize: MaterialTapTargetSize.padded,
                    ),
                    child: const Text('Get the app'),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

/// [WebStoreFrame] as the app draws it: quiet until someone is signed in, so
/// on a first visit the sign-in buttons are the page's only filled actions.
/// The bar keeps its place at the top, where a phone's app banner is looked
/// for and closed; below the sign-in it would sit under Safari's toolbar and
/// push the page's own words off a small phone.
class WebAppStoreFrame extends ConsumerWidget {
  const WebAppStoreFrame({super.key, required this.child, this.storage});

  final Widget child;

  /// Where a closed bar is remembered; the browser's own store when absent.
  final LocalKeyValueStore? storage;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final app = ref.watch(appStateProvider);
    return ListenableBuilder(
      listenable: app,
      builder: (context, child) => WebStoreFrame(
        storage: storage,
        quiet: app.status != AppStatus.authenticated,
        child: child!,
      ),
      child: child,
    );
  }
}