-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
shortcut_customization_app.ts
241 lines (202 loc) · 8.48 KB
/
shortcut_customization_app.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import './accelerator_edit_dialog.js';
import './shortcut_input.js';
import './shortcuts_page.js';
import '../strings.m.js';
import '../css/shortcut_customization_shared.css.js';
import 'chrome://resources/ash/common/navigation_view_panel.js';
import 'chrome://resources/ash/common/page_toolbar.js';
import 'chrome://resources/mojo/mojo/public/js/mojo_bindings_lite.js';
import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js';
import {NavigationViewPanelElement} from 'chrome://resources/ash/common/navigation_view_panel.js';
import {I18nMixin} from 'chrome://resources/cr_elements/i18n_mixin.js';
import {PolymerElementProperties} from 'chrome://resources/polymer/v3_0/polymer/interfaces.js';
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {AcceleratorsUpdatedObserverInterface, AcceleratorsUpdatedObserverReceiver} from '../mojom-webui/ash/webui/shortcut_customization_ui/mojom/shortcut_customization.mojom-webui.js';
import {AcceleratorEditDialogElement} from './accelerator_edit_dialog.js';
import {RequestUpdateAcceleratorEvent} from './accelerator_edit_view.js';
import {AcceleratorLookupManager} from './accelerator_lookup_manager.js';
import {ShowEditDialogEvent} from './accelerator_row.js';
import {getShortcutProvider} from './mojo_interface_provider.js';
import {getTemplate} from './shortcut_customization_app.html.js';
import {AcceleratorConfigResult, AcceleratorInfo, AcceleratorSource, MojoAcceleratorConfig, MojoLayoutInfo, ShortcutProviderInterface} from './shortcut_types.js';
import {getCategoryNameStringId, isCustomizationDisabled} from './shortcut_utils.js';
export interface ShortcutCustomizationAppElement {
$: {
navigationPanel: NavigationViewPanelElement,
};
}
declare global {
interface HTMLElementEventMap {
'edit-dialog-closed': CustomEvent<void>;
'request-update-accelerator': RequestUpdateAcceleratorEvent;
'show-edit-dialog': ShowEditDialogEvent;
}
}
/**
* @fileoverview
* 'shortcut-customization-app' is the main landing page for the shortcut
* customization app.
*/
const ShortcutCustomizationAppElementBase = I18nMixin(PolymerElement);
export class ShortcutCustomizationAppElement extends
ShortcutCustomizationAppElementBase implements
AcceleratorsUpdatedObserverInterface {
static get is(): string {
return 'shortcut-customization-app';
}
static get properties(): PolymerElementProperties {
return {
dialogShortcutTitle: {
type: String,
value: '',
},
dialogAccelerators: {
type: Array,
value: () => [],
},
dialogAction: {
type: Number,
value: 0,
},
dialogSource: {
type: Number,
value: 0,
},
showEditDialog: {
type: Boolean,
value: false,
},
showRestoreAllDialog: {
type: Boolean,
value: false,
},
};
}
protected showRestoreAllDialog: boolean;
protected dialogShortcutTitle: string;
protected dialogAccelerators: AcceleratorInfo[];
protected dialogAction: number;
protected dialogSource: AcceleratorSource;
protected showEditDialog: boolean;
private shortcutProvider: ShortcutProviderInterface = getShortcutProvider();
private acceleratorlookupManager: AcceleratorLookupManager =
AcceleratorLookupManager.getInstance();
private acceleratorsUpdatedReceiver: AcceleratorsUpdatedObserverReceiver;
override connectedCallback(): void {
super.connectedCallback();
this.fetchAccelerators();
this.addEventListener('show-edit-dialog', this.showDialog);
this.addEventListener('edit-dialog-closed', this.onDialogClosed);
this.addEventListener(
'request-update-accelerator', this.onRequestUpdateAccelerators);
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this.acceleratorsUpdatedReceiver.$.close();
this.removeEventListener('show-edit-dialog', this.showDialog);
this.removeEventListener('edit-dialog-closed', this.onDialogClosed);
this.removeEventListener(
'request-update-accelerator', this.onRequestUpdateAccelerators);
}
private fetchAccelerators(): void {
// Kickoff fetching accelerators by first fetching the accelerator configs.
this.shortcutProvider.getAccelerators().then(
({config}) => this.onAcceleratorConfigFetched(config));
// Fetch the hasLauncherButton value.
this.shortcutProvider.hasLauncherButton().then(({hasLauncherButton}) => {
this.acceleratorlookupManager.setHasLauncherButton(hasLauncherButton);
});
}
private onAcceleratorConfigFetched(config: MojoAcceleratorConfig): void {
this.acceleratorlookupManager.setAcceleratorLookup(config);
// After fetching the config infos, fetch the layout infos next.
this.shortcutProvider.getAcceleratorLayoutInfos().then(
({layoutInfos}) => this.onLayoutInfosFetched(layoutInfos));
}
private onLayoutInfosFetched(layoutInfos: MojoLayoutInfo[]): void {
this.addNavigationSelectors(layoutInfos);
this.acceleratorlookupManager.setAcceleratorLayoutLookup(layoutInfos);
// Notify pages to update their accelerators.
this.$.navigationPanel.notifyEvent('updateAccelerators');
// After fetching initial accelerators, start observing for any changes.
this.acceleratorsUpdatedReceiver =
new AcceleratorsUpdatedObserverReceiver(this);
this.shortcutProvider.addObserver(
this.acceleratorsUpdatedReceiver.$.bindNewPipeAndPassRemote());
}
// AcceleratorsUpdatedObserverInterface:
onAcceleratorsUpdated(config: MojoAcceleratorConfig): void {
this.acceleratorlookupManager.setAcceleratorLookup(config);
this.$.navigationPanel.notifyEvent('updateSubsections');
// Update the hasLauncherButton value every time accelerators are updated.
this.shortcutProvider.hasLauncherButton().then(({hasLauncherButton}) => {
this.acceleratorlookupManager.setHasLauncherButton(hasLauncherButton);
});
}
private addNavigationSelectors(layoutInfos: MojoLayoutInfo[]): void {
// A Set is used here to remove duplicates from the array of categories.
const uniqueCategoriesInOrder =
new Set(layoutInfos.map(layoutInfo => layoutInfo.category));
const pages = Array.from(uniqueCategoriesInOrder).map(category => {
const categoryNameStringId = getCategoryNameStringId(category);
const categoryName = this.i18n(categoryNameStringId);
return this.$.navigationPanel.createSelectorItem(
categoryName, 'shortcuts-page', '', `${categoryNameStringId}-page-id`,
{category});
});
this.$.navigationPanel.addSelectors(pages);
}
private showDialog(e: ShowEditDialogEvent): void {
this.dialogShortcutTitle = e.detail.description;
this.dialogAccelerators = e.detail.accelerators;
this.dialogAction = e.detail.action;
this.dialogSource = e.detail.source;
this.showEditDialog = true;
}
private onDialogClosed(): void {
this.showEditDialog = false;
this.dialogShortcutTitle = '';
this.dialogAccelerators = [];
}
private onRequestUpdateAccelerators(e: RequestUpdateAcceleratorEvent): void {
this.$.navigationPanel.notifyEvent('updateSubsections');
const updatedAccels =
this.acceleratorlookupManager.getStandardAcceleratorInfos(
e.detail.source, e.detail.action);
this.shadowRoot!.querySelector<AcceleratorEditDialogElement>('#editDialog')!
.updateDialogAccelerators(updatedAccels as AcceleratorInfo[]);
}
protected onRestoreAllDefaultClicked(): void {
this.showRestoreAllDialog = true;
}
protected onCancelRestoreButtonClicked(): void {
this.closeRestoreAllDialog();
}
protected onConfirmRestoreButtonClicked(): void {
this.shortcutProvider.restoreAllDefaults().then(({result}) => {
// TODO(jimmyxgong): Explore error state with restore all.
if (result.result === AcceleratorConfigResult.kSuccess) {
this.closeRestoreAllDialog();
}
});
}
protected closeRestoreAllDialog(): void {
this.showRestoreAllDialog = false;
}
protected shouldHideRestoreAllButton(): boolean {
return isCustomizationDisabled();
}
static get template(): HTMLTemplateElement {
return getTemplate();
}
}
declare global {
interface HTMLElementTagNameMap {
'shortcut-customization-app': ShortcutCustomizationAppElement;
}
}
customElements.define(
ShortcutCustomizationAppElement.is, ShortcutCustomizationAppElement);