Skip to content

Commit b5e9eff

Browse files
committed
Add documentation for widget manager & open handler
FIxes #8643 - Document 'WidgetManager' and related interfaces - Document 'WidgetOpenHandler' and related interfaces Signed-off-by: Tobias Ortmayr <[email protected]> Contributed on behalf of STMicroelectronics
1 parent c4f324a commit b5e9eff

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

packages/core/src/browser/widget-manager.ts

+42-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,28 @@ import { ILogger, Emitter, Event, ContributionProvider, MaybePromise, WaitUntilE
2020

2121
/* eslint-disable @typescript-eslint/no-explicit-any */
2222
export const WidgetFactory = Symbol('WidgetFactory');
23+
2324
/**
24-
* `OpenHandler` should be implemented to provide a new opener.
25+
* A {@link WidgetFactory} is used to create new widgets. Factory-specific information (options) can be passed as serializable JSON data.
26+
* The common {@link WidgetManager} collects `WidgetFactory` contributions and delegates to the corresponding factory when
27+
* a widget should be created or restored. To identify widgets the `WidgetManager` uses a description composed of the factory id and the options.
28+
* The `WidgetFactory` does support both, synchronous and asynchronous widget creation.
29+
*
30+
* ### Example usage
31+
*
32+
* ```typescript
33+
* export class MyWidget extends BaseWidget {
34+
* }
35+
*
36+
* @injectable()
37+
* export class MyWidgetFactory implements WidgetFactory {
38+
* id = 'myWidgetFactory';
39+
*
40+
* createWidget(): MaybePromise<Widget> {
41+
* return new MyWidget();
42+
* }
43+
* }
44+
* ```
2545
*/
2646
export interface WidgetFactory {
2747

@@ -31,8 +51,10 @@ export interface WidgetFactory {
3151
readonly id: string;
3252

3353
/**
34-
* Creates a widget and attaches it to the application shell.
35-
* @param options serializable JSON data.
54+
* Creates a widget using the given options.
55+
* @param options factory specific information as serializable JSON data.
56+
*
57+
* @returns the newly created widget or a promise of the widget
3658
*/
3759
createWidget(options?: any): MaybePromise<Widget>;
3860
}
@@ -82,7 +104,9 @@ export interface DidCreateWidgetEvent {
82104
}
83105

84106
/**
85-
* Creates and manages widgets.
107+
* The {@link WidgetManager} is the common component responsible for creating and managing widgets. Additional widget factories
108+
* can be registered by using the {@link WidgetFactory} contribution point. To identify a widget, created by a factory, the factory id and
109+
* the creation options are used. This key is commonly referred to as `description` of the widget.
86110
*/
87111
@injectable()
88112
export class WidgetManager {
@@ -106,13 +130,14 @@ export class WidgetManager {
106130
readonly onWillCreateWidget: Event<WillCreateWidgetEvent> = this.onWillCreateWidgetEmitter.event;
107131

108132
protected readonly onDidCreateWidgetEmitter = new Emitter<DidCreateWidgetEvent>();
133+
109134
readonly onDidCreateWidget: Event<DidCreateWidgetEvent> = this.onDidCreateWidgetEmitter.event;
110135

111136
/**
112-
* Get the list of widgets created for the given factory id.
137+
* Get the list of widgets created by the given widget factory.
113138
* @param factoryId the widget factory id.
114139
*
115-
* @returns the list of widgets created for the given factory id.
140+
* @returns the list of widgets created by the factory with the given id.
116141
*/
117142
getWidgets(factoryId: string): Widget[] {
118143
const result: Widget[] = [];
@@ -125,7 +150,9 @@ export class WidgetManager {
125150
}
126151

127152
/**
128-
* Try and get the widget.
153+
* Try to get the existing widget for the given description.
154+
* @param factoryId The widget factory id.
155+
* @param options The widget factory specific information.
129156
*
130157
* @returns the widget if available, else `undefined`.
131158
*/
@@ -140,8 +167,10 @@ export class WidgetManager {
140167

141168
/**
142169
* Get the widget for the given description.
170+
* @param factoryId The widget factory id.
171+
* @param options The widget factory specific information.
143172
*
144-
* @returns a promise resolving to the widget if available, else `undefined.
173+
* @returns a promise resolving to the widget if available, else `undefined`.
145174
*/
146175
async getWidget<T extends Widget>(factoryId: string, options?: any): Promise<T | undefined> {
147176
const key = this.toKey({ factoryId, options });
@@ -159,7 +188,11 @@ export class WidgetManager {
159188
}
160189

161190
/**
162-
* Creates or returns the widget for the given description.
191+
* Creates a new widget or returns the existing widget for the given description.
192+
* @param factoryId the widget factory id.
193+
* @param options the widget factory specific information.
194+
*
195+
* @returns a promise resolving to the widget.
163196
*/
164197
async getOrCreateWidget<T extends Widget>(factoryId: string, options?: any): Promise<T> {
165198
const key = this.toKey({ factoryId, options });

packages/core/src/browser/widget-open-handler.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import { OpenHandler, OpenerOptions } from './opener-service';
2323
import { WidgetManager } from './widget-manager';
2424

2525
export type WidgetOpenMode = 'open' | 'reveal' | 'activate';
26-
26+
/**
27+
* `WidgetOpenerOptions` define serializable generic options used by the {@link WidgetOpenHandler}.
28+
*/
2729
export interface WidgetOpenerOptions extends OpenerOptions {
2830
/**
29-
* Whether the widget should be only opened, revealed or activated.
31+
* Determines whether the widget should be only opened, revealed or activated.
3032
* By default is `activate`.
3133
*/
3234
mode?: WidgetOpenMode;
@@ -37,6 +39,9 @@ export interface WidgetOpenerOptions extends OpenerOptions {
3739
widgetOptions?: ApplicationShell.WidgetOptions;
3840
}
3941

42+
/**
43+
* Generic base class for {@link OpenHandler}s that are opening a widget for a given {@link URI}.
44+
*/
4045
@injectable()
4146
export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHandler {
4247

@@ -74,7 +79,11 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
7479

7580
/**
7681
* Open a widget for the given uri and options.
77-
* Reject if the given options is not an widget options or a widget cannot be opened.
82+
* Reject if the given options are not widget options or a widget cannot be opened.
83+
* @param uri the uri of the resource that should be opened.
84+
* @param options the widget opener options.
85+
*
86+
* @returns promise of the widget that resolves when the widget has been opened.
7887
*/
7988
async open(uri: URI, options?: WidgetOpenerOptions): Promise<W> {
8089
const widget = await this.getOrCreateWidget(uri, options);
@@ -97,7 +106,10 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
97106
}
98107

99108
/**
100-
* Return an existing widget for the given uri.
109+
* Tries to get an existing widget for the given uri.
110+
* @param uri the uri of the widget.
111+
*
112+
* @returns a promise that resolves to the existing widget or `undefined` if no widget for the given uri exists.
101113
*/
102114
getByUri(uri: URI): Promise<W | undefined> {
103115
return this.getWidget(uri);
@@ -106,14 +118,19 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
106118
/**
107119
* Return an existing widget for the given uri or creates a new one.
108120
*
109-
* It does not open a widget, use `open` instead.
121+
* It does not open a widget, use {@link WidgetOpenHandler#open} instead.
122+
* @param uri uri of the widget.
123+
*
124+
* @returns a promise of the existing or newly created widget.
110125
*/
111126
getOrCreateByUri(uri: URI): Promise<W> {
112127
return this.getOrCreateWidget(uri);
113128
}
114129

115130
/**
116-
* All opened widgets.
131+
* Retrieves all open widgets that have been opened by this handler.
132+
*
133+
* @returns all open widgets for this open handler.
117134
*/
118135
get all(): W[] {
119136
return this.widgetManager.getWidgets(this.id) as W[];
@@ -131,6 +148,12 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
131148

132149
protected abstract createWidgetOptions(uri: URI, options?: WidgetOpenerOptions): Object;
133150

151+
/**
152+
* Closes all widgets that have been opened by this open handler.
153+
* @param options the close options that should be applied to all widgets.
154+
*
155+
* @returns a promise of all closed widgets that resolves after they have been closed.
156+
*/
134157
async closeAll(options?: ApplicationShell.CloseOptions): Promise<W[]> {
135158
const closed = await Promise.all(this.all.map(widget => this.shell.closeWidget(widget.id, options)));
136159
return closed.filter(widget => !!widget) as W[];

0 commit comments

Comments
 (0)