app/lib/ui/home/performance/performance_ui.dart

287 lines
11 KiB
Dart
Raw Permalink Normal View History

2024-03-10 16:56:32 +08:00
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:starcitizen_doctor/common/utils/log.dart';
import 'package:starcitizen_doctor/data/game_performance_data.dart';
import 'package:starcitizen_doctor/widgets/widgets.dart';
import 'performance_ui_model.dart';
class HomePerformanceUI extends HookConsumerWidget {
const HomePerformanceUI({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(homePerformanceUIModelProvider);
final model = ref.read(homePerformanceUIModelProvider.notifier);
var content = makeLoading(context);
if (state.performanceMap != null) {
content = Stack(
children: [
Padding(
padding: const EdgeInsets.only(top: 12, left: 12, right: 12),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 24, right: 24),
child: Column(
children: [
if (state.showGraphicsPerformanceTip)
InfoBar(
2024-03-16 19:13:49 +08:00
title: Text(S.current
.performance_info_graphic_optimization_hint),
2024-03-15 00:01:06 +08:00
content: Text(
2024-03-16 19:13:49 +08:00
S.current
.performance_info_graphic_optimization_warning,
2024-03-10 16:56:32 +08:00
),
onClose: () => model.closeTip(),
),
const SizedBox(height: 16),
Row(
children: [
Text(
2024-03-16 19:13:49 +08:00
S.current.performance_info_current_status(
state.enabled
? S.current.performance_info_applied
: S.current.performance_info_not_applied),
2024-03-10 16:56:32 +08:00
style: const TextStyle(fontSize: 18),
),
const SizedBox(width: 32),
2024-03-15 00:01:06 +08:00
Text(
S.current.performance_action_preset,
style: const TextStyle(fontSize: 18),
2024-03-10 16:56:32 +08:00
),
2024-03-15 00:01:06 +08:00
for (final item in {
"low": S.current.performance_action_low,
"medium": S.current.performance_action_medium,
"high": S.current.performance_action_high,
"ultra": S.current.performance_action_super
2024-03-10 16:56:32 +08:00
}.entries)
Padding(
padding: const EdgeInsets.only(left: 6, right: 6),
child: Button(
child: Padding(
padding: const EdgeInsets.only(
top: 2, bottom: 2, left: 4, right: 4),
child: Text(item.value),
),
onPressed: () =>
model.onChangePreProfile(item.key)),
),
2024-03-16 19:13:49 +08:00
Text(S.current
.performance_action_info_preset_only_changes_graphics),
2024-03-10 16:56:32 +08:00
const Spacer(),
Button(
onPressed: () => model.refresh(),
child: const Padding(
padding: EdgeInsets.all(6),
child: Icon(FluentIcons.refresh),
),
),
const SizedBox(width: 12),
Button(
2024-03-15 00:01:06 +08:00
child: Text(
S.current.performance_action_reset_to_default,
style: const TextStyle(fontSize: 16),
2024-03-10 16:56:32 +08:00
),
onPressed: () => model.clean(context)),
const SizedBox(width: 24),
Button(
2024-03-15 00:01:06 +08:00
child: Text(
S.current.performance_action_apply,
style: const TextStyle(fontSize: 16),
2024-03-10 16:56:32 +08:00
),
onPressed: () => model.applyProfile(false)),
const SizedBox(width: 6),
Button(
2024-03-15 00:01:06 +08:00
child: Text(
2024-03-16 19:13:49 +08:00
S.current
.performance_action_apply_and_clear_shaders,
2024-03-15 00:01:06 +08:00
style: const TextStyle(fontSize: 16),
2024-03-10 16:56:32 +08:00
),
onPressed: () => model.applyProfile(true)),
],
),
const SizedBox(height: 16),
],
),
),
Expanded(
child: MasonryGridView.count(
crossAxisCount: 2,
mainAxisSpacing: 1,
crossAxisSpacing: 1,
itemCount: state.performanceMap!.length,
itemBuilder: (context, index) {
return makeItemGroup(context,
state.performanceMap!.entries.elementAt(index), model);
},
)),
],
),
),
if (state.workingString.isNotEmpty)
Container(
decoration: BoxDecoration(
color: Colors.black.withAlpha(150),
),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const ProgressRing(),
const SizedBox(height: 12),
Text(state.workingString),
],
),
),
)
],
);
}
return makeDefaultPage(context,
2024-03-16 19:13:49 +08:00
title:
S.current.performance_title_performance_optimization(model.scPath),
2024-03-10 16:56:32 +08:00
useBodyContainer: true,
content: content);
}
Widget makeItemGroup(
BuildContext context,
MapEntry<String?, List<GamePerformanceData>> group,
HomePerformanceUIModel model) {
return Padding(
padding: const EdgeInsets.all(12),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: FluentTheme.of(context).cardColor,
),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${group.key}",
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 6),
Container(
color: FluentTheme.of(context).cardColor.withOpacity(.2),
height: 1),
const SizedBox(height: 6),
for (final item in group.value) makeItem(context, item, model)
],
),
),
),
);
}
Widget makeItem(BuildContext context, GamePerformanceData item,
HomePerformanceUIModel model) {
return Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${item.name}",
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 12),
if (item.type == "int")
Column(
children: [
Row(
children: [
SizedBox(
width: 72,
child: TextFormBox(
key: UniqueKey(),
initialValue: "${item.value}",
onFieldSubmitted: (str) {
dPrint(str);
if (str.isEmpty) return;
final v = int.tryParse(str);
if (v != null &&
v < (item.max ?? 0) &&
v >= (item.min ?? 0)) {
item.value = v;
}
model.updateState();
},
onTapOutside: (e) {
model.updateState();
},
),
),
const SizedBox(width: 32),
SizedBox(
width: MediaQuery.of(context).size.width / 4,
child: Slider(
value: item.value?.toDouble() ?? 0,
min: item.min?.toDouble() ?? 0,
max: item.max?.toDouble() ?? 0,
onChanged: (double value) {
item.value = value.toInt();
model.updateState();
},
),
)
],
)
],
)
else if (item.type == "bool")
Column(
children: [
ToggleSwitch(
checked: item.value == 1,
onChanged: (bool value) {
item.value = value ? 1 : 0;
model.updateState();
},
)
],
)
else if (item.type == "customize")
TextFormBox(
maxLines: 10,
2024-03-16 19:13:49 +08:00
placeholder: S.current.performance_action_custom_parameters_input,
2024-03-10 16:56:32 +08:00
controller: model.customizeCtrl,
),
if (item.info != null && item.info!.isNotEmpty) ...[
const SizedBox(height: 12),
Text(
"${item.info}",
style:
TextStyle(fontSize: 14, color: Colors.white.withOpacity(.6)),
),
],
const SizedBox(height: 12),
if (item.type != "customize")
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
2024-03-16 19:13:49 +08:00
S.current.performance_info_min_max_values(
item.key ?? "", item.min ?? "", item.max ?? ""),
2024-03-10 16:56:32 +08:00
style: TextStyle(color: Colors.white.withOpacity(.6)),
)
],
),
const SizedBox(height: 6),
Container(
color: FluentTheme.of(context).cardColor.withOpacity(.1),
height: 1),
],
),
);
}
}