From 50239c5756ab431003567d7efcdfd54804df6a57 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Thu, 10 Mar 2022 16:19:25 +0100 Subject: [PATCH] Add generic monitor settings storaging --- .../src/browser/monitor-model.ts | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 arduino-ide-extension/src/browser/monitor-model.ts diff --git a/arduino-ide-extension/src/browser/monitor-model.ts b/arduino-ide-extension/src/browser/monitor-model.ts new file mode 100644 index 00000000..5082e98a --- /dev/null +++ b/arduino-ide-extension/src/browser/monitor-model.ts @@ -0,0 +1,134 @@ +import { Emitter, Event } from "@theia/core"; +import { FrontendApplicationContribution, LocalStorageService } from "@theia/core/lib/browser"; +import { inject, injectable } from "@theia/core/shared/inversify"; + +@injectable() +export class MonitorModel implements FrontendApplicationContribution { + protected static STORAGE_ID = 'arduino-monitor-model'; + + @inject(LocalStorageService) + protected readonly localStorageService: LocalStorageService; + + protected readonly onChangeEmitter: Emitter>; + + protected _autoscroll: boolean; + protected _timestamp: boolean; + protected _lineEnding: MonitorModel.EOL; + protected _interpolate: boolean; + + constructor() { + this._autoscroll = true; + this._timestamp = false; + this._interpolate = false; + this._lineEnding = MonitorModel.EOL.DEFAULT; + + this.onChangeEmitter = new Emitter< + MonitorModel.State.Change + >(); + } + + onStart(): void { + this.localStorageService + .getData(MonitorModel.STORAGE_ID) + .then(this.restoreState); + } + + get onChange(): Event> { + return this.onChangeEmitter.event; + } + + protected restoreState(state: MonitorModel.State): void { + if (!state) { + return; + } + this._autoscroll = state.autoscroll; + this._timestamp = state.timestamp; + this._lineEnding = state.lineEnding; + this._interpolate = state.interpolate; + } + + protected async storeState(): Promise { + return this.localStorageService.setData(MonitorModel.STORAGE_ID, { + autoscroll: this._autoscroll, + timestamp: this._timestamp, + lineEnding: this._lineEnding, + interpolate: this._interpolate, + }); + } + + get autoscroll(): boolean { + return this._autoscroll; + } + + toggleAutoscroll(): void { + this._autoscroll = !this._autoscroll; + this.storeState().then(() => { + this.onChangeEmitter.fire({ + property: 'autoscroll', + value: this._timestamp + }); + }); + } + + get timestamp(): boolean { + return this._timestamp; + } + + toggleTimestamp(): void { + this._timestamp = !this._timestamp; + this.storeState().then(() => + this.onChangeEmitter.fire({ + property: 'timestamp', + value: this._timestamp, + }) + ); + } + + get lineEnding(): MonitorModel.EOL { + return this._lineEnding; + } + + set lineEnding(lineEnding: MonitorModel.EOL) { + this._lineEnding = lineEnding; + this.storeState().then(() => + this.onChangeEmitter.fire({ + property: 'lineEnding', + value: this._lineEnding, + }) + ); + } + + get interpolate(): boolean { + return this._interpolate; + } + + set interpolate(i: boolean) { + this._interpolate = i; + this.storeState().then(() => + this.onChangeEmitter.fire({ + property: 'interpolate', + value: this._interpolate, + }) + ); + } +} + +export namespace MonitorModel { + export interface State { + autoscroll: boolean; + timestamp: boolean; + lineEnding: EOL; + interpolate: boolean; + } + export namespace State { + export interface Change { + readonly property: K; + readonly value: State[K]; + } + } + + export type EOL = '' | '\n' | '\r' | '\r\n'; + export namespace EOL { + export const DEFAULT: EOL = '\n'; + } +}