修复审计发现的问题,默认构建即带 Flutter 前端
PR 审核 / 后端编译 (pull_request) Canceled after 0s
PR 审核 / 后端自检 (pull_request) Canceled after 0s
PR 审核 / 前端检查(Flutter) (pull_request) Canceled after 0s
PR 审核 / 安全扫描 (pull_request) Canceled after 0s
PR 审核 / 审核汇总 (pull_request) Canceled after 0s

P0
- GC-SAVEDOG:更新改为整目录替换(旧文件备份到 .GC/update/backup,
  失败可完整回滚),不再只搬一个 exe;回滚会删掉新包独有文件
- GC-SAVEDOG:包名校验放宽为单文件名模式,Linux 启动器(无扩展名)可更新
- GC-DOWNLOAD:测速全失败时不再回退 GitCode,改为返回「不可用」并保留上次可用源
- GC-DOWNLOAD:识别并赋予 Linux 无扩展名启动器可执行位;兼容脚本清理旧版时
  也覆盖无扩展名产物
- CI:删除 nightly-prerelease(绕开 build.sh,产出无前端空壳);alpha 工作流
  去掉非法 cron,只保留手动触发

P1
- 前端:主题设置真正生效(深色/浅色/跟随系统 + 持久化),去掉死字段
- 前端:下载页轮询改为自适应单次链,页面不可见或任务结束后降频/停止
- 前端:下载完成 / 删除模型后首页列表自动刷新
- 前端:复用 http.Client,不再每次轮询新建连接
- 后端:/api/models 走缓存,不再每次全盘递归统计体积
- 设置页:外部改动能同步进表单,不再只同步一次

P2
- 错误响应统一为 ErrorDto,不再复用 LaunchResultDto
- 限流桶定期清理,避免无限增长
- 播放器窗口去重,避免同一模型重复开窗
- 封面按实际格式返回,去掉错误的 image/png 与 no-store
- 端口集中到 GcUiContract,前端可用 GC_UI_PORT 或 ui-backend.json 发现后端
- 构建:Windows 包改为散开发布,与后端共用运行时(体积约 109MB -> 72MB)
- 文档:更新仓库地址与更新流程说明

测试:dotnet build 全绿;GC.Tests 更新链路全通过;flutter analyze 无问题;
flutter test 11 项全通过。
This commit is contained in:
Morris committed 2026-11-18 20:10:03 +08:00
1 parent 461e79aa7f
commit a361c889f8
28 files changed
+1062 -417

No files matched your search

