Browse Source

Refactor StatsPanel

customisations
alemart 1 year ago
parent
commit
35ffe6ed76
1 changed files with 50 additions and 22 deletions
  1. 50
    22
      src/core/stats-panel.ts

+ 50
- 22
src/core/stats-panel.ts View File

33
 /** Icons for different power profiles */
33
 /** Icons for different power profiles */
34
 const POWER_ICON: { readonly [P in PowerPreference]: string } = Object.freeze({
34
 const POWER_ICON: { readonly [P in PowerPreference]: string } = Object.freeze({
35
     'default': '',
35
     'default': '',
36
-    'low-power': '<span style="color:#0f0">&#x1F50B</span>',
37
-    'high-performance': '<span style="color:#ff0">&#x26A1</span>'
36
+    'low-power': '&#x1F50B',
37
+    'high-performance': '&#x26A1'
38
 });
38
 });
39
 
39
 
40
 
40
 
112
      */
112
      */
113
     private _update(trackers: Tracker[], sources: Source[], fps: number, gpu: number): void
113
     private _update(trackers: Tracker[], sources: Source[], fps: number, gpu: number): void
114
     {
114
     {
115
-        const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
116
-        const sourceStats = sources.map(source => source._stats).join(', ');
117
-        const param = { // sanitzed
118
-            fps: this._colorize(fps),
119
-            gpu: this._colorize(gpu),
120
-            powerIcon: POWER_ICON[Settings.powerPreference]
121
-        };
122
-
123
-        this._container.textContent = (
124
-            `MARTINS.js v${Martins.version}
125
-            FPS: [fps] | GPU: [gpu] [powerIcon]
126
-            IN : ${sourceStats}
127
-            OUT: ${trackerStats}`
128
-        );
129
-
130
-        const fn = (_: string, x: 'fps' | 'gpu' | 'powerIcon'): string => param[x];
131
-        this._container.innerHTML = this._container.innerHTML.replace(/\[(\w+)\]/g, fn);
115
+        const get = (className: string) => this._container.getElementsByClassName(className) as HTMLCollectionOf<HTMLElement>;
116
+
117
+        // all sanitized
118
+        const lfps = get('_ar_fps');
119
+        if(lfps.length > 0) {
120
+            lfps[0].style.color = this._color(fps);
121
+            lfps[0].innerText = String(fps);
122
+        }
123
+
124
+        const lgpu = get('_ar_gpu');
125
+        if(lgpu.length > 0) {
126
+            lgpu[0].style.color = this._color(gpu);
127
+            lgpu[0].innerText = String(gpu);
128
+        }
129
+
130
+        const lpower = get('_ar_power');
131
+        if(lpower.length > 0)
132
+            lpower[0].innerHTML = POWER_ICON[Settings.powerPreference];
133
+
134
+        const lin = get('_ar_in');
135
+        if(lin.length > 0) {
136
+            const sourceStats = sources.map(source => source._stats).join(', ');
137
+            lin[0].innerText = sourceStats;
138
+        }
139
+
140
+        const lout = get('_ar_out');
141
+        if(lout.length > 0) {
142
+            const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
143
+            lout[0].innerText = trackerStats;
144
+        }
132
     }
145
     }
133
 
146
 
134
     /**
147
     /**
135
-     * Colorize a frequency number
148
+     * Associate a color to a frequency number
136
      * @param f frequency given in cycles per second
149
      * @param f frequency given in cycles per second
137
      * @returns colorized number (HTML)
150
      * @returns colorized number (HTML)
138
      */
151
      */
139
-    private _colorize(f: number): string
152
+    private _color(f: number): string
140
     {
153
     {
141
         const GREEN = '#0f0', YELLOW = '#ff0', RED = '#f33';
154
         const GREEN = '#0f0', YELLOW = '#ff0', RED = '#f33';
142
         const color3 = f >= 50 ? GREEN : (f >= 30 ? YELLOW : RED);
155
         const color3 = f >= 50 ? GREEN : (f >= 30 ? YELLOW : RED);
143
         const color2 = f >= 30 ? GREEN : RED;
156
         const color2 = f >= 30 ? GREEN : RED;
144
         const color = Settings.powerPreference != 'low-power' ? color3 : color2;
157
         const color = Settings.powerPreference != 'low-power' ? color3 : color2;
145
 
158
 
146
-        return `<span style="color:${color}">${Number(f)}</span>`;
159
+        return color;
147
     }
160
     }
148
 
161
 
149
     /**
162
     /**
154
     private _createContainer(parent: HTMLElement): HTMLDivElement
167
     private _createContainer(parent: HTMLElement): HTMLDivElement
155
     {
168
     {
156
         const container = document.createElement('div') as HTMLDivElement;
169
         const container = document.createElement('div') as HTMLDivElement;
170
+        const print = (html: string) => container.insertAdjacentHTML('beforeend', html);
157
 
171
 
158
         container.style.position = 'absolute';
172
         container.style.position = 'absolute';
159
         container.style.left = container.style.top = '0px';
173
         container.style.left = container.style.top = '0px';
165
         container.style.fontFamily = 'monospace';
179
         container.style.fontFamily = 'monospace';
166
         container.style.fontSize = '14px';
180
         container.style.fontSize = '14px';
167
 
181
 
182
+        // all sanitized
183
+        container.innerText = 'MARTINS.js ' + Martins.version;
184
+
185
+        print('<br>');
186
+        print('FPS: <span class="_ar_fps"></span> | ');
187
+        print('GPU: <span class="_ar_gpu"></span> ');
188
+        print('<span class="_ar_power"></span>');
189
+
190
+        print('<br>');
191
+        print('IN: <span class="_ar_in"></span>');
192
+
193
+        print('<br>');
194
+        print('OUT: <span class="_ar_out"></span>');
195
+
168
         parent.appendChild(container);
196
         parent.appendChild(container);
169
         return container;
197
         return container;
170
     }
198
     }

Loading…
Cancel
Save