import 'dart:async'; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:xterm/xterm.dart'; import '../../../ui/text/monospace.dart'; import '../../../ui/text/terminal_symbols_font.dart'; import '../../terminal/terminal_layout.dart'; import '../conversation/chat_card_styles.dart'; import 'chat_shell_terminal_palette.dart'; /// How strongly the selection and find matches tint the cells they cover /// (painted over the text, so the text shows through). const double chatShellTerminalSelectionAlpha = 0.35; const double chatShellTerminalMatchAlpha = 0.2; const double chatShellTerminalCurrentMatchAlpha = 0.4; /// The terminal's background: the theme's lowest surface, the most /// contrast its colours can have in either brightness. Color chatShellTerminalBackground(BuildContext context) => Theme.of(context).colorScheme.surfaceContainerLowest; /// The terminal's colours from the host theme: its lowest surface as /// background, the body text colour as foreground, the accent as cursor and /// selection, and [palette]'s sixteen ANSI colours adjusted to that /// background (see [chatShellTerminalPalette]). TerminalTheme chatShellTerminalTheme( BuildContext context, { TerminalPalette palette = TerminalPalette.theme, }) { final scheme = Theme.of(context).colorScheme; final background = chatShellTerminalBackground(context); final ansi = chatShellTerminalPalette(palette, background); final matches = chatShellTerminalMatchColours(context); return TerminalTheme( cursor: scheme.primary, selection: scheme.primary.withValues( alpha: chatShellTerminalSelectionAlpha, ), foreground: scheme.onSurface, background: background, black: ansi[0], red: ansi[1], green: ansi[2], yellow: ansi[3], blue: ansi[4], magenta: ansi[5], cyan: ansi[6], white: ansi[7], brightBlack: ansi[8], brightRed: ansi[9], brightGreen: ansi[10], brightYellow: ansi[11], brightBlue: ansi[12], brightMagenta: ansi[13], brightCyan: ansi[14], brightWhite: ansi[15], searchHitBackground: matches.hit, searchHitBackgroundCurrent: matches.current, searchHitForeground: scheme.onSurface, ); } /// The tints of find matches and of the current match: the theme's tertiary /// colour, light enough over the text that it keeps 4.5:1 contrast. ({Color hit, Color current}) chatShellTerminalMatchColours( BuildContext context, ) { final tertiary = Theme.of(context).colorScheme.tertiary; return ( hit: tertiary.withValues(alpha: chatShellTerminalMatchAlpha), current: tertiary.withValues(alpha: chatShellTerminalCurrentMatchAlpha), ); } /// The terminal's text: the host's monospace family at [fontSize] (the /// terminal text size setting), else a little under the card body size, with /// the bundled symbols font first among the fallbacks for Powerline and Nerd /// Font icons (registered on first use). /// /// Line height 1.1: the emulator rounds a row up to whole pixels (about /// 1.13 at the default size), close to other terminals' 1.0-1.2. Box /// drawing, block and Powerline characters fill the whole row whatever the /// height, so frames and bars join across rows. TerminalStyle chatShellTerminalStyle(BuildContext context, {double? fontSize}) { unawaited(ensureTerminalSymbolsFont()); return TerminalStyle( fontFamily: appMonospaceFamily, fontFamilyFallback: const [ terminalSymbolsFamily, ...kTerminalDefaultFontFamilyFallback, ], fontSize: fontSize ?? chatShellTerminalDefaultFontSize(context), height: 1.1, ); } /// The terminal's text size when the setting is the default. double chatShellTerminalDefaultFontSize(BuildContext context) { final body = chatCardBodyStyle(context) ?? const TextStyle(); return (body.fontSize ?? 14) * 0.95; } /// The size of one terminal cell in [style] at the context's text scale, /// measured exactly as the emulator measures it. Size chatShellTerminalCellSize(BuildContext context, TerminalStyle style) { final text = style.toTextStyle(); final builder = ui.ParagraphBuilder(text.getParagraphStyle()) ..pushStyle(text.getTextStyle(textScaler: MediaQuery.textScalerOf(context))) ..addText('mmmmmmmmmm'); final paragraph = builder.build() ..layout(const ui.ParagraphConstraints(width: double.infinity)); // Rows are whole pixels, as the emulator's painter rounds them. final size = Size( paragraph.maxIntrinsicWidth / 10, paragraph.height.ceilToDouble(), ); paragraph.dispose(); return size; }