Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 1 | # Chromium DevTools UI Engineering |
| 2 | |
| 3 | ## Objective and scope |
| 4 | |
| 5 | This document defines how to build Chromium DevTools UI. It aims at improving consistency, maintainability and extensibility of the current code |
| 6 | |
| 7 | **Consistency** here means to have one way to do one thing, in the context of this doc to have a single reusable component per repeated task. |
| 8 | |
| 9 | **Maintainability** is addressed here through the [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) while avoiding unnecessary indirection. |
| 10 | |
| 11 | **Extensibility** requires the ease of understanding and imitation, i.e. being able to take an existing code, understand and use it as an example to solve another problem. |
| 12 | |
| 13 | Additionally, all the changes need to be applicable to the existing code **point-wise** instead of requiring extensive rewriting. |
| 14 | |
| 15 | ## Reusable web components |
| 16 | |
| 17 | Common UI primitives need to be implemented as web components. Examples include buttons, checkboxs, or data grids. Web components should not be used solely for encapsulation, we should not have single-use web components. |
| 18 | |
| 19 | Not all reusable code should be a web component: in the Application panel we have several similar views showing key-value pairs in a datagrid with a preview sidebar. Making this a truly reusable component will lead to an unjustifiable complexity. Instead we should have extracted a base class or helpers implementing the common functionality. |
| 20 | |
| 21 | In implementation we should prefer wrapping existing code under ui/legacy over new implementation. The former is the most feature-rich (including less obvious aspects like a11y) and has stood the test of time. |
| 22 | |
| 23 | We should however strive to expose a “HTML-native” API: e.g. toolbar doesn’t need an `items` setter if its child HTML elements could define its content. Even the data grid doesn’t need to expose data grid nodes, when `<tr>` and `<td>`’s are enough. |
| 24 | |
| 25 | ## Model-View-Presenter architecture |
| 26 | |
| 27 | We should strictly separate business logic, from UI logic and presentation. This means that most of the UI code should be centered around a presenter (subclass of a `UI.Widget`) that gets a view function injected. All the logic that is not related to the DevTools UI (i.e. that would stay the same if we rewrite DevTools as a command-line tool), should belong to the model layer. |
| 28 | |
| 29 | The presenter code should make no assumptions about the details of model or view code. It should not care what race conditions CDP exposes or how many layers of `<div class=”wrapper”>` does the markup have. However, for simplicity, the injected view function should have a default implementation inlined with a presenter (see an example below). |
| 30 | |
| 31 | In tests, we use a simple stub as a view function, which allows us to test the presenter logic without any DOM manipulation. To test the view function itself we should use screenshot and e2e tests. |
| 32 | |
| 33 | ## Declarative and orchestrated DOM updates |
| 34 | |
| 35 | We should no longer use imperative API to update DOM. Instead we rely on orchestrated rendering of lit-html templates. The view function described above should be a call to lit-html `render`. The view function should be called from `UI.Widget`’s `performUpdate` method, which by default is scheduled using `requestAnimationFrame`. |
| 36 | |
| 37 | To embed another presenter (`UI.Widget`) in the lit-html template, use `<devtools-widget .widgetConfig=${widgetConfig(<class>, {foo: 1, bar: 2})}` |
| 38 | |
| 39 | This will instantiate a `Widget` class with the web component as its `element` and, optionally, will set the properties provided in the second parameter. The widget won’t be re-instantiated on the subsequent template renders, but the properties would be updated. For this to work, the widget needs to accept `HTMLElement` as a sole constructor parameter and properties need to be public members or setters. |
| 40 | |
Simon Zünd | afade15 | 2025-05-06 11:13:38 | [diff] [blame] | 41 | For backwards compatibility, the first argument to `widgetConfig` can also be a factory function: `<devtools-widget .widgetConfig=${widgetConfig(element => new MyWidget(foo, bar, element))}>`. Similar to the class constructor version, `element` is the actual `<devtools-widget>` so the following two invocations of `widgetConfig` are equivalent: `widgetConfig(MyWidget)` and `widgetConfig(element = new MyWidget(element))`. |
| 42 | |
Ergun Erdogmus | a9f216e | 2025-07-15 13:42:34 | [diff] [blame] | 43 | ## Styling |
| 44 | To prevent style conflicts in widgets without relying on shadow DOM, we use the CSS [`@scope`](https://developer.mozilla.org/en-US/docs/Web/CSS/@scope) at-rule for style encapsulation. This ensures that styles defined for a widget do not leak out and affect other components. |
| 45 | |
| 46 | To simplify this process, a helper function, `UI.Widget.widgetScoped`, is provided. This function automatically wraps the given CSS rules in an `@scope to (devtools-widget)` block. The to (devtools-widget) part is crucial, as it establishes a "lower boundary," preventing the styles from cascading into any nested child widgets (which are rendered as <devtools-widget> elements). |
| 47 | |
| 48 | ```ts |
| 49 | import {html} from 'lit-html'; |
| 50 | import * as UI from '../../ui/legacy/legacy.js'; |
| 51 | import myWidgetStyles from './myWidget.css.js'; |
| 52 | |
| 53 | render(html` |
| 54 | <style> |
| 55 | ${UI.Widget.widgetScoped(myWidgetStyles)} |
| 56 | </style> |
| 57 | <div class="container"> |
| 58 | <h3 class="title">My Widget</h3> |
| 59 | ... |
| 60 | <devtools-widget .widgetConfig=${widgetConfig(NestedWidget)}></devtools-widget> |
| 61 | </div> |
| 62 | `, this.element); |
| 63 | ``` |
| 64 | |
| 65 | In this example, styles like `.title` will apply within the parent widget but will not apply to any elements inside the nested `<devtools-widget>`. |
| 66 | |
Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 67 | ## Examples |
| 68 | |
| 69 | ```html |
Alex Rudenko | 8d6d6c2 | 2025-02-27 15:36:40 | [diff] [blame] | 70 | <devtools-widget .widgetConfig=${widgetConfig(ElementsPanel)}> |
Wolfgang Beyer | c73e130 | 2025-05-14 15:57:14 | [diff] [blame] | 71 | <devtools-split-view> |
Alex Rudenko | 8d6d6c2 | 2025-02-27 15:36:40 | [diff] [blame] | 72 | <devtools-widget slot="main" .widgetConfig=${widgetConfig(ElementsTree)}></devtools-widget> |
Mathias Bynens | ae705ff | 2025-02-12 09:51:23 | [diff] [blame] | 73 | <devtools-tab-pane slot="sidebar"> |
Alex Rudenko | 8d6d6c2 | 2025-02-27 15:36:40 | [diff] [blame] | 74 | <devtools-widget .widgetConfig=${widgetConfig(StylesPane, {element: input.element})}></devtools-widget> |
| 75 | <devtools-widget .widgetConfig=${widgetConfig(ComputedPane, {element: input.element})}></devtools-widget> |
Mathias Bynens | ae705ff | 2025-02-12 09:51:23 | [diff] [blame] | 76 | ... |
Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 77 | </devtools-tab-pane> |
Wolfgang Beyer | c73e130 | 2025-05-14 15:57:14 | [diff] [blame] | 78 | </devtools-split-view> |
Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 79 | </devtools-widget> |
| 80 | ``` |
| 81 | |
| 82 | ```ts |
| 83 | class StylesPane extends UI.Widget { |
| 84 | constructor(element, view = (input, output, target) => { |
| 85 | render(html` |
Alex Rudenko | 8d6d6c2 | 2025-02-27 15:36:40 | [diff] [blame] | 86 | <devtools-widget .widgetConfig=${widgetConfig(MetricsPane, {element: input.element})}> |
Mathias Bynens | ae705ff | 2025-02-12 09:51:23 | [diff] [blame] | 87 | </devtools-widget> |
Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 88 | <devtools-toolbar> |
Mathias Bynens | ae705ff | 2025-02-12 09:51:23 | [diff] [blame] | 89 | <devtools-filter-input @change=${input.onFilter}></devtools-filter-input> |
| 90 | <devtools-checkbox @change=${input.onShowAll}>Show All</devtools-checkbox> |
| 91 | <devtools-checkbox @change=${input.onGroup}>Group</devtools-checkbox> |
Danil Somsikov | d884ae7 | 2025-02-12 07:56:13 | [diff] [blame] | 92 | </devtools-toolbar> |
| 93 | <devtools-tree-outline> |
| 94 | ${input.properties.map(p => html`<li> |
| 95 | <dt>${p.key}</dt><dd>${renderValue(p.value)}</dd> |
| 96 | <ol>${p.subproperties.map(...)} |
| 97 | </li>`)} |
| 98 | </devtools-tree-outline>` |
| 99 | } |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | [https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/protocol\_monitor/ProtocolMonitor.ts;l=197](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/protocol_monitor/ProtocolMonitor.ts;l=197) |
| 104 | |
| 105 | [https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/developer\_resources/DeveloperResourcesListView.ts;l=86](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/developer_resources/DeveloperResourcesListView.ts;l=86) |
| 106 | |
| 107 | [https://source.chromium.org/chromium/chromium/src/+/main:third\_party/devtools-frontend/src/front\_end/panels/timeline/TimelineSelectorStatsView.ts;l=113](https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/panels/timeline/TimelineSelectorStatsView.ts;l=113) |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 108 | |
| 109 | |
| 110 | ### Unit tests |
| 111 | |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 112 | When testing presenters, rely on observable effects such as view updates or model calls. |
| 113 | |
| 114 | #### View stubbing |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 115 | |
| 116 | ```ts |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 117 | // ✅ recommended: stub the view function using createViewFunctionStub. |
| 118 | import {createViewFunctionStub} from './ViewFunctionHelpers.js'; |
| 119 | const view = createViewFunctionStub(Presenter); |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 120 | const presenter = new Presenter(view); |
| 121 | |
| 122 | // ✅ recommended: expect a view stub call in response to presenter behavior. |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 123 | present.show(); |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 124 | const input = await view.nextInput; |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 125 | |
| 126 | // ✅ recommended: expect a view stub call in response to an event from the view. |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 127 | input.onEvent(); |
| 128 | assert.deepStrictEqual(await view.nextInput, {}); |
Alex Rudenko | c6499cc | 2025-02-17 09:08:25 | [diff] [blame] | 129 | |
| 130 | // ❌ not recommended: Widget.updateComplete only reports a current view update |
| 131 | // operation status and might create flakiness depending on doSomething() implementation. |
| 132 | presenter.doSomething(); |
| 133 | await presenter.updateComplete; |
| 134 | assert.deepStrictEqual(view.lastCall.args[0], {}); |
| 135 | |
| 136 | // ❌ not recommended: awaiting for the present logic to finish might |
| 137 | // not account for async or throttled view updates. |
| 138 | await presenter.doSomething(); |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 139 | // ❌ not recommended: it is easy for such assertions to |
| 140 | // rely on the data not caused by the action being tested. |
| 141 | sinon.assert.calledWith(view, sinon.match({ data: 'smth' })); |
| 142 | ``` |
| 143 | |
| 144 | #### Model stubbing |
| 145 | |
| 146 | ```ts |
| 147 | // ✅ recommended: stub models that the presenter relies on. |
| 148 | // Note there are many good ways to stub/mock models with sinon |
| 149 | // depending on the use case and existing model code structure. |
| 150 | const cssModel = sinon.createStubInstance(SDK.CSSModel.CSSModel); |
| 151 | |
| 152 | const presenter = new Presenter(); |
| 153 | // ✅ recommended: expect model calls as the result of invoking |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 154 | // presenter's logic. |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 155 | const modelCall = expectCall(cssModel.headersForSourceURL, { |
| 156 | fakeFn: () => { |
| 157 | return false, |
| 158 | }, |
| 159 | }); |
| 160 | // ✅ recommended: expect view calls to result in output based |
| 161 | // on the mocked model. |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 162 | const viewCall = view.nextInput; |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 163 | |
| 164 | presenter.doSomething(); |
| 165 | |
| 166 | // ✅ recommended: assert arguments provided to model calls. |
| 167 | const [url] = await modelCall; |
| 168 | assert.strictEqual(url, '...'); |
| 169 | |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 170 | assert.deepStrictEqual((await viewCall).headersForSourceURL, [{...}]); |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 171 | |
| 172 | // ❌ not recommended: mocking CDP responses to make the models behave in a certain way |
Alex Rudenko | 6f29fce | 2025-03-05 16:15:51 | [diff] [blame] | 173 | // while testing a presenter is fragile. |
Alex Rudenko | e5c358e | 2025-02-17 11:31:52 | [diff] [blame] | 174 | setMockConnectionResponseHandler('CSS.getHeaders', () => ({})); |
| 175 | const presenter = new Presenter(); |
| 176 | presenter.doSomething(); |
Simon Zünd | afade15 | 2025-05-06 11:13:38 | [diff] [blame] | 177 | ``` |