selectedDevice: PropTypes.string.isRequired, viewportId: PropTypes.number.isRequired, }; } getMenuProps(device) { if (!device) { return { icon: null, label: null, tooltip: null }; } const { browser, os } = parseUserAgent(device.userAgent); let label = device.name; if (os) { label += ` ${os.name}`; if (os.version) { label += ` ${os.version}`; } } let icon = null; let tooltip = label; if (browser) { icon = `chrome://devtools/skin/images/browsers/${browser.name.toLowerCase()}.svg`; tooltip += ` ${browser.name} ${browser.version}`; } return { icon, label, tooltip }; } getSelectedDevice() { const { devices, selectedDevice } = this.props; if (!selectedDevice) { return null; } for (const type of devices.types) { for (const device of devices[type]) { if (selectedDevice === device.name) { return device; } } } return null; } renderMenuList() { const { devices, onChangeDevice, onUpdateDeviceModal, selectedDevice, viewportId, } = this.props; const menuItems = []; for (const type of devices.types) { for (const device of devices[type]) { if (device.displayed) { const { icon, label, tooltip } = this.getMenuProps(device); menuItems.push( MenuItem({ key: label, className: "device-selector-item", checked: selectedDevice === device.name, label, icon: icon || MenuItem.DUMMY_ICON, tooltip, onClick: () => onChangeDevice(viewportId, device, type), }) ); } } } menuItems.sort(function (a, b) { return a.props.label.localeCompare(b.props.label); }); if (menuItems.length) { menuItems.push(hr({ key: "separator" })); } menuItems.push( MenuItem({ key: "edit-device", label: getStr("responsive.editDeviceList2"), onClick: () => onUpdateDeviceModal(true, viewportId), }) ); return MenuList({}, menuItems); } render() { const { devices } = this.props; const selectedDevice = this.getSelectedDevice(); let { icon, label, tooltip } = this.getMenuProps(selectedDevice); if (!selectedDevice) { label = getStr("responsive.responsiveMode"); } // MenuButton is expected to be used in the toolbox document usually, // but since RDM's frame also loads theme-switching.js, we can create // MenuButtons (& HTMLTooltips) in the RDM frame document. const toolboxDoc = window.document; return MenuButton( { id: "device-selector", menuId: "device-selector-menu", toolboxDoc, className: "devtools-button devtools-dropdown-button", label, icon, title: tooltip, disabled: devices.listState !== Types.loadableState.LOADED, }, () => this.renderMenuList() ); } } module.exports = DeviceSelector; PK