Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 30 | |
Jan Scheffler | d8380b6 | 2020-07-28 16:14:15 | [diff] [blame] | 31 | |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 32 | import * as Common from '../common/common.js'; |
| 33 | import * as Components from '../components/components.js'; |
| 34 | import * as Host from '../host/host.js'; |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 35 | import * as i18n from '../i18n/i18n.js'; |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 36 | import * as Root from '../root/root.js'; |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 37 | import * as UI from '../ui/ui.js'; |
| 38 | |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 39 | import {KeybindsSettingsTab} from './KeybindsSettingsTab.js'; // eslint-disable-line no-unused-vars |
| 40 | |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 41 | export const UIStrings = { |
| 42 | /** |
| 43 | *@description Name of the Settings view |
| 44 | */ |
| 45 | settings: 'Settings', |
| 46 | /** |
| 47 | *@description Text for keyboard shortcuts |
| 48 | */ |
| 49 | shortcuts: 'Shortcuts', |
| 50 | /** |
| 51 | *@description Text in Settings Screen of the Settings |
| 52 | */ |
| 53 | preferences: 'Preferences', |
| 54 | /** |
| 55 | *@description Text of button in Settings Screen of the Settings |
| 56 | */ |
| 57 | restoreDefaultsAndReload: 'Restore defaults and reload', |
| 58 | /** |
| 59 | *@description Text in Settings Screen of the Settings |
| 60 | */ |
| 61 | experiments: 'Experiments', |
| 62 | /** |
| 63 | *@description Message shown in the experiments panel to warn users about any possible unstable features. |
| 64 | */ |
| 65 | theseExperimentsCouldBeUnstable: |
| 66 | 'These experiments could be unstable or unreliable and may require you to restart DevTools.', |
| 67 | /** |
| 68 | *@description Message text content in Settings Screen of the Settings |
| 69 | */ |
| 70 | theseExperimentsAreParticularly: 'These experiments are particularly unstable. Enable at your own risk.', |
| 71 | /** |
| 72 | *@description Warning text content in Settings Screen of the Settings |
| 73 | */ |
| 74 | warning: 'WARNING:', |
| 75 | /** |
| 76 | *@description Message to display if a setting change requires a reload of DevTools |
| 77 | */ |
| 78 | oneOrMoreSettingsHaveChanged: 'One or more settings have changed which requires a reload to take effect.', |
| 79 | }; |
| 80 | const str_ = i18n.i18n.registerUIStrings('settings/SettingsScreen.js', UIStrings); |
| 81 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 82 | |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 83 | /** @type {!SettingsScreen} */ |
| 84 | let settingsScreenInstance; |
| 85 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | /** |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 87 | * @implements {UI.View.ViewLocationResolver} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | * @unrestricted |
| 89 | */ |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 90 | export class SettingsScreen extends UI.Widget.VBox { |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 91 | /** |
| 92 | * @private |
| 93 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | constructor() { |
| 95 | super(true); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 96 | this.registerRequiredCSS('settings/settingsScreen.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | this.contentElement.classList.add('settings-window-main'); |
| 99 | this.contentElement.classList.add('vbox'); |
| 100 | |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 101 | const settingsLabelElement = document.createElement('div'); |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 102 | const settingsTitleElement = |
| 103 | UI.Utils.createShadowRootWithCoreStyles(settingsLabelElement, 'settings/settingsScreen.css') |
| 104 | .createChild('div', 'settings-window-title'); |
John Emau | b970e56 | 2019-06-05 01:17:27 | [diff] [blame] | 105 | |
| 106 | UI.ARIAUtils.markAsHeading(settingsTitleElement, 1); |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 107 | settingsTitleElement.textContent = i18nString(UIStrings.settings); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | |
Paul Lewis | 75c7d0d | 2020-03-19 12:17:26 | [diff] [blame] | 109 | this._tabbedLocation = UI.ViewManager.ViewManager.instance().createTabbedLocation( |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 110 | () => SettingsScreen._revealSettingsScreen(), 'settings-view'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 111 | const tabbedPane = this._tabbedLocation.tabbedPane(); |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 112 | tabbedPane.leftToolbar().appendToolbarItem(new UI.Toolbar.ToolbarItem(settingsLabelElement)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | tabbedPane.setShrinkableTabs(false); |
| 114 | tabbedPane.makeVerticalTabLayout(); |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 115 | const keyBindsView = UI.ViewManager.ViewManager.instance().view('keybinds'); |
| 116 | if (keyBindsView) { |
| 117 | keyBindsView.widget().then(widget => { |
| 118 | this._keybindsTab = /** @type {!KeybindsSettingsTab} */ (widget); |
| 119 | }); |
| 120 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 121 | tabbedPane.show(this.contentElement); |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 122 | tabbedPane.selectTab('preferences'); |
| 123 | tabbedPane.addEventListener(UI.TabbedPane.Events.TabInvoked, this._tabInvoked, this); |
| 124 | this._reportTabOnReveal = false; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 128 | * @param {{forceNew: ?boolean}} opts |
| 129 | */ |
| 130 | static instance(opts = {forceNew: null}) { |
| 131 | const {forceNew} = opts; |
| 132 | if (!settingsScreenInstance || forceNew) { |
| 133 | settingsScreenInstance = new SettingsScreen(); |
| 134 | } |
| 135 | |
| 136 | return settingsScreenInstance; |
| 137 | } |
| 138 | |
| 139 | /** |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 140 | * @return {!SettingsScreen} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 141 | */ |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 142 | static _revealSettingsScreen() { |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 143 | const settingsScreen = SettingsScreen.instance(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 144 | if (settingsScreen.isShowing()) { |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 145 | return settingsScreen; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 146 | } |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 147 | |
| 148 | settingsScreen._reportTabOnReveal = true; |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 149 | const dialog = new UI.Dialog.Dialog(); |
Chandani Shrestha | 08469b8 | 2019-10-02 17:22:55 | [diff] [blame] | 150 | dialog.contentElement.tabIndex = -1; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | dialog.addCloseButton(); |
Brian Cui | 8fdb148 | 2019-12-04 21:41:46 | [diff] [blame] | 152 | dialog.setOutsideClickCallback(() => {}); |
| 153 | dialog.setPointerEventsBehavior(UI.GlassPane.PointerEventsBehavior.PierceGlassPane); |
| 154 | dialog.setOutsideTabIndexBehavior(UI.Dialog.OutsideTabIndexBehavior.PreserveMainViewTabIndex); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 155 | settingsScreen.show(dialog.contentElement); |
Jack Lynch | ca3683d | 2020-09-30 22:54:57 | [diff] [blame] | 156 | dialog.setEscapeKeyCallback(settingsScreen._onEscapeKeyPressed.bind(settingsScreen)); |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 157 | |
| 158 | // UI.Dialog extends GlassPane and overrides the `show` method with a wider |
| 159 | // accepted type. However, TypeScript uses the supertype declaration to |
| 160 | // determine the full type, which requires a `!Document`. |
| 161 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | dialog.show(); |
Jack Lynch | 85edfc8 | 2020-03-09 18:11:13 | [diff] [blame] | 163 | |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 164 | return settingsScreen; |
| 165 | } |
| 166 | |
| 167 | /** |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 168 | * @param {!ShowSettingsScreenOptions=} options |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 169 | */ |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 170 | static async _showSettingsScreen(options = {name: undefined, focusTabHeader: undefined}) { |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 171 | const {name, focusTabHeader} = options; |
| 172 | const settingsScreen = SettingsScreen._revealSettingsScreen(); |
| 173 | |
| 174 | settingsScreen._selectTab(name || 'preferences'); |
Brian Cui | 55d00af | 2020-04-09 18:45:40 | [diff] [blame] | 175 | const tabbedPane = settingsScreen._tabbedLocation.tabbedPane(); |
| 176 | await tabbedPane.waitForTabElementUpdate(); |
Jack Lynch | 85edfc8 | 2020-03-09 18:11:13 | [diff] [blame] | 177 | if (focusTabHeader) { |
Jack Lynch | 85edfc8 | 2020-03-09 18:11:13 | [diff] [blame] | 178 | tabbedPane.focusSelectedTabHeader(); |
Brian Cui | 55d00af | 2020-04-09 18:45:40 | [diff] [blame] | 179 | } else { |
| 180 | tabbedPane.focus(); |
Jack Lynch | 85edfc8 | 2020-03-09 18:11:13 | [diff] [blame] | 181 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @override |
| 186 | * @param {string} locationName |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 187 | * @return {?UI.View.ViewLocation} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 188 | */ |
| 189 | resolveLocation(locationName) { |
| 190 | return this._tabbedLocation; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param {string} name |
| 195 | */ |
| 196 | _selectTab(name) { |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 197 | this._tabbedLocation.tabbedPane().selectTab(name, /* userGesture */ true); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @param {!Common.EventTarget.EventTargetEvent} event |
| 202 | */ |
| 203 | _tabInvoked(event) { |
| 204 | const eventData = /** @type {!UI.TabbedPane.EventData} */ (event.data); |
| 205 | if (!eventData.isUserGesture) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | const prevTabId = eventData.prevTabId; |
| 210 | const tabId = eventData.tabId; |
| 211 | if (!this._reportTabOnReveal && prevTabId && prevTabId === tabId) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | this._reportTabOnReveal = false; |
| 216 | this._reportSettingsPanelShown(tabId); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @param {string} tabId |
| 221 | */ |
| 222 | _reportSettingsPanelShown(tabId) { |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 223 | if (tabId === i18nString(UIStrings.shortcuts)) { |
Brian Cui | 4fef3cd | 2020-02-11 01:05:55 | [diff] [blame] | 224 | Host.userMetrics.settingsPanelShown('shortcuts'); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | Host.userMetrics.settingsPanelShown(tabId); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 229 | } |
Jack Lynch | ca3683d | 2020-09-30 22:54:57 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * @param {!Event} event |
| 233 | */ |
| 234 | _onEscapeKeyPressed(event) { |
| 235 | if (this._tabbedLocation.tabbedPane().selectedTabId === 'keybinds' && this._keybindsTab) { |
| 236 | this._keybindsTab.onEscapeKeyPressed(event); |
| 237 | } |
| 238 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 239 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 240 | |
| 241 | /** |
| 242 | * @unrestricted |
| 243 | */ |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 244 | class SettingsTab extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 245 | /** |
| 246 | * @param {string} name |
| 247 | * @param {string=} id |
| 248 | */ |
| 249 | constructor(name, id) { |
| 250 | super(); |
| 251 | this.element.classList.add('settings-tab-container'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 252 | if (id) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | this.element.id = id; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 254 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 255 | const header = this.element.createChild('header'); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 256 | UI.UIUtils.createTextChild(header.createChild('h1'), name); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | this.containerElement = this.element.createChild('div', 'settings-container-wrapper') |
| 258 | .createChild('div', 'settings-tab settings-content settings-container'); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @param {string=} name |
| 263 | * @return {!Element} |
| 264 | */ |
| 265 | _appendSection(name) { |
| 266 | const block = this.containerElement.createChild('div', 'settings-block'); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 267 | if (name) { |
| 268 | UI.ARIAUtils.markAsGroup(block); |
| 269 | const title = block.createChild('div', 'settings-section-title'); |
| 270 | title.textContent = name; |
| 271 | UI.ARIAUtils.markAsHeading(title, 2); |
Joel Einbinder | eaef616 | 2019-07-15 17:42:55 | [diff] [blame] | 272 | UI.ARIAUtils.setAccessibleName(block, name); |
Chandani Shrestha | 83bd7c9 | 2019-06-11 21:21:59 | [diff] [blame] | 273 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 274 | return block; |
| 275 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 276 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * @unrestricted |
| 280 | */ |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 281 | export class GenericSettingsTab extends SettingsTab { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 282 | constructor() { |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 283 | super(i18nString(UIStrings.preferences), 'preferences-tab-content'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 284 | |
| 285 | /** @const */ |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 286 | const explicitSectionOrder = [ |
| 287 | '', 'Appearance', 'Sources', 'Elements', 'Network', 'Performance', 'Console', 'Extensions', 'Persistence', |
| 288 | 'Debugger', 'Global' |
| 289 | ]; |
Brandon Goddard | 80d3818 | 2020-06-12 15:33:25 | [diff] [blame] | 290 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 291 | /** @type {!Map<string, !Element>} */ |
| 292 | this._nameToSection = new Map(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 293 | for (const sectionName of explicitSectionOrder) { |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 294 | this._createSectionElement(sectionName); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 295 | } |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 296 | Root.Runtime.Runtime.instance().extensions('setting').forEach(this._addSetting.bind(this)); |
| 297 | Root.Runtime.Runtime.instance().extensions(UI.SettingsUI.SettingUI).forEach(this._addSettingUI.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 298 | |
| 299 | this._appendSection().appendChild( |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 300 | UI.UIUtils.createTextButton(i18nString(UIStrings.restoreDefaultsAndReload), restoreAndReload)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 301 | |
| 302 | function restoreAndReload() { |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 303 | Common.Settings.Settings.instance().clearAll(); |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 304 | Components.Reload.reload(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 309 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 310 | * @return {boolean} |
| 311 | */ |
| 312 | static isSettingVisible(extension) { |
| 313 | const descriptor = extension.descriptor(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 314 | if (!('title' in descriptor)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 316 | } |
| 317 | if (!('category' in descriptor)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 318 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 319 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 320 | return true; |
| 321 | } |
| 322 | |
| 323 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 324 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 325 | */ |
| 326 | _addSetting(extension) { |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 327 | if (!GenericSettingsTab.isSettingVisible(extension)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 328 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 329 | } |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 330 | const extensionCategory = extension.descriptor()['category']; |
| 331 | if (!extensionCategory) { |
| 332 | return; |
| 333 | } |
| 334 | const sectionElement = this._sectionElement(extensionCategory); |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 335 | if (!sectionElement) { |
| 336 | return; |
| 337 | } |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 338 | const setting = Common.Settings.Settings.instance().moduleSetting(extension.descriptor()['settingName']); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 339 | const settingControl = UI.SettingsUI.createControlForSetting(setting); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 340 | if (settingControl) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 341 | sectionElement.appendChild(settingControl); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 342 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 346 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 347 | */ |
| 348 | _addSettingUI(extension) { |
| 349 | const descriptor = extension.descriptor(); |
| 350 | const sectionName = descriptor['category'] || ''; |
| 351 | extension.instance().then(appendCustomSetting.bind(this)); |
| 352 | |
| 353 | /** |
| 354 | * @param {!Object} object |
Tim van der Lippe | 1ebfc50 | 2020-01-15 13:45:09 | [diff] [blame] | 355 | * @this {GenericSettingsTab} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 356 | */ |
| 357 | function appendCustomSetting(object) { |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 358 | const settingUI = /** @type {!UI.SettingsUI.SettingUI} */ (object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 359 | const element = settingUI.settingElement(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 360 | if (element) { |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 361 | let sectionElement = this._sectionElement(sectionName); |
| 362 | if (!sectionElement) { |
| 363 | sectionElement = this._createSectionElement(sectionName); |
| 364 | } |
| 365 | sectionElement.appendChild(element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 366 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * @param {string} sectionName |
| 372 | * @return {!Element} |
| 373 | */ |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 374 | _createSectionElement(sectionName) { |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 375 | const uiSectionName = sectionName && i18nString(sectionName); |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 376 | const sectionElement = this._appendSection(uiSectionName); |
| 377 | this._nameToSection.set(sectionName, sectionElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 378 | return sectionElement; |
| 379 | } |
Alex Rudenko | 4bcabeb | 2020-05-06 08:43:56 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * @param {string} sectionName |
| 383 | * @return {?Element} |
| 384 | */ |
| 385 | _sectionElement(sectionName) { |
| 386 | return this._nameToSection.get(sectionName) || null; |
| 387 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 388 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * @unrestricted |
| 392 | */ |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 393 | export class ExperimentsSettingsTab extends SettingsTab { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 394 | constructor() { |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 395 | super(i18nString(UIStrings.experiments), 'experiments-tab-content'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 396 | |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 397 | const experiments = Root.Runtime.experiments.allConfigurableExperiments().sort(); |
| 398 | const unstableExperiments = experiments.filter(e => e.unstable); |
| 399 | const stableExperiments = experiments.filter(e => !e.unstable); |
| 400 | if (stableExperiments.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 401 | const experimentsSection = this._appendSection(); |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 402 | const warningMessage = i18nString(UIStrings.theseExperimentsCouldBeUnstable); |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 403 | experimentsSection.appendChild(this._createExperimentsWarningSubsection(warningMessage)); |
| 404 | for (const experiment of stableExperiments) { |
| 405 | experimentsSection.appendChild(this._createExperimentCheckbox(experiment)); |
| 406 | } |
| 407 | } |
| 408 | if (unstableExperiments.length) { |
| 409 | const experimentsSection = this._appendSection(); |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 410 | const warningMessage = i18nString(UIStrings.theseExperimentsAreParticularly); |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 411 | experimentsSection.appendChild(this._createExperimentsWarningSubsection(warningMessage)); |
| 412 | for (const experiment of unstableExperiments) { |
| 413 | experimentsSection.appendChild(this._createExperimentCheckbox(experiment)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 414 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 419 | * @param {string} warningMessage |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 420 | * @return {!Element} element |
| 421 | */ |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 422 | _createExperimentsWarningSubsection(warningMessage) { |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 423 | const subsection = document.createElement('div'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 424 | const warning = subsection.createChild('span', 'settings-experiments-warning-subsection-warning'); |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 425 | warning.textContent = i18nString(UIStrings.warning); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 426 | UI.UIUtils.createTextChild(subsection, ' '); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 427 | const message = subsection.createChild('span', 'settings-experiments-warning-subsection-message'); |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 428 | message.textContent = warningMessage; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 429 | return subsection; |
| 430 | } |
| 431 | |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 432 | /** |
| 433 | * @param {*} experiment |
| 434 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 435 | _createExperimentCheckbox(experiment) { |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 436 | const label = UI.UIUtils.CheckboxLabel.create(i18nString(experiment.title), experiment.isEnabled()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 437 | const input = label.checkboxElement; |
| 438 | input.name = experiment.name; |
| 439 | function listener() { |
| 440 | experiment.setEnabled(input.checked); |
Brandon Goddard | 413d172 | 2020-08-10 17:51:26 | [diff] [blame] | 441 | Host.userMetrics.experimentChanged(experiment.name, experiment.isEnabled()); |
Rob Paveza | c32568d | 2020-07-13 16:18:18 | [diff] [blame] | 442 | UI.InspectorView.InspectorView.instance().displayReloadRequiredWarning( |
vidorteg | c1aeb63 | 2020-10-22 02:59:33 | [diff] [blame] | 443 | i18nString(UIStrings.oneOrMoreSettingsHaveChanged)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 444 | } |
| 445 | input.addEventListener('click', listener, false); |
| 446 | |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 447 | const p = document.createElement('p'); |
Yang Guo | 33e8f6f | 2020-02-06 11:50:29 | [diff] [blame] | 448 | p.className = experiment.unstable && !experiment.isEnabled() ? 'settings-experiment-unstable' : ''; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 449 | p.appendChild(label); |
| 450 | return p; |
| 451 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 452 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 453 | |
| 454 | /** |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 455 | * @implements {UI.ActionDelegate.ActionDelegate} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | * @unrestricted |
| 457 | */ |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 458 | export class ActionDelegate { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 459 | /** |
| 460 | * @override |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 461 | * @param {!UI.Context.Context} context |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 462 | * @param {string} actionId |
| 463 | * @return {boolean} |
| 464 | */ |
| 465 | handleAction(context, actionId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 466 | switch (actionId) { |
| 467 | case 'settings.show': |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 468 | SettingsScreen._showSettingsScreen(/** @type {!ShowSettingsScreenOptions}*/ ({focusTabHeader: true})); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | return true; |
| 470 | case 'settings.documentation': |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 471 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab( |
Wolfgang Beyer | 6190ec8 | 2020-03-09 15:06:33 | [diff] [blame] | 472 | UI.UIUtils.addReferrerToURL('https://developers.google.com/web/tools/chrome-devtools/')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 473 | return true; |
| 474 | case 'settings.shortcuts': |
Jack Lynch | d77d4ca | 2020-10-16 18:27:15 | [diff] [blame] | 475 | SettingsScreen._showSettingsScreen({name: 'keybinds', focusTabHeader: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | return false; |
| 479 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 480 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 481 | |
| 482 | /** |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 483 | * @implements {Common.Revealer.Revealer} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 484 | * @unrestricted |
| 485 | */ |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 486 | export class Revealer { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 487 | /** |
| 488 | * @override |
| 489 | * @param {!Object} object |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 490 | * @return {!Promise<void>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 491 | */ |
| 492 | reveal(object) { |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 493 | console.assert(object instanceof Common.Settings.Setting); |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 494 | const setting = /** @type {!Common.Settings.Setting<*>} */ (object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 495 | let success = false; |
| 496 | |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 497 | Root.Runtime.Runtime.instance().extensions('setting').forEach(revealModuleSetting); |
| 498 | Root.Runtime.Runtime.instance().extensions(UI.SettingsUI.SettingUI).forEach(revealSettingUI); |
| 499 | Root.Runtime.Runtime.instance().extensions('view').forEach(revealSettingsView); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 500 | |
| 501 | return success ? Promise.resolve() : Promise.reject(); |
| 502 | |
| 503 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 504 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 505 | */ |
| 506 | function revealModuleSetting(extension) { |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 507 | if (!GenericSettingsTab.isSettingVisible(extension)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 508 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 509 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 510 | if (extension.descriptor()['settingName'] === setting.name) { |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 511 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.bringToFront(); |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 512 | SettingsScreen._showSettingsScreen(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | success = true; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 518 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 519 | */ |
| 520 | function revealSettingUI(extension) { |
| 521 | const settings = extension.descriptor()['settings']; |
| 522 | if (settings && settings.indexOf(setting.name) !== -1) { |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 523 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.bringToFront(); |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 524 | SettingsScreen._showSettingsScreen(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 525 | success = true; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 530 | * @param {!Root.Runtime.Extension} extension |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 531 | */ |
| 532 | function revealSettingsView(extension) { |
| 533 | const location = extension.descriptor()['location']; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 534 | if (location !== 'settings-view') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 535 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 536 | } |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 537 | const descriptor = extension.descriptor(); |
| 538 | const settings = descriptor['settings']; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 539 | if (settings && settings.indexOf(setting.name) !== -1) { |
Tim van der Lippe | 66684e9 | 2020-01-24 14:01:18 | [diff] [blame] | 540 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.bringToFront(); |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 541 | SettingsScreen._showSettingsScreen( |
| 542 | /** @type {!ShowSettingsScreenOptions}*/ ({name: extension.descriptor()['id']})); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 543 | success = true; |
| 544 | } |
| 545 | } |
| 546 | } |
Paul Lewis | 5d4133e | 2019-11-27 13:06:01 | [diff] [blame] | 547 | } |
Andres Olivares | c51c18a | 2020-10-21 02:48:44 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * @typedef {{ |
| 551 | * name: (string|undefined), |
| 552 | * focusTabHeader: (boolean|undefined) |
| 553 | * }} |
| 554 | */ |
| 555 | // @ts-ignore typedef |
| 556 | export let ShowSettingsScreenOptions; |