refactor(settings): remove dead shortcut preferences
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'dart:async' show unawaited;
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import '../models/hotkey_model.dart';
|
||||
@@ -17,8 +16,7 @@ class KeyboardShortcutsService extends ChangeNotifier {
|
||||
static KeyboardShortcutsService? _instance;
|
||||
late SettingsService _settingsService;
|
||||
final List<VoidCallback> _settingsDisposers = [];
|
||||
Map<String, String> _shortcuts = {}; // Legacy string shortcuts for backward compatibility
|
||||
Map<String, HotKey> _hotkeys = {}; // New HotKey objects
|
||||
Map<String, HotKey> _hotkeys = {};
|
||||
int _seekTimeSmall = 10; // Default, loaded from settings
|
||||
int _seekTimeLarge = 30; // Default, loaded from settings
|
||||
int _maxVolume = 100; // Default, loaded from settings (100-300%)
|
||||
@@ -52,7 +50,6 @@ class KeyboardShortcutsService extends ChangeNotifier {
|
||||
_settingsDisposers.add(() => notifier.removeListener(_onSettingsChanged));
|
||||
}
|
||||
|
||||
bind(SettingsService.keyboardShortcuts);
|
||||
bind(SettingsService.keyboardHotkeys);
|
||||
bind(SettingsService.seekTimeSmall);
|
||||
bind(SettingsService.seekTimeLarge);
|
||||
@@ -62,20 +59,17 @@ class KeyboardShortcutsService extends ChangeNotifier {
|
||||
void _onSettingsChanged() => _syncFromSettings();
|
||||
|
||||
void _syncFromSettings({bool notify = true}) {
|
||||
final shortcuts = _settingsService.read(SettingsService.keyboardShortcuts);
|
||||
final hotkeys = _settingsService.read(SettingsService.keyboardHotkeys);
|
||||
final seekTimeSmall = _settingsService.read(SettingsService.seekTimeSmall);
|
||||
final seekTimeLarge = _settingsService.read(SettingsService.seekTimeLarge);
|
||||
final maxVolume = _settingsService.read(SettingsService.maxVolume);
|
||||
|
||||
final changed =
|
||||
!mapEquals(_shortcuts, shortcuts) ||
|
||||
!_hotkeyMapsEqual(_hotkeys, hotkeys) ||
|
||||
_seekTimeSmall != seekTimeSmall ||
|
||||
_seekTimeLarge != seekTimeLarge ||
|
||||
_maxVolume != maxVolume;
|
||||
|
||||
_shortcuts = Map<String, String>.from(shortcuts);
|
||||
_hotkeys = Map<String, HotKey>.from(hotkeys);
|
||||
_seekTimeSmall = seekTimeSmall;
|
||||
_seekTimeLarge = seekTimeLarge;
|
||||
@@ -93,22 +87,13 @@ class KeyboardShortcutsService extends ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
|
||||
Map<String, String> get shortcuts => Map.from(_shortcuts);
|
||||
Map<String, HotKey> get hotkeys => Map.from(_hotkeys);
|
||||
int get maxVolume => _maxVolume;
|
||||
|
||||
String getShortcut(String action) {
|
||||
return _shortcuts[action] ?? '';
|
||||
}
|
||||
|
||||
HotKey? getHotkey(String action) {
|
||||
return _hotkeys[action];
|
||||
}
|
||||
|
||||
Future<void> setShortcut(String action, String key) async {
|
||||
await _settingsService.write(SettingsService.keyboardShortcuts, {..._shortcuts, action: key});
|
||||
}
|
||||
|
||||
Future<void> setHotkey(String action, HotKey hotkey) async {
|
||||
await _settingsService.write(SettingsService.keyboardHotkeys, {..._hotkeys, action: hotkey});
|
||||
}
|
||||
@@ -118,9 +103,7 @@ class KeyboardShortcutsService extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> resetToDefaults() async {
|
||||
final shortcuts = SettingsService.defaultKeyboardShortcuts();
|
||||
final hotkeys = SettingsService.defaultKeyboardHotkeys();
|
||||
await _settingsService.write(SettingsService.keyboardShortcuts, shortcuts);
|
||||
await _settingsService.write(SettingsService.keyboardHotkeys, hotkeys);
|
||||
}
|
||||
|
||||
|
||||
@@ -263,32 +263,6 @@ List<MpvPreset> _decodeMpvPresets(dynamic raw) {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Map<String, String> _defaultKeyboardShortcuts() => {
|
||||
'play_pause': 'Space',
|
||||
'volume_up': 'Arrow Up',
|
||||
'volume_down': 'Arrow Down',
|
||||
'seek_forward': 'Arrow Right',
|
||||
'seek_backward': 'Arrow Left',
|
||||
'seek_forward_large': 'Shift+Arrow Right',
|
||||
'seek_backward_large': 'Shift+Arrow Left',
|
||||
'fullscreen_toggle': 'F',
|
||||
'mute_toggle': 'M',
|
||||
'subtitle_toggle': 'S',
|
||||
'audio_track_next': 'A',
|
||||
'subtitle_track_next': 'Shift+S',
|
||||
'chapter_next': 'N',
|
||||
'chapter_previous': 'P',
|
||||
'speed_increase': 'Plus',
|
||||
'speed_decrease': 'Minus',
|
||||
'speed_reset': 'R',
|
||||
'zoom_in': 'Alt+Plus',
|
||||
'zoom_out': 'Alt+Minus',
|
||||
'zoom_reset': 'Alt+Backspace',
|
||||
'sub_seek_next': 'Ctrl+Right',
|
||||
'sub_seek_prev': 'Ctrl+Left',
|
||||
'screenshot': 'Ctrl+S',
|
||||
};
|
||||
|
||||
Map<String, HotKey> _defaultKeyboardHotkeys() => {
|
||||
'play_pause': const HotKey(key: PhysicalKeyboardKey.space),
|
||||
'volume_up': const HotKey(key: PhysicalKeyboardKey.arrowUp),
|
||||
@@ -319,12 +293,6 @@ Map<String, HotKey> _defaultKeyboardHotkeys() => {
|
||||
'screenshot': const HotKey(key: PhysicalKeyboardKey.keyS, modifiers: [HotKeyModifier.control]),
|
||||
};
|
||||
|
||||
Map<String, String> _decodeKeyboardShortcuts(dynamic raw) {
|
||||
final stored = (raw as Map<String, dynamic>).map((k, v) => MapEntry(k, v.toString()));
|
||||
// Merge with defaults so newly-added defaults appear without resetting customizations.
|
||||
return {..._defaultKeyboardShortcuts(), ...stored};
|
||||
}
|
||||
|
||||
Map<String, HotKey> _decodeKeyboardHotkeys(dynamic raw) {
|
||||
final result = <String, HotKey>{};
|
||||
for (final entry in (raw as Map<String, dynamic>).entries) {
|
||||
@@ -513,12 +481,6 @@ class SettingsService extends BaseSharedPreferencesService {
|
||||
);
|
||||
static const mpvConfigText = _MpvConfigTextPref();
|
||||
|
||||
static final keyboardShortcuts = JsonPref<Map<String, String>>(
|
||||
'keyboard_shortcuts',
|
||||
defaultValue: _defaultKeyboardShortcuts(),
|
||||
encode: json.encode,
|
||||
decode: _decodeKeyboardShortcuts,
|
||||
);
|
||||
static final keyboardHotkeys = JsonPref<Map<String, HotKey>>(
|
||||
'keyboard_hotkeys',
|
||||
defaultValue: _defaultKeyboardHotkeys(),
|
||||
@@ -628,7 +590,6 @@ class SettingsService extends BaseSharedPreferencesService {
|
||||
return (playerVolume: restoredVolume, persistedVolume: restoredVolume);
|
||||
}
|
||||
|
||||
static Map<String, String> defaultKeyboardShortcuts() => _defaultKeyboardShortcuts();
|
||||
static Map<String, HotKey> defaultKeyboardHotkeys() => _defaultKeyboardHotkeys();
|
||||
|
||||
/// Unknown libraries are allowed only when no filter is configured.
|
||||
@@ -896,7 +857,6 @@ class SettingsService extends BaseSharedPreferencesService {
|
||||
audioDownmixNormalize,
|
||||
downmixCenterBoost,
|
||||
themeMode,
|
||||
keyboardShortcuts,
|
||||
keyboardHotkeys,
|
||||
libraryDensity,
|
||||
episodePosterMode,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:plezy/models/hotkey_model.dart';
|
||||
import 'package:plezy/mpv/mpv.dart';
|
||||
import 'package:plezy/services/keyboard_shortcuts_service.dart';
|
||||
import 'package:plezy/services/settings_service.dart';
|
||||
@@ -14,6 +17,78 @@ void main() {
|
||||
SettingsService.resetForTesting();
|
||||
});
|
||||
|
||||
group('HotKey persistence', () {
|
||||
test('loads shortcuts saved with the shipped pre-HID key format', () async {
|
||||
resetSharedPreferencesForTest(
|
||||
initialAsync: {
|
||||
'keyboard_hotkeys': json.encode({
|
||||
'play_pause': {
|
||||
'key': 'PhysicalKeyboardKey#abcde(usbHidUsage: "0x00070013", debugName: "Key P")',
|
||||
'modifiers': ['control'],
|
||||
},
|
||||
}),
|
||||
},
|
||||
);
|
||||
SettingsService.resetForTesting();
|
||||
final service = await KeyboardShortcutsService.getInstance();
|
||||
addTearDown(service.dispose);
|
||||
|
||||
final hotkey = service.getHotkey('play_pause');
|
||||
expect(hotkey?.key, PhysicalKeyboardKey.keyP);
|
||||
expect(hotkey?.modifiers, [HotKeyModifier.control]);
|
||||
expect(service.getHotkey('volume_up')?.key, PhysicalKeyboardKey.arrowUp);
|
||||
});
|
||||
|
||||
test('saves shortcuts in the current HID format', () async {
|
||||
final service = await KeyboardShortcutsService.getInstance();
|
||||
addTearDown(service.dispose);
|
||||
|
||||
await service.setHotkey(
|
||||
'play_pause',
|
||||
const HotKey(key: PhysicalKeyboardKey.keyQ, modifiers: [HotKeyModifier.shift]),
|
||||
);
|
||||
|
||||
final stored =
|
||||
json.decode(SettingsService.instance.prefs.getString(SettingsService.keyboardHotkeys.key)!)
|
||||
as Map<String, dynamic>;
|
||||
expect(stored['play_pause'], {
|
||||
'key': '00070014',
|
||||
'modifiers': ['shift'],
|
||||
});
|
||||
expect(service.getHotkey('play_pause')?.key, PhysicalKeyboardKey.keyQ);
|
||||
expect(service.getHotkey('play_pause')?.modifiers, [HotKeyModifier.shift]);
|
||||
});
|
||||
|
||||
test('resets shortcuts to active defaults', () async {
|
||||
final service = await KeyboardShortcutsService.getInstance();
|
||||
addTearDown(service.dispose);
|
||||
await service.setHotkey(
|
||||
'play_pause',
|
||||
const HotKey(key: PhysicalKeyboardKey.f12, modifiers: [HotKeyModifier.alt]),
|
||||
);
|
||||
|
||||
await service.resetToDefaults();
|
||||
|
||||
expect(service.getHotkey('play_pause')?.key, PhysicalKeyboardKey.space);
|
||||
expect(service.getHotkey('play_pause')?.modifiers, isNull);
|
||||
final stored =
|
||||
json.decode(SettingsService.instance.prefs.getString(SettingsService.keyboardHotkeys.key)!)
|
||||
as Map<String, dynamic>;
|
||||
expect(stored['play_pause'], {'key': '0007002c', 'modifiers': <dynamic>[]});
|
||||
});
|
||||
|
||||
test('tracks resetAllSettings through the active preference listener', () async {
|
||||
final service = await KeyboardShortcutsService.getInstance();
|
||||
addTearDown(service.dispose);
|
||||
await service.setHotkey('play_pause', const HotKey(key: PhysicalKeyboardKey.f12));
|
||||
|
||||
await SettingsService.instance.resetAllSettings();
|
||||
|
||||
expect(service.getHotkey('play_pause')?.key, PhysicalKeyboardKey.space);
|
||||
expect(SettingsService.instance.prefs.containsKey(SettingsService.keyboardHotkeys.key), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('Ctrl+S takes a screenshot once while held', (tester) async {
|
||||
final service = await KeyboardShortcutsService.getInstance();
|
||||
addTearDown(service.dispose);
|
||||
|
||||
@@ -70,10 +70,8 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('SettingsService keyboard shortcut defaults', () {
|
||||
group('SettingsService keyboard hotkey defaults', () {
|
||||
test('includes Ctrl+S screenshot shortcut', () {
|
||||
expect(SettingsService.defaultKeyboardShortcuts()['screenshot'], 'Ctrl+S');
|
||||
|
||||
final hotkey = SettingsService.defaultKeyboardHotkeys()['screenshot'];
|
||||
expect(hotkey, isNotNull);
|
||||
expect(hotkey!.key, PhysicalKeyboardKey.keyS);
|
||||
|
||||
Reference in New Issue
Block a user