From cb497daee45e2a35702bf2cbaadde681115207c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Dec 2025 07:21:57 +0000 Subject: [PATCH] Address code review feedback: extract constants and fix async handling Co-authored-by: jonasbark <1151304+jonasbark@users.noreply.github.com> --- lib/pages/button_simulator.dart | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/pages/button_simulator.dart b/lib/pages/button_simulator.dart index b49350c..99785b6 100644 --- a/lib/pages/button_simulator.dart +++ b/lib/pages/button_simulator.dart @@ -30,6 +30,8 @@ class _ButtonSimulatorState extends State { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ]; + + static const Duration _keyPressDuration = Duration(milliseconds: 100); @override void initState() { @@ -44,7 +46,7 @@ class _ButtonSimulatorState extends State { super.dispose(); } - void _loadHotkeys() { + Future _loadHotkeys() async { final savedHotkeys = core.settings.getButtonSimulatorHotkeys(); // If no saved hotkeys, initialize with defaults @@ -66,10 +68,12 @@ class _ButtonSimulatorState extends State { } } - core.settings.setButtonSimulatorHotkeys(defaultHotkeys); - setState(() { - _hotkeys = defaultHotkeys; - }); + await core.settings.setButtonSimulatorHotkeys(defaultHotkeys); + if (mounted) { + setState(() { + _hotkeys = defaultHotkeys; + }); + } } else { setState(() { _hotkeys = savedHotkeys; @@ -102,7 +106,7 @@ class _ButtonSimulatorState extends State { _sendKey(context, down: true, action: action, connection: connection); // Schedule key up event Future.delayed( - Duration(milliseconds: 100), + _keyPressDuration, () { _sendKey(context, down: false, action: action, connection: connection); }, @@ -346,6 +350,8 @@ class _HotkeySettingsDialogState extends State<_HotkeySettingsDialog> { late Map _editableHotkeys; String? _editingAction; late FocusNode _focusNode; + + static final _validHotkeyPattern = RegExp(r'[0-9a-z]'); @override void initState() { @@ -366,7 +372,7 @@ class _HotkeySettingsDialogState extends State<_HotkeySettingsDialog> { final key = event.logicalKey.keyLabel.toLowerCase(); // Only allow 1-9 and a-z - if (key.length == 1 && (RegExp(r'[0-9a-z]').hasMatch(key))) { + if (key.length == 1 && _validHotkeyPattern.hasMatch(key)) { setState(() { _editableHotkeys[_editingAction!] = key; _editingAction = null;