Files
plezy/lib/widgets/hotkey_recorder_widget.dart
T
2025-11-10 10:02:34 +01:00

116 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import '../i18n/strings.g.dart';
class HotKeyRecorderWidget extends StatefulWidget {
final String actionName;
final HotKey? currentHotKey;
final Function(HotKey) onHotKeyRecorded;
final VoidCallback onCancel;
const HotKeyRecorderWidget({
super.key,
required this.actionName,
this.currentHotKey,
required this.onHotKeyRecorded,
required this.onCancel,
});
@override
State<HotKeyRecorderWidget> createState() => _HotKeyRecorderWidgetState();
}
class _HotKeyRecorderWidgetState extends State<HotKeyRecorderWidget> {
HotKey? _recordedHotKey;
@override
void initState() {
super.initState();
_recordedHotKey = widget.currentHotKey;
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(t.hotkeys.setShortcutFor(actionName: widget.actionName)),
content: SizedBox(
width: double.maxFinite,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Current shortcut:',
style: Theme.of(
context,
).textTheme.bodyMedium?.copyWith(fontWeight: FontWeight.bold),
),
const SizedBox(height: 6),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).dividerColor),
borderRadius: BorderRadius.circular(6),
),
child: Row(
children: [
Expanded(
child: HotKeyRecorder(
initalHotKey: _recordedHotKey,
onHotKeyRecorded: (hotKey) {
setState(() {
_recordedHotKey = hotKey;
});
},
),
),
if (_recordedHotKey != null)
IconButton(
icon: const Icon(Icons.backspace, size: 18),
onPressed: () {
setState(() {
_recordedHotKey = null;
});
},
padding: EdgeInsets.zero,
constraints: const BoxConstraints(
minWidth: 24,
minHeight: 24,
),
tooltip: t.hotkeys.clearShortcut,
),
],
),
),
const SizedBox(height: 8),
Text(
'Press any key combination to set a new shortcut',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Theme.of(
context,
).colorScheme.onSurface.withValues(alpha: 0.7),
),
textAlign: TextAlign.center,
),
],
),
),
),
actions: [
TextButton(onPressed: widget.onCancel, child: Text(t.common.cancel)),
TextButton(
onPressed: _recordedHotKey != null
? () => widget.onHotKeyRecorded(_recordedHotKey!)
: null,
child: Text(t.common.save),
),
],
);
}
}