import 'package:flutter/material.dart'; import '../../../ui/adaptive/glass_pill.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; return ChatBarTint.from( shade: shade, raised: 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 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() ?.inactive ?? false; @override bool updateShouldNotify(ChatPaneActivity oldWidget) => inactive != oldWidget.inactive; } /// Gives the bar's controls the tint's foreground: the Material icon buttons /// through the ambient icon colour, and the chat's own bar controls through /// [foregroundOf]. class ChatBarTintScope extends InheritedWidget { ChatBarTintScope({super.key, required this.tint, required Widget child}) : super( 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() ?.tint .foreground; @override bool updateShouldNotify(ChatBarTintScope oldWidget) => tint != oldWidget.tint; }