File: filter-ui.js

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (485 lines) | stat: -rw-r--r-- 16,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const MDCMenu = mdc.menu.MDCMenu;
const MDCMenuFoundation = mdc.menu.MDCMenuFoundation;
const maxZIndex = 999;

class FilterUI extends HTMLElement {
  constructor() {
    super();
  }

  connectedCallback() {
    this.innerHTML = `
<style>
.row {
  display: flex;
  justify-content: space-between;
  padding-bottom: 10px;
}

#filter-container {
  max-width: 400px;
  min-width: 350px;
  font-family: Roboto;
  font-size: 10pt;
  background-color: white;
  padding: 10px;
}

#saveFilter {
  margin: 20px 10px;
  margin-right: 0px;
}

</style>
<div id='filter-container'>
  <div class='sectionTitle' id='filter-ui-title'>Add Filter</div>
  <div class='section'>
    <div class='row'>
      <div class='label' title='Filter annotation to match.
      For example frame.root.damage' >Annotation </div>
      <div class='input'>
        <input list="knownAnnotations" placeholder='Substring to match'
         id='annotation' size=40>
        <!-- TODO: A fancy drop-down here would be nice. -->
      </div>
    </div>
    <div class='row'>
      <div class='label'>File name</div>
      <div class='input'>
        <input placeholder='Substring to match (usually empty!)'
         id='filename' size=40>
        <!-- TODO: A fancy drop-down here would be nice. -->
      </div>
    </div>
    <div class='row'>
      <div class='label'>Function</div>
      <div class='input'>
        <input placeholder='Substring to match (usually empty!)'
         id='functionname' size=40>
        <!-- TODO: A fancy drop-down here would be nice. -->
      </div>
    </div>


  </div>
  <div style='display: flex'>
    <div style='flex-grow: 1'>
      <div class='sectionTitle'>Action</div>
      <div class='section'>
        <div>
          <form id="actionform">
            <input type='radio' id='drawfilter' name='dowhat' value='drawfilter'/>Override
            <input type='color' id='drawcolor' name='drawcolor' value='#000000'/>
            opacity <input type='range' name='fillalpha' min='0' max='100'
             step='10' value='50' list='alphastep'/>
            <datalist id='alphastep'>
              <option>0</option><option>10</option><option>20</option>
              <option>30</option><option>40</option><option>50</option>
              <option>60</option> <option>70</option><option>80</option>
              <option>90</option><option>100</option>
            </datalist>
            <input checked type='radio' name='dowhat' value='drawcaller'/>
            Draw with caller color and opacity
            <br/>
            <input type='radio' name='dowhat' value='skip'
            title='Used as a discard filter'/>Do not draw
            <br/>
          </form>
        </div>
      </div>
    </div>
    <button id='saveFilter'>Save</button>
  </div>
<div>
`;
    this.setUpButtons_();

    // Event listener to check if custom color selected
    // to automatically select override button
    document.getElementById('drawcolor').addEventListener("change",function(){
      document.getElementById('drawfilter').checked = true;
    });
  }

  setUpButtons_() {
    const SaveFilter = () => {
      let input = this.querySelector('#filename');
      const filename = input.value || undefined;
      input = this.querySelector('#functionname');
      const func = input.value || undefined;
      input = this.querySelector('#annotation');
      const anno = input.value || undefined;

      input = this.querySelector('input[name="dowhat"]:checked');
      const action = { skipDraw: input.value === 'skip' };
      if (input.value === 'drawfilter') {
        action.color = this.querySelector('input[name="drawcolor"]').value;
        action.alpha = this.querySelector('input[name="fillalpha"]').value;
      }

      this.dispatchEvent(new CustomEvent('saveFilter', {
        detail: { selector: { filename, func, anno }, action }
      }));
    };

    this.querySelector('#saveFilter').addEventListener('click', SaveFilter);
    this.querySelector('#filter-container').addEventListener('keypress',
      (e) => { if (e.key === 'Enter') SaveFilter(); });
  }
};

window.customElements.define('filter-ui', FilterUI);

