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.

7.4 KB
Raw Blame
/// Shared desktop dialogs keep the surrounding workspace recognizable without
/// filtering live terminal pixels on every frame.
library;

import 'dart:ui' show ImageFilter;

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

import '../theme/app_theme.dart';

/// Dialogs do not blur the live workspace by default.
const double kDialogVeilBlur = 0;

/// Every modal uses the same 95% veil, including Cmd-N and Cmd-P.
const Color kDialogVeilTint = AppDesktop.darkVeil;

Color dialogVeilTintOf(BuildContext context) =>
    AppDesktop.veil(Theme.of(context).brightness);

/// The app's dialog barrier: a flat tint and the active surface.
///
/// Use it in place of `showDialog` wherever a panel should take the window's
/// full attention. It keeps `showDialog`'s shape — the same `builder`,
/// `barrierDismissible` and return type — so a call site changes by its name
/// alone.
///
/// ⚠️ [barrierDismissible] is wired by hand, because the real barrier is
/// transparent: Material dismisses on a tap only when it draws the barrier
/// itself, and this one is a widget. The [GestureDetector] below is what keeps
/// a tap outside working, and `maybePop` rather than `pop` so a route that
/// refuses to leave (an unsaved form, say) still gets to refuse.
Future<T?> showAppDialog<T>({
  required BuildContext context,
  required WidgetBuilder builder,
  bool barrierDismissible = true,
  String barrierLabel = 'Dismiss',
  Color? veilTint,
  double veilBlur = kDialogVeilBlur,
  Duration transitionDuration = Duration.zero,
  ValueChanged<bool>? onCurrentChanged,
}) => showGeneralDialog<T>(
  context: context,
  // The route's own barrier draws nothing: the veil below is the barrier.
  barrierColor: Colors.transparent,
  // Kept FALSE whatever the caller asked, and handled in the tree instead —
  // see the note above. Left true, Material would dismiss on a tap anywhere
  // over a barrier it believes is there, including a tap on the dialog.
  barrierDismissible: false,
  barrierLabel: barrierLabel,
  transitionDuration: MediaQuery.disableAnimationsOf(context)
      ? Duration.zero
      : transitionDuration,
  pageBuilder: (context, _, _) => _DialogVisibility(
    onCurrentChanged: onCurrentChanged,
    child: _AppDialogVeil(
      tint: veilTint,
      blur: veilBlur,
      dismissible: barrierDismissible,
      child: Builder(builder: builder),
    ),
  ),
  transitionBuilder: (context, anim, _, child) =>
      transitionDuration == Duration.zero ||
          MediaQuery.disableAnimationsOf(context)
      ? child
      : FadeTransition(opacity: anim, child: child),
);

/// Lets an explicit preview route preserve passive native chrome. A nested
/// route hides that preview again; this never grants input to the workspace.
class _DialogVisibility extends StatefulWidget {
  const _DialogVisibility({required this.child, this.onCurrentChanged});
  final Widget child;
  final ValueChanged<bool>? onCurrentChanged;

  @override
  State<_DialogVisibility> createState() => _DialogVisibilityState();
}

class _DialogVisibilityState extends State<_DialogVisibility> {
  bool? _current;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final current = ModalRoute.isCurrentOf(context) ?? false;
    if (current == _current) return;
    _current = current;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted && current == _current) {
        widget.onCurrentChanged?.call(current);
      }
    });
  }

  @override
  void dispose() {
    widget.onCurrentChanged?.call(false);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => widget.child;
}

/// The veil, and the dialog standing on it.
class _AppDialogVeil extends StatelessWidget {
  const _AppDialogVeil({
    required this.tint,
    required this.blur,
    required this.dismissible,
    required this.child,
  });

  final Color? tint;
  final double blur;
  final bool dismissible;
  final Widget child;

  @override
  Widget build(BuildContext context) => _DismissOnEscape(
    enabled: dismissible,
    child: Stack(
      children: [
        Positioned.fill(
          child: GestureDetector(
            // `opaque`, so a tap on the veil is taken here rather than falling
            // through to whatever the window has underneath — a terminal would
            // otherwise get the click that was meant to close the panel.
            behavior: HitTestBehavior.opaque,
            onTap: dismissible ? () => Navigator.of(context).maybePop() : null,
            child: blur == 0
                ? ColoredBox(color: tint ?? dialogVeilTintOf(context))
                : BackdropFilter(
                    filter: ImageFilter.blur(sigmaX: blur, sigmaY: blur),
                    child: ColoredBox(color: tint ?? dialogVeilTintOf(context)),
                  ),
          ),
        ),
        // ⚠️ The dialog is NOT inside the GestureDetector above: nested in it,
        // a tap on the panel itself would close the panel.
        child,
      ],
    ),
  );
}

/// Closes the dialog on Escape.
///
/// ⚠️ **This exists because `showAppDialog` passes `barrierDismissible: false`
/// to the route, and that flag is not only about taps.** Flutter wires the
/// Escape key off the same flag: a modal route built with it false installs no
/// `DismissIntent` handler, so Escape reaches nothing. The barrier here is a
/// widget rather than the route's own, so the flag has to stay false — leaving
/// it true makes Material dismiss on a tap anywhere over a barrier it believes
/// it is drawing, the panel included — and the key has to be put back by hand,
/// exactly as the tap was.
///
/// It was missed when the tap was wired, and every dialog in the app lost
/// Escape with it — including the ones whose own chrome draws an `esc` cap.
///
/// `maybePop`, matching the tap: a route that refuses to leave still gets to
/// refuse.
class _DismissOnEscape extends StatelessWidget {
  const _DismissOnEscape({required this.enabled, required this.child});

  /// False leaves the key alone, so a dialog that opted out of tap-dismissal
  /// is not quietly closable by keyboard instead.
  final bool enabled;

  final Widget child;

  @override
  Widget build(BuildContext context) {
    if (!enabled) return child;
    return Shortcuts(
      shortcuts: const {
        SingleActivator(LogicalKeyboardKey.escape): DismissIntent(),
      },
      child: Actions(
        actions: {
          DismissIntent: CallbackAction<DismissIntent>(
            onInvoke: (_) {
              Navigator.of(context).maybePop();
              return null;
            },
          ),
        },
        // A `Shortcuts` only sees a key once focus is somewhere inside it, and
        // not every dialog focuses something of its own — so the scope takes
        // focus itself when nothing else claims it.
        //
        // ⚠️ `Focus`, not `FocusScope`. Both autofocus, and a descendant that
        // also autofocuses (the model picker's search field) wins either way —
        // but a `FocusScope` additionally becomes the dialog's focus ROOT,
        // which changes where traversal wraps and what `unfocus` falls back
        // to. Nothing here wants to move those; this only needs to be a node
        // in the chain that holds focus when no descendant asks for it.
        // `skipTraversal` keeps it out of the tab order it is not a stop in.
        child: Focus(autofocus: true, skipTraversal: true, child: child),
      ),
    );
  }
}