@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 9.76 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as SDK from"../../core/sdk/sdk.js";import*as IconButton from"../../ui/components/icon_button/icon_button.js";import*as UI from"../../ui/legacy/legacy.js";import{MobileThrottlingSelector}from"./MobileThrottlingSelector.js";import{NetworkThrottlingSelector}from"./NetworkThrottlingSelector.js";import{ThrottlingPresets}from"./ThrottlingPresets.js";const UIStrings={sS:"{PH1}: {PH2}",add:"Add…",addS:"Add {PH1}",offline:"Offline",forceDisconnectedFromNetwork:"Force disconnected from network",throttling:"Throttling",cpuThrottlingIsEnabled:"CPU throttling is enabled",cpuThrottling:"CPU throttling",noThrottling:"No throttling",dSlowdown:"{PH1}× slowdown",excessConcurrency:"Exceeding the default value may degrade system performance.",resetConcurrency:"Reset to the default value",hardwareConcurrency:"Hardware concurrency",hardwareConcurrencySettingTooltip:"Override the value reported by navigator.hardwareConcurrency on the page",hardwareConcurrencyIsEnabled:"Hardware concurrency override is enabled"},str_=i18n.i18n.registerUIStrings("panels/mobile_throttling/ThrottlingManager.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let throttlingManagerInstance,actionDelegateInstance;export class ThrottlingManager{cpuThrottlingControls;cpuThrottlingRates;customNetworkConditionsSetting;currentNetworkThrottlingConditionsSetting;lastNetworkThrottlingConditions;cpuThrottlingManager;#t=!1;get hardwareConcurrencyOverrideEnabled(){return this.#t}constructor(){this.cpuThrottlingManager=SDK.CPUThrottlingManager.CPUThrottlingManager.instance(),this.cpuThrottlingControls=new Set,this.cpuThrottlingRates=ThrottlingPresets.cpuThrottlingPresets,this.customNetworkConditionsSetting=Common.Settings.Settings.instance().moduleSetting("customNetworkConditions"),this.currentNetworkThrottlingConditionsSetting=Common.Settings.Settings.instance().createSetting("preferredNetworkCondition",SDK.NetworkManager.NoThrottlingConditions),this.currentNetworkThrottlingConditionsSetting.setSerializer(new SDK.NetworkManager.ConditionsSerializer),SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged,(()=>{this.lastNetworkThrottlingConditions=this.currentNetworkThrottlingConditionsSetting.get(),this.currentNetworkThrottlingConditionsSetting.set(SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions())})),this.isDirty()&&SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(this.currentNetworkThrottlingConditionsSetting.get())}static instance(t={forceNew:null}){const{forceNew:e}=t;return throttlingManagerInstance&&!e||(throttlingManagerInstance=new ThrottlingManager),throttlingManagerInstance}decorateSelectWithNetworkThrottling(t){let e=[];const n=new NetworkThrottlingSelector((function(n){t.removeChildren(),e=[];for(let r=0;r<n.length;++r){const o=n[r],i=t.createChild("optgroup");i.label=o.title;for(const t of o.items){const n="function"==typeof t.title?t.title():t.title,r=new Option(n,n);UI.ARIAUtils.setLabel(r,i18nString(UIStrings.sS,{PH1:o.title,PH2:n})),i.appendChild(r),e.push(t)}if(r===n.length-1){const t=new Option(i18nString(UIStrings.add),i18nString(UIStrings.add));UI.ARIAUtils.setLabel(t,i18nString(UIStrings.addS,{PH1:o.title})),i.appendChild(t),e.push(null)}}return e}),(function(e){t.selectedIndex!==e&&(t.selectedIndex=e)}),this.customNetworkConditionsSetting);return t.addEventListener("change",(function(){if(t.selectedIndex===t.options.length-1)n.revealAndUpdate();else{const r=e[t.selectedIndex];r&&n.optionSelected(r)}}),!1),n}createOfflineToolbarCheckbox(){const t=new UI.Toolbar.ToolbarCheckbox(i18nString(UIStrings.offline),i18nString(UIStrings.forceDisconnectedFromNetwork),function(){if(t.checked())SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.OfflineConditions);else{const t=this.lastNetworkThrottlingConditions.download||this.lastNetworkThrottlingConditions.upload?this.lastNetworkThrottlingConditions:SDK.NetworkManager.NoThrottlingConditions;SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(t)}}.bind(this));return SDK.NetworkManager.MultitargetNetworkManager.instance().addEventListener(SDK.NetworkManager.MultitargetNetworkManager.Events.ConditionsChanged,(function(){t.setChecked(SDK.NetworkManager.MultitargetNetworkManager.instance().isOffline())})),t.setChecked(SDK.NetworkManager.MultitargetNetworkManager.instance().isOffline()),t}createMobileThrottlingButton(){const t=new UI.Toolbar.ToolbarMenuButton((function(t){for(let o=0;o<e.length;++o){const i=e[o];i&&(i.title===ThrottlingPresets.getCustomConditions().title&&i.description===ThrottlingPresets.getCustomConditions().description||t.defaultSection().appendCheckboxItem(i.title,r.optionSelected.bind(r,i),n===o))}}));t.setTitle(i18nString(UIStrings.throttling)),t.setGlyph(""),t.turnIntoSelect(),t.setDarkText();let e=[],n=-1;const r=new MobileThrottlingSelector((function(t){e=[];for(const n of t){for(const t of n.items)e.push(t);e.push(null)}return e}),(function(r){n=r;const o=e[r];o&&(t.setText(o.title),t.setTitle(`${o.title} ${o.description}`))}));return t}updatePanelIcon(){const t=this.cpuThrottlingManager.cpuThrottlingRate();if(t===SDK.CPUThrottlingManager.CPUThrottlingRates.NoThrottling&&!this.hardwareConcurrencyOverrideEnabled)return void UI.InspectorView.InspectorView.instance().setPanelIcon("timeline",null);const e=new IconButton.Icon.Icon;e.data={iconName:"warning-filled",color:"var(--icon-warning)",width:"14px",height:"14px"};const n=[];t!==SDK.CPUThrottlingManager.CPUThrottlingRates.NoThrottling&&n.push(i18nString(UIStrings.cpuThrottlingIsEnabled)),this.hardwareConcurrencyOverrideEnabled&&n.push(i18nString(UIStrings.hardwareConcurrencyIsEnabled)),e.title=n.join("\n"),UI.InspectorView.InspectorView.instance().setPanelIcon("timeline",e)}setCPUThrottlingRate(t){this.cpuThrottlingManager.setCPUThrottlingRate(t),t!==SDK.CPUThrottlingManager.CPUThrottlingRates.NoThrottling&&Host.userMetrics.actionTaken(Host.UserMetrics.Action.CpuThrottlingEnabled);const e=this.cpuThrottlingRates.indexOf(t);for(const t of this.cpuThrottlingControls)t.setSelectedIndex(e);this.updatePanelIcon()}createCPUThrottlingSelector(){const t=new UI.Toolbar.ToolbarComboBox((t=>this.setCPUThrottlingRate(this.cpuThrottlingRates[t.target.selectedIndex])),i18nString(UIStrings.cpuThrottling));this.cpuThrottlingControls.add(t);const e=this.cpuThrottlingManager.cpuThrottlingRate();for(let n=0;n<this.cpuThrottlingRates.length;++n){const r=this.cpuThrottlingRates[n],o=1===r?i18nString(UIStrings.noThrottling):i18nString(UIStrings.dSlowdown,{PH1:r}),i=t.createOption(o);t.addOption(i),e===r&&t.setSelectedIndex(n)}return t}createHardwareConcurrencySelector(){const t=new UI.Toolbar.ToolbarItem(UI.UIUtils.createInput("devtools-text-input","number"));t.setTitle(i18nString(UIStrings.hardwareConcurrencySettingTooltip));const e=t.element;e.min="1",t.setEnabled(!1);const n=new UI.Toolbar.ToolbarCheckbox(i18nString(UIStrings.hardwareConcurrency),i18nString(UIStrings.hardwareConcurrencySettingTooltip)),r=new UI.Toolbar.ToolbarButton("Reset concurrency","undo");r.setTitle(i18nString(UIStrings.resetConcurrency));const o=new IconButton.Icon.Icon;o.data={iconName:"warning-filled",color:"var(--icon-warning)",width:"14px",height:"14px"};const i=new UI.Toolbar.ToolbarItem(o);return i.setTitle(i18nString(UIStrings.excessConcurrency)),n.inputElement.disabled=!0,r.element.classList.add("timeline-concurrency-hidden"),i.element.classList.add("timeline-concurrency-hidden"),this.cpuThrottlingManager.getHardwareConcurrency().then((o=>{if(void 0===o)return;const a=t=>{t>=1&&this.cpuThrottlingManager.setHardwareConcurrency(t),t>o?i.element.classList.remove("timeline-concurrency-hidden"):i.element.classList.add("timeline-concurrency-hidden"),t===o?r.element.classList.add("timeline-concurrency-hidden"):r.element.classList.remove("timeline-concurrency-hidden")};e.value=`${o}`,e.oninput=()=>a(Number(e.value)),n.inputElement.disabled=!1,n.inputElement.addEventListener("change",(()=>{this.#t=n.checked(),this.updatePanelIcon(),t.setEnabled(this.hardwareConcurrencyOverrideEnabled),a(this.hardwareConcurrencyOverrideEnabled?Number(e.value):o)})),r.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(()=>{e.value=`${o}`,a(o)}))})),{input:t,reset:r,warning:i,toggle:n}}setHardwareConcurrency(t){this.cpuThrottlingManager.setHardwareConcurrency(t)}isDirty(){const t=SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions(),e=this.currentNetworkThrottlingConditionsSetting.get();return!SDK.NetworkManager.networkConditionsEqual(t,e)}}export class ActionDelegate{static instance(t={forceNew:null}){const{forceNew:e}=t;return actionDelegateInstance&&!e||(actionDelegateInstance=new ActionDelegate),actionDelegateInstance}handleAction(t,e){return"network-conditions.network-online"===e?(SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.NoThrottlingConditions),!0):"network-conditions.network-low-end-mobile"===e?(SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.Slow3GConditions),!0):"network-conditions.network-mid-tier-mobile"===e?(SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.Fast3GConditions),!0):"network-conditions.network-offline"===e&&(SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(SDK.NetworkManager.OfflineConditions),!0)}}export function throttlingManager(){return ThrottlingManager.instance()}