| 21 | |
| 22 | @singleton() |
| 23 | export class SettingsController |
| 24 | extends Controller |
| 25 | implements ISettingsController |
| 26 | { |
| 27 | private readonly editorService: IEditorService; |
| 28 | private readonly settingsService: ISettingsService; |
| 29 | private readonly localeService: ILocaleService; |
| 30 | private readonly notificationService: INotificationService; |
| 31 | private readonly builtinService: IBuiltinService; |
| 32 | |
| 33 | constructor() { |
| 34 | super(); |
| 35 | this.editorService = container.resolve(EditorService); |
| 36 | this.settingsService = container.resolve(SettingsService); |
| 37 | this.localeService = container.resolve(LocaleService); |
| 38 | this.notificationService = container.resolve(NotificationService); |
| 39 | this.builtinService = container.resolve(BuiltinService); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Delay the each Settings change event 600 milliseconds, |
| 44 | * and then call the `update` and `emit` functions; |
| 45 | */ |
| 46 | private onChangeSettings = debounce((args) => { |
| 47 | this.settingsService.update(args); |
| 48 | this.emit(SettingsEvent.OnChange, args); |
| 49 | }, 600); |
| 50 | |
| 51 | public initView() { |
| 52 | const { SETTING_ID } = this.builtinService.getConstants(); |
| 53 | this.editorService.onUpdateTab((tab) => { |
| 54 | if (tab.id === SETTING_ID) { |
| 55 | const settingsValue = this.settingsService.normalizeFlatObject( |
| 56 | tab.data?.value || '' |
| 57 | ); |
| 58 | this.onChangeSettings(settingsValue); |
| 59 | } |
| 60 | }); |
| 61 | this.localeService.onChange((prev: ILocale, next: ILocale) => { |
| 62 | this.notifyLocaleChanged(prev, next); |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | private notifyLocaleChanged(prev: ILocale, next: ILocale) { |
| 67 | const { SETTING_ID } = this.builtinService.getConstants(); |
| 68 | const notify = { |
| 69 | id: SETTING_ID!, |
| 70 | value: next, |
| 71 | render(value) { |
| 72 | /* istanbul ignore next */ |
| 73 | return <LocaleNotification key={next.id} locale={next.name} />; |
| 74 | }, |
| 75 | }; |
| 76 | if (!this.notificationService.getState().showNotifications) { |
| 77 | this.notificationService.toggleNotification(); |
| 78 | } |
| 79 | this.notificationService.add([notify]); |
| 80 | } |