// A <datalist> element containing values of previously seen filter annotations.
let dataListElement = undefined;
let knownAnnotations = new Set();
// Called when processing new sources from a frame.
function notifyUiOfNewSource(source) {
  if (dataListElement === undefined) {
    dataListElement = document.createElement("datalist");
    dataListElement.id = "knownAnnotations";
    document.body.appendChild(dataListElement);
  }

  if (!knownAnnotations.has(source.anno)) {
    let option = document.createElement("option");
    option.value = source.anno;
    dataListElement.appendChild(option);

    knownAnnotations.add(source.anno);
  }
}

function createFilterChip(filter) {
  const chip = document.createElement('div');
  chip.className = "mdc-chip";
  chip.setAttribute("role", "row");
  chip.style.margin = '5px';
  chip.style.borderRadius = '0px';
  chip.style.zIndex = maxZIndex;
  if (filter.shouldDraw) {
    chip.style.border = filter.drawColor ?
      `2px solid ${filter.drawColor}` : `1px solid black`;
    if (filter.fillAlpha > 0) {
      var alpha = Math.min(.6, parseFloat(filter.fillAlpha) / 100);
      var rgba = filter.drawColor + DrawCall.alphaFloatToHex(alpha);
      chip.style.backgroundColor = rgba;
      chip.style.fontWeight = 'bold';
    }
    else {
      chip.style.backgroundColor = `white`;
    }
  }
  else {
    chip.style.border = `1px dashed grey`;
    chip.style.backgroundColor = `white`;
  }

  chip.innerHTML =
    `
  <input checked type="checkbox"/>
  <span role="gridcell">
  <span class="mdc-chip__text" id="filterstring"></span>
  </span>
  <span role="gridcell">
    <div class="mdc-menu-surface--anchor">
    <i class="material-icons-outlined mdc-chip__icon mdc-chip__icon--trailing"
       tabindex="-1" role="button" id="filtermenu">more_vert</i>
      <div id="filterchipmenu" class="mdc-menu mdc-menu-surface">
        <ul class="mdc-list" role="menu" aria-hidden="true"
         aria-orientation="vertical" tabindex="-1">
          <li class="mdc-list-item" role="menuitem"
          id="filter-moveprev" onclick="movePrev(this)">
            <span class="mdc-list-item__ripple"></span>
            <span class="mdc-list-item__text">
            <i class="material-icons-outlined">arrow_back_ios</i>Move Previous
            </span>
          </li>
          <li class="mdc-list-item" role="menuitem"
          id="filter-movenext" onclick="moveNext(this)">
            <span class="mdc-list-item__ripple"></span>
            <span class="mdc-list-item__text">
            <i class="material-icons-outlined">arrow_forward_ios</i> Move Next
            </span>
          </li>
          <li class="mdc-list-item"
           role="menuitem" id="filter-edit" onclick="showEditFilterPopup(this)">
            <span class="mdc-list-item__ripple"></span>
            <span class="mdc-list-item__text">
            <i class="material-icons-outlined">edit</i> Edit</span>
          </li>
          <li class="mdc-list-item" role="menuitem"
           id="filter-delete" onclick="deleteFilter(this)">
            <span class="mdc-list-item__ripple"></span>
            <span class="mdc-list-item__text">
            <i class="material-icons-outlined">delete</i> Delete</span>
          </li>
        </ul>
      </div>
    </div>
  </span>
  `;

  chip.querySelector('#filterstring').innerHTML = filter.createUIString();

  chip.querySelector('#filtermenu').addEventListener('click', () => {
    const menu = new MDCMenu(chip.querySelector('#filterchipmenu'));
    menu.setAnchorMargin({ top: 25 });
    menu.open = true;
  });

  const check = chip.querySelector('input');
  check.checked = filter.enabled;
  check.addEventListener('change', () => {
    filter.enabled = !!check.checked;
    locallyStoreFilters();
    Player.instance.refresh();
    Filter.sendStreamFilters();
  });

  return chip;
}

