feat(preferences): update preferences structure and enhance shortcut management

- Updated the preferences configuration with new shortcut definitions and types for better management.
- Introduced a method to get and subscribe to preference changes in the PreferenceService, improving reactivity.
- Refactored SelectionService to utilize the new preference management system, enhancing code clarity and maintainability.
- Updated SelectionAssistantSettings to use the new preference hooks for managing selection settings.
- Adjusted the generated preferences file to reflect the latest configuration changes.
This commit is contained in:
fullex
2025-08-15 12:54:24 +08:00
parent 30e6883333
commit e15005d1cf
5 changed files with 177 additions and 87 deletions
+16 -2
View File
@@ -94,6 +94,20 @@ export class PreferenceService {
return this.cache[key] ?? DefaultPreferences.default[key]
}
/**
* Get a single preference value from memory cache and subscribe to changes
* @param key - The preference key to get
* @param callback - The callback function to call when the preference changes
* @returns The current value of the preference
*/
public getAndSubscribeChange<K extends PreferenceKeyType>(
key: K,
callback: (newValue: PreferenceDefaultScopeType[K], oldValue?: PreferenceDefaultScopeType[K]) => void
): PreferenceDefaultScopeType[K] {
const value = this.get(key)
this.subscribeChange(key, callback)
return value
}
/**
* Set a single preference value
* Updates both database and memory cache, then broadcasts changes to all listeners
@@ -261,11 +275,11 @@ export class PreferenceService {
*/
public subscribeChange<K extends PreferenceKeyType>(
key: K,
callback: (newValue: PreferenceDefaultScopeType[K], oldValue: PreferenceDefaultScopeType[K]) => void
callback: (newValue: PreferenceDefaultScopeType[K], oldValue?: PreferenceDefaultScopeType[K]) => void
): () => void {
const listener = (changedKey: string, newValue: any, oldValue: any) => {
if (changedKey === key) {
callback(newValue, oldValue)
callback(newValue as PreferenceDefaultScopeType[K], oldValue as PreferenceDefaultScopeType[K])
}
}