summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/reducers/ui.js
blob: bf03353bf352089345dde539a5c5a0c444331494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  CHANGE_DISPLAY_PIXEL_RATIO,
  CHANGE_USER_AGENT,
  TOGGLE_LEFT_ALIGNMENT,
  TOGGLE_RELOAD_ON_TOUCH_SIMULATION,
  TOGGLE_RELOAD_ON_USER_AGENT,
  TOGGLE_TOUCH_SIMULATION,
  TOGGLE_USER_AGENT_INPUT,
} = require("resource://devtools/client/responsive/actions/index.js");

const LEFT_ALIGNMENT_ENABLED = "devtools.responsive.leftAlignViewport.enabled";
const RELOAD_ON_TOUCH_SIMULATION =
  "devtools.responsive.reloadConditions.touchSimulation";
const RELOAD_ON_USER_AGENT = "devtools.responsive.reloadConditions.userAgent";
const SHOW_USER_AGENT_INPUT = "devtools.responsive.showUserAgentInput";
const TOUCH_SIMULATION_ENABLED = "devtools.responsive.touchSimulation.enabled";
const USER_AGENT = "devtools.responsive.userAgent";

const INITIAL_UI = {
  // The pixel ratio of the display.
  displayPixelRatio: 0,
  // Whether or not the viewports are left aligned.
  leftAlignmentEnabled: Services.prefs.getBoolPref(
    LEFT_ALIGNMENT_ENABLED,
    false
  ),
  // Whether or not to reload when touch simulation is toggled.
  reloadOnTouchSimulation: Services.prefs.getBoolPref(
    RELOAD_ON_TOUCH_SIMULATION,
    false
  ),
  // Whether or not to reload when user agent is changed.
  reloadOnUserAgent: Services.prefs.getBoolPref(RELOAD_ON_USER_AGENT, false),
  // Whether or not to show the user agent input in the toolbar.
  showUserAgentInput: Services.prefs.getBoolPref(SHOW_USER_AGENT_INPUT, false),
  // Whether or not touch simulation is enabled.
  touchSimulationEnabled: Services.prefs.getBoolPref(
    TOUCH_SIMULATION_ENABLED,
    false
  ),
  // The user agent of the viewport.
  userAgent: Services.prefs.getCharPref(USER_AGENT, ""),
};

const reducers = {
  [CHANGE_DISPLAY_PIXEL_RATIO](ui, { displayPixelRatio }) {
    return {
      ...ui,
      displayPixelRatio,
    };
  },

  [CHANGE_USER_AGENT](ui, { userAgent }) {
    Services.prefs.setCharPref(USER_AGENT, userAgent);

    return {
      ...ui,
      userAgent,
    };
  },

  [TOGGLE_LEFT_ALIGNMENT](ui, { enabled }) {
    const leftAlignmentEnabled =
      enabled !== undefined ? enabled : !ui.leftAlignmentEnabled;

    Services.prefs.setBoolPref(LEFT_ALIGNMENT_ENABLED, leftAlignmentEnabled);

    return {
      ...ui,
      leftAlignmentEnabled,
    };
  },

  [TOGGLE_RELOAD_ON_TOUCH_SIMULATION](ui, { enabled }) {
    const reloadOnTouchSimulation =
      enabled !== undefined ? enabled : !ui.reloadOnTouchSimulation;

    Services.prefs.setBoolPref(
      RELOAD_ON_TOUCH_SIMULATION,
      reloadOnTouchSimulation
    );

    return {
      ...ui,
      reloadOnTouchSimulation,
    };
  },

  [TOGGLE_RELOAD_ON_USER_AGENT](ui, { enabled }) {
    const reloadOnUserAgent =
      enabled !== undefined ? enabled : !ui.reloadOnUserAgent;

    Services.prefs.setBoolPref(RELOAD_ON_USER_AGENT, reloadOnUserAgent);

    return {
      ...ui,
      reloadOnUserAgent,
    };
  },

  [TOGGLE_TOUCH_SIMULATION](ui, { enabled }) {
    Services.prefs.setBoolPref(TOUCH_SIMULATION_ENABLED, enabled);

    return {
      ...ui,
      touchSimulationEnabled: enabled,
    };
  },

  [TOGGLE_USER_AGENT_INPUT](ui, { enabled }) {
    const showUserAgentInput =
      enabled !== undefined ? enabled : !ui.showUserAgentInput;

    Services.prefs.setBoolPref(SHOW_USER_AGENT_INPUT, showUserAgentInput);

    return {
      ...ui,
      showUserAgentInput,
    };
  },
};

module.exports = function (ui = INITIAL_UI, action) {
  const reducer = reducers[action.type];
  if (!reducer) {
    return ui;
  }
  return reducer(ui, action);
};