+59 -9
View File
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
@@ -334,16 +335,65 @@ List<String> _stringList(dynamic value) =>
/// GC-UI 后端客户端。
///
/// 后端是本地伴随进程,默认监听 127.0.0.1:17890;端口可用
/// `--dart-define=GC_UI_BASE_URL=http://127.0.0.1:12345` 覆盖。
/// 后端是本地伴随进程,端口由启动器(GC-BOOT)选定后通过 `GC_UI_PORT`
/// 环境变量传进来 —— 这样端口被占用时换端口不用重新编译前端。
/// 也可用 `--dart-define=GC_UI_BASE_URL=http://127.0.0.1:12345` 强制指定。
class GcApi {
GcApi({String? baseUrl})
: baseUrl = baseUrl ?? defaultBaseUrl;
static const String defaultBaseUrl = String.fromEnvironment(
'GC_UI_BASE_URL',
defaultValue: 'http://127.0.0.1:17890',
);
/// 复用同一个 HTTP 连接:轮询(下载任务每秒一次)时每次都新建连接
/// 会不断握手,桌面端也能明显看到延迟抖动。
static final http.Client _client = http.Client();
/// 兜底默认端口,与后端 GC.Services.GcUiContract.DefaultPort 保持一致。
static const int fallbackPort = 17890;
static String get defaultBaseUrl {
const fromDefine = String.fromEnvironment('GC_UI_BASE_URL');
if (fromDefine.isNotEmpty) return fromDefine;
final fromEnvUrl = Platform.environment['GC_UI_BASE_URL'];
if (fromEnvUrl != null && fromEnvUrl.isNotEmpty) return fromEnvUrl;
// 启动器通过 GC_UI_PORT 传端口(主路径)。
final port = int.tryParse(Platform.environment['GC_UI_PORT'] ?? '');
if (port != null) return 'http://127.0.0.1:$port';
// 手动起后端 / 前端调试时没有环境变量:读后端写的 .GC/ui-backend.json。
final discovered = _endpointFileUrl();
if (discovered != null) return discovered;
return 'http://127.0.0.1:$fallbackPort';
}
/// 从运行目录下的 `.GC/ui-backend.json` 发现后端地址。
///
/// 后端每次启动都会写这个文件(port / pid / url)。只认 24 小时内的记录:
/// 更早的多半是上次遗留的,连过去只会得到一个「无法连接」。
static String? _endpointFileUrl() {
try {
final file = File('${Directory.current.path}/.GC/ui-backend.json');
if (!file.existsSync()) return null;
final age = DateTime.now().difference(file.lastModifiedSync());
if (age.inHours >= 24) return null;
final decoded = jsonDecode(file.readAsStringSync());
if (decoded is! Map<String, dynamic>) return null;
final url = decoded['url'];
if (url is String && url.startsWith('http://127.0.0.1:')) return url;
final port = (decoded['port'] as num?)?.toInt();
if (port != null && port > 0 && port < 65536) {
return 'http://127.0.0.1:$port';
}
} catch (_) {
// 文件损坏 / 无权限:退回默认端口即可
}
return null;
}
final String baseUrl;
@@ -357,17 +407,17 @@ class GcApi {
try {
switch (method) {
case 'GET':
response = await http.get(uri).timeout(const Duration(seconds: 30));
response = await _client.get(uri).timeout(const Duration(seconds: 30));
break;
case 'POST':
response = await http
response = await _client
.post(uri,
headers: const {'Content-Type': 'application/json'},
body: body == null ? null : jsonEncode(body))
.timeout(const Duration(seconds: 120));
break;
case 'DELETE':
response = await http.delete(uri).timeout(const Duration(seconds: 30));
response = await _client.delete(uri).timeout(const Duration(seconds: 30));
break;
default:
throw ApiException('不支持的方法 $method');
+74 -10
View File
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'api_client.dart';
@@ -24,26 +26,75 @@ class ShellController extends ChangeNotifier {
String updateDetail = '';
String launcherNotice = '';
/// 模型库版本号:安装 / 删除 / 下载完成后自增,首页据此重新拉列表。
///
/// 首页被 `Offstage` 保活,切走再切回不会重建;没有这个信号时,
/// 下载完模型回到首页仍然显示旧的空列表。
int modelsRevision = 0;
Timer? _retryTimer;
int _retryAttempt = 0;
bool get backendReady => settings != null;
Future<void> bootstrap() async {
try {
// 健康检查与设置并行请求:两者互不依赖,串行会白白多等一次网络往返。
final healthFuture = api.health();
final settingsFuture = api.settings();
health = await healthFuture;
settings = await settingsFuture;
loadError = null;
_applySettings(settings!);
} catch (error) {
loadError = error.toString();
_retryTimer?.cancel();
HealthInfo? healthResult;
SettingsInfo? settingsResult;
Object? firstError;
// 两个请求互不依赖,并行发;各自捕获异常,避免一方失败时
// 另一个的 Future 抛成未处理异常(Flutter 会在控制台打出恶性警告)。
await Future.wait<void>(<Future<void>>[
() async {
try {
healthResult = await api.health();
} catch (error) {
firstError ??= error;
}
}(),
() async {
try {
settingsResult = await api.settings();
} catch (error) {
firstError ??= error;
}
}(),
]);
if (healthResult != null) health = healthResult;
if (settingsResult != null) {
settings = settingsResult;
_applySettings(settingsResult!);
}
if (firstError != null && settingsResult == null) {
loadError = firstError.toString();
_scheduleRetry();
} else {
loadError = null;
_retryAttempt = 0;
}
notifyListeners();
}
/// 后端没起来时自动重试(2s、4s、8s…最多 5 次),不必让用户手动点。
void _scheduleRetry() {
if (_retryAttempt >= 5) return;
final seconds = 1 << _retryAttempt; // 1,2,4,8,16
_retryAttempt++;
_retryTimer?.cancel();
_retryTimer = Timer(Duration(seconds: seconds), () {
if (settings == null) bootstrap();
});
}
void _applySettings(SettingsInfo value) {
I18n.setLanguage(value.language);
GcPalette.apply(value.accentColor);
GcPalette.applyTheme(value.theme);
}
void goTo(int value) {
@@ -52,6 +103,12 @@ class ShellController extends ChangeNotifier {
notifyListeners();
}
/// 模型库有变化(安装 / 删除 / 下载完成):通知首页刷新。
void modelsChanged() {
modelsRevision++;
notifyListeners();
}
Future<void> reloadSettings() async {
try {
settings = await api.settings();
@@ -112,6 +169,7 @@ class ShellController extends ChangeNotifier {
detail: result.error ?? '',
notice: result.launcherMessage ?? '',
);
if (result.updatedModels > 0) modelsChanged();
} catch (error) {
setUpdateStatus(
busy: false,
@@ -120,4 +178,10 @@ class ShellController extends ChangeNotifier {
);
}
}
@override
void dispose() {
_retryTimer?.cancel();
super.dispose();
}
}
+10
View File
@@ -64,6 +64,11 @@ class I18n {
'set.subtitle': '模型源、启动时内存分配与本地存储。',
'set.general': '通用',
'set.appearance': '外观',
'set.theme': '主题模式',
'set.theme.desc': '深色、浅色,或跟随系统设置。',
'set.theme.dark': '深色',
'set.theme.light': '浅色',
'set.theme.system': '跟随系统',
'set.accent': '主题颜色',
'set.accent.desc': '选择启动器的主色调,按钮、滑块与高亮会同步换色。',
'set.accent.custom': '自定义颜色',
@@ -189,6 +194,11 @@ class I18n {
'set.subtitle': 'Model sources, startup memory allocation and local storage.',
'set.general': 'General',
'set.appearance': 'Appearance',
'set.theme': 'Theme mode',
'set.theme.desc': 'Dark, light, or follow the system setting.',
'set.theme.dark': 'Dark',
'set.theme.light': 'Light',
'set.theme.system': 'System',
'set.accent': 'Theme color',
'set.accent.desc': 'Pick the launcher accent color; buttons, sliders and highlights follow it.',
'set.accent.custom': 'Custom color',
+24 -7
View File
@@ -12,7 +12,7 @@ void main() {
runApp(const GcUiApp());
}
/// GC-UI 前端:Flutter 桌面应用(Windows / macOS)。
/// GC-UI 前端:Flutter 桌面应用(Windows / macOS / Linux)。
///
/// 界面全部在这里;模型扫描、下载、更新、设置、模型窗口宿主都在后端
/// (GC-UI/backend/GC.UI.Server,本地 HTTP 服务)。
@@ -28,7 +28,7 @@ class _GcUiAppState extends State<GcUiApp> {
@override
Widget build(BuildContext context) {
// 语言与强调色都会触发重建:只重建 MaterialApp 的主题层,
// 语言、强调色、主题模式都会触发重建:只重建 MaterialApp 的主题层,
// 不触碰页面状态(页面由 ShellPage 自己持有)。
return ValueListenableBuilder<String>(
valueListenable: I18n.language,
@@ -36,11 +36,28 @@ class _GcUiAppState extends State<GcUiApp> {
return ValueListenableBuilder<Color>(
valueListenable: GcPalette.accent,
builder: (context, accent, _) {
return MaterialApp(
title: 'Morris GC',
debugShowCheckedModeBanner: false,
theme: buildGcTheme(),
home: ShellPage(controller: _controller),
return ValueListenableBuilder<ThemeMode>(
valueListenable: GcPalette.mode,
builder: (context, mode, _) {
return MaterialApp(
title: 'Morris GC',
debugShowCheckedModeBanner: false,
theme: buildGcTheme(Brightness.light),
darkTheme: buildGcTheme(Brightness.dark),
themeMode: mode,
// GcColors 是静态取值,必须按当前实际亮度(含跟随系统)绑定,
// 再用 key 强制子树重建,否则切主题后颜色仍是上一套。
builder: (context, child) {
final brightness = Theme.of(context).brightness;
GcColors.bind(brightness);
return KeyedSubtree(
key: ValueKey<bool>(brightness == Brightness.dark),
child: child ?? const SizedBox.shrink(),
);
},
home: ShellPage(controller: _controller),
);
},
);
},
);
+148 -49
View File
@@ -28,10 +28,14 @@ class _HomePageViewState extends State<HomePageView> {
bool _loading = true;
String? _error;
String? _notice;
int _lastRevision = 0;
@override
void initState() {
super.initState();
widget.controller.addListener(_onControllerChanged);
_lastRevision = widget.controller.modelsRevision;
final preset = widget.initialModels;
if (preset != null) {
_models = preset;
@@ -41,6 +45,22 @@ class _HomePageViewState extends State<HomePageView> {
}
}
@override
void dispose() {
widget.controller.removeListener(_onControllerChanged);
super.dispose();
}
/// 下载完成 / 删除模型后重新拉列表:页面被 Offstage 保活,不会自己重建。
void _onControllerChanged() {
if (!mounted) return;
final revision = widget.controller.modelsRevision;
if (revision == _lastRevision) return;
_lastRevision = revision;
if (widget.initialModels != null) return; // 截图 / 测试用的预置数据
_load();
}
Future<void> _load() async {
setState(() {
_loading = true;
@@ -128,7 +148,7 @@ class _HomePageViewState extends State<HomePageView> {
Expanded(
child: Text(
T('home.installed'),
style: const TextStyle(
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -151,7 +171,7 @@ class _HomePageViewState extends State<HomePageView> {
padding: const EdgeInsets.all(12),
child: Text(
_notice!,
style: const TextStyle(fontSize: 12, color: GcColors.warn),
style: TextStyle(fontSize: 12, color: GcColors.warn),
),
),
],
@@ -167,7 +187,7 @@ class _HomePageViewState extends State<HomePageView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(T('common.error'),
style: const TextStyle(color: GcColors.danger)),
style: TextStyle(color: GcColors.danger)),
const SizedBox(height: 8),
GcHint(_error!),
],
@@ -314,7 +334,7 @@ class _HomePageViewState extends State<HomePageView> {
children: <Widget>[
Text(
T('home.empty'),
style: const TextStyle(
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -326,7 +346,7 @@ class _HomePageViewState extends State<HomePageView> {
child: Text(
T('home.emptyHint'),
textAlign: TextAlign.center,
style: const TextStyle(
style: TextStyle(
fontSize: 12,
color: GcColors.faint,
height: 1.5,
@@ -488,7 +508,7 @@ class _ModelCardState extends State<_ModelCard> {
child: Text(
model.name,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
style: TextStyle(
fontSize: 14.5,
fontWeight: FontWeight.w700,
color: GcColors.ink,
@@ -498,7 +518,7 @@ class _ModelCardState extends State<_ModelCard> {
const SizedBox(width: 8),
Text(
model.sizeText,
style: const TextStyle(
style: TextStyle(
fontSize: 10.5,
color: GcColors.faint,
),
@@ -664,6 +684,12 @@ class _DownloadPageViewState extends State<DownloadPageView> {
String? _error;
Timer? _poll;
/// 页面是否可见(被切走后仍保活,但没必要继续请求后端)。
bool _visible = true;
/// 上一次轮询时是否有活跃任务,用来判断「刚刚全部结束」。
bool _hadActive = false;
@override
void initState() {
super.initState();
@@ -677,7 +703,14 @@ class _DownloadPageViewState extends State<DownloadPageView> {
return;
}
_load();
_poll = Timer.periodic(const Duration(seconds: 1), (_) => _refreshJobs());
_schedulePoll(const Duration(seconds: 2));
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// TickerMode 由外壳的 Offstage 控制:切到别的页时变 false。
_visible = TickerMode.of(context);
}
@override
@@ -686,6 +719,35 @@ class _DownloadPageViewState extends State<DownloadPageView> {
super.dispose();
}
/// 单次 Timer 链:有活跃任务时 1 秒一次,空闲时降到 5 秒,不可见时 10 秒。
///
/// 以前是 `Timer.periodic(1s)` 且永不停止 —— 只要进过下载页,之后每秒都
/// 在请求 `/api/download/jobs`,哪怕没有任何任务、页面也早就切走了。
void _schedulePoll(Duration delay) {
_poll?.cancel();
_poll = Timer(delay, _pollOnce);
}
Future<void> _pollOnce() async {
if (!mounted) return;
if (!_visible) {
_schedulePoll(const Duration(seconds: 10));
return;
}
_hadActive = _jobs.any((job) => job.isActive);
await _refreshJobs();
if (!mounted) return;
final active = _jobs.any((job) => job.isActive);
// 活跃 → 空闲:这一轮里下载结束了,首页的模型列表已经过期。
if (_hadActive && !active) widget.controller.modelsChanged();
_hadActive = active;
_schedulePoll(Duration(seconds: active ? 1 : 5));
}
Future<void> _load() async {
setState(() {
_loading = true;
@@ -818,7 +880,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
Expanded(
child: Text(
source.name,
style: const TextStyle(
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -826,7 +888,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
),
),
if (!source.enabled)
const Text(
Text(
'off',
style: TextStyle(
fontSize: 10, color: GcColors.faint),
@@ -836,7 +898,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
const SizedBox(height: 2),
Text(
source.host,
style: const TextStyle(
style: TextStyle(
fontSize: 10.5, color: GcColors.faint),
),
],
@@ -867,7 +929,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
Expanded(
child: Text(
T('dl.available'),
style: const TextStyle(
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -896,7 +958,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(T('common.error'),
style: const TextStyle(color: GcColors.danger)),
style: TextStyle(color: GcColors.danger)),
const SizedBox(height: 8),
GcHint(_error!),
],
@@ -908,7 +970,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(T('dl.empty'),
style: const TextStyle(color: GcColors.ink)),
style: TextStyle(color: GcColors.ink)),
const SizedBox(height: 8),
GcHint(T('dl.emptyHint')),
],
@@ -930,7 +992,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
children: <Widget>[
Text(
model.name,
style: const TextStyle(
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -939,7 +1001,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
const SizedBox(width: 8),
Text(
'v${model.version} · ${model.sizeText}',
style: const TextStyle(
style: TextStyle(
fontSize: 10.5,
color: GcColors.faint),
),
@@ -956,7 +1018,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
),
child: Text(
T('dl.installed'),
style: const TextStyle(
style: TextStyle(
fontSize: 10,
color: GcColors.ok),
),
@@ -969,7 +1031,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
model.description,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
style: TextStyle(
fontSize: 11.5,
color: GcColors.muted,
height: 1.4,
@@ -1032,7 +1094,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
Expanded(
child: Text(
'${job.name} v${job.version}',
style: const TextStyle(
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -1079,7 +1141,7 @@ class _DownloadPageViewState extends State<DownloadPageView> {
const SizedBox(height: 4),
Text(
job.error ?? job.detailText,
style: const TextStyle(
style: TextStyle(
fontSize: 10.5, color: GcColors.faint),
),
],
@@ -1127,7 +1189,11 @@ class _SettingsPageViewState extends State<SettingsPageView> {
int _fps = 120;
bool _autoUpdate = true;
String _language = 'auto';
bool _synced = false;
ThemeMode _themeMode = ThemeMode.dark;
/// 上一次同步所用的设置对象。以前用的是 `bool _synced` —— 只同步一次,
/// 之后外部(更新状态条、其它页面)改了设置,本页仍显示旧值。
SettingsInfo? _syncedFrom;
String? _status;
@override
@@ -1137,13 +1203,14 @@ class _SettingsPageViewState extends State<SettingsPageView> {
}
void _syncFrom(SettingsInfo settings) {
if (_synced) return;
if (identical(_syncedFrom, settings)) return;
_syncedFrom = settings;
_memory = settings.memoryMb.toDouble();
_memoryAuto = settings.memoryAuto;
_fps = settings.renderFps;
_autoUpdate = settings.autoUpdateOnStartup;
_language = settings.language;
_synced = true;
_themeMode = GcPalette.parseMode(settings.theme);
}
@override
@@ -1159,7 +1226,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
children: <Widget>[
Text(
T('set.title'),
style: const TextStyle(
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: GcColors.ink,
@@ -1199,7 +1266,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
Row(
children: <Widget>[
SizedBox(width: 160, child: Text(T('set.language'),
style: const TextStyle(fontSize: 12.5, color: GcColors.muted))),
style: TextStyle(fontSize: 12.5, color: GcColors.muted))),
DropdownButton<String>(
value: _language,
dropdownColor: GcColors.surfaceAlt,
@@ -1228,7 +1295,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
Row(
children: <Widget>[
SizedBox(width: 160, child: Text(T('set.fps'),
style: const TextStyle(fontSize: 12.5, color: GcColors.muted))),
style: TextStyle(fontSize: 12.5, color: GcColors.muted))),
Expanded(
child: Slider(
value: _fps.toDouble(),
@@ -1247,7 +1314,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
SizedBox(
width: 46,
child: Text('$_fps',
style: const TextStyle(
style: TextStyle(
fontSize: 12.5, color: GcColors.ink)),
),
],
@@ -1267,6 +1334,38 @@ class _SettingsPageViewState extends State<SettingsPageView> {
children: <Widget>[
GcSectionTitle(T('set.appearance')),
const SizedBox(height: 8),
GcHint(T('set.theme.desc')),
const SizedBox(height: 10),
SegmentedButton<ThemeMode>(
segments: <ButtonSegment<ThemeMode>>[
ButtonSegment<ThemeMode>(
value: ThemeMode.dark,
label: Text(T('set.theme.dark')),
icon: const Icon(Icons.dark_mode_outlined, size: 15),
),
ButtonSegment<ThemeMode>(
value: ThemeMode.light,
label: Text(T('set.theme.light')),
icon: const Icon(Icons.light_mode_outlined, size: 15),
),
ButtonSegment<ThemeMode>(
value: ThemeMode.system,
label: Text(T('set.theme.system')),
icon: const Icon(Icons.brightness_auto_outlined, size: 15),
),
],
selected: <ThemeMode>{_themeMode},
showSelectedIcon: false,
onSelectionChanged: (selection) async {
final mode = selection.first;
setState(() => _themeMode = mode);
GcPalette.mode.value = mode; // 立即预览
await widget.controller
.saveSettings(SettingsPatch(theme: GcPalette.modeToName(mode)));
if (mounted) setState(() => _status = T('common.applied'));
},
),
const SizedBox(height: 16),
GcHint(T('set.accent.desc')),
const SizedBox(height: 14),
Wrap(
@@ -1281,7 +1380,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
const SizedBox(height: 12),
Text(
'${T('set.accent')}: ${GcPalette.toHex(current)}',
style: const TextStyle(fontSize: 11.5, color: GcColors.faint),
style: TextStyle(fontSize: 11.5, color: GcColors.faint),
),
],
),
@@ -1337,12 +1436,12 @@ class _SettingsPageViewState extends State<SettingsPageView> {
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Icon(Icons.colorize_rounded,
Icon(Icons.colorize_rounded,
size: 16, color: GcColors.faint),
const SizedBox(width: 6),
Text(
T('set.accent.custom'),
style: const TextStyle(fontSize: 11.5, color: GcColors.muted),
style: TextStyle(fontSize: 11.5, color: GcColors.muted),
),
],
),
@@ -1363,12 +1462,12 @@ class _SettingsPageViewState extends State<SettingsPageView> {
content: TextField(
controller: controller,
autofocus: true,
style: const TextStyle(color: GcColors.ink),
style: TextStyle(color: GcColors.ink),
decoration: InputDecoration(
hintText: '#8FB6FF',
hintStyle: const TextStyle(color: GcColors.faint),
hintStyle: TextStyle(color: GcColors.faint),
helperText: T('set.accent.hexHint'),
helperStyle: const TextStyle(color: GcColors.faint, fontSize: 11),
helperStyle: TextStyle(color: GcColors.faint, fontSize: 11),
),
onSubmitted: (value) => Navigator.of(context).pop(value),
),
@@ -1424,7 +1523,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
Expanded(
child: Text(
T('set.memory.auto'),
style: const TextStyle(fontSize: 12.5, color: GcColors.muted),
style: TextStyle(fontSize: 12.5, color: GcColors.muted),
),
),
],
@@ -1453,7 +1552,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
width: 96,
child: Text(
'${_memory.round()} MB',
style: const TextStyle(fontSize: 12.5, color: GcColors.ink),
style: TextStyle(fontSize: 12.5, color: GcColors.ink),
),
),
],
@@ -1463,7 +1562,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
children: <Widget>[
Text(
'${T('set.memory.current')}: ${settings.memoryUsageText}',
style: const TextStyle(fontSize: 11.5, color: GcColors.faint),
style: TextStyle(fontSize: 11.5, color: GcColors.faint),
),
],
),
@@ -1494,7 +1593,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
children: <Widget>[
Text(
source.name,
style: const TextStyle(
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -1503,7 +1602,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
const SizedBox(height: 2),
Text(
source.url,
style: const TextStyle(
style: TextStyle(
fontSize: 10.5, color: GcColors.faint),
),
],
@@ -1511,7 +1610,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
),
Text(
source.enabled ? T('set.sourceEnabled') : '—',
style: const TextStyle(fontSize: 11, color: GcColors.faint),
style: TextStyle(fontSize: 11, color: GcColors.faint),
),
],
),
@@ -1566,12 +1665,12 @@ class _SettingsPageViewState extends State<SettingsPageView> {
SizedBox(
width: 160,
child: Text(label,
style: const TextStyle(fontSize: 12.5, color: GcColors.muted)),
style: TextStyle(fontSize: 12.5, color: GcColors.muted)),
),
Expanded(
child: SelectableText(
value,
style: const TextStyle(fontSize: 11.5, color: GcColors.faint),
style: TextStyle(fontSize: 11.5, color: GcColors.faint),
),
),
],
@@ -1601,7 +1700,7 @@ class _SettingsPageViewState extends State<SettingsPageView> {
Expanded(
child: Text(
T('update.auto'),
style: const TextStyle(fontSize: 12.5, color: GcColors.muted),
style: TextStyle(fontSize: 12.5, color: GcColors.muted),
),
),
],
@@ -1709,7 +1808,7 @@ class _AboutPageViewState extends State<AboutPageView> {
children: <Widget>[
Text(
T('nav.about'),
style: const TextStyle(
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: GcColors.ink,
@@ -1727,7 +1826,7 @@ class _AboutPageViewState extends State<AboutPageView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(T('common.error'),
style: const TextStyle(color: GcColors.danger)),
style: TextStyle(color: GcColors.danger)),
const SizedBox(height: 8),
GcHint(_error!),
const SizedBox(height: 12),
@@ -1745,7 +1844,7 @@ class _AboutPageViewState extends State<AboutPageView> {
children: <Widget>[
Text(
_about?.appName ?? 'Morris GC',
style: const TextStyle(
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
color: GcColors.ink,
@@ -1815,7 +1914,7 @@ class _AboutPageViewState extends State<AboutPageView> {
Expanded(
child: Text(
contributor.name,
style: const TextStyle(
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -1825,7 +1924,7 @@ class _AboutPageViewState extends State<AboutPageView> {
Text(
T('about.contributions',
<Object?>[contributor.contributions]),
style: const TextStyle(
style: TextStyle(
fontSize: 11, color: GcColors.faint),
),
],
@@ -1846,12 +1945,12 @@ class _AboutPageViewState extends State<AboutPageView> {
SizedBox(
width: 160,
child: Text(label,
style: const TextStyle(fontSize: 12.5, color: GcColors.muted)),
style: TextStyle(fontSize: 12.5, color: GcColors.muted)),
),
Expanded(
child: SelectableText(
value,
style: const TextStyle(fontSize: 12, color: GcColors.ink),
style: TextStyle(fontSize: 12, color: GcColors.ink),
),
),
],
+7 -7
View File
@@ -146,7 +146,7 @@ class _Sidebar extends StatelessWidget {
),
),
const SizedBox(width: 10),
const Expanded(
Expanded(
child: Text(
'Morris GC',
style: TextStyle(
@@ -186,7 +186,7 @@ class _Sidebar extends StatelessWidget {
padding: const EdgeInsets.only(left: 14),
child: Text(
'v${settings?.version ?? '—'}',
style: const TextStyle(fontSize: 10.5, color: GcColors.faint),
style: TextStyle(fontSize: 10.5, color: GcColors.faint),
),
),
Padding(
@@ -258,7 +258,7 @@ class _UpdateBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
decoration: BoxDecoration(
color: GcColors.surface,
border: Border(top: BorderSide(color: GcColors.line)),
),
@@ -274,7 +274,7 @@ class _UpdateBar extends StatelessWidget {
children: <Widget>[
Text(
controller.updateStatus,
style: const TextStyle(
style: TextStyle(
fontSize: 12.5,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -284,13 +284,13 @@ class _UpdateBar extends StatelessWidget {
if (controller.updateDetail.isNotEmpty)
Text(
controller.updateDetail,
style: const TextStyle(fontSize: 11, color: GcColors.faint),
style: TextStyle(fontSize: 11, color: GcColors.faint),
overflow: TextOverflow.ellipsis,
),
if (controller.launcherNotice.isNotEmpty)
Text(
controller.launcherNotice,
style: const TextStyle(fontSize: 11, color: GcColors.warn),
style: TextStyle(fontSize: 11, color: GcColors.warn),
),
],
),
@@ -331,7 +331,7 @@ class _BackendDown extends StatelessWidget {
children: <Widget>[
Text(
T('common.error'),
style: const TextStyle(
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w600,
color: GcColors.ink,
+113 -41
View File
@@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
/// 主题强调色(可在设置页自定义,并持久化到后端 settings.accentColor)。
/// 主题偏好(可在设置页切换,并持久化到后端 settings.theme)。
///
/// 用 [ValueNotifier] 持有当前颜色:设置页改色后,`main.dart` 监听到变化
/// 会重建 `MaterialApp.theme`,所有引用 [GcColors.accent] 的组件随之换色。
/// 支持深色 / 浅色 / 跟随系统三种;[mode] 变化会让 `main.dart` 重建
/// `MaterialApp`,`themeMode` 交给 Flutter 决定实际亮度。
class GcPalette {
GcPalette._();
@@ -13,6 +13,9 @@ class GcPalette {
static final ValueNotifier<Color> accent =
ValueNotifier<Color>(defaultAccent);
/// 当前主题模式(dark / light / system)。
static final ValueNotifier<ThemeMode> mode = ValueNotifier<ThemeMode>(ThemeMode.dark);
/// 设置页可选的预设色板。
static const List<Color> presets = <Color>[
Color(0xFF8FB6FF), // 极光蓝(默认)
@@ -25,6 +28,37 @@ class GcPalette {
Color(0xFFB0BEC5), // 石墨灰
];
/// 把后端设置里的 `theme` 字符串转成 [ThemeMode](未知值按深色处理)。
static ThemeMode parseMode(String? value) {
switch ((value ?? '').trim().toLowerCase()) {
case 'light':
return ThemeMode.light;
case 'system':
return ThemeMode.system;
case 'dark':
default:
return ThemeMode.dark;
}
}
/// 把 [ThemeMode] 转回后端设置的字符串。
static String modeToName(ThemeMode value) {
switch (value) {
case ThemeMode.light:
return 'light';
case ThemeMode.system:
return 'system';
case ThemeMode.dark:
return 'dark';
}
}
/// 应用主题模式(来自后端设置)。
static void applyTheme(String? value) {
final next = parseMode(value);
if (mode.value != next) mode.value = next;
}
/// 把 `#RRGGBB` / `RRGGBB` 解析为颜色;非法返回 null。
static Color? parseHex(String? value) {
if (value == null) return null;
@@ -53,86 +87,124 @@ class GcPalette {
}
}
/// 与 Avalonia 启动器一致的深色配色。
/// 配色:浅色 / 深色两套,按当前 [Brightness] 取值。
///
/// 以前这里是 const 常量,主题只能是深色 —— 设置里的 `theme` 字段写了也没人读。
/// 改成 getter 后,所有引用点自动跟随主题;代价是不能再放进 const 上下文。
class GcColors {
GcColors._();
static const Color appBg = Color(0xFF0A1226);
static const Color sideBg = Color(0xFF0C1529);
static const Color surface = Color(0xFF111C33);
static const Color surfaceAlt = Color(0xFF16233C);
static const Color line = Color(0xFF1B2740);
static const Color ink = Color(0xFFF2F6FF);
static const Color muted = Color(0xFFA9B8D4);
static const Color faint = Color(0xFF7C90B4);
static const Color warn = Color(0xFFFFC46B);
static const Color danger = Color(0xFFFF7A7A);
static const Color ok = Color(0xFF6BE3A2);
static bool _dark = true;
static bool get isDark => _dark;
/// 由 `MaterialApp.builder` 在每次构建前按实际亮度绑定。
static void bind(Brightness brightness) => _dark = brightness == Brightness.dark;
static Color _pick(int dark, int light) => Color(_dark ? dark : light);
static Color get appBg => _pick(0xFF0A1226, 0xFFF3F6FB);
static Color get sideBg => _pick(0xFF0C1529, 0xFFFFFFFF);
static Color get surface => _pick(0xFF111C33, 0xFFFFFFFF);
static Color get surfaceAlt => _pick(0xFF16233C, 0xFFE9EFFA);
static Color get line => _pick(0xFF1B2740, 0xFFD8E2F2);
static Color get ink => _pick(0xFFF2F6FF, 0xFF0F1B2D);
static Color get muted => _pick(0xFFA9B8D4, 0xFF46587A);
static Color get faint => _pick(0xFF7C90B4, 0xFF7286A6);
static Color get warn => _pick(0xFFFFC46B, 0xFFB26A00);
static Color get danger => _pick(0xFFFF7A7A, 0xFFC0392B);
static Color get ok => _pick(0xFF6BE3A2, 0xFF1E8E5A);
/// 侧栏「MENU」一类最弱的说明文字。
static Color get ghost => _pick(0xFF4C5F80, 0xFF97A8C4);
/// 当前强调色(动态,跟随设置)。
static Color get accent => GcPalette.accent.value;
/// 强调色的加深版本,用于主按钮 / 轨道。
/// 强调色的加深版本,用于主按钮 / 轨道。浅色主题下需要反向调整,
/// 否则强调色在浅背景上对比度不足。
static Color get accentStrong => _strongen(accent);
static Color _strongen(Color color) {
final hsl = HSLColor.fromColor(color);
final lightness = _dark ? hsl.lightness - 0.10 : hsl.lightness - 0.22;
return hsl
.withSaturation((hsl.saturation + 0.12).clamp(0.0, 1.0))
.withLightness((hsl.lightness - 0.10).clamp(0.0, 1.0))
.withLightness(lightness.clamp(0.0, 1.0))
.toColor();
}
}
ThemeData buildGcTheme() {
final base = ThemeData.dark(useMaterial3: true);
final accent = GcColors.accent;
final accentStrong = GcColors.accentStrong;
/// 按指定亮度构建主题。
///
/// 注意:不要在这里改 [GcColors] 的全局状态 —— MaterialApp 会同时构建
/// theme 与 darkTheme,后构建的那个会把状态写脏。亮度绑定由 `main.dart`
/// 的 builder 统一完成。
ThemeData buildGcTheme(Brightness brightness) {
final isDark = brightness == Brightness.dark;
final base = isDark
? ThemeData.dark(useMaterial3: true)
: ThemeData.light(useMaterial3: true);
Color pick(int dark, int light) => Color(isDark ? dark : light);
final appBg = pick(0xFF0A1226, 0xFFF3F6FB);
final surface = pick(0xFF111C33, 0xFFFFFFFF);
final surfaceAlt = pick(0xFF16233C, 0xFFE9EFFA);
final line = pick(0xFF1B2740, 0xFFD8E2F2);
final ink = pick(0xFFF2F6FF, 0xFF0F1B2D);
final danger = pick(0xFFFF7A7A, 0xFFC0392B);
final accent = GcPalette.accent.value;
final hsl = HSLColor.fromColor(accent);
final accentStrong = hsl
.withSaturation((hsl.saturation + 0.12).clamp(0.0, 1.0))
.withLightness((hsl.lightness - (isDark ? 0.10 : 0.22)).clamp(0.0, 1.0))
.toColor();
return base.copyWith(
scaffoldBackgroundColor: GcColors.appBg,
scaffoldBackgroundColor: appBg,
colorScheme: base.colorScheme.copyWith(
brightness: brightness,
primary: accentStrong,
secondary: accent,
surface: GcColors.surface,
error: GcColors.danger,
surface: surface,
error: danger,
),
textTheme: base.textTheme.apply(
bodyColor: GcColors.ink,
displayColor: GcColors.ink,
bodyColor: ink,
displayColor: ink,
),
dividerColor: GcColors.line,
cardTheme: const CardTheme(
color: GcColors.surface,
dividerColor: line,
cardTheme: CardTheme(
color: surface,
elevation: 0,
margin: EdgeInsets.zero,
),
sliderTheme: base.sliderTheme.copyWith(
activeTrackColor: accentStrong,
thumbColor: accent,
inactiveTrackColor: GcColors.line,
inactiveTrackColor: line,
),
switchTheme: SwitchThemeData(
thumbColor: WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.selected)
? accent
: GcColors.faint,
(states) => states.contains(WidgetState.selected) ? accent : GcColors.faint,
),
trackColor: WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.selected)
? accentStrong.withOpacity(0.45)
: GcColors.line,
: line,
),
),
progressIndicatorTheme: ProgressIndicatorThemeData(
color: accent,
linearTrackColor: GcColors.line,
linearTrackColor: line,
),
tooltipTheme: const TooltipThemeData(
tooltipTheme: TooltipThemeData(
decoration: BoxDecoration(
color: GcColors.surfaceAlt,
borderRadius: BorderRadius.all(Radius.circular(6)),
color: surfaceAlt,
borderRadius: const BorderRadius.all(Radius.circular(6)),
),
textStyle: TextStyle(color: GcColors.ink, fontSize: 12),
textStyle: TextStyle(color: ink, fontSize: 12),
),
);
}
@@ -174,7 +246,7 @@ class GcSectionTitle extends StatelessWidget {
Expanded(
child: Text(
text,
style: const TextStyle(
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: GcColors.ink,
@@ -196,6 +268,6 @@ class GcHint extends StatelessWidget {
@override
Widget build(BuildContext context) => Text(
text,
style: const TextStyle(fontSize: 12, color: GcColors.faint, height: 1.5),
style: TextStyle(fontSize: 12, color: GcColors.faint, height: 1.5),
);
}