Fladder/lib/models/settings/key_combinations.dart
PartyDonut fa30e634b4
feat: Customizable shortcuts/hotkeys (#439)
This implements the logic for allowing hotkeys with modifiers.
Implemented globalhotkeys and videocontrol hotkeys
Also implements saving the forward backwards seconds to the user.

Co-authored-by: PartyDonut <PartyDonut@users.noreply.github.com>
2025-08-08 16:36:50 +02:00

70 lines
1.8 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:fladder/screens/settings/widgets/key_listener.dart';
part 'key_combinations.freezed.dart';
part 'key_combinations.g.dart';
@Freezed(toJson: true, fromJson: true)
class KeyCombination with _$KeyCombination {
const KeyCombination._();
factory KeyCombination({
@LogicalKeyboardSerializer() LogicalKeyboardKey? modifier,
@LogicalKeyboardSerializer() required LogicalKeyboardKey key,
}) = _KeyCombination;
factory KeyCombination.fromJson(Map<String, dynamic> json) => _$KeyCombinationFromJson(json);
@override
bool operator ==(covariant other) {
return other is KeyCombination && other.key.keyId == key.keyId && other.modifier?.keyId == modifier?.keyId;
}
@override
int get hashCode => key.hashCode ^ modifier.hashCode;
String get label => [modifier?.label, key.label].nonNulls.join(" + ");
static final Set<LogicalKeyboardKey> shiftKeys = {
LogicalKeyboardKey.shift,
LogicalKeyboardKey.shiftLeft,
LogicalKeyboardKey.shiftRight,
};
static final altKeys = {
LogicalKeyboardKey.alt,
LogicalKeyboardKey.altRight,
LogicalKeyboardKey.altLeft,
};
static final ctrlKeys = {
LogicalKeyboardKey.control,
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.controlRight,
};
static final modifierKeys = {
...shiftKeys,
...altKeys,
...ctrlKeys,
};
}
class LogicalKeyboardSerializer extends JsonConverter<LogicalKeyboardKey, String> {
const LogicalKeyboardSerializer();
@override
LogicalKeyboardKey fromJson(String json) {
return LogicalKeyboardKey.findKeyByKeyId(int.parse(jsonDecode(json))) ?? LogicalKeyboardKey.abort;
}
@override
String toJson(LogicalKeyboardKey object) {
return jsonEncode(object.keyId.toString());
}
}