Skip to content

Commit 124f0c4

Browse files
author
Akos Kitta
committed
fix: editor toolbar enablement
Signed-off-by: Akos Kitta <[email protected]>
1 parent 93e2082 commit 124f0c4

File tree

3 files changed

+85
-47
lines changed

3 files changed

+85
-47
lines changed

Diff for: arduino-ide-extension/src/browser/create/create-features.ts

+56-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { AuthenticationSession } from '../../node/auth/types';
88
import { ArduinoPreferences } from '../arduino-preferences';
99
import { AuthenticationClientService } from '../auth/authentication-client-service';
1010
import { LocalCacheFsProvider } from '../local-cache/local-cache-fs-provider';
11+
import { CreateUri } from './create-uri';
12+
13+
export type CloudSketchState = 'push' | 'pull';
1114

1215
@injectable()
1316
export class CreateFeatures implements FrontendApplicationContribution {
@@ -18,13 +21,22 @@ export class CreateFeatures implements FrontendApplicationContribution {
1821
@inject(LocalCacheFsProvider)
1922
private readonly localCacheFsProvider: LocalCacheFsProvider;
2023

24+
/**
25+
* The keys are the Create URI of the sketches.
26+
*/
27+
private readonly _cloudSketchStates = new Map<string, CloudSketchState>();
2128
private readonly onDidChangeSessionEmitter = new Emitter<
2229
AuthenticationSession | undefined
2330
>();
2431
private readonly onDidChangeEnabledEmitter = new Emitter<boolean>();
32+
private readonly onDidChangeCloudSketchStateEmitter = new Emitter<{
33+
uri: URI;
34+
state: CloudSketchState | undefined;
35+
}>();
2536
private readonly toDispose = new DisposableCollection(
2637
this.onDidChangeSessionEmitter,
27-
this.onDidChangeEnabledEmitter
38+
this.onDidChangeEnabledEmitter,
39+
this.onDidChangeCloudSketchStateEmitter
2840
);
2941
private _enabled: boolean;
3042
private _session: AuthenticationSession | undefined;
@@ -64,14 +76,55 @@ export class CreateFeatures implements FrontendApplicationContribution {
6476
return this.onDidChangeEnabledEmitter.event;
6577
}
6678

67-
get enabled(): boolean {
68-
return this._enabled;
79+
get onDidChangeCloudSketchState(): Event<{
80+
uri: URI;
81+
state: CloudSketchState | undefined;
82+
}> {
83+
return this.onDidChangeCloudSketchStateEmitter.event;
6984
}
7085

7186
get session(): AuthenticationSession | undefined {
7287
return this._session;
7388
}
7489

90+
get enabled(): boolean {
91+
return this._enabled;
92+
}
93+
94+
get cloudSketchStates(): {
95+
uri: URI;
96+
state: CloudSketchState | undefined;
97+
}[] {
98+
return Array.from(this._cloudSketchStates.entries()).map(
99+
([uri, state]) => ({ uri: new URI(uri), state })
100+
);
101+
}
102+
103+
cloudSketchState(uri: URI): CloudSketchState | undefined {
104+
return this._cloudSketchStates.get(uri.toString());
105+
}
106+
107+
setCloudSketchState(uri: URI, state: CloudSketchState | undefined): void {
108+
if (uri.scheme !== CreateUri.scheme) {
109+
throw new Error(
110+
`Expected a URI with '${uri.scheme}' scheme. Got: ${uri.toString()}`
111+
);
112+
}
113+
const key = uri.toString();
114+
if (!state) {
115+
if (!this._cloudSketchStates.delete(key)) {
116+
console.warn(
117+
`Could not reset the cloud sketch state of ${key}. No state existed for the the cloud sketch.`
118+
);
119+
} else {
120+
this.onDidChangeCloudSketchStateEmitter.fire({ uri, state: undefined });
121+
}
122+
} else {
123+
this._cloudSketchStates.set(key, state);
124+
this.onDidChangeCloudSketchStateEmitter.fire({ uri, state });
125+
}
126+
}
127+
75128
/**
76129
* `true` if the sketch is under `directories.data/RemoteSketchbook`. Otherwise, `false`.
77130
* Returns with `undefined` if `dataDirUri` is `undefined`.

Diff for: arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-contributions.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
21
import {
32
ContextMenuRenderer,
43
RenderContextMenuOptions,
@@ -34,6 +33,7 @@ import { ApplicationConnectionStatusContribution } from '../../theia/core/connec
3433
import { SketchbookCommands } from '../sketchbook/sketchbook-commands';
3534
import { CloudSketchbookCommands } from './cloud-sketchbook-commands';
3635
import { CloudSketchbookTree } from './cloud-sketchbook-tree';
36+
import { CreateUri } from '../../create/create-uri';
3737

3838
const SKETCHBOOKSYNC__CONTEXT = ['arduino-sketchbook-sync--context'];
3939

@@ -61,8 +61,6 @@ export class CloudSketchbookContribution extends CloudSketchContribution {
6161
private readonly configServiceClient: ConfigServiceClient;
6262
@inject(ApplicationConnectionStatusContribution)
6363
private readonly connectionStatus: ApplicationConnectionStatusContribution;
64-
@inject(ContextKeyService)
65-
private readonly contextKeyService: ContextKeyService;
6664

6765
private readonly onDidChangeToolbarEmitter = new Emitter<void>();
6866
private readonly toDisposeBeforeNewContextMenu = new DisposableCollection();
@@ -82,17 +80,9 @@ export class CloudSketchbookContribution extends CloudSketchContribution {
8280
}),
8381
this.createFeatures.onDidChangeSession(() => this.fireToolbarChange()),
8482
this.createFeatures.onDidChangeEnabled(() => this.fireToolbarChange()),
85-
this.contextKeyService.onDidChange((event) => {
86-
if (
87-
event.affects({
88-
has(candidate: string) {
89-
return candidate === 'cloudSketchState';
90-
},
91-
})
92-
) {
93-
this.fireToolbarChange();
94-
}
95-
}),
83+
this.createFeatures.onDidChangeCloudSketchState(() =>
84+
this.fireToolbarChange()
85+
),
9686
]);
9787
}
9888

@@ -115,15 +105,13 @@ export class CloudSketchbookContribution extends CloudSketchContribution {
115105
tooltip: CloudSketchbookCommands.PULL_SKETCH__TOOLBAR.label,
116106
priority: -2,
117107
onDidChange: this.onDidChangeToolbar,
118-
when: 'cloudSketchState != pulling && cloudSketchState != pushing',
119108
});
120109
registry.registerItem({
121110
id: CloudSketchbookCommands.PUSH_SKETCH__TOOLBAR.id,
122111
command: CloudSketchbookCommands.PUSH_SKETCH__TOOLBAR.id,
123112
tooltip: CloudSketchbookCommands.PUSH_SKETCH__TOOLBAR.label,
124113
priority: -1,
125114
onDidChange: this.onDidChangeToolbar,
126-
when: 'cloudSketchState != pulling && cloudSketchState != pushing',
127115
});
128116
}
129117

@@ -341,7 +329,17 @@ export class CloudSketchbookContribution extends CloudSketchContribution {
341329
if (this.connectionStatus.offlineStatus === 'internet') {
342330
return false;
343331
}
344-
return true;
332+
// no pull/push context for the current cloud sketch
333+
const sketch = this.currentCloudSketch;
334+
if (sketch) {
335+
const cloudUri = this.createFeatures.cloudUri(sketch);
336+
if (cloudUri) {
337+
return !this.createFeatures.cloudSketchState(
338+
CreateUri.toUri(cloudUri.path.toString())
339+
);
340+
}
341+
}
342+
return false;
345343
}
346344

347345
private isCloudSketchDirNodeCommandArg(
@@ -351,7 +349,8 @@ export class CloudSketchbookContribution extends CloudSketchContribution {
351349
} {
352350
return (
353351
CloudSketchbookCommands.Arg.is(arg) &&
354-
CloudSketchbookTree.CloudSketchDirNode.is(arg.node)
352+
CloudSketchbookTree.CloudSketchDirNode.is(arg.node) &&
353+
!this.createFeatures.cloudSketchState(arg.node.remoteUri)
355354
);
356355
}
357356

Diff for: arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-tree.ts

+12-26
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ import {
3737
pullingSketch,
3838
pushingSketch,
3939
} from '../../contributions/cloud-contribution';
40-
import {
41-
ContextKey,
42-
ContextKeyService,
43-
} from '@theia/core/lib/browser/context-key-service';
40+
import { CloudSketchState, CreateFeatures } from '../../create/create-features';
4441

4542
const MESSAGE_TIMEOUT = 5 * 1000;
4643
const deepmerge = require('deepmerge').default;
@@ -67,19 +64,13 @@ export class CloudSketchbookTree extends SketchbookTree {
6764
@inject(ApplicationConnectionStatusContribution)
6865
private readonly connectionStatus: ApplicationConnectionStatusContribution;
6966

70-
@inject(ContextKeyService)
71-
private readonly contextKeyService: ContextKeyService;
72-
73-
private cloudSketchState: ContextKey<string> | undefined;
67+
@inject(CreateFeatures)
68+
private readonly createFeatures: CreateFeatures;
7469

7570
protected override init(): void {
7671
this.toDispose.push(
7772
this.connectionStatus.onOfflineStatusDidChange(() => this.refresh())
7873
);
79-
this.cloudSketchState = this.contextKeyService.createKey<string>(
80-
'cloudSketchState',
81-
undefined
82-
);
8374
super.init();
8475
}
8576

@@ -149,7 +140,7 @@ export class CloudSketchbookTree extends SketchbookTree {
149140
}
150141
return this.runWithState(
151142
node,
152-
'pulling',
143+
'pull',
153144
async (node) => {
154145
const commandsCopy = node.commands;
155146
node.commands = [];
@@ -217,7 +208,7 @@ export class CloudSketchbookTree extends SketchbookTree {
217208
}
218209
return this.runWithState(
219210
node,
220-
'pushing',
211+
'push',
221212
async (node) => {
222213
if (!CloudSketchbookTree.CloudSketchTreeNode.isSynced(node)) {
223214
throw new Error(
@@ -352,11 +343,11 @@ export class CloudSketchbookTree extends SketchbookTree {
352343

353344
private async runWithState<T>(
354345
node: CloudSketchbookTree.CloudSketchDirNode & Partial<DecoratedTreeNode>,
355-
state: CloudSketchbookTree.CloudSketchDirNode.State,
346+
state: CloudSketchState,
356347
task: (node: CloudSketchbookTree.CloudSketchDirNode) => MaybePromise<T>,
357348
noProgress = false
358349
): Promise<T> {
359-
this.cloudSketchState?.set(state);
350+
this.createFeatures.setCloudSketchState(node.remoteUri, state);
360351
try {
361352
const result = await (noProgress
362353
? task(node)
@@ -371,18 +362,15 @@ export class CloudSketchbookTree extends SketchbookTree {
371362
await this.refresh(node);
372363
return result;
373364
} finally {
374-
this.cloudSketchState?.set(undefined);
365+
this.createFeatures.setCloudSketchState(node.remoteUri, undefined);
375366
}
376367
}
377368

378-
private taskMessage(
379-
state: CloudSketchbookTree.CloudSketchDirNode.State,
380-
input: string
381-
): string {
369+
private taskMessage(state: CloudSketchState, input: string): string {
382370
switch (state) {
383-
case 'pulling':
371+
case 'pull':
384372
return pullingSketch(input);
385-
case 'pushing':
373+
case 'push':
386374
return pushingSketch(input);
387375
default:
388376
assertUnreachable(state);
@@ -700,7 +688,7 @@ export namespace CloudSketchbookTree {
700688
export interface CloudSketchDirNode
701689
extends Omit<SketchbookTree.SketchDirNode, 'fileStat'>,
702690
CloudSketchTreeNode {
703-
state?: CloudSketchDirNode.State;
691+
state?: CloudSketchState;
704692
isPublic?: boolean;
705693
sketchId?: string;
706694
commands?: Command[];
@@ -709,7 +697,5 @@ export namespace CloudSketchbookTree {
709697
export function is(node: TreeNode | undefined): node is CloudSketchDirNode {
710698
return SketchbookTree.SketchDirNode.is(node);
711699
}
712-
713-
export type State = 'pulling' | 'pushing';
714700
}
715701
}

0 commit comments

Comments
 (0)