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.

11.9 KB
Raw Blame
import 'dart:async';

import 'package:harness/shared/theme/app_icons.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../../shared/theme/app_theme.dart' as grid;
import '../../shared/theme/appearance_prefs_store.dart';
import '../../shared/theme/color_palette.dart';
import '../../shared/widgets/section_heading.dart';

/// Appearance ▸ Workspace's first group (task #3006): Light, Dark or Match
/// macOS, then the light theme and the dark theme that choice shows, each
/// chosen in its own list.
class PaletteSection extends StatelessWidget {
  const PaletteSection({super.key, this.store});
  final AppearancePrefsStore? store;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    final preferences = store ?? appearancePrefsStore;
    return ValueListenableBuilder<AppearancePrefs>(
      valueListenable: preferences,
      builder: (context, prefs, _) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SectionHeading('Theme'),
          const SizedBox(height: 12),
          SegmentedButton<AppearanceMode>(
            key: const ValueKey('appearance-mode'),
            showSelectedIcon: false,
            expandedInsets: EdgeInsets.zero,
            style: SegmentedButton.styleFrom(
              selectedBackgroundColor: grid.AppDesktop.selection,
              selectedForegroundColor: grid.AppDesktop.onSelection,
            ),
            segments: [
              for (final mode in AppearanceMode.values)
                ButtonSegment(
                  value: mode,
                  label: Text(
                    mode.labelFor(defaultTargetPlatform),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
            ],
            selected: {prefs.mode},
            onSelectionChanged: (selection) =>
                unawaited(preferences.setMode(selection.first)),
          ),
          const SizedBox(height: 20),
          _PaletteGroup(
            key: const ValueKey('palettes-light'),
            title: 'Light theme',
            palettes: [
              for (final palette in HarnessPalette.values)
                if (!palette.isDark) palette,
            ],
            selected: prefs.lightPalette,
            onChoose: (palette) => unawaited(preferences.setPalette(palette)),
          ),
          const SizedBox(height: 20),
          _PaletteGroup(
            key: const ValueKey('palettes-dark'),
            title: 'Dark theme',
            palettes: [
              for (final palette in HarnessPalette.values)
                if (palette.isDark) palette,
            ],
            selected: prefs.darkPalette,
            onChoose: (palette) => unawaited(preferences.setPalette(palette)),
          ),
          const SizedBox(height: 24),
        ],
      ),
    );
  }
}

class _PaletteGroup extends StatelessWidget {
  const _PaletteGroup({
    super.key,
    required this.title,
    required this.palettes,
    required this.selected,
    required this.onChoose,
  });
  final String title;
  final List<HarnessPalette> palettes;
  final HarnessPalette selected;
  final ValueChanged<HarnessPalette> onChoose;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: grid.AppType.label(color: grid.AppPalette.textSecondary),
        ),
        const SizedBox(height: 8),
        LayoutBuilder(
          builder: (context, constraints) {
            final columns = constraints.maxWidth >= 660
                ? 3
                : constraints.maxWidth >= 360
                ? 2
                : 1;
            final width = (constraints.maxWidth - 12 * (columns - 1)) / columns;
            return Wrap(
              spacing: 12,
              runSpacing: 12,
              children: [
                for (final palette in palettes)
                  SizedBox(
                    width: width,
                    child: _PaletteChoice(
                      palette: palette,
                      selected: selected == palette,
                      onChoose: () => onChoose(palette),
                    ),
                  ),
              ],
            );
          },
        ),
      ],
    );
  }
}

