Web Inspector: Styles Redesign: turn on CSS spreadsheet editor by default
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Base / Setting.js
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 WI.Setting = class Setting extends WI.Object
33 {
34     constructor(name, defaultValue)
35     {
36         super();
37
38         this._name = name;
39
40         let inspectionLevel = InspectorFrontendHost ? InspectorFrontendHost.inspectionLevel() : 1;
41         let levelString = inspectionLevel > 1 ? "-" + inspectionLevel : "";
42         this._localStorageKey = `com.apple.WebInspector${levelString}.${name}`;
43         this._defaultValue = defaultValue;
44     }
45
46     // Public
47
48     get name()
49     {
50         return this._name;
51     }
52
53     get value()
54     {
55         if ("_value" in this)
56             return this._value;
57
58         // Make a copy of the default value so changes to object values don't modify the default value.
59         this._value = JSON.parse(JSON.stringify(this._defaultValue));
60
61         if (!window.InspectorTest && window.localStorage && this._localStorageKey in window.localStorage) {
62             try {
63                 this._value = JSON.parse(window.localStorage[this._localStorageKey]);
64             } catch {
65                 delete window.localStorage[this._localStorageKey];
66             }
67         }
68
69         return this._value;
70     }
71
72     set value(value)
73     {
74         if (this._value === value)
75             return;
76
77         this._value = value;
78
79         if (!window.InspectorTest && window.localStorage) {
80             try {
81                 // Use Object.shallowEqual to properly compare objects.
82                 if (Object.shallowEqual(this._value, this._defaultValue))
83                     delete window.localStorage[this._localStorageKey];
84                 else
85                     window.localStorage[this._localStorageKey] = JSON.stringify(this._value);
86             } catch {
87                 console.error("Error saving setting with name: " + this._name);
88             }
89         }
90
91         this.dispatchEventToListeners(WI.Setting.Event.Changed, this._value, {name: this._name});
92     }
93
94     reset()
95     {
96         // Make a copy of the default value so changes to object values don't modify the default value.
97         this.value = JSON.parse(JSON.stringify(this._defaultValue));
98     }
99 };
100
101 WI.Setting.Event = {
102     Changed: "setting-changed"
103 };
104
105 WI.settings = {
106     autoLogProtocolMessages: new WI.Setting("auto-collect-protocol-messages", false),
107     autoLogTimeStats: new WI.Setting("auto-collect-time-stats", false),
108     enableUncaughtExceptionReporter: new WI.Setting("enable-uncaught-exception-reporter", true),
109     enableLayoutFlashing: new WI.Setting("enable-layout-flashing", false),
110     enableLineWrapping: new WI.Setting("enable-line-wrapping", false),
111     indentUnit: new WI.Setting("indent-unit", 4),
112     tabSize: new WI.Setting("tab-size", 4),
113     indentWithTabs: new WI.Setting("indent-with-tabs", false),
114     showWhitespaceCharacters: new WI.Setting("show-whitespace-characters", false),
115     showInvalidCharacters: new WI.Setting("show-invalid-characters", false),
116     clearLogOnNavigate: new WI.Setting("clear-log-on-navigate", true),
117     clearNetworkOnNavigate: new WI.Setting("clear-network-on-navigate", true),
118     zoomFactor: new WI.Setting("zoom-factor", 1),
119     layoutDirection: new WI.Setting("layout-direction-override", "system"),
120     showScopeChainOnPause: new WI.Setting("show-scope-chain-sidebar", true),
121     showImageGrid: new WI.Setting("show-image-grid", false),
122     showCanvasPath: new WI.Setting("show-canvas-path", false),
123     selectedNetworkDetailContentViewIdentifier: new WI.Setting("network-detail-content-view-identifier", "preview"),
124
125     // Experimental
126     experimentalLegacyStyleEditor: new WI.Setting("experimental-legacy-style-editor", false),
127     experimentalEnableLayersTab: new WI.Setting("experimental-enable-layers-tab", false),
128 };