|
1 |
| -import { injectable } from '@theia/core/shared/inversify'; |
2 |
| -import { CommandHandler } from '@theia/core/lib/common/command'; |
3 |
| -import { MenuModelRegistry } from './contribution'; |
| 1 | +import * as remote from '@theia/core/electron-shared/@electron/remote'; |
| 2 | +import type { CommandHandler } from '@theia/core/lib/common/command'; |
| 3 | +import { nls } from '@theia/core/lib/common/nls'; |
| 4 | +import { waitForEvent } from '@theia/core/lib/common/promise-util'; |
| 5 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 6 | +import { SketchContainer, SketchesError } from '../../common/protocol'; |
4 | 7 | import { ArduinoMenus } from '../menu/arduino-menus';
|
| 8 | +import { WindowServiceExt } from '../theia/core/window-service-ext'; |
| 9 | +import { MenuModelRegistry, URI } from './contribution'; |
5 | 10 | import { Examples } from './examples';
|
6 |
| -import { SketchContainer, SketchesError } from '../../common/protocol'; |
7 | 11 | import { OpenSketch } from './open-sketch';
|
8 |
| -import { nls } from '@theia/core/lib/common/nls'; |
9 | 12 |
|
10 | 13 | @injectable()
|
11 | 14 | export class Sketchbook extends Examples {
|
| 15 | + @inject(WindowServiceExt) |
| 16 | + private readonly windowService: WindowServiceExt; |
| 17 | + private _currentSketchbookPath: string | undefined; |
| 18 | + |
12 | 19 | override onStart(): void {
|
13 | 20 | this.sketchServiceClient.onSketchbookDidChange(() => this.update());
|
14 | 21 | this.configService.onDidChangeSketchDirUri(() => this.update());
|
| 22 | + // If this window is changing the settings, and sketchbook location is included in the changeset, |
| 23 | + // then the users must be warned about the invalid sketches from the new sketchbook. |
| 24 | + this.settingsService.onWillChangeSoon(async (unsavedSettings) => { |
| 25 | + // The sketchbook location is about to change. |
| 26 | + if (unsavedSettings.sketchbookPath !== this._currentSketchbookPath) { |
| 27 | + // Listen on both the settings and sketchbook location did change events |
| 28 | + const timeout = 5_000; |
| 29 | + const results = await Promise.allSettled([ |
| 30 | + waitForEvent(this.settingsService.onDidChange, timeout), |
| 31 | + waitForEvent(this.configService.onDidChangeSketchDirUri, timeout), |
| 32 | + ]); |
| 33 | + if (results.every(({ status }) => status === 'fulfilled')) { |
| 34 | + this.doUpdate(true); |
| 35 | + } else { |
| 36 | + console.warn( |
| 37 | + 'Expected the sketchbook location to change but it did not.' |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + const maybeUpdateCurrentSketchbookPath = async ( |
| 43 | + sketchDirUri: URI | undefined = this.configService.tryGetSketchDirUri() |
| 44 | + ) => { |
| 45 | + if (sketchDirUri) { |
| 46 | + const candidateSketchbookPath = await this.fileService.fsPath( |
| 47 | + sketchDirUri |
| 48 | + ); |
| 49 | + if (candidateSketchbookPath !== this._currentSketchbookPath) { |
| 50 | + this._currentSketchbookPath = candidateSketchbookPath; |
| 51 | + } |
| 52 | + } |
| 53 | + }; |
| 54 | + this.configService.onDidChangeSketchDirUri( |
| 55 | + maybeUpdateCurrentSketchbookPath |
| 56 | + ); |
| 57 | + maybeUpdateCurrentSketchbookPath(); |
15 | 58 | }
|
16 | 59 |
|
17 | 60 | override async onReady(): Promise<void> {
|
18 |
| - this.update(); |
| 61 | + this.windowService |
| 62 | + .isFirstWindow() |
| 63 | + // only the first window should warn about invalid sketch folder names. |
| 64 | + .then((firstWindow) => this.doUpdate(firstWindow)); |
19 | 65 | }
|
20 | 66 |
|
21 | 67 | protected override update(): void {
|
22 |
| - this.sketchService.getSketches({}).then((container) => { |
23 |
| - this.register(container); |
24 |
| - this.menuManager.update(); |
25 |
| - }); |
| 68 | + // on regular menu updates, do not raise warning dialogs about invalid sketch folder names |
| 69 | + this.doUpdate(false); |
| 70 | + } |
| 71 | + |
| 72 | + private doUpdate(raiseInvalidSketchFoldersWarning?: boolean): void { |
| 73 | + this.sketchService |
| 74 | + .getSketches({}) |
| 75 | + .then(({ container, sketchesInInvalidFolder }) => { |
| 76 | + if ( |
| 77 | + raiseInvalidSketchFoldersWarning && |
| 78 | + sketchesInInvalidFolder.length |
| 79 | + ) { |
| 80 | + Promise.all( |
| 81 | + sketchesInInvalidFolder.map(async (ref) => { |
| 82 | + const fsPath = await this.fileService.fsPath(new URI(ref.uri)); |
| 83 | + return { |
| 84 | + name: ref.name, |
| 85 | + path: fsPath, |
| 86 | + }; |
| 87 | + }) |
| 88 | + ).then((dialogInputs) => |
| 89 | + dialogInputs.reduce(async (acc, { name, path }) => { |
| 90 | + await acc; |
| 91 | + return remote.dialog.showMessageBox(remote.getCurrentWindow(), { |
| 92 | + title: nls.localize( |
| 93 | + 'arduino/sketch/ignoringSketchWithBadNameTitle', |
| 94 | + 'Ignoring sketch with bad name' |
| 95 | + ), |
| 96 | + message: nls.localize( |
| 97 | + 'arduino/sketch/ignoringSketchWithBadNameMessage', |
| 98 | + 'The sketch "{0}" cannot be used. Sketch names must contain only basic letters and numbers. (ASCII-only with no spaces, and it cannot start with a number). To get rid of this message, remove the sketch from {1}', |
| 99 | + name, |
| 100 | + path |
| 101 | + ), |
| 102 | + type: 'warning', |
| 103 | + }) as Promise<unknown>; |
| 104 | + }, Promise.resolve()) |
| 105 | + ); |
| 106 | + } |
| 107 | + this.register(container); |
| 108 | + this.menuManager.update(); |
| 109 | + }); |
26 | 110 | }
|
27 | 111 |
|
28 | 112 | override registerMenus(registry: MenuModelRegistry): void {
|
|
0 commit comments