function showCreateFilterPopup(anchor) {
  const filterUi = document.createElement('filter-ui');
  filterUi.addEventListener('saveFilter', (event) => {
    if (event.detail.selector && event.detail.action) {
      const filter =
        new Filter(true, event.detail.selector, event.detail.action);
      const chip = createFilterChip(filter);
      const list = document.querySelector('#filters');
      list.appendChild(chip);
      hideModal();
      refreshFilterSet();
    }
  });
  filterUi.style.position = 'absolute';
  filterUi.style.top = (anchor.offsetTop + anchor.offsetHeight) + 'px';
  filterUi.style.left = (anchor.offsetLeft + 20) + 'px';
  filterUi.style.zIndex = maxZIndex;

  showModal(filterUi, '#annotation');
}

// Traverses through all the filter chips and enables/disables
// "move next/prev" button clickability depending on if
// the chip is at the beginning/middle/end of list
function refreshFilterSet() {
  const list = document.querySelector('#filters');
  for (var chip = list.firstElementChild;
    chip !== null; chip = chip.nextSibling) {
    if (chip === list.firstElementChild) {
      chip.querySelector('#filter-moveprev')
        .classList.add("mdc-list-item--disabled");
    }
    else {
      chip.querySelector('#filter-moveprev')
        .classList.remove("mdc-list-item--disabled");
    }
    if (chip.nextSibling === null) {
      chip.querySelector('#filter-movenext')
        .classList.add("mdc-list-item--disabled");
    }
    else {
      chip.querySelector('#filter-movenext')
        .classList.remove("mdc-list-item--disabled");
    }
  }
  locallyStoreFilters();
  Player.instance.refresh();
  Filter.sendStreamFilters();
}

function movePrev(item) {
  if (item.classList.contains("mdc-list-item--disabled")) {
    return;
  }
  var chip = item.closest(".mdc-chip");
  const menu = new MDCMenu(chip.querySelector('#filterchipmenu'));
  menu.open = false;
  var index = Array.prototype.indexOf.call(chip.parentNode.children, chip);
  Filter.swapFilters(index - 1, index);
  var prevChip = chip.previousElementSibling;
  chip.parentNode.insertBefore(chip, prevChip);
  refreshFilterSet();
}

function moveNext(item) {
  if (item.classList.contains("mdc-list-item--disabled")) {
    return;
  }
  var chip = item.closest(".mdc-chip");
  const menu = new MDCMenu(chip.querySelector('#filterchipmenu'));
  menu.open = false;
  var index = Array.prototype.indexOf.call(chip.parentNode.children, chip);
  Filter.swapFilters(index, index + 1);
  var nextChip = chip.nextElementSibling;
  chip.parentNode.insertBefore(nextChip, chip);
  refreshFilterSet();
}

function createFilterComplete(enabled, selector, action, index) {
  var newFilter =
    new Filter(enabled, selector, action, index);
  const newChip = createFilterChip(newFilter);
  const list = document.querySelector('#filters');
  list.appendChild(newChip);
  hideModal();
  refreshFilterSet();
  return newChip;
}

function showEditFilterPopup(item) {
  var chip = item.closest(".mdc-chip");
  var index = Array.prototype.indexOf.call(chip.parentNode.children, chip);
  var filter = Filter.getFilter(index);

  const menu = new MDCMenu(chip.querySelector('#filterchipmenu'));
  menu.open = false;

  const filterUi = document.createElement('filter-ui');
  const isEnabled = filter.enabled;
  filterUi.addEventListener('saveFilter', (event) => {
    if (event.detail.selector && event.detail.action) {
      var newChip = createFilterComplete(isEnabled, event.detail.selector,
        event.detail.action, index);
      chip.replaceWith(newChip);
    }
  });
  filterUi.style.position = 'absolute';
  filterUi.style.top = (chip.offsetTop + chip.offsetHeight) + 'px';
  filterUi.style.left = (chip.offsetLeft + 20) + 'px';
  filterUi.style.zIndex = maxZIndex;

  showModal(filterUi, '#annotation');

  // fill form from filter data
  filterUi.querySelector('#filter-ui-title').innerHTML = "Edit Filter";
  filterUi.querySelector('#filename').value = filter.file;
  filterUi.querySelector('#functionname').value = filter.func;
  filterUi.querySelector('#annotation').value = filter.anno;

  var actionform = filterUi.querySelector('#actionform');
  actionform.dowhat.value = !filter.shouldDraw ? 'skip' :
    filter.drawColor ? 'drawfilter' : 'drawcaller';
  actionform.drawcolor.value = filter.drawColor;
  actionform.fillalpha.value = filter.fillAlpha;
}

