veilidchat/lib/veilid_processor/views/developer.dart

306 lines
11 KiB
Dart
Raw Normal View History

2023-10-09 22:11:39 -04:00
import 'package:ansicolor/ansicolor.dart';
2023-10-09 16:52:37 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-10-09 22:11:39 -04:00
import 'package:cool_dropdown/cool_dropdown.dart';
import 'package:cool_dropdown/models/cool_dropdown_item.dart';
2023-10-09 16:52:37 -04:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2023-10-09 22:11:39 -04:00
import 'package:flutter_animate/flutter_animate.dart';
2023-10-09 16:52:37 -04:00
import 'package:flutter_translate/flutter_translate.dart';
import 'package:go_router/go_router.dart';
2023-10-09 17:06:28 -04:00
import 'package:loggy/loggy.dart';
2023-10-09 22:11:39 -04:00
import 'package:quickalert/quickalert.dart';
2024-01-04 22:29:43 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-10-09 16:52:37 -04:00
import 'package:xterm/xterm.dart';
2024-04-07 23:16:06 -04:00
import '../../layout/layout.dart';
2024-01-04 22:29:43 -05:00
import '../../theme/theme.dart';
2023-12-26 20:26:54 -05:00
import '../../tools/tools.dart';
2023-10-09 16:52:37 -04:00
final globalDebugTerminal = Terminal(
maxLines: 50000,
);
const kDefaultTerminalStyle = TerminalStyle(
fontSize: 11,
// height: 1.2,
fontFamily: 'Source Code Pro');
2023-12-26 20:26:54 -05:00
class DeveloperPage extends StatefulWidget {
2023-10-09 16:52:37 -04:00
const DeveloperPage({super.key});
@override
2024-02-28 20:32:37 -05:00
State<DeveloperPage> createState() => _DeveloperPageState();
2023-10-09 16:52:37 -04:00
}
2024-02-28 20:32:37 -05:00
class _DeveloperPageState extends State<DeveloperPage> {
2023-10-09 22:11:39 -04:00
final _terminalController = TerminalController();
final _debugCommandController = TextEditingController();
final _logLevelController = DropdownController(duration: 250.ms);
final List<CoolDropdownItem<LogLevel>> _logLevelDropdownItems = [];
var _logLevelDropDown = log.level.logLevel;
var _showEllet = false;
2023-10-09 16:52:37 -04:00
@override
void initState() {
super.initState();
2024-02-28 20:32:37 -05:00
WidgetsBinding.instance.addPostFrameCallback((_) async {
await changeWindowSetup(
TitleBarStyle.normal, OrientationCapability.normal);
});
2023-10-09 22:11:39 -04:00
_terminalController.addListener(() {
2023-10-09 16:52:37 -04:00
setState(() {});
});
2023-10-09 22:11:39 -04:00
for (var i = 0; i < logLevels.length; i++) {
_logLevelDropdownItems.add(CoolDropdownItem<LogLevel>(
label: logLevelName(logLevels[i]),
icon: Text(logLevelEmoji(logLevels[i])),
value: logLevels[i]));
}
2023-10-09 16:52:37 -04:00
}
2023-10-09 22:11:39 -04:00
void _debugOut(String out) {
final pen = AnsiPen()..cyan(bold: true);
final colorOut = pen(out);
debugPrint(colorOut);
globalDebugTerminal.write(colorOut.replaceAll('\n', '\r\n'));
}
2023-10-09 16:52:37 -04:00
Future<void> _sendDebugCommand(String debugCommand) async {
2024-04-07 16:19:45 -04:00
if (debugCommand == 'pool allocations') {
DHTRecordPool.instance.debugPrintAllocations();
return;
}
if (debugCommand == 'pool opened') {
DHTRecordPool.instance.debugPrintOpened();
return;
}
2024-04-10 16:13:08 -04:00
if (debugCommand.startsWith('change_log_ignore ')) {
final args = debugCommand.split(' ');
if (args.length < 3) {
_debugOut('Incorrect number of arguments');
return;
}
final layer = args[1];
final changes = args[2].split(',');
Veilid.instance.changeLogIgnore(layer, changes);
return;
}
2023-10-09 22:11:39 -04:00
if (debugCommand == 'ellet') {
setState(() {
_showEllet = !_showEllet;
});
return;
}
2024-04-07 16:19:45 -04:00
2023-10-09 22:11:39 -04:00
_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');
}
}
Future<void> clear(BuildContext context) async {
globalDebugTerminal.buffer.clear();
if (context.mounted) {
showInfoToast(context, translate('developer.cleared'));
}
2023-10-09 16:52:37 -04:00
}
Future<void> copySelection(BuildContext context) async {
2023-10-09 22:11:39 -04:00
final selection = _terminalController.selection;
2023-10-09 16:52:37 -04:00
if (selection != null) {
final text = globalDebugTerminal.buffer.getText(selection);
2023-10-09 22:11:39 -04:00
_terminalController.clearSelection();
2023-10-09 16:52:37 -04:00
await Clipboard.setData(ClipboardData(text: text));
if (context.mounted) {
showInfoToast(context, translate('developer.copied'));
}
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
2023-10-09 22:11:39 -04:00
final textTheme = theme.textTheme;
2023-10-09 16:52:37 -04:00
final scale = theme.extension<ScaleScheme>()!;
// WidgetsBinding.instance.addPostFrameCallback((_) {
// if (!_isScrolling && _wantsBottom) {
// _scrollToBottom();
// }
// });
return Scaffold(
backgroundColor: scale.primaryScale.primary,
2024-04-07 23:16:06 -04:00
appBar: DefaultAppBar(
title: Text(translate('developer.title')),
2023-10-09 16:52:37 -04:00
leading: IconButton(
icon: Icon(Icons.arrow_back, color: scale.primaryScale.primaryText),
2023-10-09 16:52:37 -04:00
onPressed: () => GoRouterHelper(context).pop(),
),
actions: [
IconButton(
icon: const Icon(Icons.copy),
color: scale.primaryScale.primaryText,
disabledColor: scale.primaryScale.primaryText.withAlpha(0x3F),
2023-10-09 22:11:39 -04:00
onPressed: _terminalController.selection == null
2023-10-09 16:52:37 -04:00
? null
: () async {
await copySelection(context);
2023-10-09 17:06:28 -04:00
}),
2023-10-09 22:11:39 -04:00
IconButton(
icon: const Icon(Icons.clear_all),
color: scale.primaryScale.primaryText,
disabledColor: scale.primaryScale.primaryText.withAlpha(0x3F),
2023-10-09 22:11:39 -04:00
onPressed: () async {
await QuickAlert.show(
context: context,
type: QuickAlertType.confirm,
title: translate('developer.are_you_sure_clear'),
titleColor: scale.primaryScale.appText,
textColor: scale.primaryScale.subtleText,
confirmBtnColor: scale.primaryScale.primary,
cancelBtnTextStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18,
color: scale.primaryScale.appText),
backgroundColor: scale.primaryScale.appBackground,
headerBackgroundColor: scale.primaryScale.primary,
2023-10-09 22:11:39 -04:00
confirmBtnText: translate('button.ok'),
cancelBtnText: translate('button.cancel'),
onConfirmBtnTap: () async {
Navigator.pop(context);
if (context.mounted) {
await clear(context);
}
});
}),
CoolDropdown<LogLevel>(
controller: _logLevelController,
defaultItem: _logLevelDropdownItems
.singleWhere((x) => x.value == _logLevelDropDown),
onChange: (value) {
setState(() {
_logLevelDropDown = value;
Loggy('').level = getLogOptions(value);
setVeilidLogLevel(value);
_logLevelController.close();
});
},
resultOptions: ResultOptions(
width: 64,
height: 40,
render: ResultRender.icon,
icon: SizedBox(
width: 10,
height: 10,
child: CustomPaint(
painter: DropdownArrowPainter(
color: scale.primaryScale.primaryText))),
2023-10-09 22:11:39 -04:00
textStyle: textTheme.labelMedium!
.copyWith(color: scale.primaryScale.primaryText),
2023-10-09 22:11:39 -04:00
padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
openBoxDecoration: BoxDecoration(
color: scale.primaryScale.border,
borderRadius: BorderRadius.circular(8),
),
boxDecoration: BoxDecoration(
color: scale.primaryScale.hoverBorder,
borderRadius: BorderRadius.circular(8),
),
2023-10-09 22:11:39 -04:00
),
dropdownOptions: DropdownOptions(
width: 160,
align: DropdownAlign.right,
duration: 150.ms,
color: scale.primaryScale.elementBackground,
borderSide: BorderSide(color: scale.primaryScale.border),
borderRadius: BorderRadius.circular(8),
padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
),
dropdownTriangleOptions: const DropdownTriangleOptions(
align: DropdownTriangleAlign.right),
dropdownItemOptions: DropdownItemOptions(
selectedTextStyle: textTheme.labelMedium!
2024-04-07 23:16:06 -04:00
.copyWith(color: scale.primaryScale.appText),
2023-10-09 22:11:39 -04:00
textStyle: textTheme.labelMedium!
2024-04-07 23:16:06 -04:00
.copyWith(color: scale.primaryScale.appText),
2023-10-09 22:11:39 -04:00
selectedBoxDecoration: BoxDecoration(
color: scale.primaryScale.activeElementBackground),
mainAxisAlignment: MainAxisAlignment.spaceBetween,
padding: const EdgeInsets.fromLTRB(8, 4, 8, 4),
selectedPadding: const EdgeInsets.fromLTRB(8, 4, 8, 4)),
dropdownList: _logLevelDropdownItems,
).paddingLTRB(0, 0, 8, 0)
2023-10-09 16:52:37 -04:00
],
),
2023-10-09 22:11:39 -04:00
body: SafeArea(
child: Column(children: [
Stack(alignment: AlignmentDirectional.center, children: [
Image.asset('assets/images/ellet.png'),
TerminalView(globalDebugTerminal,
textStyle: kDefaultTerminalStyle,
controller: _terminalController,
//autofocus: true,
backgroundOpacity: _showEllet ? 0.75 : 1.0,
onSecondaryTapDown: (details, offset) async {
2023-10-09 16:52:37 -04:00
await copySelection(context);
2023-10-09 22:11:39 -04:00
})
]).expanded(),
2023-10-09 16:52:37 -04:00
TextField(
controller: _debugCommandController,
decoration: InputDecoration(
filled: true,
contentPadding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
enabledBorder: OutlineInputBorder(
2023-10-09 16:52:37 -04:00
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
2023-10-09 16:52:37 -04:00
fillColor: scale.primaryScale.subtleBackground,
hintText: translate('developer.command'),
suffixIcon: IconButton(
icon: Icon(Icons.send,
color: _debugCommandController.text.isEmpty
? scale.primaryScale.primary.withAlpha(0x3F)
: scale.primaryScale.primary),
2023-10-09 22:11:39 -04:00
onPressed: _debugCommandController.text.isEmpty
? null
: () async {
final debugCommand = _debugCommandController.text;
_debugCommandController.clear();
await _sendDebugCommand(debugCommand);
},
2023-10-09 16:52:37 -04:00
)),
2023-10-09 22:11:39 -04:00
onChanged: (_) {
setState(() => {});
},
2023-10-09 16:52:37 -04:00
onSubmitted: (debugCommand) async {
_debugCommandController.clear();
await _sendDebugCommand(debugCommand);
},
).paddingAll(4)
2023-10-09 22:11:39 -04:00
])));
2023-10-09 16:52:37 -04:00
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
2023-10-09 22:11:39 -04:00
properties
..add(DiagnosticsProperty<TerminalController>(
'terminalController', _terminalController))
..add(
DiagnosticsProperty<LogLevel>('logLevelDropDown', _logLevelDropDown));
2023-10-09 16:52:37 -04:00
}
}