-
-
Notifications
You must be signed in to change notification settings - Fork 431
Show user fields dialog again if upload fails #1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { DisposableCollection, nls } from '@theia/core/lib/common'; | ||
import { BoardUserField, CoreError } from '../../common/protocol'; | ||
import { BoardsServiceProvider } from '../boards/boards-service-provider'; | ||
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog'; | ||
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; | ||
import { MenuModelRegistry, Contribution } from './contribution'; | ||
import { UploadSketch } from './upload-sketch'; | ||
|
||
@injectable() | ||
export class UserFields extends Contribution { | ||
private boardRequiresUserFields = false; | ||
private userFieldsSet = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not simplify this and if there are user fields for an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that we would lose the value of the user field. As far as I know, we don't want that, we want to preserve the value, but give the user the possibility to change it. |
||
private readonly cachedUserFields: Map<string, BoardUserField[]> = new Map(); | ||
private readonly menuActionsDisposables = new DisposableCollection(); | ||
|
||
@inject(UserFieldsDialog) | ||
private readonly userFieldsDialog: UserFieldsDialog; | ||
|
||
@inject(BoardsServiceProvider) | ||
private readonly boardsServiceProvider: BoardsServiceProvider; | ||
|
||
@inject(MenuModelRegistry) | ||
private readonly menuRegistry: MenuModelRegistry; | ||
|
||
protected override init(): void { | ||
super.init(); | ||
this.boardsServiceProvider.onBoardsConfigChanged(async () => { | ||
const userFields = | ||
await this.boardsServiceProvider.selectedBoardUserFields(); | ||
this.boardRequiresUserFields = userFields.length > 0; | ||
this.registerMenus(this.menuRegistry); | ||
}); | ||
} | ||
|
||
override registerMenus(registry: MenuModelRegistry): void { | ||
this.menuActionsDisposables.dispose(); | ||
if (this.boardRequiresUserFields) { | ||
this.menuActionsDisposables.push( | ||
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, { | ||
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id, | ||
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label, | ||
order: '2', | ||
}) | ||
); | ||
} else { | ||
this.menuActionsDisposables.push( | ||
registry.registerMenuNode( | ||
ArduinoMenus.SKETCH__MAIN_GROUP, | ||
new PlaceholderMenuNode( | ||
ArduinoMenus.SKETCH__MAIN_GROUP, | ||
// commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id, | ||
UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label, | ||
{ order: '2' } | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
private selectedFqbnAddress(): string | undefined { | ||
const { boardsConfig } = this.boardsServiceProvider; | ||
const fqbn = boardsConfig.selectedBoard?.fqbn; | ||
if (!fqbn) { | ||
return undefined; | ||
} | ||
const address = | ||
boardsConfig.selectedBoard?.port?.address || | ||
boardsConfig.selectedPort?.address; | ||
if (!address) { | ||
return undefined; | ||
} | ||
return fqbn + '|' + address; | ||
} | ||
|
||
private async showUserFieldsDialog( | ||
key: string | ||
): Promise<BoardUserField[] | undefined> { | ||
const cached = this.cachedUserFields.get(key); | ||
// Deep clone the array of board fields to avoid editing the cached ones | ||
this.userFieldsDialog.value = cached ? cached.slice() : await this.boardsServiceProvider.selectedBoardUserFields(); | ||
const result = await this.userFieldsDialog.open(); | ||
if (!result) { | ||
return; | ||
} | ||
|
||
this.userFieldsSet = true; | ||
this.cachedUserFields.set(key, result); | ||
return result; | ||
} | ||
|
||
async checkUserFieldsDialog(forceOpen = false): Promise<boolean> { | ||
const key = this.selectedFqbnAddress(); | ||
if (!key) { | ||
return false; | ||
} | ||
/* | ||
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)) | ||
) { | ||
return true; | ||
} | ||
const userFieldsFilledIn = Boolean(await this.showUserFieldsDialog(key)); | ||
return userFieldsFilledIn; | ||
} | ||
|
||
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; | ||
return true; | ||
} | ||
this.messageService.error( | ||
nls.localize( | ||
'arduino/sketch/userFieldsNotFoundError', | ||
"Can't find user fields for connected board" | ||
) | ||
); | ||
this.userFieldsSet = false; | ||
return false; | ||
} | ||
|
||
getUserFields(): BoardUserField[] { | ||
const fqbnAddress = this.selectedFqbnAddress(); | ||
if (!fqbnAddress) { | ||
return []; | ||
} | ||
return this.cachedUserFields.get(fqbnAddress) ?? []; | ||
} | ||
|
||
isRequired(): boolean { | ||
return this.boardRequiresUserFields; | ||
} | ||
|
||
notifyFailedWithError(e: Error): void { | ||
if ( | ||
AlbyIanna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.boardRequiresUserFields && | ||
CoreError.UploadFailed.is(e) | ||
) { | ||
this.userFieldsSet = false; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not pass the
fqbn
from here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason since I can easily get them from the
boardsServiceProvider
. Passing it here would just add a parameter, with no benefit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly this; why do you want to calculate it twice?
verify
, andupload
will require thefqbn
anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO how the user fields are stored is not related to the upload. I may want to open that dialog even if I'm not uploading.