ui.js (3732B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 CHANGE_DISPLAY_PIXEL_RATIO, 9 CHANGE_USER_AGENT, 10 TOGGLE_LEFT_ALIGNMENT, 11 TOGGLE_RELOAD_ON_TOUCH_SIMULATION, 12 TOGGLE_RELOAD_ON_USER_AGENT, 13 TOGGLE_TOUCH_SIMULATION, 14 TOGGLE_USER_AGENT_INPUT, 15 } = require("resource://devtools/client/responsive/actions/index.js"); 16 17 const LEFT_ALIGNMENT_ENABLED = "devtools.responsive.leftAlignViewport.enabled"; 18 const RELOAD_ON_TOUCH_SIMULATION = 19 "devtools.responsive.reloadConditions.touchSimulation"; 20 const RELOAD_ON_USER_AGENT = "devtools.responsive.reloadConditions.userAgent"; 21 const SHOW_USER_AGENT_INPUT = "devtools.responsive.showUserAgentInput"; 22 const TOUCH_SIMULATION_ENABLED = "devtools.responsive.touchSimulation.enabled"; 23 const USER_AGENT = "devtools.responsive.userAgent"; 24 25 const INITIAL_UI = { 26 // The pixel ratio of the display. 27 displayPixelRatio: 0, 28 // Whether or not the viewports are left aligned. 29 leftAlignmentEnabled: Services.prefs.getBoolPref( 30 LEFT_ALIGNMENT_ENABLED, 31 false 32 ), 33 // Whether or not to reload when touch simulation is toggled. 34 reloadOnTouchSimulation: Services.prefs.getBoolPref( 35 RELOAD_ON_TOUCH_SIMULATION, 36 false 37 ), 38 // Whether or not to reload when user agent is changed. 39 reloadOnUserAgent: Services.prefs.getBoolPref(RELOAD_ON_USER_AGENT, false), 40 // Whether or not to show the user agent input in the toolbar. 41 showUserAgentInput: Services.prefs.getBoolPref(SHOW_USER_AGENT_INPUT, false), 42 // Whether or not touch simulation is enabled. 43 touchSimulationEnabled: Services.prefs.getBoolPref( 44 TOUCH_SIMULATION_ENABLED, 45 false 46 ), 47 // The user agent of the viewport. 48 userAgent: Services.prefs.getCharPref(USER_AGENT, ""), 49 }; 50 51 const reducers = { 52 [CHANGE_DISPLAY_PIXEL_RATIO](ui, { displayPixelRatio }) { 53 return { 54 ...ui, 55 displayPixelRatio, 56 }; 57 }, 58 59 [CHANGE_USER_AGENT](ui, { userAgent }) { 60 Services.prefs.setCharPref(USER_AGENT, userAgent); 61 62 return { 63 ...ui, 64 userAgent, 65 }; 66 }, 67 68 [TOGGLE_LEFT_ALIGNMENT](ui, { enabled }) { 69 const leftAlignmentEnabled = 70 enabled !== undefined ? enabled : !ui.leftAlignmentEnabled; 71 72 Services.prefs.setBoolPref(LEFT_ALIGNMENT_ENABLED, leftAlignmentEnabled); 73 74 return { 75 ...ui, 76 leftAlignmentEnabled, 77 }; 78 }, 79 80 [TOGGLE_RELOAD_ON_TOUCH_SIMULATION](ui, { enabled }) { 81 const reloadOnTouchSimulation = 82 enabled !== undefined ? enabled : !ui.reloadOnTouchSimulation; 83 84 Services.prefs.setBoolPref( 85 RELOAD_ON_TOUCH_SIMULATION, 86 reloadOnTouchSimulation 87 ); 88 89 return { 90 ...ui, 91 reloadOnTouchSimulation, 92 }; 93 }, 94 95 [TOGGLE_RELOAD_ON_USER_AGENT](ui, { enabled }) { 96 const reloadOnUserAgent = 97 enabled !== undefined ? enabled : !ui.reloadOnUserAgent; 98 99 Services.prefs.setBoolPref(RELOAD_ON_USER_AGENT, reloadOnUserAgent); 100 101 return { 102 ...ui, 103 reloadOnUserAgent, 104 }; 105 }, 106 107 [TOGGLE_TOUCH_SIMULATION](ui, { enabled }) { 108 Services.prefs.setBoolPref(TOUCH_SIMULATION_ENABLED, enabled); 109 110 return { 111 ...ui, 112 touchSimulationEnabled: enabled, 113 }; 114 }, 115 116 [TOGGLE_USER_AGENT_INPUT](ui, { enabled }) { 117 const showUserAgentInput = 118 enabled !== undefined ? enabled : !ui.showUserAgentInput; 119 120 Services.prefs.setBoolPref(SHOW_USER_AGENT_INPUT, showUserAgentInput); 121 122 return { 123 ...ui, 124 showUserAgentInput, 125 }; 126 }, 127 }; 128 129 module.exports = function (ui = INITIAL_UI, action) { 130 const reducer = reducers[action.type]; 131 if (!reducer) { 132 return ui; 133 } 134 return reducer(ui, action); 135 };