Refactor: Use maps instead of if-else chains for key mappings

Co-authored-by: jonasbark <1151304+jonasbark@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-30 12:41:13 +00:00
parent 0c94852246
commit 32e4b9762b
2 changed files with 22 additions and 28 deletions

View File

@@ -65,21 +65,16 @@ class MethodChannelKeyPressSimulator extends KeyPressSimulatorPlatform {
@override
Future<void> simulateMediaKey(PhysicalKeyboardKey mediaKey) async {
// Map PhysicalKeyboardKey to string identifier since keyCode is null for media keys
String? keyIdentifier;
if (mediaKey == PhysicalKeyboardKey.mediaPlayPause) {
keyIdentifier = 'playPause';
} else if (mediaKey == PhysicalKeyboardKey.mediaStop) {
keyIdentifier = 'stop';
} else if (mediaKey == PhysicalKeyboardKey.mediaTrackNext) {
keyIdentifier = 'next';
} else if (mediaKey == PhysicalKeyboardKey.mediaTrackPrevious) {
keyIdentifier = 'previous';
} else if (mediaKey == PhysicalKeyboardKey.audioVolumeUp) {
keyIdentifier = 'volumeUp';
} else if (mediaKey == PhysicalKeyboardKey.audioVolumeDown) {
keyIdentifier = 'volumeDown';
}
final keyMap = {
PhysicalKeyboardKey.mediaPlayPause: 'playPause',
PhysicalKeyboardKey.mediaStop: 'stop',
PhysicalKeyboardKey.mediaTrackNext: 'next',
PhysicalKeyboardKey.mediaTrackPrevious: 'previous',
PhysicalKeyboardKey.audioVolumeUp: 'volumeUp',
PhysicalKeyboardKey.audioVolumeDown: 'volumeDown',
};
final keyIdentifier = keyMap[mediaKey];
if (keyIdentifier == null) {
throw UnsupportedError('Unsupported media key: $mediaKey');
}

View File

@@ -12,6 +12,7 @@
#include <memory>
#include <sstream>
#include <unordered_map>
using flutter::EncodableList;
using flutter::EncodableMap;
@@ -261,23 +262,21 @@ void KeypressSimulatorWindowsPlugin::SimulateMediaKey(
std::string keyIdentifier = std::get<std::string>(args.at(EncodableValue("key")));
// Map string identifier to Windows virtual key codes
UINT vkCode = 0;
if (keyIdentifier == "playPause") {
vkCode = VK_MEDIA_PLAY_PAUSE;
} else if (keyIdentifier == "stop") {
vkCode = VK_MEDIA_STOP;
} else if (keyIdentifier == "next") {
vkCode = VK_MEDIA_NEXT_TRACK;
} else if (keyIdentifier == "previous") {
vkCode = VK_MEDIA_PREV_TRACK;
} else if (keyIdentifier == "volumeUp") {
vkCode = VK_VOLUME_UP;
} else if (keyIdentifier == "volumeDown") {
vkCode = VK_VOLUME_DOWN;
} else {
static const std::unordered_map<std::string, UINT> keyMap = {
{"playPause", VK_MEDIA_PLAY_PAUSE},
{"stop", VK_MEDIA_STOP},
{"next", VK_MEDIA_NEXT_TRACK},
{"previous", VK_MEDIA_PREV_TRACK},
{"volumeUp", VK_VOLUME_UP},
{"volumeDown", VK_VOLUME_DOWN}
};
auto it = keyMap.find(keyIdentifier);
if (it == keyMap.end()) {
result->Error("UNSUPPORTED_KEY", "Unsupported media key identifier");
return;
}
UINT vkCode = it->second;
// Send key down event
INPUT inputs[2] = {};