class _PaletteChoice extends StatelessWidget {
  const _PaletteChoice({
    required this.palette,
    required this.selected,
    required this.onChoose,
  });
  final HarnessPalette palette;
  final bool selected;
  final VoidCallback onChoose;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return Semantics(
      button: true,
      selected: selected,
      onTap: onChoose,
      label: '${palette.label} palette',
      child: ExcludeSemantics(
        child: TextButton(
          key: ValueKey('palette-${palette.name}'),
          onPressed: onChoose,
          style: ButtonStyle(
            padding: const WidgetStatePropertyAll(EdgeInsets.all(8)),
            backgroundColor: WidgetStatePropertyAll(palette.panel),
            // Each card is drawn in its own palette, so its ink is that
            // palette's foreground — white on the dark ones, near-black on
            // the light ones — whatever the app is wearing.
            overlayColor: WidgetStatePropertyAll(
              palette.foreground.withValues(alpha: .10),
            ),
            side: WidgetStateProperty.resolveWith(
              (states) => BorderSide(
                color: selected
                    ? palette.accent
                    : MediaQuery.highContrastOf(context)
                    ? palette.foreground.withValues(alpha: .6)
                    : palette.foreground.withValues(alpha: .12),
                width: MediaQuery.highContrastOf(context) ? 2 : 1.5,
              ),
            ),
            shape: WidgetStatePropertyAll(
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _WorkspacePreview(palette: palette),
              const SizedBox(height: 8),
              Row(
                children: [
                  Expanded(
                    child: Text(
                      palette.label,
                      style: grid.AppType.label(color: palette.foreground),
                    ),
                  ),
                  SizedBox(
                    width: 16,
                    child: selected
                        ? Icon(AppIcons.check, size: 16, color: palette.accent)
                        : null,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class _WorkspacePreview extends StatelessWidget {
  const _WorkspacePreview({required this.palette});
  final HarnessPalette palette;

  @override
  Widget build(BuildContext context) {
    grid.AppTheme.watch(context);
    return ClipRRect(
      borderRadius: BorderRadius.circular(4),
      child: SizedBox(
        height: 76,
        child: ColoredBox(
          color: palette.workspace,
          child: Column(
            children: [
              ColoredBox(
                color: palette.tabBar,
                child: SizedBox(
                  height: 16,
                  child: Row(
                    children: [
                      const SizedBox(width: 6),
                      for (var i = 0; i < 3; i++)
                        Padding(
                          padding: const EdgeInsets.only(right: 3),
                          child: Container(
                            width: 3,
                            height: 3,
                            decoration: BoxDecoration(
                              color: palette.foreground.withValues(alpha: .38),
                              shape: BoxShape.circle,
                            ),
                          ),
                        ),
                      const SizedBox(width: 6),
                      Container(
                        width: 40,
                        height: 12,
                        margin: const EdgeInsets.only(top: 4),
                        color: palette.workspace,
                      ),
                      const Spacer(),
                      Container(
                        width: 28,
                        height: 6,
                        margin: const EdgeInsets.only(right: 6),
                        decoration: BoxDecoration(
                          color: palette.search,
                          borderRadius: BorderRadius.circular(3),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4),
                  child: Row(
                    children: [
                      // A theme's own sidebar (Plum's plum) shows in its card.
                      if (palette.sidebar != palette.workspace) ...[
                        Container(
                          width: 18,
                          decoration: BoxDecoration(
                            color: palette.sidebar,
                            borderRadius: BorderRadius.circular(3),
                          ),
                        ),
                        const SizedBox(width: 4),
                      ],
                      for (var pane = 0; pane < 2; pane++) ...[
                        if (pane > 0) const SizedBox(width: 4),
                        Expanded(
                          child: Container(
                            padding: const EdgeInsets.all(6),
                            decoration: BoxDecoration(
                              color: palette.background,
                              borderRadius: BorderRadius.circular(3),
                              border: pane == 0
                                  ? Border.all(
                                      color: palette.accent.withValues(
                                        alpha: 0.65,
                                      ),
                                    )
                                  : null,
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  width: 20,
                                  height: 3,
                                  color: palette.accent,
                                ),
                                const SizedBox(height: 6),
                                FractionallySizedBox(
                                  widthFactor: 0.85,
                                  child: Container(
                                    height: 2,
                                    color: palette.foreground.withValues(
                                      alpha: .54,
                                    ),
                                  ),
                                ),
                                const SizedBox(height: 4),
                                FractionallySizedBox(
                                  widthFactor: 0.6,
                                  child: Container(
                                    height: 2,
                                    color: palette.foreground.withValues(
                                      alpha: .30,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}