// A project's sidebar icon: the operator's chosen icon in its colour, a custom // icon image from the project, or a folder. OpenChamber's managed chats have // no project and get a chat bubble instead. import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import '../api/models.dart'; import '../state.dart'; /// The Flutter equivalent of OpenChamber's `PROJECT_ICON_MAP` keys. IconData? projectIconData(String key) => switch (key) { 'code' => Icons.code, 'terminal' => Icons.terminal, 'rocket' => Icons.rocket_launch_outlined, 'flask' => Icons.science_outlined, 'gamepad' => Icons.sports_esports_outlined, 'briefcase' => Icons.work_outline, 'home' => Icons.home_outlined, 'globe' => Icons.public, 'leaf' => Icons.eco_outlined, 'shield' => Icons.shield_outlined, 'palette' => Icons.palette_outlined, 'server' => Icons.dns_outlined, 'phone' => Icons.smartphone, 'database' => Icons.storage, 'lightbulb' => Icons.lightbulb_outline, 'music' => Icons.music_note_outlined, 'camera' => Icons.photo_camera_outlined, 'book' => Icons.menu_book_outlined, 'heart' => Icons.favorite_outline, 'cpu' => Icons.memory, 'cloud' => Icons.cloud_outlined, 'store' => Icons.storefront_outlined, 'school' => Icons.school_outlined, 'chat' => Icons.chat_bubble_outline, 'mail' => Icons.mail_outline, 'map' => Icons.place_outlined, 'robot' => Icons.smart_toy_outlined, 'ai' => Icons.psychology_outlined, 'plugin' => Icons.extension_outlined, 'tools' => Icons.build_outlined, 'chart' => Icons.bar_chart, 'writing' => Icons.edit_outlined, 'image' => Icons.image_outlined, 'film' => Icons.movie_outlined, 'trophy' => Icons.emoji_events_outlined, 'travel' => Icons.flight, 'coffee' => Icons.coffee_outlined, 'bug' => Icons.bug_report_outlined, 'apple' => Icons.apple, 'android' => Icons.android, 'windows' => Icons.window, 'github' => Icons.code, 'stack' => Icons.layers_outlined, 'finance' => Icons.account_balance_outlined, 'team' => Icons.groups_outlined, 'fire' => Icons.local_fire_department_outlined, 'planet' => Icons.public, _ => null, }; /// A stored project colour as a Flutter colour. Newer projects store hex; /// older ones a theme/syntax key, resolved against the app's scheme. Color? projectColor(String value, ColorScheme scheme) { final hex = projectHexColor(value); if (hex != null) return hex; return switch (value.trim()) { 'primary' => scheme.primary, 'error' => scheme.error, 'success' => Colors.green, 'keyword' => scheme.primary, 'string' => Colors.green, 'number' => Colors.orange, 'type' => Colors.teal, 'constant' => Colors.blue, 'comment' => scheme.onSurfaceVariant, _ => null, }; } Color? projectHexColor(String value) { final text = value.trim(); if (!text.startsWith('#')) return null; var hex = text.substring(1); if (hex.length == 3) { hex = hex.split('').map((c) => '$c$c').join(); } if (hex.length != 6 && hex.length != 8) return null; final parsed = int.tryParse(hex, radix: 16); if (parsed == null) return null; return Color(hex.length == 6 ? 0xff000000 | parsed : parsed); } String _hexOf(Color color) => '#${(color.toARGB32() & 0xffffff).toRadixString(16).padLeft(6, '0')}'; class ProjectIcon extends StatelessWidget { final AppState app; final ProjectInfo? project; final bool chat; final double size; const ProjectIcon({ super.key, required this.app, this.project, this.chat = false, this.size = 16, }); @override Widget build(BuildContext context) { final theme = Theme.of(context); final scheme = theme.colorScheme; if (chat) { return Icon(Icons.chat_bubble_outline, size: size, color: scheme.primary); } final project = this.project; if (project == null) { return Icon(Icons.folder_outlined, size: size, color: theme.hintColor); } final color = projectColor(project.color, scheme) ?? theme.hintColor; final glyph = Icon(projectIconData(project.icon) ?? Icons.folder_outlined, size: size, color: color); if (!project.hasIconImage) return glyph; final future = app.projectIcon( project, dark: theme.brightness == Brightness.dark, iconColor: _hexOf(scheme.onSurface), ); return FutureBuilder( future: future, builder: (context, snapshot) { final bytes = snapshot.data; if (bytes == null || bytes.isEmpty) { // Loading or unavailable: keep the glyph so the row does not jump. return glyph; } final Widget image = project.iconImageMime.contains('svg') ? SvgPicture.memory(bytes, width: size, height: size, fit: BoxFit.contain) : Image.memory(bytes, width: size, height: size, fit: BoxFit.contain, gaplessPlayback: true); final background = projectHexColor(project.iconBackground); if (background == null) return image; return Container( width: size, height: size, decoration: BoxDecoration( color: background, borderRadius: BorderRadius.circular(3), ), clipBehavior: Clip.antiAlias, child: image, ); }, ); } }