Files
openchamber-android-native/lib/widgets/project_icon.dart
T
blumia 79221e57d7 feat: group the server's chats and use OpenChamber's project icons
Sessions under the server's managed chats root (chats/<date>/session-…) were
grouped per directory, so each one appeared as its own session-xxxxx band.
GET /api/fs/home now supplies the chats root plus its canonical and legacy
aliases; every chat folder becomes the single 聊天 group. The new-session
picker can target 聊天 too: the first send creates a fresh chat directory
before creating the session, and a new session started from an existing chat
gets a new one instead of sharing its folder.

Projects merge their OpenChamber settings row (label, icon key, colour,
iconBackground and uploaded/auto icon image) with /api/project. Group
headers, the picker and the draft chip draw the chosen icon in its colour,
and custom images are fetched through the client transport (SVG included)
with a themed fallback to the glyph. The chat group uses a chat bubble rather
than a folder.

0.4.8 (408).
2026-11-23 17:42:49 +08:00

166 lines
5.5 KiB
Dart

// 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<Uint8List?>(
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,
);
},
);
}
}