-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathmain.js
More file actions
197 lines (173 loc) · 8.11 KB
/
main.js
File metadata and controls
197 lines (173 loc) · 8.11 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2015 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global Phoenix*/
define(function (require, exports, module) {
const AppInit = require("utils/AppInit"),
Menus = require("command/Menus"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
Strings = require("strings"),
PreferencesManager = require("preferences/PreferencesManager"),
ViewUtils = require("utils/ViewUtils"),
KeyBindingManager = require("command/KeyBindingManager"),
Metrics = require("utils/Metrics"),
WorkspaceManager = require("view/WorkspaceManager");
// Constants
const PREFS_PURE_CODE = "noDistractions",
CMD_TOGGLE_PURE_CODE = "view.togglePureCode",
CMD_TOGGLE_FULLSCREEN = "view.toggleFullscreen",
CMD_TOGGLE_PANELS = "view.togglePanels";
//key binding keys
const togglePureCodeKey = "Shift-F11",
toggleFullScreenKey = "F11",
toggleFullScreenKeyMac = "Cmd-F11",
togglePanelsKey = "Ctrl-Shift-1",
togglePanelsKeyMac = "Cmd-Shift-1",
togglePanelsKey_EN = "Ctrl-Shift-~",
togglePanelsKeyMac_EN = "Cmd-Shift-~";
//locals
let _previouslyOpenPanelIDs = [],
panelsToggled = false,
layoutUpdated = false;
/**
* @private
* Updates the command checked status based on the preference for noDestraction mode
*/
function _updateCheckedState() {
CommandManager.get(CMD_TOGGLE_PURE_CODE).setChecked(PreferencesManager.get(PREFS_PURE_CODE));
Phoenix.app.isFullscreen().then(isFullScreen =>{
CommandManager.get(CMD_TOGGLE_FULLSCREEN).setChecked(isFullScreen);
});
}
/**
* @private
* toggles noDisraction preference
*/
function _togglePureCode() {
PreferencesManager.set(PREFS_PURE_CODE, !PreferencesManager.get(PREFS_PURE_CODE));
Metrics.countEvent(Metrics.EVENT_TYPE.UI, 'noDistractions', 'toggle');
}
async function _toggleFullScreen() {
Metrics.countEvent(Metrics.EVENT_TYPE.UI, 'fullscreen', 'toggle');
Phoenix.app.isFullscreen().then(isFullScreen =>{
Phoenix.app.setFullscreen(!isFullScreen)
.then(_updateCheckedState);
});
}
/**
* hide all open panels
*/
function _hidePanelsIfRequired() {
_previouslyOpenPanelIDs = [];
// Batch-hide all open bottom panel tabs in one pass, avoiding O(n)
// intermediate tab activations and layout recalcs.
let hiddenBottomPanels = WorkspaceManager.hideAllOpenBottomPanels();
_previouslyOpenPanelIDs = hiddenBottomPanels;
// Hide any remaining visible panels (e.g. plugin side-panels)
let panelIDs = WorkspaceManager.getAllPanelIDs();
for (let i = 0; i < panelIDs.length; i++) {
let panel = WorkspaceManager.getPanelForID(panelIDs[i]);
if (panel && panel.isVisible()) {
panel.hide();
_previouslyOpenPanelIDs.push(panelIDs[i]);
}
}
}
/**
* show all open panels that was previously hidden by _hidePanelsIfRequired()
*/
function _showPanelsIfRequired() {
var panelIDs = _previouslyOpenPanelIDs;
panelIDs.forEach(function (panelID) {
var panel = WorkspaceManager.getPanelForID(panelID);
if (panel) {
panel.show();
}
});
_previouslyOpenPanelIDs = [];
}
function _updateLayout() {
layoutUpdated = true;
panelsToggled = false;
}
/**
* We toggle panels in certain cases only :
* 1. if a panel is shown, toggle can hide it, and successive toggle can show the panel and repeat.
* 2. if a panel is hidden by toggle, and say the workspace changed making another panel visible by some operation;
* we reset toggle states so that toggle would hide the panel already present in the workspace.
* The already hidden panel should not be shown in the specific case for better UX.
*/
function _togglePanels() {
panelsToggled = !panelsToggled;
if (panelsToggled) {
_hidePanelsIfRequired();
layoutUpdated = false;
panelsToggled = true;
} else if (!layoutUpdated) {
_showPanelsIfRequired();
}
Metrics.countEvent(Metrics.EVENT_TYPE.UI, 'noDistractions', 'togglePanels');
}
PreferencesManager.definePreference(PREFS_PURE_CODE, "boolean", false, {
description: Strings.DESCRIPTION_PURE_CODING_SURFACE
});
WorkspaceManager.on(WorkspaceManager.EVENT_WORKSPACE_PANEL_SHOWN, _updateLayout);
/**
* Register the Commands , add the Menu Items and key bindings
*/
AppInit.appReady(function () {
CommandManager.register(Strings.CMD_TOGGLE_PURE_CODE, CMD_TOGGLE_PURE_CODE, _togglePureCode);
CommandManager.register(Strings.CMD_TOGGLE_FULLSCREEN, CMD_TOGGLE_FULLSCREEN, _toggleFullScreen);
CommandManager.register(Strings.CMD_TOGGLE_PANELS, CMD_TOGGLE_PANELS, _togglePanels);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(CMD_TOGGLE_PANELS, "", Menus.AFTER, Commands.VIEW_HIDE_SIDEBAR);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(CMD_TOGGLE_PURE_CODE, togglePureCodeKey, Menus.AFTER, CMD_TOGGLE_PANELS);
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(CMD_TOGGLE_FULLSCREEN, [ {key: toggleFullScreenKey}, {key: toggleFullScreenKeyMac, platform: "mac"} ], Menus.AFTER, CMD_TOGGLE_PURE_CODE);
//default toggle panel shortcut was ctrl+shift+` as it is present in one vertical line in the keyboard. However, we later learnt
//from IQE team than non-English keyboards does not have the ` char. So added one more shortcut ctrl+shift+1 which will be preferred
KeyBindingManager.addBinding(CMD_TOGGLE_PANELS, [ {key: togglePanelsKey}, {key: togglePanelsKeyMac, platform: "mac"} ]);
KeyBindingManager.addBinding(CMD_TOGGLE_PANELS, [ {key: togglePanelsKey_EN}, {key: togglePanelsKeyMac_EN, platform: "mac"} ]);
PreferencesManager.on("change", PREFS_PURE_CODE, function () {
const inDesignMode = WorkspaceManager.isInDesignMode &&
WorkspaceManager.isInDesignMode();
if (PreferencesManager.get(PREFS_PURE_CODE)) {
if (inDesignMode) {
// In design mode the live preview already fills the editor
// area and the main toolbar is the visible surface; just
// collapse the sidebar so LP can stretch to the full width.
CommandManager.execute(Commands.HIDE_SIDEBAR);
} else {
ViewUtils.hideMainToolBar();
CommandManager.execute(Commands.HIDE_SIDEBAR);
_hidePanelsIfRequired();
}
} else {
if (inDesignMode) {
CommandManager.execute(Commands.SHOW_SIDEBAR);
} else {
ViewUtils.showMainToolBar();
CommandManager.execute(Commands.SHOW_SIDEBAR);
_showPanelsIfRequired();
}
}
_updateCheckedState();
});
});
});