1414// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
1515// *****************************************************************************
1616
17- import { Command , CommandRegistry , ContributionProvider , Emitter , MaybePromise , MessageService } from '@theia/core' ;
17+ import { Command , CommandRegistry , ContributionProvider , Emitter , MaybePromise , MessageService , nls } from '@theia/core' ;
1818import { KeybindingRegistry , Widget } from '@theia/core/lib/browser' ;
1919import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state' ;
2020import { Deferred } from '@theia/core/lib/common/promise-util' ;
2121import { injectable , inject , postConstruct , named } from '@theia/core/shared/inversify' ;
22- import { ToolbarDefaultsFactory } from './toolbar-defaults' ;
2322import {
2423 DeflatedToolbarTree ,
2524 ToolbarContribution ,
@@ -31,13 +30,13 @@ import { ToolbarStorageProvider, TOOLBAR_BAD_JSON_ERROR_MESSAGE } from './toolba
3130import { ReactToolbarItemImpl , RenderedToolbarItemImpl , TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar/tab-toolbar-item' ;
3231import { ContextKeyService } from '@theia/core/lib/browser/context-key-service' ;
3332import { LabelParser } from '@theia/core/lib/browser/label-parser' ;
33+ import { ToolbarCommands } from './toolbar-constants' ;
3434
3535@injectable ( )
3636export class ToolbarController {
3737 @inject ( ToolbarStorageProvider ) protected readonly storageProvider : ToolbarStorageProvider ;
3838 @inject ( FrontendApplicationStateService ) protected readonly appState : FrontendApplicationStateService ;
3939 @inject ( MessageService ) protected readonly messageService : MessageService ;
40- @inject ( ToolbarDefaultsFactory ) protected readonly defaultsFactory : ( ) => DeflatedToolbarTree ;
4140 @inject ( CommandRegistry ) commandRegistry : CommandRegistry ;
4241 @inject ( ContextKeyService ) contextKeyService : ContextKeyService ;
4342 @inject ( KeybindingRegistry ) keybindingRegistry : KeybindingRegistry ;
@@ -64,30 +63,32 @@ export class ToolbarController {
6463 this . toolbarModelDidUpdateEmitter . fire ( ) ;
6564 }
6665
67- protected inflateItems ( schema : DeflatedToolbarTree ) : ToolbarTreeSchema {
66+ protected inflateItems ( schema ? : DeflatedToolbarTree ) : ToolbarTreeSchema {
6867 const newTree : ToolbarTreeSchema = {
6968 items : {
7069 [ ToolbarAlignment . LEFT ] : [ ] ,
7170 [ ToolbarAlignment . CENTER ] : [ ] ,
7271 [ ToolbarAlignment . RIGHT ] : [ ] ,
7372 } ,
7473 } ;
75- for ( const column of Object . keys ( schema . items ) ) {
76- const currentColumn = schema . items [ column as ToolbarAlignment ] ;
77- for ( const group of currentColumn ) {
78- const newGroup : TabBarToolbarItem [ ] = [ ] ;
79- for ( const item of group ) {
80- if ( item . group === 'contributed' ) {
81- const contribution = this . getContributionByID ( item . id ) ;
82- if ( contribution ) {
83- newGroup . push ( new ReactToolbarItemImpl ( this . commandRegistry , this . contextKeyService , contribution ) ) ;
74+ if ( schema ) {
75+ for ( const column of Object . keys ( schema . items ) ) {
76+ const currentColumn = schema . items [ column as ToolbarAlignment ] ;
77+ for ( const group of currentColumn ) {
78+ const newGroup : TabBarToolbarItem [ ] = [ ] ;
79+ for ( const item of group ) {
80+ if ( item . group === 'contributed' ) {
81+ const contribution = this . getContributionByID ( item . id ) ;
82+ if ( contribution ) {
83+ newGroup . push ( new ReactToolbarItemImpl ( this . commandRegistry , this . contextKeyService , contribution ) ) ;
84+ }
85+ } else {
86+ newGroup . push ( new RenderedToolbarItemImpl ( this . commandRegistry , this . contextKeyService , this . keybindingRegistry , this . labelParser , item ) ) ;
8487 }
85- } else {
86- newGroup . push ( new RenderedToolbarItemImpl ( this . commandRegistry , this . contextKeyService , this . keybindingRegistry , this . labelParser , item ) ) ;
8788 }
88- }
89- if ( newGroup . length ) {
90- newTree . items [ column as ToolbarAlignment ] . push ( newGroup ) ;
89+ if ( newGroup . length ) {
90+ newTree . items [ column as ToolbarAlignment ] . push ( newGroup ) ;
91+ }
9192 }
9293 }
9394 }
@@ -121,14 +122,14 @@ export class ToolbarController {
121122 protected async resolveToolbarItems ( ) : Promise < ToolbarTreeSchema > {
122123 await this . storageProvider . ready ;
123124
124- if ( this . storageProvider . toolbarItems ) {
125- try {
126- return this . inflateItems ( this . storageProvider . toolbarItems ) ;
127- } catch ( e ) {
128- this . messageService . error ( TOOLBAR_BAD_JSON_ERROR_MESSAGE ) ;
125+ if ( ! this . storageProvider . toolbarItems ) {
126+ const resetLabel = ToolbarCommands . RESET_TOOLBAR . label ! ;
127+ const answer = await this . messageService . error ( nls . localize ( 'theia/toolbar/jsonError' , TOOLBAR_BAD_JSON_ERROR_MESSAGE ) , resetLabel ) ;
128+ if ( answer === resetLabel ) {
129+ await this . restoreToolbarDefaults ( ) ;
129130 }
130131 }
131- return this . inflateItems ( this . defaultsFactory ( ) ) ;
132+ return this . inflateItems ( this . storageProvider . toolbarItems ) ;
132133 }
133134
134135 async swapValues (
@@ -142,8 +143,8 @@ export class ToolbarController {
142143 } ) ;
143144 }
144145
145- async clearAll ( ) : Promise < boolean > {
146- return this . withBusy < boolean > ( ( ) => this . storageProvider . clearAll ( ) ) ;
146+ async restoreToolbarDefaults ( ) : Promise < boolean > {
147+ return this . withBusy < boolean > ( ( ) => this . storageProvider . restoreToolbarDefaults ( ) ) ;
147148 }
148149
149150 async openOrCreateJSONFile ( doOpen = false ) : Promise < Widget | undefined > {
0 commit comments