- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathuser-fields.ts
129 lines (114 loc) · 4.08 KB
/
user-fields.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
import{nls}from'@theia/core/lib/common/nls';
import{inject,injectable}from'@theia/core/shared/inversify';
import{BoardUserField,CoreError}from'../../common/protocol';
import{BoardsServiceProvider}from'../boards/boards-service-provider';
import{UserFieldsDialog}from'../dialogs/user-fields/user-fields-dialog';
import{ArduinoMenus}from'../menu/arduino-menus';
import{Contribution,MenuModelRegistry}from'./contribution';
import{UploadSketch}from'./upload-sketch';
@injectable()
exportclassUserFieldsextendsContribution{
privateboardRequiresUserFields=false;
privateuserFieldsSet=false;
privatereadonlycachedUserFields: Map<string,BoardUserField[]>=newMap();
@inject(UserFieldsDialog)
privatereadonlyuserFieldsDialog: UserFieldsDialog;
@inject(BoardsServiceProvider)
privatereadonlyboardsServiceProvider: BoardsServiceProvider;
protectedoverrideinit(): void{
super.init();
this.boardsServiceProvider.onBoardsConfigDidChange(()=>this.refresh());
}
overrideonReady(): void{
this.boardsServiceProvider.ready.then(()=>this.refresh());
}
overrideregisterMenus(registry: MenuModelRegistry): void{
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP,{
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label,
order: '2',
});
}
privateasyncrefresh(): Promise<void>{
constuserFields=
awaitthis.boardsServiceProvider.selectedBoardUserFields();
this.boardRequiresUserFields=userFields.length>0;
this.menuManager.update();
}
privateselectedFqbnAddress(): string|undefined{
const{ boardsConfig }=this.boardsServiceProvider;
constfqbn=boardsConfig.selectedBoard?.fqbn;
if(!fqbn){
returnundefined;
}
constaddress=boardsConfig.selectedPort?.address||'';
returnfqbn+'|'+address;
}
privateasyncshowUserFieldsDialog(
key: string
): Promise<BoardUserField[]|undefined>{
constcached=this.cachedUserFields.get(key);
// Deep clone the array of board fields to avoid editing the cached ones
this.userFieldsDialog.value=cached
? cached.slice()
: awaitthis.boardsServiceProvider.selectedBoardUserFields();
constresult=awaitthis.userFieldsDialog.open();
if(!result){
return;
}
this.userFieldsSet=true;
this.cachedUserFields.set(key,result);
returnresult;
}
asynccheckUserFieldsDialog(forceOpen=false): Promise<boolean>{
constkey=this.selectedFqbnAddress();
if(!key){
returnfalse;
}
/*
If the board requires to be configured with user fields, we want
to show the user fields dialog, but only if they weren't already
filled in or if they were filled in, but the previous upload failed.
*/
if(
!forceOpen&&
(!this.boardRequiresUserFields||
(this.cachedUserFields.has(key)&&this.userFieldsSet))
){
returntrue;
}
constuserFieldsFilledIn=Boolean(awaitthis.showUserFieldsDialog(key));
returnuserFieldsFilledIn;
}
checkUserFieldsForUpload(): boolean{
// TODO: This does not belong here.
// IDE2 should not do any preliminary checks but let the CLI fail and then toast a user consumable error message.
if(!this.boardRequiresUserFields||this.getUserFields().length>0){
this.userFieldsSet=true;
returntrue;
}
this.messageService.error(
nls.localize(
'arduino/sketch/userFieldsNotFoundError',
"Can't find user fields for connected board"
)
);
this.userFieldsSet=false;
returnfalse;
}
getUserFields(): BoardUserField[]{
constfqbnAddress=this.selectedFqbnAddress();
if(!fqbnAddress){
return[];
}
returnthis.cachedUserFields.get(fqbnAddress)??[];
}
isRequired(): boolean{
returnthis.boardRequiresUserFields;
}
notifyFailedWithError(e: Error): void{
if(this.boardRequiresUserFields&&CoreError.UploadFailed.is(e)){
this.userFieldsSet=false;
}
}
}