Files
pipelines/app/lib/features/chat/conversation/chat_bar_tint.dart
T
alan 60b4c495f1 Colour the send button with the chat colour
A coloured chat's send button uses the chat's seeded primary and its
on-colour, like the focus ring; stop stays the host's error red and
uncoloured chats keep the host button.
2026-11-10 02:33:14 +00:00

163 lines
5.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/glass_pill.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../ui/sessions/chat_color_palette.dart';
/// How strongly the chat's shade colours the bar's glass pieces over the
/// host's raised surface, on a light and on a dark page. The dark value stays
/// at the low end so a warm hue (amber) keeps its own colour over the dark
/// grey instead of turning olive.
const double chatBarTintLight = 0.16;
const double chatBarTintDark = 0.16;
/// The share of the tint an inactive split pane's bar keeps.
const double chatBarInactiveFade = 0.5;
/// A Material tonal scheme seeded from [shade] in the page's brightness: its
/// `onPrimaryContainer` labels the tinted bar and its `primary` (tone 40 on
/// light, 80 on dark) draws the composer's focus ring.
ColorScheme chatSeededScheme(Color shade, {required bool dark}) =>
ColorScheme.fromSeed(
seedColor: shade,
brightness: dark ? Brightness.dark : Brightness.light,
);
/// A coloured chat's bar: the glass pieces' [fill] (translucent as every
/// glass piece is, unless solid) and the [foreground] its labels, icons and
/// controls draw in.
@immutable
class ChatBarTint {
const ChatBarTint({required this.fill, required this.foreground});
/// The bar's colours for [shade] over the host's [raised] surface. An
/// [inactive] split pane keeps [chatBarInactiveFade] of the tint; [solid]
/// (the system's high-contrast setting) leaves the fill opaque, as the
/// glass does then.
factory ChatBarTint.from({
required Color shade,
required Color raised,
required bool dark,
bool inactive = false,
bool solid = false,
}) {
final strength =
(dark ? chatBarTintDark : chatBarTintLight) *
(inactive ? chatBarInactiveFade : 1);
final opaque = Color.alphaBlend(shade.withValues(alpha: strength), raised);
return ChatBarTint(
fill: solid ? opaque : opaque.withValues(alpha: GlassPill.fillOpacity),
foreground: chatSeededScheme(shade, dark: dark).onPrimaryContainer,
);
}
final Color fill;
final Color foreground;
@override
bool operator ==(Object other) =>
other is ChatBarTint &&
other.fill == fill &&
other.foreground == foreground;
@override
int get hashCode => Object.hash(fill, foreground);
}
/// The bar tint for the chat colour [name] on this host, or null for a chat
/// with no colour (its bar stays neutral). Reads [ChatPaneActivity].
ChatBarTint? chatBarTintOf(BuildContext context, String? name) {
final shade = chatColorShade(context, name);
if (shade == null) return null;
final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
return ChatBarTint.from(
shade: shade,
raised: cupertino
? CupertinoColors.systemGrey5.resolveFrom(context)
: Theme.of(context).colorScheme.surfaceContainerHigh,
dark: chatColorDarkSurface(context),
inactive: ChatPaneActivity.inactiveOf(context),
solid: MediaQuery.maybeHighContrastOf(context) ?? false,
);
}
/// The composer's focus ring: the seeded scheme's `primary` for a coloured
/// chat, else the host's primary colour; always drawn at full opacity.
/// The seeded scheme of a coloured chat in this context, or null for a chat
/// with no colour (which keeps the host's own controls).
ColorScheme? chatSchemeOf(BuildContext context, String? name) {
final shade = chatColorShade(context, name);
if (shade == null) return null;
return chatSeededScheme(shade, dark: chatColorDarkSurface(context));
}
Color chatFocusRingColor(BuildContext context, String? name) {
final shade = chatColorShade(context, name);
if (shade != null) {
return chatSeededScheme(shade, dark: chatColorDarkSurface(context)).primary;
}
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? CupertinoTheme.of(context).primaryColor
: Theme.of(context).colorScheme.primary;
}
/// Marks a split pane that is not the active one, so its bar fades its tint.
class ChatPaneActivity extends InheritedWidget {
const ChatPaneActivity({
super.key,
required this.inactive,
required super.child,
});
final bool inactive;
static bool inactiveOf(BuildContext context) =>
context
.dependOnInheritedWidgetOfExactType<ChatPaneActivity>()
?.inactive ??
false;
@override
bool updateShouldNotify(ChatPaneActivity oldWidget) =>
inactive != oldWidget.inactive;
}
/// Gives the bar's controls the tint's foreground: Material icon buttons
/// through the ambient icon colour, Cupertino buttons (Back, Files, More)
/// through the theme's primary colour, their native tint, and the chat's own
/// bar controls through [foregroundOf].
class ChatBarTintScope extends InheritedWidget {
ChatBarTintScope({
super.key,
required this.tint,
required bool cupertino,
required Widget child,
}) : super(
child: Builder(
builder: (context) => cupertino
? CupertinoTheme(
data: CupertinoTheme.of(
context,
).copyWith(primaryColor: tint.foreground),
child: child,
)
: IconTheme.merge(
data: IconThemeData(color: tint.foreground),
child: child,
),
),
);
final ChatBarTint tint;
/// The tinted bar's foreground around [context], or null in a neutral bar.
static Color? foregroundOf(BuildContext context) => context
.dependOnInheritedWidgetOfExactType<ChatBarTintScope>()
?.tint
.foreground;
@override
bool updateShouldNotify(ChatBarTintScope oldWidget) => tint != oldWidget.tint;
}