function deleteFilter(item) {
  var chip = item.closest(".mdc-chip");
  const menu = new MDCMenu(chip.querySelector('#filterchipmenu'));
  menu.open = false;
  var index = Array.prototype.indexOf.call(chip.parentNode.children, chip);
  Filter.deleteFilter(index);
  chip.parentNode.removeChild(chip);
  refreshFilterSet();
}

// Stores filter instances in local storage
function locallyStoreFilters() {
  var filterInstances = Filter.instances;
  // Store string representation of filterInstances list in local storage.
  localStorage.setItem('filterInstances', JSON.stringify(filterInstances));
}

// Restores filter instances from local storage.
function restoreFilters() {
  const retrievedFilterString = localStorage.getItem('filterInstances');
  console.log(" Filter string=" + retrievedFilterString);
  // Add default filters to the instances list.
  FilterUIDefault.initialize();

  if (!retrievedFilterString) {
    return;
  }

  var retrievedFilterInstances = JSON.parse(retrievedFilterString);
  // Remove any default filter duplicates from restored filters.
  for (const defaultFilter of defaultFilters) {
    retrievedFilterInstances = retrievedFilterInstances.filter
        (instance => !isDuplicate(instance, defaultFilter));
  }
  // Re-create non-default filter chips from local storage.
  // Pre-existing filters are appended behind the default ones.
  retrievedFilterInstances.forEach((instance) =>
    createFilterComplete(instance.enabled_,
      instance.selector_, instance.action_));
}

// Checks if one filter is a duplicate of another.
// NOTE: Custom equality check needed to avoid marking same style
// of filters with different user states and indices as non-duplicates.
function isDuplicate(filter1, filter2) {
  if (!filter1 || !filter2) {
    return false;
  }
  if (filter1.selector_.filename !== filter2.selector_.filename) {
    return false;
  }
  if (filter1.selector_.func !== filter2.selector_.func) {
    return false;
  }
  if (filter1.selector_.anno !== filter2.selector_.anno) {
    return false;
  }

  return true;
}

const defaultFilters = [
    {
      selector_: { filename: "", func: "", anno: "frame.render_pass.meta" },
      action_: { skipDraw: false },
      enabled_: false
    },
    {
      selector_: { filename: "", func: "", anno: "frame.render_pass.quad" },
      action_: { skipDraw: false, color: '#000000', alpha: "10" },
      enabled_: true
    },
    {
      selector_: { filename: "", func: "", anno: "frame.render_pass.damage" },
      action_: { skipDraw: false, color: '#FF0000', alpha: "20" },
      enabled_: true
    },
    {
      selector_: {
        filename: "",
        func: "",
        anno: "frame.render_pass.output_rect",
      },
      action_: { skipDraw: false },
      enabled_: false,
    },
    {
      selector_: { filename: "", func: "", anno: "overlay.selected.rect" },
      action_: { skipDraw: false, color: '#22FF22', alpha: "20" },
      enabled_: true
    },
    {
      selector_: { filename: "", func: "", anno: "overlay.outgoing.damage" },
      action_: { skipDraw: false, color: '#AA00AA', alpha: "20" },
      enabled_: true
    },
    {
      selector_: { filename: "", func: "", anno: "frame.render_pass.material" },
      action_: { skipDraw: false },
      enabled_: false
    }
];


// Default filters should probably load off disk.
const FilterUIDefault = {
  initialize() {
    defaultFilters.forEach((instance) =>
      createFilterComplete(instance.enabled_,
                 instance.selector_, instance.action_))
  }
};