Christien Rioux 77c68aa45f ui cleanup
2025-03-17 00:51:16 -04:00

360 lines
13 KiB
Dart

import 'dart:async';
import 'package:animated_custom_dropdown/custom_dropdown.dart';
import 'package:ansicolor/ansicolor.dart';
import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:go_router/go_router.dart';
import 'package:loggy/loggy.dart';
import 'package:veilid_support/veilid_support.dart';
import 'package:xterm/xterm.dart';
import '../../layout/layout.dart';
import '../../notifications/notifications.dart';
import '../../theme/models/scale_theme/scale_custom_dropdown_theme.dart';
import '../../theme/theme.dart';
import '../../tools/tools.dart';
import 'history_text_editing_controller.dart';
final globalDebugTerminal = Terminal(
maxLines: 50000,
);
const kDefaultTerminalStyle = TerminalStyle(
fontSize: 11,
// height: 1.2,
fontFamily: 'Source Code Pro');
class LogLevelDropdownItem {
const LogLevelDropdownItem(
{required this.label, required this.icon, required this.value});
final String label;
final Widget icon;
final LogLevel value;
}
class DeveloperPage extends StatefulWidget {
const DeveloperPage({super.key});
@override
State<DeveloperPage> createState() => _DeveloperPageState();
}
class _DeveloperPageState extends State<DeveloperPage> {
@override
void initState() {
super.initState();
_historyController = HistoryTextEditingController(setState: setState);
_terminalController.addListener(() {
setState(() {});
});
for (var i = 0; i < logLevels.length; i++) {
_logLevelDropdownItems.add(LogLevelDropdownItem(
label: logLevelName(logLevels[i]),
icon: Text(logLevelEmoji(logLevels[i])),
value: logLevels[i]));
}
}
void _debugOut(String out) {
final pen = AnsiPen()..cyan(bold: true);
final colorOut = pen(out);
debugPrint(colorOut);
globalDebugTerminal.write(colorOut.replaceAll('\n', '\r\n'));
}
Future<bool> _sendDebugCommand(String debugCommand) async {
try {
setState(() {
_busy = true;
});
if (debugCommand == 'pool allocations') {
try {
DHTRecordPool.instance.debugPrintAllocations();
} on Exception catch (e, st) {
_debugOut('<<< ERROR\n$e\n<<< STACK\n$st');
return false;
}
return true;
}
if (debugCommand == 'pool opened') {
try {
DHTRecordPool.instance.debugPrintOpened();
} on Exception catch (e, st) {
_debugOut('<<< ERROR\n$e\n<<< STACK\n$st');
return false;
}
return true;
}
if (debugCommand.startsWith('change_log_ignore ')) {
final args = debugCommand.split(' ');
if (args.length < 3) {
_debugOut('Incorrect number of arguments');
return false;
}
final layer = args[1];
final changes = args[2].split(',');
try {
Veilid.instance.changeLogIgnore(layer, changes);
} on Exception catch (e, st) {
_debugOut('<<< ERROR\n$e\n<<< STACK\n$st');
return false;
}
return true;
}
if (debugCommand == 'ellet') {
setState(() {
_showEllet = !_showEllet;
});
return true;
}
_debugOut('DEBUG >>>\n$debugCommand\n');
try {
final out = await Veilid.instance.debug(debugCommand);
_debugOut('<<< DEBUG\n$out\n');
} on Exception catch (e, st) {
_debugOut('<<< ERROR\n$e\n<<< STACK\n$st');
return false;
}
return true;
} finally {
setState(() {
_busy = false;
});
}
}
Future<void> clear(BuildContext context) async {
globalDebugTerminal.buffer.clear();
if (context.mounted) {
context
.read<NotificationsCubit>()
.info(text: translate('developer.cleared'));
}
}
Future<void> copySelection(BuildContext context) async {
final selection = _terminalController.selection;
if (selection != null) {
final text = globalDebugTerminal.buffer.getText(selection);
_terminalController.clearSelection();
await Clipboard.setData(ClipboardData(text: text));
if (context.mounted) {
context
.read<NotificationsCubit>()
.info(text: translate('developer.copied'));
}
}
}
Future<void> copyAll(BuildContext context) async {
final text = globalDebugTerminal.buffer.getText();
await Clipboard.setData(ClipboardData(text: text));
if (context.mounted) {
context
.read<NotificationsCubit>()
.info(text: translate('developer.copied_all'));
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
final scaleTheme = theme.extension<ScaleTheme>()!;
final dropdownTheme = scaleTheme.customDropdownTheme();
final scaleConfig = theme.extension<ScaleConfig>()!;
final hintColor = scaleConfig.useVisualIndicators
? scale.primaryScale.primaryText
: scale.primaryScale.primary;
return Scaffold(
backgroundColor: scale.primaryScale.border,
appBar: DefaultAppBar(
title: Text(translate('developer.title')),
leading: IconButton(
icon: Icon(Icons.arrow_back, color: scale.primaryScale.borderText),
onPressed: () => GoRouterHelper(context).pop(),
),
actions: [
IconButton(
icon: const Icon(Icons.copy),
color: scale.primaryScale.borderText,
disabledColor: scale.primaryScale.borderText.withAlpha(0x3F),
onPressed: _terminalController.selection == null
? null
: () async {
await copySelection(context);
}),
IconButton(
icon: const Icon(Icons.copy_all),
color: scale.primaryScale.borderText,
disabledColor: scale.primaryScale.borderText.withAlpha(0x3F),
onPressed: () async {
await copyAll(context);
}),
IconButton(
icon: const Icon(Icons.clear_all),
color: scale.primaryScale.borderText,
disabledColor: scale.primaryScale.borderText.withAlpha(0x3F),
onPressed: () async {
final confirm = await showConfirmModal(
context: context,
title: translate('confirmation.confirm'),
text: translate('developer.are_you_sure_clear'),
);
if (confirm && context.mounted) {
await clear(context);
}
}),
SizedBox.fromSize(
size: const Size(140, 48),
child: CustomDropdown<LogLevelDropdownItem>(
items: _logLevelDropdownItems,
initialItem: _logLevelDropdownItems
.singleWhere((x) => x.value == _logLevelDropDown),
onChanged: (item) {
if (item != null) {
setState(() {
_logLevelDropDown = item.value;
Loggy('').level = getLogOptions(item.value);
setVeilidLogLevel(item.value);
});
}
},
headerBuilder: (context, item, enabled) => Row(children: [
item.icon,
const Spacer(),
Text(item.label).copyWith(style: dropdownTheme.textStyle)
]),
listItemBuilder: (context, item, enabled, onItemSelect) =>
Row(children: [
item.icon,
const Spacer(),
Text(item.label).copyWith(style: dropdownTheme.textStyle)
]),
decoration: dropdownTheme.decoration,
disabledDecoration: dropdownTheme.disabledDecoration,
listItemPadding: dropdownTheme.listItemPadding,
itemsListPadding: dropdownTheme.itemsListPadding,
expandedHeaderPadding: dropdownTheme.expandedHeaderPadding,
closedHeaderPadding: dropdownTheme.closedHeaderPadding,
)).paddingLTRB(0, 4, 8, 4),
],
),
body: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: SafeArea(
child: Column(children: [
Stack(alignment: AlignmentDirectional.center, children: [
Image.asset('assets/images/ellet.png'),
TerminalView(globalDebugTerminal,
textStyle: kDefaultTerminalStyle,
controller: _terminalController,
keyboardType: TextInputType.none,
//autofocus: true,
backgroundOpacity: _showEllet ? 0.75 : 1.0,
onSecondaryTapDown: (details, offset) async {
await copySelection(context);
})
]).expanded(),
TextField(
enabled: !_busy,
controller: _historyController.controller,
focusNode: _historyController.focusNode,
onTapOutside: (event) {
FocusManager.instance.primaryFocus?.unfocus();
},
decoration: InputDecoration(
filled: true,
contentPadding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
enabledBorder:
const OutlineInputBorder(borderSide: BorderSide.none),
border:
const OutlineInputBorder(borderSide: BorderSide.none),
focusedBorder:
const OutlineInputBorder(borderSide: BorderSide.none),
fillColor: scale.primaryScale.elementBackground,
hoverColor: scale.primaryScale.elementBackground,
hintStyle: scaleTheme.textTheme.labelMedium!.copyWith(
color: scaleConfig.useVisualIndicators
? hintColor.withAlpha(0x7F)
: hintColor),
hintText: translate('developer.command'),
suffixIcon: IconButton(
icon: Icon(Icons.send,
color: _historyController.controller.text.isEmpty
? hintColor.withAlpha(0x7F)
: hintColor),
onPressed:
(_historyController.controller.text.isEmpty || _busy)
? null
: () async {
final debugCommand =
_historyController.controller.text;
_historyController.controller.clear();
await _sendDebugCommand(debugCommand);
},
)),
onChanged: (_) {
setState(() => {});
},
onEditingComplete: () {
// part of the default action if onEditingComplete is null
_historyController.controller.clearComposing();
// don't give up focus though
},
onSubmitted: (debugCommand) async {
if (debugCommand.isEmpty) {
return;
}
final ok = await _sendDebugCommand(debugCommand);
if (ok) {
setState(() {
_historyController.submit(debugCommand);
});
}
},
).paddingAll(4)
]))));
}
////////////////////////////////////////////////////////////////////////////
final _terminalController = TerminalController();
late final HistoryTextEditingController _historyController;
final List<LogLevelDropdownItem> _logLevelDropdownItems = [];
var _logLevelDropDown = log.level.logLevel;
var _showEllet = false;
var _busy = false;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty<TerminalController>(
'terminalController', _terminalController))
..add(
DiagnosticsProperty<LogLevel>('logLevelDropDown', _logLevelDropDown));
}
}